Files
3x-ui/internal/sub/build_urls_test.go
T
n0ctal ac8cb505d1 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>
2026-06-15 16:23:47 +02:00

141 lines
4.8 KiB
Go

package sub
import (
"path/filepath"
"strings"
"testing"
"github.com/mhsanaei/3x-ui/v3/internal/database"
)
func initSubDB(t *testing.T) {
t.Helper()
if err := database.InitDB(filepath.Join(t.TempDir(), "x-ui.db")); err != nil {
t.Fatalf("InitDB: %v", err)
}
// Close the handle before t.TempDir cleanup so Windows doesn't refuse to
// remove the still-open sqlite file.
t.Cleanup(func() { _ = database.CloseDB() })
}
// The subscription page's Copy URL must be built from the same host the
// subscriber reached the page on (after PrepareForRequest normalizes away a
// loopback/bind address) — never the raw listen IP. A subscriber that hit a
// loopback bind should see "localhost", not "127.0.0.1".
func TestBuildURLs_NormalizesListenIP(t *testing.T) {
initSubDB(t)
s := &SubService{}
s.PrepareForRequest("127.0.0.1")
subURL, _, _ := s.BuildURLs("/sub/", "/json/", "/clash/", "ABC")
if strings.Contains(subURL, "127.0.0.1") {
t.Fatalf("listen IP leaked into Copy URL: %q", subURL)
}
if !strings.Contains(subURL, "localhost") {
t.Fatalf("Copy URL = %q, want a localhost host", subURL)
}
if !strings.HasSuffix(subURL, "/sub/ABC") {
t.Fatalf("Copy URL = %q, want it to end with /sub/ABC", subURL)
}
}
// A subscriber arriving on a real domain gets that exact domain in the Copy
// URL, with the configured sub port — matching the Client Information page.
func TestBuildURLs_UsesSubscriberDomain(t *testing.T) {
initSubDB(t)
s := &SubService{}
s.PrepareForRequest("sub.example.com")
subURL, jsonURL, clashURL := s.BuildURLs("/sub/", "/json/", "/clash/", "ABC")
if subURL != "http://sub.example.com:2096/sub/ABC" {
t.Fatalf("subURL = %q", subURL)
}
if jsonURL != "http://sub.example.com:2096/json/ABC" {
t.Fatalf("jsonURL = %q", jsonURL)
}
if clashURL != "http://sub.example.com:2096/clash/ABC" {
t.Fatalf("clashURL = %q", clashURL)
}
}
func TestBuildURLs_EmptySubId(t *testing.T) {
initSubDB(t)
s := &SubService{}
s.PrepareForRequest("sub.example.com")
a, b, c := s.BuildURLs("/sub/", "/json/", "/clash/", "")
if a != "" || b != "" || c != "" {
t.Fatalf("empty subId must yield empty URLs, got %q %q %q", a, b, c)
}
}
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.
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")
}
}