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:
w3struk
2026-06-11 23:05:38 +05:00
committed by GitHub
parent 7bcc5830c6
commit ec45d3491a
2 changed files with 75 additions and 2 deletions
+27 -2
View File
@@ -2123,12 +2123,37 @@ func (s *SubService) BuildURLs(subPath, subJsonPath, subClashPath, subId string)
base := s.settingService.BuildSubURIBase(s.address)
subURL = s.buildSingleURL(configuredSubURI, base, subPath, subId)
subJsonURL = s.buildSingleURL(configuredSubJsonURI, base, subJsonPath, subId)
subClashURL = s.buildSingleURL(configuredSubClashURI, base, subClashPath, subId)
// When subURI is explicitly configured (reverse-proxy setup), use its
// scheme+host as the base for JSON and Clash URLs so they match the
// reverse-proxy endpoint instead of the raw sub-server port. Fall back
// to the request-derived base if subURI is empty or can't be parsed
// into a scheme+host (e.g. a malformed value with no scheme).
jsonClashBase := base
if configuredSubURI != "" {
if derived := s.extractBaseFromURI(configuredSubURI); derived != "" {
jsonClashBase = derived
}
}
subJsonURL = s.buildSingleURL(configuredSubJsonURI, jsonClashBase, subJsonPath, subId)
subClashURL = s.buildSingleURL(configuredSubClashURI, jsonClashBase, subClashPath, subId)
return subURL, subJsonURL, subClashURL
}
// extractBaseFromURI extracts scheme://host from a configured URI.
// e.g., "https://example.com/sub-xxx/" → "https://example.com".
// Returns "" when the URI is empty or lacks a scheme/host, so callers can
// fall back to the request-derived base instead of emitting a broken value.
func (s *SubService) extractBaseFromURI(uri string) string {
u, err := url.Parse(uri)
if err != nil || u.Scheme == "" || u.Host == "" {
return ""
}
return fmt.Sprintf("%s://%s", u.Scheme, u.Host)
}
// buildSingleURL constructs a single URL using configured URI or base components
func (s *SubService) buildSingleURL(configuredURI, base, basePath, subId string) string {
if configuredURI != "" {