1
0

client_flow_isolation_test.go 9.1 KB

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