client_bulk_flow_test.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package service
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/mhsanaei/3x-ui/v3/internal/database"
  6. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  7. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  8. )
  9. // mkInboundStream is mkInbound with explicit stream settings, needed to make an
  10. // inbound flow-eligible (VLESS + tcp + reality/tls).
  11. func mkInboundStream(t *testing.T, port int, proto model.Protocol, settings, stream string) *model.Inbound {
  12. t.Helper()
  13. ib := &model.Inbound{
  14. Tag: string(proto) + "-stream-" + emailSafe(port),
  15. Enable: true,
  16. Port: port,
  17. Protocol: proto,
  18. Settings: settings,
  19. StreamSettings: stream,
  20. }
  21. if err := database.GetDB().Create(ib).Error; err != nil {
  22. t.Fatalf("create inbound %d: %v", port, err)
  23. }
  24. return ib
  25. }
  26. func emailSafe(port int) string {
  27. return string(rune('a'+port%26)) + string(rune('a'+(port/26)%26))
  28. }
  29. func flowOf(t *testing.T, svc *ClientService, email string) string {
  30. t.Helper()
  31. rec, err := svc.GetRecordByEmail(nil, email)
  32. if err != nil {
  33. t.Fatalf("GetRecordByEmail(%q): %v", email, err)
  34. }
  35. return rec.Flow
  36. }
  37. const realityStream = `{"network":"tcp","security":"reality"}`
  38. const wsStream = `{"network":"ws","security":"none"}`
  39. // TestBulkAdjust_FlowSetAndClear covers the happy path: a vision flow is applied
  40. // on an eligible VLESS inbound and later cleared with the "none" directive. Both
  41. // transitions are real config changes, so they must request a restart.
  42. func TestBulkAdjust_FlowSetAndClear(t *testing.T) {
  43. setupBulkDB(t)
  44. svc := &ClientService{}
  45. inboundSvc := &InboundService{}
  46. clients := []model.Client{
  47. {Email: "f1@x", ID: "11111111-1111-1111-1111-111111111111", SubID: "f1", Enable: true},
  48. {Email: "f2@x", ID: "22222222-2222-2222-2222-222222222222", SubID: "f2", Enable: true},
  49. }
  50. ib := mkInboundStream(t, 30001, model.VLESS, clientsSettings(t, clients), realityStream)
  51. if err := svc.SyncInbound(nil, ib.Id, clients); err != nil {
  52. t.Fatalf("seed: %v", err)
  53. }
  54. emails := emailsOf(clients)
  55. // Set vision flow.
  56. res, restart, err := svc.BulkAdjust(inboundSvc, emails, 0, 0, "xtls-rprx-vision-udp443")
  57. if err != nil {
  58. t.Fatalf("BulkAdjust set: %v", err)
  59. }
  60. if res.Adjusted != 2 {
  61. t.Fatalf("expected 2 adjusted, got %d (skipped=%v)", res.Adjusted, res.Skipped)
  62. }
  63. if !restart {
  64. t.Fatalf("setting flow should request a restart")
  65. }
  66. for _, e := range emails {
  67. if got := flowOf(t, svc, e); got != "xtls-rprx-vision-udp443" {
  68. t.Fatalf("%s flow = %q, want xtls-rprx-vision-udp443", e, got)
  69. }
  70. }
  71. // Setting the same flow again is a no-op: honored (counted) but no restart.
  72. if _, restart2, err := svc.BulkAdjust(inboundSvc, emails, 0, 0, "xtls-rprx-vision-udp443"); err != nil {
  73. t.Fatalf("BulkAdjust idempotent: %v", err)
  74. } else if restart2 {
  75. t.Fatalf("re-setting identical flow should not request a restart")
  76. }
  77. // Clear flow.
  78. cres, crestart, err := svc.BulkAdjust(inboundSvc, emails, 0, 0, "none")
  79. if err != nil {
  80. t.Fatalf("BulkAdjust clear: %v", err)
  81. }
  82. if cres.Adjusted != 2 {
  83. t.Fatalf("expected 2 cleared, got %d (skipped=%v)", cres.Adjusted, cres.Skipped)
  84. }
  85. if !crestart {
  86. t.Fatalf("clearing flow should request a restart")
  87. }
  88. for _, e := range emails {
  89. if got := flowOf(t, svc, e); got != "" {
  90. t.Fatalf("%s flow = %q, want empty after clear", e, got)
  91. }
  92. }
  93. }
  94. // TestBulkAdjust_FlowIneligibleSkipped verifies a vision flow is refused on an
  95. // inbound that cannot carry it (ws transport), reported as skipped, and the
  96. // client's flow is left untouched.
  97. func TestBulkAdjust_FlowIneligibleSkipped(t *testing.T) {
  98. setupBulkDB(t)
  99. svc := &ClientService{}
  100. inboundSvc := &InboundService{}
  101. clients := []model.Client{
  102. {Email: "ws1@x", ID: "33333333-3333-3333-3333-333333333333", SubID: "ws1", Enable: true},
  103. }
  104. ib := mkInboundStream(t, 30101, model.VLESS, clientsSettings(t, clients), wsStream)
  105. if err := svc.SyncInbound(nil, ib.Id, clients); err != nil {
  106. t.Fatalf("seed: %v", err)
  107. }
  108. res, restart, err := svc.BulkAdjust(inboundSvc, []string{"ws1@x"}, 0, 0, "xtls-rprx-vision")
  109. if err != nil {
  110. t.Fatalf("BulkAdjust: %v", err)
  111. }
  112. if res.Adjusted != 0 {
  113. t.Fatalf("ineligible inbound should adjust nothing, got %d", res.Adjusted)
  114. }
  115. if restart {
  116. t.Fatalf("no change should not request a restart")
  117. }
  118. if len(res.Skipped) != 1 || res.Skipped[0].Email != "ws1@x" {
  119. t.Fatalf("expected ws1@x in skipped, got %v", res.Skipped)
  120. }
  121. if got := flowOf(t, svc, "ws1@x"); got != "" {
  122. t.Fatalf("flow should stay empty on ineligible inbound, got %q", got)
  123. }
  124. }
  125. // TestBulkAdjust_NoDirectiveErrors guards the relaxed precondition: with no
  126. // days, traffic, or flow set there is nothing to do.
  127. func TestBulkAdjust_NoDirectiveErrors(t *testing.T) {
  128. setupBulkDB(t)
  129. svc := &ClientService{}
  130. inboundSvc := &InboundService{}
  131. if _, _, err := svc.BulkAdjust(inboundSvc, []string{"any@x"}, 0, 0, ""); err == nil {
  132. t.Fatalf("expected error when no adjustment is specified")
  133. }
  134. // An unknown flow directive is ignored (treated as ""), so it also errors.
  135. if _, _, err := svc.BulkAdjust(inboundSvc, []string{"any@x"}, 0, 0, "bogus-flow"); err == nil {
  136. t.Fatalf("unknown flow should be ignored and error like an empty directive")
  137. }
  138. }
  139. // TestBulkAdjust_DaysApplyDespiteIneligibleFlow is the regression for the review
  140. // blocker: when a client on a flow-ineligible inbound is adjusted with BOTH a
  141. // days/traffic delta AND a flow directive, the days/traffic change must still be
  142. // persisted to ClientTraffic (not just the inbound JSON / ClientRecord) and the
  143. // client must count as adjusted, while the unhonored flow is reported separately.
  144. func TestBulkAdjust_DaysApplyDespiteIneligibleFlow(t *testing.T) {
  145. setupBulkDB(t)
  146. svc := &ClientService{}
  147. inboundSvc := &InboundService{}
  148. const day = int64(24 * 60 * 60 * 1000)
  149. const gb = int64(1) << 30
  150. baseExpiry := time.Now().UnixMilli() + 30*day
  151. baseTotal := 10 * gb
  152. clients := []model.Client{
  153. {Email: "mix@x", ID: "44444444-4444-4444-4444-444444444444", SubID: "mix", Enable: true, ExpiryTime: baseExpiry, TotalGB: baseTotal},
  154. }
  155. ib := mkInboundStream(t, 30201, model.VLESS, clientsSettings(t, clients), wsStream)
  156. if err := svc.SyncInbound(nil, ib.Id, clients); err != nil {
  157. t.Fatalf("seed: %v", err)
  158. }
  159. // ClientTraffic is the store the enforcement job reads; seed it to match.
  160. if err := database.GetDB().Create(&xray.ClientTraffic{Email: "mix@x", Enable: true, ExpiryTime: baseExpiry, Total: baseTotal}).Error; err != nil {
  161. t.Fatalf("seed traffic: %v", err)
  162. }
  163. res, _, err := svc.BulkAdjust(inboundSvc, []string{"mix@x"}, 7, gb, "xtls-rprx-vision")
  164. if err != nil {
  165. t.Fatalf("BulkAdjust: %v", err)
  166. }
  167. if res.Adjusted != 1 {
  168. t.Fatalf("days/traffic should still be applied: Adjusted=%d skipped=%v", res.Adjusted, res.Skipped)
  169. }
  170. if len(res.Skipped) != 1 || res.Skipped[0].Email != "mix@x" {
  171. t.Fatalf("expected mix@x reported for the unhonored flow, got %v", res.Skipped)
  172. }
  173. wantExpiry := baseExpiry + 7*day
  174. wantTotal := baseTotal + gb
  175. // ClientRecord (inbound-derived) advanced.
  176. if rec, err := svc.GetRecordByEmail(nil, "mix@x"); err != nil {
  177. t.Fatalf("record: %v", err)
  178. } else if rec.ExpiryTime != wantExpiry || rec.TotalGB != wantTotal {
  179. t.Fatalf("ClientRecord not advanced: expiry=%d total=%d", rec.ExpiryTime, rec.TotalGB)
  180. }
  181. // ClientTraffic advanced in lockstep — no divergence.
  182. var ct xray.ClientTraffic
  183. if err := database.GetDB().Where("email = ?", "mix@x").First(&ct).Error; err != nil {
  184. t.Fatalf("traffic row: %v", err)
  185. }
  186. if ct.ExpiryTime != wantExpiry || ct.Total != wantTotal {
  187. t.Fatalf("ClientTraffic diverged: expiry=%d total=%d, want expiry=%d total=%d", ct.ExpiryTime, ct.Total, wantExpiry, wantTotal)
  188. }
  189. // Flow left untouched on the ineligible inbound.
  190. if got := flowOf(t, svc, "mix@x"); got != "" {
  191. t.Fatalf("flow should stay empty on ineligible inbound, got %q", got)
  192. }
  193. }