client_flow_isolation_test.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. package service
  2. import (
  3. "path/filepath"
  4. "testing"
  5. "github.com/mhsanaei/3x-ui/v3/internal/database"
  6. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  7. )
  8. func TestClientWithInboundFlow_GatesByInboundCapability(t *testing.T) {
  9. const vision = "xtls-rprx-vision"
  10. const enc = `{"encryption":"mlkem768x25519plus.native.0rtt.G3cdPSd1-NnlpTbWNSM5vHsT5VNzWfFzYSKwbUMnV1Y"}`
  11. cases := []struct {
  12. name string
  13. protocol model.Protocol
  14. streamSettings string
  15. settings string
  16. wantFlow string
  17. }{
  18. {"vless tcp reality keeps flow", model.VLESS, `{"network":"tcp","security":"reality"}`, "", vision},
  19. {"vless tcp tls keeps flow", model.VLESS, `{"network":"tcp","security":"tls"}`, "", vision},
  20. {"vless ws tls clears flow", model.VLESS, `{"network":"ws","security":"tls"}`, "", ""},
  21. {"vless grpc tls clears flow", model.VLESS, `{"network":"grpc","security":"tls"}`, "", ""},
  22. {"vless tcp none clears flow", model.VLESS, `{"network":"tcp","security":"none"}`, "", ""},
  23. {"vmess tcp tls clears flow", model.VMESS, `{"network":"tcp","security":"tls"}`, "", ""},
  24. {"empty stream clears flow", model.VLESS, "", "", ""},
  25. // vlessenc (ML-KEM) keeps Vision flow without transport TLS only on XHTTP.
  26. // TCP without tls/reality clears it even with vlessenc set.
  27. {"vless tcp vlessenc clears flow", model.VLESS, `{"network":"tcp","security":"none"}`, enc, ""},
  28. {"vless xhttp vlessenc keeps flow", model.VLESS, `{"network":"xhttp","security":"none"}`, enc, vision},
  29. {"vless xhttp no encryption clears flow", model.VLESS, `{"network":"xhttp","security":"none"}`, `{"encryption":"none"}`, ""},
  30. {"vless xhttp empty settings clears flow", model.VLESS, `{"network":"xhttp","security":"none"}`, "", ""},
  31. }
  32. for _, tc := range cases {
  33. t.Run(tc.name, func(t *testing.T) {
  34. ib := &model.Inbound{Protocol: tc.protocol, StreamSettings: tc.streamSettings, Settings: tc.settings}
  35. got := clientWithInboundFlow(model.Client{Email: "[email protected]", Flow: vision}, ib)
  36. if got.Flow != tc.wantFlow {
  37. t.Errorf("Flow = %q, want %q", got.Flow, tc.wantFlow)
  38. }
  39. })
  40. }
  41. }
  42. func TestFlowIsolation_VisionDoesNotLeakToWsInbound(t *testing.T) {
  43. dbDir := t.TempDir()
  44. t.Setenv("XUI_DB_FOLDER", dbDir)
  45. if err := database.InitDB(filepath.Join(dbDir, "x-ui.db")); err != nil {
  46. t.Fatalf("InitDB: %v", err)
  47. }
  48. t.Cleanup(func() { _ = database.CloseDB() })
  49. db := database.GetDB()
  50. wsTls := &model.Inbound{Tag: "vless-ws", Enable: true, Port: 30001, Protocol: model.VLESS, StreamSettings: `{"network":"ws","security":"tls"}`}
  51. if err := db.Create(wsTls).Error; err != nil {
  52. t.Fatalf("create ws+tls inbound: %v", err)
  53. }
  54. reality := &model.Inbound{Tag: "vless-reality", Enable: true, Port: 30002, Protocol: model.VLESS, StreamSettings: `{"network":"tcp","security":"reality"}`}
  55. if err := db.Create(reality).Error; err != nil {
  56. t.Fatalf("create reality inbound: %v", err)
  57. }
  58. svc := ClientService{}
  59. const email = "[email protected]"
  60. const uid = "ce8d33df-3a64-4f10-8f9b-91c3a8e0c003"
  61. const vision = "xtls-rprx-vision"
  62. source := model.Client{Email: email, ID: uid, Enable: true, Flow: vision}
  63. for _, ib := range []*model.Inbound{wsTls, reality} {
  64. gated := clientWithInboundFlow(source, ib)
  65. if err := svc.SyncInbound(nil, ib.Id, []model.Client{gated}); err != nil {
  66. t.Fatalf("SyncInbound(%s): %v", ib.Tag, err)
  67. }
  68. }
  69. realityList, err := svc.ListForInbound(nil, reality.Id)
  70. if err != nil {
  71. t.Fatalf("ListForInbound(reality): %v", err)
  72. }
  73. if len(realityList) != 1 || realityList[0].Flow != vision {
  74. t.Errorf("Reality inbound should keep flow=%q, got %#v", vision, realityList)
  75. }
  76. wsList, err := svc.ListForInbound(nil, wsTls.Id)
  77. if err != nil {
  78. t.Fatalf("ListForInbound(ws): %v", err)
  79. }
  80. if len(wsList) != 1 || wsList[0].Flow != "" {
  81. t.Errorf("WS+TLS inbound must not inherit Vision flow (#4628), got %#v", wsList)
  82. }
  83. }
  84. func TestEffectiveFlow_NonFlowInboundSyncedLastDoesNotHideVision(t *testing.T) {
  85. dbDir := t.TempDir()
  86. t.Setenv("XUI_DB_FOLDER", dbDir)
  87. if err := database.InitDB(filepath.Join(dbDir, "x-ui.db")); err != nil {
  88. t.Fatalf("InitDB: %v", err)
  89. }
  90. t.Cleanup(func() { _ = database.CloseDB() })
  91. db := database.GetDB()
  92. reality := &model.Inbound{Tag: "vless-reality", Enable: true, Port: 40001, Protocol: model.VLESS, StreamSettings: `{"network":"tcp","security":"reality"}`}
  93. if err := db.Create(reality).Error; err != nil {
  94. t.Fatalf("create reality inbound: %v", err)
  95. }
  96. hysteria := &model.Inbound{Tag: "hysteria", Enable: true, Port: 40002, Protocol: model.Hysteria, StreamSettings: `{"security":"tls"}`}
  97. if err := db.Create(hysteria).Error; err != nil {
  98. t.Fatalf("create hysteria inbound: %v", err)
  99. }
  100. svc := ClientService{}
  101. const email = "[email protected]"
  102. const uid = "ce8d33df-3a64-4f10-8f9b-91c3a8e0c099"
  103. const vision = "xtls-rprx-vision"
  104. source := model.Client{Email: email, ID: uid, Auth: uid, Enable: true, Flow: vision}
  105. // Reproduce #4792 ordering: the flow-capable inbound (Reality) syncs first,
  106. // the non-flow inbound (Hysteria) syncs last and wipes clients.Flow to "".
  107. for _, ib := range []*model.Inbound{reality, hysteria} {
  108. gated := clientWithInboundFlow(source, ib)
  109. if err := svc.SyncInbound(nil, ib.Id, []model.Client{gated}); err != nil {
  110. t.Fatalf("SyncInbound(%s): %v", ib.Tag, err)
  111. }
  112. }
  113. rec, err := svc.GetRecordByEmail(nil, email)
  114. if err != nil {
  115. t.Fatalf("GetRecordByEmail: %v", err)
  116. }
  117. if rec.Flow != "" {
  118. t.Logf("note: canonical clients.Flow = %q (denormalized, not authoritative)", rec.Flow)
  119. }
  120. got, err := svc.EffectiveFlow(nil, rec.Id)
  121. if err != nil {
  122. t.Fatalf("EffectiveFlow: %v", err)
  123. }
  124. if got != vision {
  125. t.Errorf("EffectiveFlow = %q, want %q — the edit form would show a blank flow (#4792)", got, vision)
  126. }
  127. }
  128. func TestEffectiveFlow_ClearedFlowStaysCleared(t *testing.T) {
  129. dbDir := t.TempDir()
  130. t.Setenv("XUI_DB_FOLDER", dbDir)
  131. if err := database.InitDB(filepath.Join(dbDir, "x-ui.db")); err != nil {
  132. t.Fatalf("InitDB: %v", err)
  133. }
  134. t.Cleanup(func() { _ = database.CloseDB() })
  135. db := database.GetDB()
  136. reality := &model.Inbound{Tag: "vless-reality", Enable: true, Port: 41001, Protocol: model.VLESS, StreamSettings: `{"network":"tcp","security":"reality"}`}
  137. if err := db.Create(reality).Error; err != nil {
  138. t.Fatalf("create reality inbound: %v", err)
  139. }
  140. hysteria := &model.Inbound{Tag: "hysteria", Enable: true, Port: 41002, Protocol: model.Hysteria, StreamSettings: `{"security":"tls"}`}
  141. if err := db.Create(hysteria).Error; err != nil {
  142. t.Fatalf("create hysteria inbound: %v", err)
  143. }
  144. svc := ClientService{}
  145. const email = "[email protected]"
  146. const uid = "ce8d33df-3a64-4f10-8f9b-91c3a8e0c0aa"
  147. // User chose no flow: every inbound carries "". A non-empty guard in
  148. // SyncInbound would make this impossible to express; EffectiveFlow must
  149. // still report "".
  150. source := model.Client{Email: email, ID: uid, Auth: uid, Enable: true, Flow: ""}
  151. for _, ib := range []*model.Inbound{reality, hysteria} {
  152. gated := clientWithInboundFlow(source, ib)
  153. if err := svc.SyncInbound(nil, ib.Id, []model.Client{gated}); err != nil {
  154. t.Fatalf("SyncInbound(%s): %v", ib.Tag, err)
  155. }
  156. }
  157. rec, err := svc.GetRecordByEmail(nil, email)
  158. if err != nil {
  159. t.Fatalf("GetRecordByEmail: %v", err)
  160. }
  161. got, err := svc.EffectiveFlow(nil, rec.Id)
  162. if err != nil {
  163. t.Fatalf("EffectiveFlow: %v", err)
  164. }
  165. if got != "" {
  166. t.Errorf("EffectiveFlow = %q, want empty (cleared flow must stay cleared)", got)
  167. }
  168. }
  169. func TestAttach_PreservesVisionFlowWhenCanonicalColumnZeroed(t *testing.T) {
  170. dbDir := t.TempDir()
  171. t.Setenv("XUI_DB_FOLDER", dbDir)
  172. if err := database.InitDB(filepath.Join(dbDir, "x-ui.db")); err != nil {
  173. t.Fatalf("InitDB: %v", err)
  174. }
  175. t.Cleanup(func() { _ = database.CloseDB() })
  176. db := database.GetDB()
  177. const email = "[email protected]"
  178. const uid = "ce8d33df-3a64-4f10-8f9b-91c3a8e0c111"
  179. const sub = "subvision000001"
  180. const vision = "xtls-rprx-vision"
  181. const realityStream = `{"network":"tcp","security":"reality"}`
  182. svc := ClientService{}
  183. source := model.Client{Email: email, ID: uid, SubID: sub, Enable: true, Flow: vision}
  184. reality1 := &model.Inbound{
  185. Tag: "vless-reality-1", Enable: true, Port: 42001, Protocol: model.VLESS,
  186. StreamSettings: realityStream,
  187. Settings: clientsSettings(t, []model.Client{source}),
  188. }
  189. if err := db.Create(reality1).Error; err != nil {
  190. t.Fatalf("create reality1: %v", err)
  191. }
  192. reality2 := &model.Inbound{
  193. Tag: "vless-reality-2", Enable: true, Port: 42002, Protocol: model.VLESS,
  194. StreamSettings: realityStream, Settings: `{"clients":[]}`,
  195. }
  196. if err := db.Create(reality2).Error; err != nil {
  197. t.Fatalf("create reality2: %v", err)
  198. }
  199. wsTls := &model.Inbound{
  200. Tag: "vless-ws", Enable: true, Port: 42003, Protocol: model.VLESS,
  201. StreamSettings: `{"network":"ws","security":"tls"}`, Settings: `{"clients":[]}`,
  202. }
  203. if err := db.Create(wsTls).Error; err != nil {
  204. t.Fatalf("create ws: %v", err)
  205. }
  206. if err := svc.SyncInbound(nil, reality1.Id, []model.Client{clientWithInboundFlow(source, reality1)}); err != nil {
  207. t.Fatalf("SyncInbound(reality1): %v", err)
  208. }
  209. rec, err := svc.GetRecordByEmail(nil, email)
  210. if err != nil {
  211. t.Fatalf("GetRecordByEmail: %v", err)
  212. }
  213. if err := db.Model(&model.ClientRecord{}).Where("id = ?", rec.Id).Update("flow", "").Error; err != nil {
  214. t.Fatalf("zero canonical flow: %v", err)
  215. }
  216. inboundSvc := &InboundService{}
  217. if _, err := svc.Attach(inboundSvc, rec.Id, []int{reality2.Id, wsTls.Id}); err != nil {
  218. t.Fatalf("Attach: %v", err)
  219. }
  220. reality2List, err := svc.ListForInbound(nil, reality2.Id)
  221. if err != nil {
  222. t.Fatalf("ListForInbound(reality2): %v", err)
  223. }
  224. if len(reality2List) != 1 || reality2List[0].Flow != vision {
  225. t.Errorf("attached flow-capable inbound must inherit Vision via EffectiveFlow (#4834), got %#v", reality2List)
  226. }
  227. wsList, err := svc.ListForInbound(nil, wsTls.Id)
  228. if err != nil {
  229. t.Fatalf("ListForInbound(ws): %v", err)
  230. }
  231. if len(wsList) != 1 || wsList[0].Flow != "" {
  232. t.Errorf("attached non-flow inbound must not receive Vision flow, got %#v", wsList)
  233. }
  234. }