1
0

client_flow_isolation_test.go 12 KB

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