mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-09-02 00:17:13 +00:00
feat(mtproto): adopt dolonet/mtg-multi and make MTProto inbounds multi-client
Replace the upstream 9seconds/mtg sidecar with the dolonet/mtg-multi fork so a single MTProto inbound can serve many per-user secrets. Each panel client is now one named FakeTLS secret in the fork's [secrets] section: clients are first-class (attach/detach, limits, expiry, per-client tg:// links) exactly like every other protocol, mirroring the WireGuard multi-client model. Per-client traffic and online status come from the fork's /stats JSON API (its Prometheus output has no per-user label), fed into the existing email-keyed client_traffics accumulator; an optional throttle caps concurrent connections. A one-time seeder converts each legacy single-secret inbound into a one-client inbound. The fork ships only linux/darwin amd64/arm64 binaries but is pure Go, so provisioning builds it from source for every supported platform (release.yml, DockerInit.sh) while keeping the panel-expected mtg-<os>-<arch> filename and the 'run' verb, so process.go is untouched. Also fixes a pre-existing update.sh gap that never renamed the mtg binary for armv6/armv7 updates.
This commit is contained in:
@@ -7,48 +7,36 @@ import (
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
||||
)
|
||||
|
||||
func TestParseMetricLine(t *testing.T) {
|
||||
name, labels, val, err := parseMetricLine(`mtg_traffic{direction="to_client"} 12345`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if name != "mtg_traffic" {
|
||||
t.Fatalf("name=%q", name)
|
||||
}
|
||||
if labels["direction"] != "to_client" {
|
||||
t.Fatalf("labels=%v", labels)
|
||||
}
|
||||
if val != 12345 {
|
||||
t.Fatalf("val=%v", val)
|
||||
}
|
||||
|
||||
name2, _, val2, err2 := parseMetricLine(`mtg_concurrency 7`)
|
||||
if err2 != nil {
|
||||
t.Fatal(err2)
|
||||
}
|
||||
if name2 != "mtg_concurrency" || val2 != 7 {
|
||||
t.Fatalf("got %q %v", name2, val2)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInstanceFromInbound(t *testing.T) {
|
||||
aliceSecret := "ee0123456789abcdef0123456789abcdef6578616d706c652e636f6d"
|
||||
ib := &model.Inbound{
|
||||
Id: 3,
|
||||
Tag: "inbound-3",
|
||||
Listen: "0.0.0.0",
|
||||
Port: 8443,
|
||||
Protocol: model.MTProto,
|
||||
Settings: `{"fakeTlsDomain":"example.com","secret":"",` +
|
||||
Settings: `{"fakeTlsDomain":"example.com",` +
|
||||
`"debug":true,"proxyProtocolListener":true,"preferIp":"prefer-ipv4",` +
|
||||
`"domainFronting":{"ip":"127.0.0.1","port":9443,"proxyProtocol":true},` +
|
||||
`"routeThroughXray":true,"routeXrayPort":50000}`,
|
||||
`"throttleMaxConnections":5000,` +
|
||||
`"routeThroughXray":true,"routeXrayPort":50000,` +
|
||||
`"clients":[` +
|
||||
`{"email":"alice","secret":"` + aliceSecret + `","enable":true},` +
|
||||
`{"email":"bob","secret":"","enable":true},` +
|
||||
`{"email":"carol","secret":"eeaa","enable":false}]}`,
|
||||
}
|
||||
inst, ok := InstanceFromInbound(ib)
|
||||
if !ok {
|
||||
t.Fatal("expected a usable instance")
|
||||
}
|
||||
if inst.Secret == "" {
|
||||
t.Fatal("secret should be healed to a non-empty value")
|
||||
if len(inst.Secrets) != 1 {
|
||||
t.Fatalf("only the enabled client with a secret should be served, got %d: %+v", len(inst.Secrets), inst.Secrets)
|
||||
}
|
||||
if inst.Secrets[0].Name != "alice" {
|
||||
t.Fatalf("secret name should be the client email, got %q", inst.Secrets[0].Name)
|
||||
}
|
||||
if inst.Secrets[0].Secret != aliceSecret {
|
||||
t.Fatalf("a valid secret must be preserved, got %q", inst.Secrets[0].Secret)
|
||||
}
|
||||
if inst.Port != 8443 || inst.Id != 3 {
|
||||
t.Fatalf("bad instance %+v", inst)
|
||||
@@ -59,6 +47,9 @@ func TestInstanceFromInbound(t *testing.T) {
|
||||
if inst.FrontingIP != "127.0.0.1" || inst.FrontingPort != 9443 || !inst.FrontingProxyProtocol {
|
||||
t.Fatalf("domain-fronting not parsed: %+v", inst)
|
||||
}
|
||||
if inst.ThrottleMaxConnections != 5000 {
|
||||
t.Fatalf("throttle not parsed: %+v", inst)
|
||||
}
|
||||
if !inst.RouteThroughXray || inst.XrayRoutePort != 50000 {
|
||||
t.Fatalf("xray routing not parsed: %+v", inst)
|
||||
}
|
||||
@@ -66,13 +57,21 @@ func TestInstanceFromInbound(t *testing.T) {
|
||||
if _, ok := InstanceFromInbound(&model.Inbound{Protocol: model.VLESS}); ok {
|
||||
t.Fatal("non-mtproto inbound should not produce an instance")
|
||||
}
|
||||
|
||||
noSecrets := &model.Inbound{Protocol: model.MTProto, Settings: `{"clients":[{"email":"x","secret":"","enable":true}]}`}
|
||||
if _, ok := InstanceFromInbound(noSecrets); ok {
|
||||
t.Fatal("an inbound with no active secret should not produce an instance")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderConfig(t *testing.T) {
|
||||
// A bare instance emits only the required keys and the prometheus block,
|
||||
// with no optional keys and no [domain-fronting] section.
|
||||
bare := renderConfig(Instance{Secret: "ee00", Listen: "0.0.0.0", Port: 8443}, 5000)
|
||||
for _, unwanted := range []string{"debug", "proxy-protocol-listener", "prefer-ip", "[domain-fronting]"} {
|
||||
// A bare instance emits only the required keys, api-bind-to, and the
|
||||
// [secrets] section, with no optional keys and no [domain-fronting].
|
||||
bare := renderConfig(Instance{
|
||||
Secrets: []SecretEntry{{Name: "alice", Secret: "ee00"}},
|
||||
Listen: "0.0.0.0", Port: 8443,
|
||||
}, 5000)
|
||||
for _, unwanted := range []string{"debug", "proxy-protocol-listener", "prefer-ip", "[domain-fronting]", "[stats.prometheus]", "[throttle]"} {
|
||||
if strings.Contains(bare, unwanted) {
|
||||
t.Fatalf("bare config should not contain %q:\n%s", unwanted, bare)
|
||||
}
|
||||
@@ -80,57 +79,73 @@ func TestRenderConfig(t *testing.T) {
|
||||
if !strings.Contains(bare, `bind-to = "0.0.0.0:8443"`) {
|
||||
t.Fatalf("missing bind-to:\n%s", bare)
|
||||
}
|
||||
if !strings.Contains(bare, "[stats.prometheus]") || !strings.Contains(bare, "127.0.0.1:5000") {
|
||||
t.Fatalf("prometheus block must always be present:\n%s", bare)
|
||||
if !strings.Contains(bare, `api-bind-to = "127.0.0.1:5000"`) {
|
||||
t.Fatalf("api-bind-to must always be present:\n%s", bare)
|
||||
}
|
||||
if !strings.Contains(bare, "[secrets]") || !strings.Contains(bare, `"alice" = "ee00"`) {
|
||||
t.Fatalf("secrets block must carry the client secret:\n%s", bare)
|
||||
}
|
||||
|
||||
// A fully configured instance emits every option and the fronting section.
|
||||
// A fully configured instance emits every option, the fronting section (as
|
||||
// host, not the fork-deprecated ip), the throttle block, and [secrets] last.
|
||||
full := renderConfig(Instance{
|
||||
Secret: "ee11", Listen: "0.0.0.0", Port: 443,
|
||||
Secrets: []SecretEntry{{Name: "alice", Secret: "ee11"}},
|
||||
Listen: "0.0.0.0", Port: 443,
|
||||
Debug: true, ProxyProtocolListener: true, PreferIP: "only-ipv6",
|
||||
FrontingIP: "127.0.0.1", FrontingPort: 9443, FrontingProxyProtocol: true,
|
||||
ThrottleMaxConnections: 5000,
|
||||
}, 6000)
|
||||
for _, want := range []string{
|
||||
"debug = true\n",
|
||||
"proxy-protocol-listener = true\n",
|
||||
`prefer-ip = "only-ipv6"`,
|
||||
"[domain-fronting]",
|
||||
`ip = "127.0.0.1"`,
|
||||
`host = "127.0.0.1"`,
|
||||
"port = 9443",
|
||||
"proxy-protocol = true\n",
|
||||
"[throttle]",
|
||||
"max-connections = 5000",
|
||||
} {
|
||||
if !strings.Contains(full, want) {
|
||||
t.Fatalf("full config missing %q:\n%s", want, full)
|
||||
}
|
||||
}
|
||||
// TOML requires top-level keys before any [section] header.
|
||||
if strings.Contains(full, `ip = "127.0.0.1"`) {
|
||||
t.Fatalf("domain-fronting must use host, not the deprecated ip key:\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.
|
||||
if strings.Index(full, "prefer-ip") > strings.Index(full, "[domain-fronting]") {
|
||||
t.Fatalf("top-level keys must precede the [domain-fronting] section:\n%s", full)
|
||||
}
|
||||
if strings.LastIndex(full, "[domain-fronting]") > strings.Index(full, "[stats.prometheus]") {
|
||||
t.Fatalf("[domain-fronting] must precede [stats.prometheus]:\n%s", full)
|
||||
if strings.LastIndex(full, "[secrets]") < strings.Index(full, "[domain-fronting]") {
|
||||
t.Fatalf("[secrets] must be the final section:\n%s", full)
|
||||
}
|
||||
if strings.LastIndex(full, "[secrets]") < strings.Index(full, "[throttle]") {
|
||||
t.Fatalf("[throttle] must precede [secrets]:\n%s", full)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderConfigXrayEgress(t *testing.T) {
|
||||
// Routing through Xray emits a [network] proxies upstream pointing at the
|
||||
// loopback SOCKS bridge, before the prometheus block.
|
||||
// loopback SOCKS bridge, before the [secrets] section.
|
||||
routed := renderConfig(Instance{
|
||||
Secret: "ee22", Listen: "0.0.0.0", Port: 443,
|
||||
Secrets: []SecretEntry{{Name: "a", Secret: "ee22"}},
|
||||
Listen: "0.0.0.0", Port: 443,
|
||||
RouteThroughXray: true, XrayRoutePort: 50000,
|
||||
}, 7000)
|
||||
if !strings.Contains(routed, "[network]") ||
|
||||
!strings.Contains(routed, `proxies = ["socks5://127.0.0.1:50000"]`) {
|
||||
t.Fatalf("routed config must emit the SOCKS upstream:\n%s", routed)
|
||||
}
|
||||
if strings.Index(routed, "[network]") > strings.Index(routed, "[stats.prometheus]") {
|
||||
t.Fatalf("[network] must precede [stats.prometheus]:\n%s", routed)
|
||||
if strings.Index(routed, "[network]") > strings.Index(routed, "[secrets]") {
|
||||
t.Fatalf("[network] must precede [secrets]:\n%s", routed)
|
||||
}
|
||||
|
||||
// Without the flag (or without a port) the section is omitted.
|
||||
for _, inst := range []Instance{
|
||||
{Secret: "ee", Listen: "0.0.0.0", Port: 443},
|
||||
{Secret: "ee", Listen: "0.0.0.0", Port: 443, RouteThroughXray: true},
|
||||
{Secrets: []SecretEntry{{Name: "a", Secret: "ee"}}, Listen: "0.0.0.0", Port: 443},
|
||||
{Secrets: []SecretEntry{{Name: "a", Secret: "ee"}}, Listen: "0.0.0.0", Port: 443, RouteThroughXray: true},
|
||||
} {
|
||||
if got := renderConfig(inst, 7000); strings.Contains(got, "[network]") {
|
||||
t.Fatalf("unrouted config must omit [network]:\n%s", got)
|
||||
@@ -139,7 +154,7 @@ func TestRenderConfigXrayEgress(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestFingerprintReactsToOptions(t *testing.T) {
|
||||
base := Instance{Secret: "ee", Listen: "0.0.0.0", Port: 443}
|
||||
base := Instance{Secrets: []SecretEntry{{Name: "a", Secret: "ee"}}, Listen: "0.0.0.0", Port: 443}
|
||||
for name, mutate := range map[string]func(*Instance){
|
||||
"debug": func(i *Instance) { i.Debug = true },
|
||||
"listener": func(i *Instance) { i.ProxyProtocolListener = true },
|
||||
@@ -147,10 +162,16 @@ func TestFingerprintReactsToOptions(t *testing.T) {
|
||||
"frontingIP": func(i *Instance) { i.FrontingIP = "127.0.0.1" },
|
||||
"frontingPort": func(i *Instance) { i.FrontingPort = 9443 },
|
||||
"frontingProxy": func(i *Instance) { i.FrontingProxyProtocol = true },
|
||||
"throttle": func(i *Instance) { i.ThrottleMaxConnections = 5000 },
|
||||
"routeXray": func(i *Instance) { i.RouteThroughXray = true },
|
||||
"routeXrayPort": func(i *Instance) { i.XrayRoutePort = 50000 },
|
||||
"addSecret": func(i *Instance) { i.Secrets = append(i.Secrets, SecretEntry{Name: "b", Secret: "ff"}) },
|
||||
"changeSecret": func(i *Instance) { i.Secrets = []SecretEntry{{Name: "a", Secret: "ee99"}} },
|
||||
} {
|
||||
changed := base
|
||||
if strings.HasPrefix(name, "addSecret") || strings.HasPrefix(name, "changeSecret") {
|
||||
changed.Secrets = append([]SecretEntry(nil), base.Secrets...)
|
||||
}
|
||||
mutate(&changed)
|
||||
if base.fingerprint() == changed.fingerprint() {
|
||||
t.Fatalf("fingerprint must change when %s changes", name)
|
||||
|
||||
Reference in New Issue
Block a user