build_urls_test.go 4.8 KB

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