manager_test.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. AdTag: "0123456789abcdef0123456789abcdef",
  91. PublicIPv4: "1.2.3.4",
  92. PublicIPv6: "2001:db8::1",
  93. }, 6000)
  94. for _, want := range []string{
  95. "debug = true\n",
  96. "proxy-protocol-listener = true\n",
  97. `prefer-ip = "only-ipv6"`,
  98. `ad-tag = "0123456789abcdef0123456789abcdef"`,
  99. `public-ipv4 = "1.2.3.4"`,
  100. `public-ipv6 = "2001:db8::1"`,
  101. "[domain-fronting]",
  102. `host = "127.0.0.1"`,
  103. "port = 9443",
  104. "proxy-protocol = true\n",
  105. "[throttle]",
  106. "max-connections = 5000",
  107. } {
  108. if !strings.Contains(full, want) {
  109. t.Fatalf("full config missing %q:\n%s", want, full)
  110. }
  111. }
  112. if strings.Contains(full, `ip = "127.0.0.1"`) {
  113. t.Fatalf("domain-fronting must use host, not the deprecated ip key:\n%s", full)
  114. }
  115. // TOML requires top-level keys before any [section] header, and [secrets]
  116. // must be the final section so trailing keys are not swallowed by a table.
  117. if strings.Index(full, "prefer-ip") > strings.Index(full, "[domain-fronting]") {
  118. t.Fatalf("top-level keys must precede the [domain-fronting] section:\n%s", full)
  119. }
  120. if strings.LastIndex(full, "[secrets]") < strings.Index(full, "[domain-fronting]") {
  121. t.Fatalf("[secrets] must be the final section:\n%s", full)
  122. }
  123. if strings.LastIndex(full, "[secrets]") < strings.Index(full, "[throttle]") {
  124. t.Fatalf("[throttle] must precede [secrets]:\n%s", full)
  125. }
  126. }
  127. func TestRenderConfigXrayEgress(t *testing.T) {
  128. // Routing through Xray emits a [network] proxies upstream pointing at the
  129. // loopback SOCKS bridge, before the [secrets] section.
  130. routed := renderConfig(Instance{
  131. Secrets: []SecretEntry{{Name: "a", Secret: "ee22"}},
  132. Listen: "0.0.0.0", Port: 443,
  133. RouteThroughXray: true, XrayRoutePort: 50000,
  134. }, 7000)
  135. if !strings.Contains(routed, "[network]") ||
  136. !strings.Contains(routed, `proxies = ["socks5://127.0.0.1:50000"]`) {
  137. t.Fatalf("routed config must emit the SOCKS upstream:\n%s", routed)
  138. }
  139. if strings.Index(routed, "[network]") > strings.Index(routed, "[secrets]") {
  140. t.Fatalf("[network] must precede [secrets]:\n%s", routed)
  141. }
  142. // Without the flag (or without a port) the section is omitted.
  143. for _, inst := range []Instance{
  144. {Secrets: []SecretEntry{{Name: "a", Secret: "ee"}}, Listen: "0.0.0.0", Port: 443},
  145. {Secrets: []SecretEntry{{Name: "a", Secret: "ee"}}, Listen: "0.0.0.0", Port: 443, RouteThroughXray: true},
  146. } {
  147. if got := renderConfig(inst, 7000); strings.Contains(got, "[network]") {
  148. t.Fatalf("unrouted config must omit [network]:\n%s", got)
  149. }
  150. }
  151. }
  152. func TestFingerprintSplit(t *testing.T) {
  153. base := Instance{Secrets: []SecretEntry{{Name: "a", Secret: "ee"}}, Listen: "0.0.0.0", Port: 443}
  154. for name, mutate := range map[string]func(*Instance){
  155. "debug": func(i *Instance) { i.Debug = true },
  156. "listener": func(i *Instance) { i.ProxyProtocolListener = true },
  157. "preferIp": func(i *Instance) { i.PreferIP = "only-ipv4" },
  158. "frontingIP": func(i *Instance) { i.FrontingIP = "127.0.0.1" },
  159. "frontingPort": func(i *Instance) { i.FrontingPort = 9443 },
  160. "frontingProxy": func(i *Instance) { i.FrontingProxyProtocol = true },
  161. "throttle": func(i *Instance) { i.ThrottleMaxConnections = 5000 },
  162. "routeXray": func(i *Instance) { i.RouteThroughXray = true },
  163. "routeXrayPort": func(i *Instance) { i.XrayRoutePort = 50000 },
  164. "port": func(i *Instance) { i.Port = 8443 },
  165. "listen": func(i *Instance) { i.Listen = "127.0.0.1" },
  166. "publicIpv4": func(i *Instance) { i.PublicIPv4 = "1.2.3.4" },
  167. "publicIpv6": func(i *Instance) { i.PublicIPv6 = "2001:db8::1" },
  168. } {
  169. t.Run("structural/"+name, func(t *testing.T) {
  170. changed := base
  171. mutate(&changed)
  172. if base.structuralFingerprint() == changed.structuralFingerprint() {
  173. t.Fatalf("structural fingerprint must change when %s changes", name)
  174. }
  175. if base.secretsFingerprint() != changed.secretsFingerprint() {
  176. t.Fatalf("secrets fingerprint must stay put when %s changes", name)
  177. }
  178. })
  179. }
  180. for name, mutate := range map[string]func(*Instance){
  181. "add": func(i *Instance) { i.Secrets = append(i.Secrets, SecretEntry{Name: "b", Secret: "ff"}) },
  182. "rekey": func(i *Instance) { i.Secrets = []SecretEntry{{Name: "a", Secret: "ee99"}} },
  183. "remove": func(i *Instance) { i.Secrets = nil },
  184. "rename": func(i *Instance) { i.Secrets = []SecretEntry{{Name: "a2", Secret: "ee"}} },
  185. "adTag": func(i *Instance) { i.AdTag = "0123456789abcdef0123456789abcdef" },
  186. } {
  187. t.Run("secrets/"+name, func(t *testing.T) {
  188. changed := base
  189. changed.Secrets = append([]SecretEntry(nil), base.Secrets...)
  190. mutate(&changed)
  191. if base.secretsFingerprint() == changed.secretsFingerprint() {
  192. t.Fatalf("secrets fingerprint must change on a %s", name)
  193. }
  194. if base.structuralFingerprint() != changed.structuralFingerprint() {
  195. t.Fatalf("structural fingerprint must stay put on a %s", name)
  196. }
  197. })
  198. }
  199. t.Run("orderInsensitive", func(t *testing.T) {
  200. forward := Instance{Secrets: []SecretEntry{{Name: "alice", Secret: "ee11"}, {Name: "bob", Secret: "ee22"}}}
  201. reversed := Instance{Secrets: []SecretEntry{{Name: "bob", Secret: "ee22"}, {Name: "alice", Secret: "ee11"}}}
  202. if got, want := forward.secretsFingerprint(), "adtag=|alice=ee11|bob=ee22"; got != want {
  203. t.Fatalf("secrets fingerprint must join sorted pairs: got %q, want %q", got, want)
  204. }
  205. if forward.secretsFingerprint() != reversed.secretsFingerprint() {
  206. t.Fatal("secrets fingerprint must not depend on client order")
  207. }
  208. })
  209. }