fix(subscriptions): avoid shared mutable state during generation (#5270)

* fix(subscriptions): avoid shared mutable state during generation

* fix(subscriptions): serve external-link-only subs in JSON/Clash; load remark settings per request

The ForRequest refactor added an early `len(inbounds) == 0` return to
GetJson/GetClash that fired before external links were fetched, so a
subscription whose only entries are external links (or whose inbounds are
all disabled) rendered empty in the JSON and Clash formats. Drop the
premature check — the existing inbounds+externalLinks empty guard already
covers the truly-empty case.

Also load datepicker/emailInRemark in PrepareForRequest rather than only in
getSubs, so JSON and Clash remarks honor these settings instead of seeing
the zero values (emailInRemark previously depended on the shared-state leak
this PR fixes).

Add a regression test covering an external-link-only sub across both formats.

---------

Co-authored-by: Sanaei <ho3ein.sanaei@gmail.com>
This commit is contained in:
n0ctal
2026-06-15 19:23:47 +05:00
committed by GitHub
parent 71616b7cf2
commit ac8cb505d1
7 changed files with 139 additions and 52 deletions
+21
View File
@@ -70,6 +70,27 @@ func TestBuildURLs_EmptySubId(t *testing.T) {
}
}
func TestForRequestDoesNotMutateSharedService(t *testing.T) {
initSubDB(t)
base := &SubService{}
first := base.ForRequest("first.example.com")
second := base.ForRequest("second.example.com")
if base.address != "" || base.nodesByID != nil {
t.Fatalf("ForRequest mutated the shared service: address=%q nodes=%v", base.address, base.nodesByID)
}
firstURL, _, _ := first.BuildURLs("/sub/", "/json/", "/clash/", "ABC")
secondURL, _, _ := second.BuildURLs("/sub/", "/json/", "/clash/", "ABC")
if !strings.Contains(firstURL, "first.example.com") {
t.Fatalf("first request URL = %q, want first.example.com", firstURL)
}
if !strings.Contains(secondURL, "second.example.com") {
t.Fatalf("second request URL = %q, want second.example.com", secondURL)
}
}
// 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.