manager_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package mtproto
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  6. )
  7. func TestParseMetricLine(t *testing.T) {
  8. name, labels, val, err := parseMetricLine(`mtg_traffic{direction="to_client"} 12345`)
  9. if err != nil {
  10. t.Fatal(err)
  11. }
  12. if name != "mtg_traffic" {
  13. t.Fatalf("name=%q", name)
  14. }
  15. if labels["direction"] != "to_client" {
  16. t.Fatalf("labels=%v", labels)
  17. }
  18. if val != 12345 {
  19. t.Fatalf("val=%v", val)
  20. }
  21. name2, _, val2, err2 := parseMetricLine(`mtg_concurrency 7`)
  22. if err2 != nil {
  23. t.Fatal(err2)
  24. }
  25. if name2 != "mtg_concurrency" || val2 != 7 {
  26. t.Fatalf("got %q %v", name2, val2)
  27. }
  28. }
  29. func TestInstanceFromInbound(t *testing.T) {
  30. ib := &model.Inbound{
  31. Id: 3,
  32. Tag: "inbound-3",
  33. Listen: "0.0.0.0",
  34. Port: 8443,
  35. Protocol: model.MTProto,
  36. Settings: `{"fakeTlsDomain":"example.com","secret":"",` +
  37. `"debug":true,"proxyProtocolListener":true,"preferIp":"prefer-ipv4",` +
  38. `"domainFronting":{"ip":"127.0.0.1","port":9443,"proxyProtocol":true},` +
  39. `"routeThroughXray":true,"routeXrayPort":50000}`,
  40. }
  41. inst, ok := InstanceFromInbound(ib)
  42. if !ok {
  43. t.Fatal("expected a usable instance")
  44. }
  45. if inst.Secret == "" {
  46. t.Fatal("secret should be healed to a non-empty value")
  47. }
  48. if inst.Port != 8443 || inst.Id != 3 {
  49. t.Fatalf("bad instance %+v", inst)
  50. }
  51. if !inst.Debug || !inst.ProxyProtocolListener || inst.PreferIP != "prefer-ipv4" {
  52. t.Fatalf("scalar options not parsed: %+v", inst)
  53. }
  54. if inst.FrontingIP != "127.0.0.1" || inst.FrontingPort != 9443 || !inst.FrontingProxyProtocol {
  55. t.Fatalf("domain-fronting not parsed: %+v", inst)
  56. }
  57. if !inst.RouteThroughXray || inst.XrayRoutePort != 50000 {
  58. t.Fatalf("xray routing not parsed: %+v", inst)
  59. }
  60. if _, ok := InstanceFromInbound(&model.Inbound{Protocol: model.VLESS}); ok {
  61. t.Fatal("non-mtproto inbound should not produce an instance")
  62. }
  63. }
  64. func TestRenderConfig(t *testing.T) {
  65. // A bare instance emits only the required keys and the prometheus block,
  66. // with no optional keys and no [domain-fronting] section.
  67. bare := renderConfig(Instance{Secret: "ee00", Listen: "0.0.0.0", Port: 8443}, 5000)
  68. for _, unwanted := range []string{"debug", "proxy-protocol-listener", "prefer-ip", "[domain-fronting]"} {
  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, "[stats.prometheus]") || !strings.Contains(bare, "127.0.0.1:5000") {
  77. t.Fatalf("prometheus block must always be present:\n%s", bare)
  78. }
  79. // A fully configured instance emits every option and the fronting section.
  80. full := renderConfig(Instance{
  81. Secret: "ee11", Listen: "0.0.0.0", Port: 443,
  82. Debug: true, ProxyProtocolListener: true, PreferIP: "only-ipv6",
  83. FrontingIP: "127.0.0.1", FrontingPort: 9443, FrontingProxyProtocol: true,
  84. }, 6000)
  85. for _, want := range []string{
  86. "debug = true\n",
  87. "proxy-protocol-listener = true\n",
  88. `prefer-ip = "only-ipv6"`,
  89. "[domain-fronting]",
  90. `ip = "127.0.0.1"`,
  91. "port = 9443",
  92. "proxy-protocol = true\n",
  93. } {
  94. if !strings.Contains(full, want) {
  95. t.Fatalf("full config missing %q:\n%s", want, full)
  96. }
  97. }
  98. // TOML requires top-level keys before any [section] header.
  99. if strings.Index(full, "prefer-ip") > strings.Index(full, "[domain-fronting]") {
  100. t.Fatalf("top-level keys must precede the [domain-fronting] section:\n%s", full)
  101. }
  102. if strings.LastIndex(full, "[domain-fronting]") > strings.Index(full, "[stats.prometheus]") {
  103. t.Fatalf("[domain-fronting] must precede [stats.prometheus]:\n%s", full)
  104. }
  105. }
  106. func TestRenderConfigXrayEgress(t *testing.T) {
  107. // Routing through Xray emits a [network] proxies upstream pointing at the
  108. // loopback SOCKS bridge, before the prometheus block.
  109. routed := renderConfig(Instance{
  110. Secret: "ee22", Listen: "0.0.0.0", Port: 443,
  111. RouteThroughXray: true, XrayRoutePort: 50000,
  112. }, 7000)
  113. if !strings.Contains(routed, "[network]") ||
  114. !strings.Contains(routed, `proxies = ["socks5://127.0.0.1:50000"]`) {
  115. t.Fatalf("routed config must emit the SOCKS upstream:\n%s", routed)
  116. }
  117. if strings.Index(routed, "[network]") > strings.Index(routed, "[stats.prometheus]") {
  118. t.Fatalf("[network] must precede [stats.prometheus]:\n%s", routed)
  119. }
  120. // Without the flag (or without a port) the section is omitted.
  121. for _, inst := range []Instance{
  122. {Secret: "ee", Listen: "0.0.0.0", Port: 443},
  123. {Secret: "ee", Listen: "0.0.0.0", Port: 443, RouteThroughXray: true},
  124. } {
  125. if got := renderConfig(inst, 7000); strings.Contains(got, "[network]") {
  126. t.Fatalf("unrouted config must omit [network]:\n%s", got)
  127. }
  128. }
  129. }
  130. func TestFingerprintReactsToOptions(t *testing.T) {
  131. base := Instance{Secret: "ee", Listen: "0.0.0.0", Port: 443}
  132. for name, mutate := range map[string]func(*Instance){
  133. "debug": func(i *Instance) { i.Debug = true },
  134. "listener": func(i *Instance) { i.ProxyProtocolListener = true },
  135. "preferIp": func(i *Instance) { i.PreferIP = "only-ipv4" },
  136. "frontingIP": func(i *Instance) { i.FrontingIP = "127.0.0.1" },
  137. "frontingPort": func(i *Instance) { i.FrontingPort = 9443 },
  138. "frontingProxy": func(i *Instance) { i.FrontingProxyProtocol = true },
  139. "routeXray": func(i *Instance) { i.RouteThroughXray = true },
  140. "routeXrayPort": func(i *Instance) { i.XrayRoutePort = 50000 },
  141. } {
  142. changed := base
  143. mutate(&changed)
  144. if base.fingerprint() == changed.fingerprint() {
  145. t.Fatalf("fingerprint must change when %s changes", name)
  146. }
  147. }
  148. }