mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-08-24 03:47:15 +00:00
refactor(mtproto): manage ad-tags per client only
The inbound-level ad-tag duplicated the per-client override for no gain: the fork's global tag applied to every secret anyway, so one value had two homes and they could drift. The inbound form field, the settings key, and the global ad-tag in the generated config and in the PUT /secrets body are gone; the tag is set on each client instead. Existing inbound-level values are intentionally not migrated; a leftover settings key is stripped on the next save.
This commit is contained in:
@@ -54,13 +54,9 @@ type Instance struct {
|
||||
// fair-share algorithm; zero disables throttling.
|
||||
ThrottleMaxConnections int
|
||||
|
||||
// AdTag is a 32-hex Telegram advertising tag; when set, mtg routes clients
|
||||
// through Telegram middle proxies so a sponsored channel shows in their chat
|
||||
// list. It is part of the reloadable secret config, so a change is applied
|
||||
// via /reload without dropping connections. PublicIPv4/PublicIPv6 pin the
|
||||
// proxy's reachable address the middle proxy needs; they are omitted when
|
||||
// empty so mtg auto-detects, and a change forces a restart.
|
||||
AdTag string
|
||||
// PublicIPv4/PublicIPv6 pin the proxy's reachable address the Telegram
|
||||
// middle proxy needs when clients carry advertising tags; they are omitted
|
||||
// when empty so mtg auto-detects, and a change forces a restart.
|
||||
PublicIPv4 string
|
||||
PublicIPv6 string
|
||||
|
||||
@@ -104,15 +100,15 @@ func (inst Instance) structuralFingerprint() string {
|
||||
// secretsFingerprint identifies the reloadable secret config regardless of
|
||||
// client order, so a reordered clients array in the stored settings does not
|
||||
// read as a change. It moves whenever a client is added, removed, disabled,
|
||||
// re-keyed, or re-tagged, or the global advertising tag changes — all of which
|
||||
// mtg applies in place without dropping connections.
|
||||
// re-keyed, or re-tagged — all of which mtg applies in place without dropping
|
||||
// connections.
|
||||
func (inst Instance) secretsFingerprint() string {
|
||||
pairs := make([]string, 0, len(inst.Secrets))
|
||||
for _, e := range inst.Secrets {
|
||||
pairs = append(pairs, e.Name+"="+e.Secret+";tag="+e.AdTag)
|
||||
}
|
||||
slices.Sort(pairs)
|
||||
return "adtag=" + inst.AdTag + "|" + strings.Join(pairs, "|")
|
||||
return strings.Join(pairs, "|")
|
||||
}
|
||||
|
||||
// Traffic is a per-client traffic delta scraped from an mtg /stats endpoint. Tag
|
||||
@@ -183,7 +179,6 @@ func InstanceFromInbound(ib *model.Inbound) (Instance, bool) {
|
||||
ThrottleMaxConnections int `json:"throttleMaxConnections"`
|
||||
RouteThroughXray bool `json:"routeThroughXray"`
|
||||
RouteXrayPort int `json:"routeXrayPort"`
|
||||
AdTag string `json:"adTag"`
|
||||
PublicIPv4 string `json:"publicIpv4"`
|
||||
PublicIPv6 string `json:"publicIpv6"`
|
||||
Clients []struct {
|
||||
@@ -221,7 +216,6 @@ func InstanceFromInbound(ib *model.Inbound) (Instance, bool) {
|
||||
ThrottleMaxConnections: parsed.ThrottleMaxConnections,
|
||||
RouteThroughXray: parsed.RouteThroughXray,
|
||||
XrayRoutePort: parsed.RouteXrayPort,
|
||||
AdTag: usableAdTag(parsed.AdTag),
|
||||
PublicIPv4: strings.TrimSpace(parsed.PublicIPv4),
|
||||
PublicIPv6: strings.TrimSpace(parsed.PublicIPv6),
|
||||
}, true
|
||||
@@ -490,9 +484,6 @@ func renderConfig(inst Instance, apiPort int, apiToken string) string {
|
||||
if apiToken != "" {
|
||||
fmt.Fprintf(&b, "api-token = %q\n", apiToken)
|
||||
}
|
||||
if inst.AdTag != "" {
|
||||
fmt.Fprintf(&b, "ad-tag = %q\n", inst.AdTag)
|
||||
}
|
||||
if inst.PublicIPv4 != "" {
|
||||
fmt.Fprintf(&b, "public-ipv4 = %q\n", inst.PublicIPv4)
|
||||
}
|
||||
@@ -564,7 +555,6 @@ type secretPutEntry struct {
|
||||
|
||||
type secretsPutBody struct {
|
||||
Secrets map[string]secretPutEntry `json:"secrets"`
|
||||
AdTag string `json:"ad_tag,omitempty"`
|
||||
}
|
||||
|
||||
func secretsPayload(inst Instance) secretsPutBody {
|
||||
@@ -572,7 +562,7 @@ func secretsPayload(inst Instance) secretsPutBody {
|
||||
for _, e := range inst.Secrets {
|
||||
secrets[e.Name] = secretPutEntry{Secret: e.Secret, AdTag: e.AdTag}
|
||||
}
|
||||
return secretsPutBody{Secrets: secrets, AdTag: inst.AdTag}
|
||||
return secretsPutBody{Secrets: secrets}
|
||||
}
|
||||
|
||||
// newAPIToken mints the bearer token one mtg process and its manager share for
|
||||
|
||||
@@ -128,7 +128,6 @@ func TestApplySecrets(t *testing.T) {
|
||||
inst := mtgInst(1,
|
||||
SecretEntry{Name: "alice", Secret: "ee01"},
|
||||
SecretEntry{Name: "bob", Secret: "ee02", AdTag: "fedcba9876543210fedcba9876543210"})
|
||||
inst.AdTag = "0123456789abcdef0123456789abcdef"
|
||||
if got := applySecrets(serverPort(t, srv), "sesame", inst); got != tc.want {
|
||||
t.Fatalf("applySecrets = %v, want %v", got, tc.want)
|
||||
}
|
||||
@@ -138,11 +137,11 @@ func TestApplySecrets(t *testing.T) {
|
||||
if gotAuth != "Bearer sesame" {
|
||||
t.Fatalf("expected the bearer token on the request, got %q", gotAuth)
|
||||
}
|
||||
if gotBody.Secrets["alice"].Secret != "ee01" || gotBody.AdTag != "0123456789abcdef0123456789abcdef" {
|
||||
t.Fatalf("payload must carry the secret and ad-tag: %+v", gotBody)
|
||||
if gotBody.Secrets["alice"].Secret != "ee01" {
|
||||
t.Fatalf("payload must carry the secret: %+v", gotBody)
|
||||
}
|
||||
if gotBody.Secrets["alice"].AdTag != "" || gotBody.Secrets["bob"].AdTag != "fedcba9876543210fedcba9876543210" {
|
||||
t.Fatalf("payload must carry per-client ad-tag overrides only where set: %+v", gotBody)
|
||||
t.Fatalf("payload must carry per-client ad-tags only where set: %+v", gotBody)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -40,10 +40,7 @@ func TestInstanceFromInbound(t *testing.T) {
|
||||
t.Fatalf("a valid secret must be preserved, got %q", inst.Secrets[0].Secret)
|
||||
}
|
||||
if inst.Secrets[0].AdTag != "fedcba9876543210fedcba9876543210" {
|
||||
t.Fatalf("the client ad-tag override must be parsed, got %q", inst.Secrets[0].AdTag)
|
||||
}
|
||||
if inst.AdTag != "0123456789abcdef0123456789abcdef" {
|
||||
t.Fatalf("the inbound ad-tag must be trimmed and kept, got %q", inst.AdTag)
|
||||
t.Fatalf("the client ad-tag must be parsed, got %q", inst.Secrets[0].AdTag)
|
||||
}
|
||||
if inst.Port != 8443 || inst.Id != 3 {
|
||||
t.Fatalf("bad instance %+v", inst)
|
||||
@@ -70,15 +67,13 @@ func TestInstanceFromInbound(t *testing.T) {
|
||||
t.Fatal("an inbound with no active secret should not produce an instance")
|
||||
}
|
||||
|
||||
badTags := &model.Inbound{Protocol: model.MTProto, Settings: `{"adTag":"nope",` +
|
||||
`"clients":[{"email":"x","secret":"ee00","adTag":"deadbeef","enable":true}]}`}
|
||||
badTags := &model.Inbound{Protocol: model.MTProto, Settings: `{"clients":[{"email":"x","secret":"ee00","adTag":"deadbeef","enable":true}]}`}
|
||||
badInst, ok := InstanceFromInbound(badTags)
|
||||
if !ok {
|
||||
t.Fatal("expected a usable instance despite malformed ad tags")
|
||||
t.Fatal("expected a usable instance despite a malformed ad tag")
|
||||
}
|
||||
if badInst.AdTag != "" || badInst.Secrets[0].AdTag != "" {
|
||||
t.Fatalf("malformed ad tags must be dropped so the generated config stays valid, got global=%q client=%q",
|
||||
badInst.AdTag, badInst.Secrets[0].AdTag)
|
||||
if badInst.Secrets[0].AdTag != "" {
|
||||
t.Fatalf("a malformed ad tag must be dropped so the generated config stays valid, got %q", badInst.Secrets[0].AdTag)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,7 +111,6 @@ func TestRenderConfig(t *testing.T) {
|
||||
Debug: true, ProxyProtocolListener: true, PreferIP: "only-ipv6",
|
||||
FrontingIP: "127.0.0.1", FrontingPort: 9443, FrontingProxyProtocol: true,
|
||||
ThrottleMaxConnections: 5000,
|
||||
AdTag: "0123456789abcdef0123456789abcdef",
|
||||
PublicIPv4: "1.2.3.4",
|
||||
PublicIPv6: "2001:db8::1",
|
||||
}, 6000, "sesame")
|
||||
@@ -125,7 +119,6 @@ func TestRenderConfig(t *testing.T) {
|
||||
"proxy-protocol-listener = true\n",
|
||||
`prefer-ip = "only-ipv6"`,
|
||||
`api-token = "sesame"`,
|
||||
`ad-tag = "0123456789abcdef0123456789abcdef"`,
|
||||
`public-ipv4 = "1.2.3.4"`,
|
||||
`public-ipv6 = "2001:db8::1"`,
|
||||
"[domain-fronting]",
|
||||
@@ -144,8 +137,11 @@ func TestRenderConfig(t *testing.T) {
|
||||
if strings.Contains(full, `ip = "127.0.0.1"`) {
|
||||
t.Fatalf("domain-fronting must use host, not the deprecated ip key:\n%s", full)
|
||||
}
|
||||
if strings.Contains(full, "ad-tag =") {
|
||||
t.Fatalf("no global ad-tag must be emitted, tags are per client:\n%s", full)
|
||||
}
|
||||
if strings.Contains(full, `"alice" = "0123456789abcdef0123456789abcdef"`) || strings.Contains(full, `"alice" = ""`) {
|
||||
t.Fatalf("a client without an override must not appear in [secret-ad-tags]:\n%s", full)
|
||||
t.Fatalf("a client without a tag must not appear in [secret-ad-tags]:\n%s", full)
|
||||
}
|
||||
// TOML requires top-level keys before any [section] header, and [secrets]
|
||||
// must be the final section so trailing keys are not swallowed by a table.
|
||||
@@ -225,7 +221,6 @@ func TestFingerprintSplit(t *testing.T) {
|
||||
"rekey": func(i *Instance) { i.Secrets = []SecretEntry{{Name: "a", Secret: "ee99"}} },
|
||||
"remove": func(i *Instance) { i.Secrets = nil },
|
||||
"rename": func(i *Instance) { i.Secrets = []SecretEntry{{Name: "a2", Secret: "ee"}} },
|
||||
"adTag": func(i *Instance) { i.AdTag = "0123456789abcdef0123456789abcdef" },
|
||||
"clientAdTag": func(i *Instance) {
|
||||
i.Secrets = []SecretEntry{{Name: "a", Secret: "ee", AdTag: "0123456789abcdef0123456789abcdef"}}
|
||||
},
|
||||
@@ -246,7 +241,7 @@ func TestFingerprintSplit(t *testing.T) {
|
||||
t.Run("orderInsensitive", func(t *testing.T) {
|
||||
forward := Instance{Secrets: []SecretEntry{{Name: "alice", Secret: "ee11"}, {Name: "bob", Secret: "ee22"}}}
|
||||
reversed := Instance{Secrets: []SecretEntry{{Name: "bob", Secret: "ee22"}, {Name: "alice", Secret: "ee11"}}}
|
||||
if got, want := forward.secretsFingerprint(), "adtag=|alice=ee11;tag=|bob=ee22;tag="; got != want {
|
||||
if got, want := forward.secretsFingerprint(), "alice=ee11;tag=|bob=ee22;tag="; got != want {
|
||||
t.Fatalf("secrets fingerprint must join sorted pairs: got %q, want %q", got, want)
|
||||
}
|
||||
if forward.secretsFingerprint() != reversed.secretsFingerprint() {
|
||||
|
||||
Reference in New Issue
Block a user