mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-08-24 20:07:13 +00:00
fix: derive JSON/Clash subscription URLs from configured subURI (#5203)
* fix: derive JSON/Clash subscription URLs from configured subURI When subURI is explicitly configured (reverse-proxy setup) but subJsonURI or subClashURI are not, BuildSubURIBase generates URLs with the raw sub- server port (2096) and the wrong scheme (http), producing broken links on the subscription page (e.g. http://domain:2096/json/SUB_ID). Fix: in BuildURLs, when subURI is set, extract its scheme+host and use that as the base for all unconfigured sibling URLs instead of calling BuildSubURIBase. This ensures JSON and Clash Copy URLs match the reverse- proxy endpoint. Fixes: JSON/Clash subscription URLs shown on the subscription info page now correctly inherit the configured subURI's scheme and host. * fix(sub): fall back to request base when configured subURI is unparseable Harden the JSON/Clash URL derivation added for the reverse-proxy fix: extractBaseFromURI now returns "" when the configured subURI has no scheme/host, and BuildURLs falls back to the request-derived base in that case instead of emitting a broken value (e.g. ":///json/ABC"). Add a regression test covering a scheme-less subURI. --------- Co-authored-by: w3struk <w3struk@gmail.com> Co-authored-by: Sanaei <ho3ein.sanaei@gmail.com>
This commit is contained in:
@@ -69,3 +69,51 @@ func TestBuildURLs_EmptySubId(t *testing.T) {
|
||||
t.Fatalf("empty subId must yield empty URLs, got %q %q %q", a, b, c)
|
||||
}
|
||||
}
|
||||
|
||||
// A subscriber arriving via a reverse proxy (subURI configured with full
|
||||
// HTTPS URL) must see the same scheme+host in the JSON and Clash Copy
|
||||
// URLs as in the main subURL — not the raw sub-server port 2096.
|
||||
func TestBuildURLs_DerivesJsonFromConfiguredSubURI(t *testing.T) {
|
||||
initSubDB(t)
|
||||
s := &SubService{}
|
||||
s.PrepareForRequest("sub.example.com")
|
||||
|
||||
// Simulate the admin having set subURI (reverse-proxy setup).
|
||||
database.GetDB().Exec(
|
||||
"INSERT INTO settings (key, value) VALUES (?, ?)",
|
||||
"subURI", "https://example.com/sub-xxx/")
|
||||
|
||||
subURL, jsonURL, clashURL := s.BuildURLs("/sub-xxx/", "/json/", "/clash/", "ABC")
|
||||
|
||||
if subURL != "https://example.com/sub-xxx/ABC" {
|
||||
t.Fatalf("subURL = %q", subURL)
|
||||
}
|
||||
if jsonURL != "https://example.com/json/ABC" {
|
||||
t.Fatalf("jsonURL = %q (should derive scheme+host from subURI), want %q", jsonURL, "https://example.com/json/ABC")
|
||||
}
|
||||
if clashURL != "https://example.com/clash/ABC" {
|
||||
t.Fatalf("clashURL = %q (should derive scheme+host from subURI), want %q", clashURL, "https://example.com/clash/ABC")
|
||||
}
|
||||
}
|
||||
|
||||
// A malformed subURI (no scheme/host) must not leak a broken base into the
|
||||
// JSON/Clash URLs; BuildURLs should fall back to the request-derived base.
|
||||
func TestBuildURLs_MalformedSubURIFallsBackToRequestBase(t *testing.T) {
|
||||
initSubDB(t)
|
||||
s := &SubService{}
|
||||
s.PrepareForRequest("sub.example.com")
|
||||
|
||||
// A value with no scheme can't yield a usable scheme+host.
|
||||
database.GetDB().Exec(
|
||||
"INSERT INTO settings (key, value) VALUES (?, ?)",
|
||||
"subURI", "example.com/sub-xxx/")
|
||||
|
||||
_, jsonURL, clashURL := s.BuildURLs("/sub-xxx/", "/json/", "/clash/", "ABC")
|
||||
|
||||
if jsonURL != "http://sub.example.com:2096/json/ABC" {
|
||||
t.Fatalf("jsonURL = %q, want fallback to request base %q", jsonURL, "http://sub.example.com:2096/json/ABC")
|
||||
}
|
||||
if clashURL != "http://sub.example.com:2096/clash/ABC" {
|
||||
t.Fatalf("clashURL = %q, want fallback to request base %q", clashURL, "http://sub.example.com:2096/clash/ABC")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user