1
0

inbound_flow_restore_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package service
  2. import (
  3. "encoding/json"
  4. "path/filepath"
  5. "testing"
  6. "github.com/mhsanaei/3x-ui/v3/internal/database"
  7. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  8. )
  9. // restoreVisionFlowForEligibleInbound must re-add Vision to a client whose flow
  10. // was stripped while the XHTTP inbound was not yet vlessenc-encrypted, but only
  11. // when the client's intended flow (its flow_override on a sibling) is Vision,
  12. // only on now-eligible inbounds, and never overwriting an explicit flow.
  13. func TestRestoreVisionFlowForEligibleInbound(t *testing.T) {
  14. dbDir := t.TempDir()
  15. t.Setenv("XUI_DB_FOLDER", dbDir)
  16. if err := database.InitDB(filepath.Join(dbDir, "x-ui.db")); err != nil {
  17. t.Fatalf("InitDB: %v", err)
  18. }
  19. t.Cleanup(func() { _ = database.CloseDB() })
  20. db := database.GetDB()
  21. const vision = "xtls-rprx-vision"
  22. const realityStream = `{"network":"tcp","security":"reality"}`
  23. const xhttpEnc = `{"network":"xhttp","security":"reality"}`
  24. const encSettings = `"decryption":"mlkem768x25519plus.native.0rtt.KEY","encryption":"mlkem768x25519plus.native.0rtt.KEY"`
  25. cs := &ClientService{}
  26. ibSvc := &InboundService{}
  27. // Sibling reality inbound where the client legitimately has Vision.
  28. sibling := &model.Inbound{
  29. Tag: "sib", Enable: true, Port: 51001, Protocol: model.VLESS, StreamSettings: realityStream,
  30. Settings: `{"clients":[{"id":"u1","email":"keep@x","flow":"` + vision + `","subId":"s1","enable":true}]}`,
  31. }
  32. if err := db.Create(sibling).Error; err != nil {
  33. t.Fatalf("create sibling: %v", err)
  34. }
  35. keep, _ := ibSvc.GetClients(sibling)
  36. if err := cs.SyncInbound(nil, sibling.Id, keep); err != nil {
  37. t.Fatalf("sync sibling: %v", err)
  38. }
  39. // A client with no intended Vision anywhere — must NOT be touched.
  40. other := &model.Inbound{
  41. Tag: "oth", Enable: true, Port: 51002, Protocol: model.VLESS, StreamSettings: realityStream,
  42. Settings: `{"clients":[{"id":"u2","email":"none@x","subId":"s2","enable":true}]}`,
  43. }
  44. if err := db.Create(other).Error; err != nil {
  45. t.Fatalf("create other: %v", err)
  46. }
  47. oc, _ := ibSvc.GetClients(other)
  48. if err := cs.SyncInbound(nil, other.Id, oc); err != nil {
  49. t.Fatalf("sync other: %v", err)
  50. }
  51. // The now-eligible XHTTP inbound: keep@x has empty flow (was stripped),
  52. // none@x has empty flow (no Vision anywhere), set@x has an explicit empty
  53. // stays empty unless intended Vision.
  54. target := `{` + encSettings + `,"clients":[` +
  55. `{"id":"u1","email":"keep@x","flow":"","subId":"s1","enable":true},` +
  56. `{"id":"u2","email":"none@x","flow":"","subId":"s2","enable":true}` +
  57. `]}`
  58. out, changed := ibSvc.restoreVisionFlowForEligibleInbound(nil, target, xhttpEnc, model.VLESS)
  59. if !changed {
  60. t.Fatal("expected changed=true")
  61. }
  62. var parsed map[string]any
  63. if err := json.Unmarshal([]byte(out), &parsed); err != nil {
  64. t.Fatalf("parse out: %v", err)
  65. }
  66. flows := map[string]string{}
  67. for _, c := range parsed["clients"].([]any) {
  68. cm := c.(map[string]any)
  69. flows[cm["email"].(string)], _ = cm["flow"].(string)
  70. }
  71. if flows["keep@x"] != vision {
  72. t.Errorf("keep@x flow = %q, want Vision (intended on sibling)", flows["keep@x"])
  73. }
  74. if flows["none@x"] != "" {
  75. t.Errorf("none@x flow = %q, want empty (no Vision intent)", flows["none@x"])
  76. }
  77. // Ineligible inbound (xhttp without encryption) must be a no-op.
  78. noenc := `{"clients":[{"id":"u1","email":"keep@x","flow":"","subId":"s1","enable":true}]}`
  79. if _, ch := ibSvc.restoreVisionFlowForEligibleInbound(nil, noenc, `{"network":"xhttp","security":"reality"}`, model.VLESS); ch {
  80. t.Error("ineligible xhttp (no vlessenc) must not change")
  81. }
  82. // Non-VLESS must be a no-op.
  83. if _, ch := ibSvc.restoreVisionFlowForEligibleInbound(nil, target, xhttpEnc, model.VMESS); ch {
  84. t.Error("non-VLESS must not change")
  85. }
  86. }