build_urls_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package sub
  2. import (
  3. "path/filepath"
  4. "strings"
  5. "testing"
  6. "github.com/mhsanaei/3x-ui/v3/internal/database"
  7. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  8. )
  9. func initSubDB(t *testing.T) {
  10. t.Helper()
  11. if err := database.InitDB(filepath.Join(t.TempDir(), "x-ui.db")); err != nil {
  12. t.Fatalf("InitDB: %v", err)
  13. }
  14. // Close the handle before t.TempDir cleanup so Windows doesn't refuse to
  15. // remove the still-open sqlite file.
  16. t.Cleanup(func() { _ = database.CloseDB() })
  17. }
  18. // The subscription page's Copy URL must be built from the same host the
  19. // subscriber reached the page on (after PrepareForRequest normalizes away a
  20. // loopback/bind address) — never the raw listen IP. A subscriber that hit a
  21. // loopback bind should see "localhost", not "127.0.0.1".
  22. func TestBuildURLs_NormalizesListenIP(t *testing.T) {
  23. initSubDB(t)
  24. s := &SubService{}
  25. s.PrepareForRequest("127.0.0.1")
  26. subURL, _, _ := s.BuildURLs("/sub/", "/json/", "/clash/", "ABC")
  27. if strings.Contains(subURL, "127.0.0.1") {
  28. t.Fatalf("listen IP leaked into Copy URL: %q", subURL)
  29. }
  30. if !strings.Contains(subURL, "localhost") {
  31. t.Fatalf("Copy URL = %q, want a localhost host", subURL)
  32. }
  33. if !strings.HasSuffix(subURL, "/sub/ABC") {
  34. t.Fatalf("Copy URL = %q, want it to end with /sub/ABC", subURL)
  35. }
  36. }
  37. // A subscriber arriving on a real domain gets that exact domain in the Copy
  38. // URL, with the configured sub port — matching the Client Information page.
  39. func TestBuildURLs_UsesSubscriberDomain(t *testing.T) {
  40. initSubDB(t)
  41. s := &SubService{}
  42. s.PrepareForRequest("sub.example.com")
  43. subURL, jsonURL, clashURL := s.BuildURLs("/sub/", "/json/", "/clash/", "ABC")
  44. if subURL != "http://sub.example.com:2096/sub/ABC" {
  45. t.Fatalf("subURL = %q", subURL)
  46. }
  47. if jsonURL != "http://sub.example.com:2096/json/ABC" {
  48. t.Fatalf("jsonURL = %q", jsonURL)
  49. }
  50. if clashURL != "http://sub.example.com:2096/clash/ABC" {
  51. t.Fatalf("clashURL = %q", clashURL)
  52. }
  53. }
  54. // A local wildcard inbound (no node, no custom share address, blank/0.0.0.0
  55. // listen) must not advertise the raw request host when it carries a client IP
  56. // that leaked in behind NAT/proxy. The admin's configured panel host wins for
  57. // this last-resort fallback; without a configured host the request host stands.
  58. func TestResolveInboundAddress_PrefersConfiguredHostOverClientIP(t *testing.T) {
  59. initSubDB(t)
  60. local := &model.Inbound{Listen: "", ShareAddrStrategy: "node"}
  61. s := &SubService{}
  62. s.PrepareForRequest("192.168.1.50") // a client LAN IP that reached the panel
  63. if got := s.resolveInboundAddress(local); got != "192.168.1.50" {
  64. t.Fatalf("with no configured host the request host stands, got %q", got)
  65. }
  66. if err := database.GetDB().Create(&model.Setting{Key: "subDomain", Value: "panel.example.com"}).Error; err != nil {
  67. t.Fatalf("set subDomain: %v", err)
  68. }
  69. s2 := &SubService{}
  70. s2.PrepareForRequest("192.168.1.50")
  71. if got := s2.resolveInboundAddress(local); got != "panel.example.com" {
  72. t.Fatalf("configured host must win over the leaked client IP, got %q", got)
  73. }
  74. }
  75. func TestBuildURLs_EmptySubId(t *testing.T) {
  76. initSubDB(t)
  77. s := &SubService{}
  78. s.PrepareForRequest("sub.example.com")
  79. a, b, c := s.BuildURLs("/sub/", "/json/", "/clash/", "")
  80. if a != "" || b != "" || c != "" {
  81. t.Fatalf("empty subId must yield empty URLs, got %q %q %q", a, b, c)
  82. }
  83. }
  84. func TestForRequestDoesNotMutateSharedService(t *testing.T) {
  85. initSubDB(t)
  86. base := &SubService{}
  87. first := base.ForRequest("first.example.com")
  88. second := base.ForRequest("second.example.com")
  89. if base.address != "" || base.nodesByID != nil {
  90. t.Fatalf("ForRequest mutated the shared service: address=%q nodes=%v", base.address, base.nodesByID)
  91. }
  92. firstURL, _, _ := first.BuildURLs("/sub/", "/json/", "/clash/", "ABC")
  93. secondURL, _, _ := second.BuildURLs("/sub/", "/json/", "/clash/", "ABC")
  94. if !strings.Contains(firstURL, "first.example.com") {
  95. t.Fatalf("first request URL = %q, want first.example.com", firstURL)
  96. }
  97. if !strings.Contains(secondURL, "second.example.com") {
  98. t.Fatalf("second request URL = %q, want second.example.com", secondURL)
  99. }
  100. }
  101. // A subscriber arriving via a reverse proxy (subURI configured with full
  102. // HTTPS URL) must see the same scheme+host in the JSON and Clash Copy
  103. // URLs as in the main subURL — not the raw sub-server port 2096.
  104. func TestBuildURLs_DerivesJsonFromConfiguredSubURI(t *testing.T) {
  105. initSubDB(t)
  106. s := &SubService{}
  107. s.PrepareForRequest("sub.example.com")
  108. // Simulate the admin having set subURI (reverse-proxy setup).
  109. database.GetDB().Exec(
  110. "INSERT INTO settings (key, value) VALUES (?, ?)",
  111. "subURI", "https://example.com/sub-xxx/")
  112. subURL, jsonURL, clashURL := s.BuildURLs("/sub-xxx/", "/json/", "/clash/", "ABC")
  113. if subURL != "https://example.com/sub-xxx/ABC" {
  114. t.Fatalf("subURL = %q", subURL)
  115. }
  116. if jsonURL != "https://example.com/json/ABC" {
  117. t.Fatalf("jsonURL = %q (should derive scheme+host from subURI), want %q", jsonURL, "https://example.com/json/ABC")
  118. }
  119. if clashURL != "https://example.com/clash/ABC" {
  120. t.Fatalf("clashURL = %q (should derive scheme+host from subURI), want %q", clashURL, "https://example.com/clash/ABC")
  121. }
  122. }
  123. // A malformed subURI (no scheme/host) must not leak a broken base into the
  124. // JSON/Clash URLs; BuildURLs should fall back to the request-derived base.
  125. func TestBuildURLs_MalformedSubURIFallsBackToRequestBase(t *testing.T) {
  126. initSubDB(t)
  127. s := &SubService{}
  128. s.PrepareForRequest("sub.example.com")
  129. // A value with no scheme can't yield a usable scheme+host.
  130. database.GetDB().Exec(
  131. "INSERT INTO settings (key, value) VALUES (?, ?)",
  132. "subURI", "example.com/sub-xxx/")
  133. _, jsonURL, clashURL := s.BuildURLs("/sub-xxx/", "/json/", "/clash/", "ABC")
  134. if jsonURL != "http://sub.example.com:2096/json/ABC" {
  135. t.Fatalf("jsonURL = %q, want fallback to request base %q", jsonURL, "http://sub.example.com:2096/json/ABC")
  136. }
  137. if clashURL != "http://sub.example.com:2096/clash/ABC" {
  138. t.Fatalf("clashURL = %q, want fallback to request base %q", clashURL, "http://sub.example.com:2096/clash/ABC")
  139. }
  140. }