1
0

client_flow_isolation_test.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. }