build_urls_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. // A subscriber arriving via a reverse proxy (subURI configured with full
  63. // HTTPS URL) must see the same scheme+host in the JSON and Clash Copy
  64. // URLs as in the main subURL — not the raw sub-server port 2096.
  65. func TestBuildURLs_DerivesJsonFromConfiguredSubURI(t *testing.T) {
  66. initSubDB(t)
  67. s := &SubService{}
  68. s.PrepareForRequest("sub.example.com")
  69. // Simulate the admin having set subURI (reverse-proxy setup).
  70. database.GetDB().Exec(
  71. "INSERT INTO settings (key, value) VALUES (?, ?)",
  72. "subURI", "https://example.com/sub-xxx/")
  73. subURL, jsonURL, clashURL := s.BuildURLs("/sub-xxx/", "/json/", "/clash/", "ABC")
  74. if subURL != "https://example.com/sub-xxx/ABC" {
  75. t.Fatalf("subURL = %q", subURL)
  76. }
  77. if jsonURL != "https://example.com/json/ABC" {
  78. t.Fatalf("jsonURL = %q (should derive scheme+host from subURI), want %q", jsonURL, "https://example.com/json/ABC")
  79. }
  80. if clashURL != "https://example.com/clash/ABC" {
  81. t.Fatalf("clashURL = %q (should derive scheme+host from subURI), want %q", clashURL, "https://example.com/clash/ABC")
  82. }
  83. }
  84. // A malformed subURI (no scheme/host) must not leak a broken base into the
  85. // JSON/Clash URLs; BuildURLs should fall back to the request-derived base.
  86. func TestBuildURLs_MalformedSubURIFallsBackToRequestBase(t *testing.T) {
  87. initSubDB(t)
  88. s := &SubService{}
  89. s.PrepareForRequest("sub.example.com")
  90. // A value with no scheme can't yield a usable scheme+host.
  91. database.GetDB().Exec(
  92. "INSERT INTO settings (key, value) VALUES (?, ?)",
  93. "subURI", "example.com/sub-xxx/")
  94. _, jsonURL, clashURL := s.BuildURLs("/sub-xxx/", "/json/", "/clash/", "ABC")
  95. if jsonURL != "http://sub.example.com:2096/json/ABC" {
  96. t.Fatalf("jsonURL = %q, want fallback to request base %q", jsonURL, "http://sub.example.com:2096/json/ABC")
  97. }
  98. if clashURL != "http://sub.example.com:2096/clash/ABC" {
  99. t.Fatalf("clashURL = %q, want fallback to request base %q", clashURL, "http://sub.example.com:2096/clash/ABC")
  100. }
  101. }