1
0

manager_test.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package mtproto
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  6. )
  7. func TestInstanceFromInbound(t *testing.T) {
  8. aliceSecret := "ee0123456789abcdef0123456789abcdef6578616d706c652e636f6d"
  9. ib := &model.Inbound{
  10. Id: 3,
  11. Tag: "inbound-3",
  12. Listen: "0.0.0.0",
  13. Port: 8443,
  14. Protocol: model.MTProto,
  15. Settings: `{"fakeTlsDomain":"example.com",` +
  16. `"debug":true,"proxyProtocolListener":true,"preferIp":"prefer-ipv4",` +
  17. `"domainFronting":{"ip":"127.0.0.1","port":9443,"proxyProtocol":true},` +
  18. `"throttleMaxConnections":5000,` +
  19. `"routeThroughXray":true,"routeXrayPort":50000,` +
  20. `"clients":[` +
  21. `{"email":"alice","secret":"` + aliceSecret + `","enable":true},` +
  22. `{"email":"bob","secret":"","enable":true},` +
  23. `{"email":"carol","secret":"eeaa","enable":false}]}`,
  24. }
  25. inst, ok := InstanceFromInbound(ib)
  26. if !ok {
  27. t.Fatal("expected a usable instance")
  28. }
  29. if len(inst.Secrets) != 1 {
  30. t.Fatalf("only the enabled client with a secret should be served, got %d: %+v", len(inst.Secrets), inst.Secrets)
  31. }
  32. if inst.Secrets[0].Name != "alice" {
  33. t.Fatalf("secret name should be the client email, got %q", inst.Secrets[0].Name)
  34. }
  35. if inst.Secrets[0].Secret != aliceSecret {
  36. t.Fatalf("a valid secret must be preserved, got %q", inst.Secrets[0].Secret)
  37. }
  38. if inst.Port != 8443 || inst.Id != 3 {
  39. t.Fatalf("bad instance %+v", inst)
  40. }
  41. if !inst.Debug || !inst.ProxyProtocolListener || inst.PreferIP != "prefer-ipv4" {
  42. t.Fatalf("scalar options not parsed: %+v", inst)
  43. }
  44. if inst.FrontingIP != "127.0.0.1" || inst.FrontingPort != 9443 || !inst.FrontingProxyProtocol {
  45. t.Fatalf("domain-fronting not parsed: %+v", inst)
  46. }
  47. if inst.ThrottleMaxConnections != 5000 {
  48. t.Fatalf("throttle not parsed: %+v", inst)
  49. }
  50. if !inst.RouteThroughXray || inst.XrayRoutePort != 50000 {
  51. t.Fatalf("xray routing not parsed: %+v", inst)
  52. }
  53. if _, ok := InstanceFromInbound(&model.Inbound{Protocol: model.VLESS}); ok {
  54. t.Fatal("non-mtproto inbound should not produce an instance")
  55. }
  56. noSecrets := &model.Inbound{Protocol: model.MTProto, Settings: `{"clients":[{"email":"x","secret":"","enable":true}]}`}
  57. if _, ok := InstanceFromInbound(noSecrets); ok {
  58. t.Fatal("an inbound with no active secret should not produce an instance")
  59. }
  60. }
  61. func TestRenderConfig(t *testing.T) {
  62. // A bare instance emits only the required keys, api-bind-to, and the
  63. // [secrets] section, with no optional keys and no [domain-fronting].
  64. bare := renderConfig(Instance{
  65. Secrets: []SecretEntry{{Name: "alice", Secret: "ee00"}},
  66. Listen: "0.0.0.0", Port: 8443,
  67. }, 5000)
  68. for _, unwanted := range []string{"debug", "proxy-protocol-listener", "prefer-ip", "[domain-fronting]", "[stats.prometheus]", "[throttle]"} {
  69. if strings.Contains(bare, unwanted) {
  70. t.Fatalf("bare config should not contain %q:\n%s", unwanted, bare)
  71. }
  72. }
  73. if !strings.Contains(bare, `bind-to = "0.0.0.0:8443"`) {
  74. t.Fatalf("missing bind-to:\n%s", bare)
  75. }
  76. if !strings.Contains(bare, `api-bind-to = "127.0.0.1:5000"`) {
  77. t.Fatalf("api-bind-to must always be present:\n%s", bare)
  78. }
  79. if !strings.Contains(bare, "[secrets]") || !strings.Contains(bare, `"alice" = "ee00"`) {
  80. t.Fatalf("secrets block must carry the client secret:\n%s", bare)
  81. }
  82. // A fully configured instance emits every option, the fronting section (as
  83. // host, not the fork-deprecated ip), the throttle block, and [secrets] last.
  84. full := renderConfig(Instance{
  85. Secrets: []SecretEntry{{Name: "alice", Secret: "ee11"}},
  86. Listen: "0.0.0.0", Port: 443,
  87. Debug: true, ProxyProtocolListener: true, PreferIP: "only-ipv6",
  88. FrontingIP: "127.0.0.1", FrontingPort: 9443, FrontingProxyProtocol: true,
  89. ThrottleMaxConnections: 5000,
  90. }, 6000)
  91. for _, want := range []string{
  92. "debug = true\n",
  93. "proxy-protocol-listener = true\n",
  94. `prefer-ip = "only-ipv6"`,
  95. "[domain-fronting]",
  96. `host = "127.0.0.1"`,
  97. "port = 9443",
  98. "proxy-protocol = true\n",
  99. "[throttle]",
  100. "max-connections = 5000",
  101. } {
  102. if !strings.Contains(full, want) {
  103. t.Fatalf("full config missing %q:\n%s", want, full)
  104. }
  105. }
  106. if strings.Contains(full, `ip = "127.0.0.1"`) {
  107. t.Fatalf("domain-fronting must use host, not the deprecated ip key:\n%s", full)
  108. }
  109. // TOML requires top-level keys before any [section] header, and [secrets]
  110. // must be the final section so trailing keys are not swallowed by a table.
  111. if strings.Index(full, "prefer-ip") > strings.Index(full, "[domain-fronting]") {
  112. t.Fatalf("top-level keys must precede the [domain-fronting] section:\n%s", full)
  113. }
  114. if strings.LastIndex(full, "[secrets]") < strings.Index(full, "[domain-fronting]") {
  115. t.Fatalf("[secrets] must be the final section:\n%s", full)
  116. }
  117. if strings.LastIndex(full, "[secrets]") < strings.Index(full, "[throttle]") {
  118. t.Fatalf("[throttle] must precede [secrets]:\n%s", full)
  119. }
  120. }
  121. func TestRenderConfigXrayEgress(t *testing.T) {
  122. // Routing through Xray emits a [network] proxies upstream pointing at the
  123. // loopback SOCKS bridge, before the [secrets] section.
  124. routed := renderConfig(Instance{
  125. Secrets: []SecretEntry{{Name: "a", Secret: "ee22"}},
  126. Listen: "0.0.0.0", Port: 443,
  127. RouteThroughXray: true, XrayRoutePort: 50000,
  128. }, 7000)
  129. if !strings.Contains(routed, "[network]") ||
  130. !strings.Contains(routed, `proxies = ["socks5://127.0.0.1:50000"]`) {
  131. t.Fatalf("routed config must emit the SOCKS upstream:\n%s", routed)
  132. }
  133. if strings.Index(routed, "[network]") > strings.Index(routed, "[secrets]") {
  134. t.Fatalf("[network] must precede [secrets]:\n%s", routed)
  135. }
  136. // Without the flag (or without a port) the section is omitted.
  137. for _, inst := range []Instance{
  138. {Secrets: []SecretEntry{{Name: "a", Secret: "ee"}}, Listen: "0.0.0.0", Port: 443},
  139. {Secrets: []SecretEntry{{Name: "a", Secret: "ee"}}, Listen: "0.0.0.0", Port: 443, RouteThroughXray: true},
  140. } {
  141. if got := renderConfig(inst, 7000); strings.Contains(got, "[network]") {
  142. t.Fatalf("unrouted config must omit [network]:\n%s", got)
  143. }
  144. }
  145. }
  146. func TestFingerprintReactsToOptions(t *testing.T) {
  147. base := Instance{Secrets: []SecretEntry{{Name: "a", Secret: "ee"}}, Listen: "0.0.0.0", Port: 443}
  148. for name, mutate := range map[string]func(*Instance){
  149. "debug": func(i *Instance) { i.Debug = true },
  150. "listener": func(i *Instance) { i.ProxyProtocolListener = true },
  151. "preferIp": func(i *Instance) { i.PreferIP = "only-ipv4" },
  152. "frontingIP": func(i *Instance) { i.FrontingIP = "127.0.0.1" },
  153. "frontingPort": func(i *Instance) { i.FrontingPort = 9443 },
  154. "frontingProxy": func(i *Instance) { i.FrontingProxyProtocol = true },
  155. "throttle": func(i *Instance) { i.ThrottleMaxConnections = 5000 },
  156. "routeXray": func(i *Instance) { i.RouteThroughXray = true },
  157. "routeXrayPort": func(i *Instance) { i.XrayRoutePort = 50000 },
  158. "addSecret": func(i *Instance) { i.Secrets = append(i.Secrets, SecretEntry{Name: "b", Secret: "ff"}) },
  159. "changeSecret": func(i *Instance) { i.Secrets = []SecretEntry{{Name: "a", Secret: "ee99"}} },
  160. } {
  161. changed := base
  162. if strings.HasPrefix(name, "addSecret") || strings.HasPrefix(name, "changeSecret") {
  163. changed.Secrets = append([]SecretEntry(nil), base.Secrets...)
  164. }
  165. mutate(&changed)
  166. if base.fingerprint() == changed.fingerprint() {
  167. t.Fatalf("fingerprint must change when %s changes", name)
  168. }
  169. }
  170. }