fix(sub): drive display remarks from the template and split multi-host subpage links

Unify remark generation around the Remark Template. Display contexts (Clients-page QR/Info modals and the HTML sub info page) now render the template name-only client/identity part instead of a hardcoded fallback; the subscription body keeps the full template on a client first link and name-only thereafter. The default template gains the email token so the client email shows by default again (#5532).

BuildPageData now splits each multi-link entry (one link per host of an inbound) into a separate row, so the sub page no longer collapses several host links onto a single mangled line. QR captions on the Clients QR modal and the sub page reuse the link fragment remark.
This commit is contained in:
MHSanaei
2026-06-24 16:45:23 +02:00
parent 5dbd5b1d12
commit b0c1156dd6
8 changed files with 97 additions and 37 deletions

View File

@@ -13,7 +13,7 @@ export class AllSetting {
pageSize = 25; pageSize = 25;
expireDiff = 0; expireDiff = 0;
trafficDiff = 0; trafficDiff = 0;
remarkTemplate = '{{INBOUND}}|📊{{TRAFFIC_LEFT}}|⏳{{DAYS_LEFT}}D'; remarkTemplate = '{{INBOUND}}-{{EMAIL}}|📊{{TRAFFIC_LEFT}}|⏳{{DAYS_LEFT}}D';
datepicker: 'gregorian' | 'jalalian' = 'gregorian'; datepicker: 'gregorian' | 'jalalian' = 'gregorian';
tgBotEnable = false; tgBotEnable = false;
tgBotToken = ''; tgBotToken = '';

View File

@@ -106,7 +106,7 @@ export default function ClientQrModal({
children: ( children: (
<QrPanel <QrPanel
value={link} value={link}
remark={`${client?.email || ''} #${idx + 1}`} remark={parts?.remark || `${client?.email || ''} #${idx + 1}`}
showQr={!isPostQuantumLink(link)} showQr={!isPostQuantumLink(link)}
/> />
), ),

View File

@@ -421,7 +421,7 @@ export default function SubPage() {
const parts = parseLinkParts(link); const parts = parseLinkParts(link);
const fallback = `Link ${idx + 1}`; const fallback = `Link ${idx + 1}`;
const rowTitle = parts?.remark || fallback; const rowTitle = parts?.remark || fallback;
const qrLabel = [parts?.remark, linkEmails[idx]].filter(Boolean).join('-') || rowTitle; const qrLabel = parts?.remark || rowTitle;
const canQr = !isPostQuantumLink(link); const canQr = !isPostQuantumLink(link);
return ( return (
<div key={link} className="sub-link-row"> <div key={link} className="sub-link-row">

View File

@@ -0,0 +1,37 @@
package sub
import (
"reflect"
"strings"
"testing"
"github.com/mhsanaei/3x-ui/v3/internal/xray"
)
// A single getSubs entry can hold several links (one per host of an inbound)
// joined by newlines. BuildPageData must split them into one entry per link, with
// the email replicated, so the subpage renders one row per host instead of
// collapsing them onto a single mangled line.
func TestBuildPageData_SplitsMultiHostLinks(t *testing.T) {
s := &SubService{}
subs := []string{
"vless://a@h1:443?type=tcp#DE-john@x\nvless://a@h2:443?type=tcp#DE-john@x\nvless://a@h3:443?type=tcp#DE-john@x",
"vless://b@h:443?type=tcp#FR-alice@x",
}
emails := []string{"john@x", "alice@x"}
page := s.BuildPageData("s1", "", xray.ClientTraffic{}, 0, subs, emails, "", "", "", "/", "", "")
if len(page.Result) != 4 {
t.Fatalf("Result len = %d, want 4 (3 host links + 1 single link)", len(page.Result))
}
for i, link := range page.Result {
if strings.Contains(link, "\n") {
t.Fatalf("Result[%d] still multi-line: %q", i, link)
}
}
wantEmails := []string{"john@x", "john@x", "john@x", "alice@x"}
if !reflect.DeepEqual(page.Emails, wantEmails) {
t.Fatalf("Emails = %v, want %v", page.Emails, wantEmails)
}
}

View File

@@ -491,7 +491,7 @@ func nameOnlyTemplate(template string) string {
// effectiveTemplate picks which template to expand for one body link: the full // effectiveTemplate picks which template to expand for one body link: the full
// template (with the per-client info) for a client's first link, and the // template (with the per-client info) for a client's first link, and the
// name-only template for every link thereafter — so the info shows once. Only // name-only template for every link thereafter — so the info shows once. Only
// called in the subscription-body context (displays bypass the template). // called in the subscription-body context (displays render name-only directly).
func (s *SubService) effectiveTemplate(email string) string { func (s *SubService) effectiveTemplate(email string) string {
translated := translateUISingleBrackets(s.remarkTemplate) translated := translateUISingleBrackets(s.remarkTemplate)
if s.usageShown == nil { if s.usageShown == nil {
@@ -515,22 +515,25 @@ func (s *SubService) genTemplatedRemark(inbound *model.Inbound, client model.Cli
hostRemark: hostRemark, hostRemark: hostRemark,
transport: transport, transport: transport,
} }
tmpl := s.effectiveTemplate(client.Email) var tmpl string
// Fall back to the config name when the template is empty or expands to if s.subscriptionBody {
// nothing (e.g. an all-unlimited template whose only segments dropped out). tmpl = s.effectiveTemplate(client.Email)
} else {
tmpl = nameOnlyTemplate(translateUISingleBrackets(s.remarkTemplate))
}
if out := expandRemarkVars(tmpl, ctx); strings.TrimSpace(out) != "" { if out := expandRemarkVars(tmpl, ctx); strings.TrimSpace(out) != "" {
return out return out
} }
return ctx.configName() return ctx.configName()
} }
// genHostRemark builds one host endpoint's remark for a specific client. In the // genHostRemark builds one host endpoint's remark for a specific client. With a
// subscription body the {{HOST}} token carries the host's remark and the rest of // remark template set it is template-driven (body shows the full template on the
// the template still applies; displays show the config name, host and email. // first link and the name-only part thereafter; displays render the name-only
// part). With no template it falls back to inbound, host and email joined by "-".
func (s *SubService) genHostRemark(inbound *model.Inbound, client model.Client, hostRemark string, transport string) string { func (s *SubService) genHostRemark(inbound *model.Inbound, client model.Client, hostRemark string, transport string) string {
if !s.subscriptionBody { if s.remarkTemplate != "" {
name := remarkContext{inbound: inbound, hostRemark: hostRemark}.configName() return s.genTemplatedRemark(inbound, client, hostRemark, transport)
return fallbackRemark(name, hostRemark, client.Email)
} }
return s.genTemplatedRemark(inbound, client, hostRemark, transport) return fallbackRemark(inbound.Remark, hostRemark, client.Email)
} }

View File

@@ -164,15 +164,15 @@ func hostRemarkService(template string) (*SubService, *model.Inbound, model.Clie
return s, inbound, client return s, inbound, client
} }
// The config name is always the inbound's own remark; the host endpoint's remark // With no template configured, genHostRemark falls back to the inbound remark,
// never substitutes it (it is reachable only through {{HOST}}). // host and email joined by "-".
func TestGenHostRemark_ConfigNameUsesInbound(t *testing.T) { func TestGenHostRemark_NoTemplate_Fallback(t *testing.T) {
s, inbound, client := hostRemarkService("") // no template → config name only s, inbound, client := hostRemarkService("")
if got := s.genHostRemark(inbound, client, "Relay", ""); got != "DE" { if got := s.genHostRemark(inbound, client, "Relay", ""); got != "DE-Relay-john@example.com" {
t.Fatalf("genHostRemark = %q, want %q (inbound remark, host ignored)", got, "DE") t.Fatalf("genHostRemark = %q, want %q", got, "DE-Relay-john@example.com")
} }
if got := s.genHostRemark(inbound, client, "", ""); got != "DE" { if got := s.genHostRemark(inbound, client, "", ""); got != "DE-john@example.com" {
t.Fatalf("genHostRemark (no host remark) = %q, want %q", got, "DE") t.Fatalf("genHostRemark (no host remark) = %q, want %q", got, "DE-john@example.com")
} }
} }
@@ -232,23 +232,29 @@ func TestUsageOnFirstLinkOnly(t *testing.T) {
} }
func TestRemarkInDisplayContext(t *testing.T) { func TestRemarkInDisplayContext(t *testing.T) {
s, inbound, client := hostRemarkService("{{INBOUND}}|📊{{TRAFFIC_LEFT}}|⏳{{DAYS_LEFT}}D") s, inbound, client := hostRemarkService("{{INBOUND}}-{{EMAIL}}|📊{{TRAFFIC_LEFT}}|⏳{{DAYS_LEFT}}D")
s.subscriptionBody = false s.subscriptionBody = false
if got := s.genHostRemark(inbound, client, "CDN", ""); got != "DE-CDN-john@example.com" { const want = "DE-john@example.com"
t.Fatalf("display host link = %q, want %q", got, "DE-CDN-john@example.com") if got := s.genHostRemark(inbound, client, "CDN", ""); got != want {
t.Fatalf("display host link = %q, want %q", got, want)
} }
if got := s.genHostRemark(inbound, client, "", ""); got != "DE-john@example.com" { if got := s.genHostRemark(inbound, client, "", ""); got != want {
t.Fatalf("display host link (no host) = %q, want %q", got, "DE-john@example.com") t.Fatalf("display host link (no host) = %q, want %q", got, want)
} }
if got := s.genRemark(inbound, client.Email, "", ""); got != "DE-john@example.com" { if got := s.genRemark(inbound, client.Email, "", ""); got != want {
t.Fatalf("display genRemark = %q, want %q", got, "DE-john@example.com") t.Fatalf("display genRemark = %q, want %q", got, want)
}
s2, inbound2, client2 := hostRemarkService("{{INBOUND}}-{{HOST}}|📊{{TRAFFIC_LEFT}}")
s2.subscriptionBody = false
if got := s2.genHostRemark(inbound2, client2, "CDN", ""); got != "DE-CDN" {
t.Fatalf("display host link with HOST token = %q, want %q", got, "DE-CDN")
} }
} }
// nameOnlyTemplate drops the info part (and its leading decoration), keeping name. // nameOnlyTemplate drops the info part (and its leading decoration), keeping name.
func TestNameOnlyTemplate(t *testing.T) { func TestNameOnlyTemplate(t *testing.T) {
cases := map[string]string{ cases := map[string]string{
"{{INBOUND}}|📊{{TRAFFIC_LEFT}}|⏳{{DAYS_LEFT}}D": "{{INBOUND}}", // the default → name only "{{INBOUND}}|📊{{TRAFFIC_LEFT}}|⏳{{DAYS_LEFT}}D": "{{INBOUND}}", // usage tail stripped
"{{EMAIL}} {{INBOUND}} ⏳{{DAYS_LEFT}}": "{{EMAIL}} {{INBOUND}}", // multi-token name survives the trim "{{EMAIL}} {{INBOUND}} ⏳{{DAYS_LEFT}}": "{{EMAIL}} {{INBOUND}}", // multi-token name survives the trim
"{{INBOUND}} | {{STATUS}}": "{{INBOUND}}", "{{INBOUND}} | {{STATUS}}": "{{INBOUND}}",
"{{INBOUND}}-{{EMAIL}}": "{{INBOUND}}-{{EMAIL}}", // no info tokens → unchanged "{{INBOUND}}-{{EMAIL}}": "{{INBOUND}}-{{EMAIL}}", // no info tokens → unchanged

View File

@@ -1634,11 +1634,12 @@ func cloneStringMap(source map[string]string) map[string]string {
} }
// genRemark builds the remark for a non-host link (raw default / legacy // genRemark builds the remark for a non-host link (raw default / legacy
// externalProxy / synthetic JSON-Clash entry). In the subscription body a set // externalProxy / synthetic JSON-Clash entry). A set remark template drives it
// remark template takes over; otherwise (and in every display context) the // in both the body and display contexts (genTemplatedRemark renders the
// remark is just the config name (inbound remark, then extra). // name-only part on displays); with no template it falls back to the inbound
// remark, extra and email joined by "-".
func (s *SubService) genRemark(inbound *model.Inbound, email string, extra string, transport string) string { func (s *SubService) genRemark(inbound *model.Inbound, email string, extra string, transport string) string {
if s.remarkTemplate != "" && s.subscriptionBody { if s.remarkTemplate != "" {
return s.genTemplatedRemark(inbound, s.lookupClient(inbound, email), extra, transport) return s.genTemplatedRemark(inbound, s.lookupClient(inbound, email), extra, transport)
} }
return fallbackRemark(inbound.Remark, extra, email) return fallbackRemark(inbound.Remark, extra, email)
@@ -2336,6 +2337,19 @@ func (s *SubService) BuildPageData(subId string, hostHeader string, traffic xray
datepicker = "gregorian" datepicker = "gregorian"
} }
pageLinks := make([]string, 0, len(subs))
pageEmails := make([]string, 0, len(subs))
for i, sub := range subs {
email := ""
if i < len(emails) {
email = emails[i]
}
for _, link := range splitLinkLines(sub) {
pageLinks = append(pageLinks, link)
pageEmails = append(pageEmails, email)
}
}
return PageData{ return PageData{
Host: hostHeader, Host: hostHeader,
BasePath: basePath, BasePath: basePath,
@@ -2357,8 +2371,8 @@ func (s *SubService) BuildPageData(subId string, hostHeader string, traffic xray
SubClashUrl: subClashURL, SubClashUrl: subClashURL,
SubTitle: subTitle, SubTitle: subTitle,
SubSupportUrl: subSupportUrl, SubSupportUrl: subSupportUrl,
Result: subs, Result: pageLinks,
Emails: emails, Emails: pageEmails,
} }
} }

View File

@@ -55,7 +55,7 @@ var defaultValueMap = map[string]string{
"pageSize": "25", "pageSize": "25",
"expireDiff": "0", "expireDiff": "0",
"trafficDiff": "0", "trafficDiff": "0",
"remarkTemplate": "{{INBOUND}}|📊{{TRAFFIC_LEFT}}|⏳{{DAYS_LEFT}}D", "remarkTemplate": "{{INBOUND}}-{{EMAIL}}|📊{{TRAFFIC_LEFT}}|⏳{{DAYS_LEFT}}D",
"timeLocation": "Local", "timeLocation": "Local",
"tgBotEnable": "false", "tgBotEnable": "false",
"tgBotToken": "", "tgBotToken": "",