inbound_mtproto_test.go 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package service
  2. import (
  3. "encoding/json"
  4. "testing"
  5. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  6. )
  7. func TestMtprotoRoutesThroughXray(t *testing.T) {
  8. cases := map[string]struct {
  9. ib *model.Inbound
  10. want bool
  11. }{
  12. "routed": {&model.Inbound{Protocol: model.MTProto, Settings: `{"routeThroughXray":true}`}, true},
  13. "off": {&model.Inbound{Protocol: model.MTProto, Settings: `{"routeThroughXray":false}`}, false},
  14. "absent": {&model.Inbound{Protocol: model.MTProto, Settings: `{}`}, false},
  15. "non-mtproto": {&model.Inbound{Protocol: model.VLESS, Settings: `{"routeThroughXray":true}`}, false},
  16. "bad json": {&model.Inbound{Protocol: model.MTProto, Settings: `{nope`}, false},
  17. "nil": {nil, false},
  18. }
  19. for name, c := range cases {
  20. if got := mtprotoRoutesThroughXray(c.ib); got != c.want {
  21. t.Fatalf("%s: got %v want %v", name, got, c.want)
  22. }
  23. }
  24. }
  25. func routeXrayPortOf(t *testing.T, settings string) int {
  26. t.Helper()
  27. var parsed map[string]any
  28. if err := json.Unmarshal([]byte(settings), &parsed); err != nil {
  29. t.Fatalf("settings not valid JSON: %v\n%s", err, settings)
  30. }
  31. return settingsRouteXrayPort(parsed)
  32. }
  33. func TestNormalizeMtprotoXrayPort(t *testing.T) {
  34. s := &InboundService{}
  35. // Non-mtproto inbounds are left alone.
  36. ib := &model.Inbound{Protocol: model.VLESS, Settings: `{"x":1}`}
  37. if err := s.normalizeMtprotoXrayPort(ib, ""); err != nil {
  38. t.Fatal(err)
  39. }
  40. if ib.Settings != `{"x":1}` {
  41. t.Fatalf("non-mtproto settings must be untouched, got %s", ib.Settings)
  42. }
  43. // Routing on with no existing port allocates a fresh one.
  44. ib = &model.Inbound{Protocol: model.MTProto, Settings: `{"routeThroughXray":true}`}
  45. if err := s.normalizeMtprotoXrayPort(ib, ""); err != nil {
  46. t.Fatal(err)
  47. }
  48. if p := routeXrayPortOf(t, ib.Settings); p <= 0 {
  49. t.Fatalf("a routed inbound must get a port, got %d", p)
  50. }
  51. // On update, the stored port wins over both a missing and a client-echoed
  52. // value — the backend owns it, so no churn and no client override.
  53. ib = &model.Inbound{Protocol: model.MTProto, Settings: `{"routeThroughXray":true,"routeXrayPort":99999}`}
  54. if err := s.normalizeMtprotoXrayPort(ib, `{"routeThroughXray":true,"routeXrayPort":51000}`); err != nil {
  55. t.Fatal(err)
  56. }
  57. if p := routeXrayPortOf(t, ib.Settings); p != 51000 {
  58. t.Fatalf("stored port must win, got %d", p)
  59. }
  60. // An already-present port (no old settings) is stable and not re-marshaled.
  61. const stable = `{"routeThroughXray":true,"routeXrayPort":52000}`
  62. ib = &model.Inbound{Protocol: model.MTProto, Settings: stable}
  63. if err := s.normalizeMtprotoXrayPort(ib, ""); err != nil {
  64. t.Fatal(err)
  65. }
  66. if ib.Settings != stable {
  67. t.Fatalf("stable settings must pass through untouched, got %s", ib.Settings)
  68. }
  69. // Turning routing off strips both the bridge port and the inert outbound.
  70. ib = &model.Inbound{Protocol: model.MTProto, Settings: `{"routeThroughXray":false,"routeXrayPort":53000,"outboundTag":"warp"}`}
  71. if err := s.normalizeMtprotoXrayPort(ib, ""); err != nil {
  72. t.Fatal(err)
  73. }
  74. if p := routeXrayPortOf(t, ib.Settings); p != 0 {
  75. t.Fatalf("disabling routing must drop the port, got %d", p)
  76. }
  77. var parsed map[string]any
  78. if err := json.Unmarshal([]byte(ib.Settings), &parsed); err != nil {
  79. t.Fatal(err)
  80. }
  81. if _, ok := parsed["outboundTag"]; ok {
  82. t.Fatalf("disabling routing must drop the inert outbound tag, got %s", ib.Settings)
  83. }
  84. }