client_flow_isolation_test.go 12 KB

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