Per SETTINGS-AUDIT (all three tiers approved 2026-10-23): Dead settings: - panelOutbound, ipLimitAllowlist (+ orphan parser job), outboundDownThreshold - tg fake events outbound.down/outbound.up/xray.crash (no publisher in lite) - phantom routing sub-tabs (Happ/Clash/Incy) and subJsonRules direct-rules tab (no backend keys); subHideSettings moved to the subscription info tab first Upstream remnants: - ldap* / smtp* model+schema keys, EmailNotifications orphan component - twoFactorEnable/twoFactorToken + login-page probe (no backend handler) - externalTrafficInform* Newly approved removals: - JSON subscription family (producer, routes, UA auto-detect, mux/finalmask/ observatory) and Clash subscription family (producer, routes, UA detect) - SubBalancer (table, service, controller, settings tab) - subThemeDir (subscription page falls back to the built-in template) - datepicker jalalian option (gregorian only; Persian picker removed) JSON/Clash endpoints now 404; link-format output pinned by regression tests. Guarded keys untouched: webListen/webPort/webBasePath/certs, tg* chain, expireDiff/trafficDiff/tgBotProxy, subHideSettings, pageSize/tgLang.
131 lines
4.2 KiB
Go
131 lines
4.2 KiB
Go
package sub
|
|
|
|
import (
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database"
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
|
)
|
|
|
|
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/", "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 := s.BuildURLs("/sub/", "ABC")
|
|
|
|
if subURL != "http://sub.example.com:2096/sub/ABC" {
|
|
t.Fatalf("subURL = %q", subURL)
|
|
}
|
|
}
|
|
|
|
// A local wildcard inbound (no node, no custom share address, blank/0.0.0.0
|
|
// listen) must not advertise the raw request host when it carries a client IP
|
|
// that leaked in behind NAT/proxy. The admin's configured panel host wins for
|
|
// this last-resort fallback; without a configured host the request host stands.
|
|
func TestResolveInboundAddress_PrefersConfiguredHostOverClientIP(t *testing.T) {
|
|
initSubDB(t)
|
|
local := &model.Inbound{Listen: "", ShareAddrStrategy: "node"}
|
|
|
|
s := &SubService{}
|
|
s.PrepareForRequest("192.168.1.50") // a client LAN IP that reached the panel
|
|
if got := s.resolveInboundAddress(local); got != "192.168.1.50" {
|
|
t.Fatalf("with no configured host the request host stands, got %q", got)
|
|
}
|
|
|
|
if err := database.GetDB().Create(&model.Setting{Key: "subDomain", Value: "panel.example.com"}).Error; err != nil {
|
|
t.Fatalf("set subDomain: %v", err)
|
|
}
|
|
s2 := &SubService{}
|
|
s2.PrepareForRequest("192.168.1.50")
|
|
if got := s2.resolveInboundAddress(local); got != "panel.example.com" {
|
|
t.Fatalf("configured host must win over the leaked client IP, got %q", got)
|
|
}
|
|
}
|
|
|
|
func TestBuildURLs_EmptySubId(t *testing.T) {
|
|
initSubDB(t)
|
|
s := &SubService{}
|
|
s.PrepareForRequest("sub.example.com")
|
|
a := s.BuildURLs("/sub/", "")
|
|
if a != "" {
|
|
t.Fatalf("empty subId must yield an empty URL, got %q", a)
|
|
}
|
|
}
|
|
|
|
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/", "ABC")
|
|
secondURL := second.BuildURLs("/sub/", "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) gets that exact base in the Copy URL.
|
|
func TestBuildURLs_UsesConfiguredSubURI(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 := s.BuildURLs("/sub-xxx/", "ABC")
|
|
|
|
if subURL != "https://example.com/sub-xxx/ABC" {
|
|
t.Fatalf("subURL = %q", subURL)
|
|
}
|
|
}
|