manager_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package mtproto
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/mhsanaei/3x-ui/v3/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. }
  40. inst, ok := InstanceFromInbound(ib)
  41. if !ok {
  42. t.Fatal("expected a usable instance")
  43. }
  44. if inst.Secret == "" {
  45. t.Fatal("secret should be healed to a non-empty value")
  46. }
  47. if inst.Port != 8443 || inst.Id != 3 {
  48. t.Fatalf("bad instance %+v", inst)
  49. }
  50. if !inst.Debug || !inst.ProxyProtocolListener || inst.PreferIP != "prefer-ipv4" {
  51. t.Fatalf("scalar options not parsed: %+v", inst)
  52. }
  53. if inst.FrontingIP != "127.0.0.1" || inst.FrontingPort != 9443 || !inst.FrontingProxyProtocol {
  54. t.Fatalf("domain-fronting not parsed: %+v", inst)
  55. }
  56. if _, ok := InstanceFromInbound(&model.Inbound{Protocol: model.VLESS}); ok {
  57. t.Fatal("non-mtproto inbound should not produce an instance")
  58. }
  59. }
  60. func TestRenderConfig(t *testing.T) {
  61. // A bare instance emits only the required keys and the prometheus block,
  62. // with no optional keys and no [domain-fronting] section.
  63. bare := renderConfig(Instance{Secret: "ee00", Listen: "0.0.0.0", Port: 8443}, 5000)
  64. for _, unwanted := range []string{"debug", "proxy-protocol-listener", "prefer-ip", "[domain-fronting]"} {
  65. if strings.Contains(bare, unwanted) {
  66. t.Fatalf("bare config should not contain %q:\n%s", unwanted, bare)
  67. }
  68. }
  69. if !strings.Contains(bare, `bind-to = "0.0.0.0:8443"`) {
  70. t.Fatalf("missing bind-to:\n%s", bare)
  71. }
  72. if !strings.Contains(bare, "[stats.prometheus]") || !strings.Contains(bare, "127.0.0.1:5000") {
  73. t.Fatalf("prometheus block must always be present:\n%s", bare)
  74. }
  75. // A fully configured instance emits every option and the fronting section.
  76. full := renderConfig(Instance{
  77. Secret: "ee11", Listen: "0.0.0.0", Port: 443,
  78. Debug: true, ProxyProtocolListener: true, PreferIP: "only-ipv6",
  79. FrontingIP: "127.0.0.1", FrontingPort: 9443, FrontingProxyProtocol: true,
  80. }, 6000)
  81. for _, want := range []string{
  82. "debug = true\n",
  83. "proxy-protocol-listener = true\n",
  84. `prefer-ip = "only-ipv6"`,
  85. "[domain-fronting]",
  86. `ip = "127.0.0.1"`,
  87. "port = 9443",
  88. "proxy-protocol = true\n",
  89. } {
  90. if !strings.Contains(full, want) {
  91. t.Fatalf("full config missing %q:\n%s", want, full)
  92. }
  93. }
  94. // TOML requires top-level keys before any [section] header.
  95. if strings.Index(full, "prefer-ip") > strings.Index(full, "[domain-fronting]") {
  96. t.Fatalf("top-level keys must precede the [domain-fronting] section:\n%s", full)
  97. }
  98. if strings.LastIndex(full, "[domain-fronting]") > strings.Index(full, "[stats.prometheus]") {
  99. t.Fatalf("[domain-fronting] must precede [stats.prometheus]:\n%s", full)
  100. }
  101. }
  102. func TestFingerprintReactsToOptions(t *testing.T) {
  103. base := Instance{Secret: "ee", Listen: "0.0.0.0", Port: 443}
  104. for name, mutate := range map[string]func(*Instance){
  105. "debug": func(i *Instance) { i.Debug = true },
  106. "listener": func(i *Instance) { i.ProxyProtocolListener = true },
  107. "preferIp": func(i *Instance) { i.PreferIP = "only-ipv4" },
  108. "frontingIP": func(i *Instance) { i.FrontingIP = "127.0.0.1" },
  109. "frontingPort": func(i *Instance) { i.FrontingPort = 9443 },
  110. "frontingProxy": func(i *Instance) { i.FrontingProxyProtocol = true },
  111. } {
  112. changed := base
  113. mutate(&changed)
  114. if base.fingerprint() == changed.fingerprint() {
  115. t.Fatalf("fingerprint must change when %s changes", name)
  116. }
  117. }
  118. }