inbound_flow_restore_test.go 3.6 KB

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