mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-07-07 13:24:21 +00:00
5eec178483
Add a per-inbound "Route through Xray" toggle (off by default) plus an optional outbound picker on MTProto inbounds. mtg only supports a SOCKS5 upstream, so when enabled the panel injects a loopback SOCKS bridge into the generated Xray config — tagged with the inbound's own tag — and mtg dials Telegram through it via a [network] proxies upstream. The router then governs Telegram egress: matchable in the Routing tab, or forced to a chosen outbound/balancer via the picker. - mtproto: Instance carries RouteThroughXray + XrayRoutePort (in the fingerprint); InstanceFromInbound parses them; renderConfig emits the socks5 [network] upstream; freeLocalPort exported as FreeLocalPort. - xray.go: injectMtprotoEgress appends the loopback SOCKS bridge and prepends an optional inboundTag->outbound/balancer rule, hot-appliable like injectPanelEgress. - inbound.go: backend-owned egress port persisted in settings, allocated once and carried across edits (stored value wins); stripped with the inert outboundTag when routing is off; allocation failure fails the save; routed add/update/del force a config regen. - mtproto_job: skip folding mtg metrics for routed inbounds (the bridge, carrying the inbound tag, is metered by xray_traffic_job) to avoid double-counting. - frontend: toggle + outbound/balancer Select (useOutboundTags) on the MTProto form; i18n keys for all locales.
95 lines
3.3 KiB
Go
95 lines
3.3 KiB
Go
package service
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
|
)
|
|
|
|
func TestMtprotoRoutesThroughXray(t *testing.T) {
|
|
cases := map[string]struct {
|
|
ib *model.Inbound
|
|
want bool
|
|
}{
|
|
"routed": {&model.Inbound{Protocol: model.MTProto, Settings: `{"routeThroughXray":true}`}, true},
|
|
"off": {&model.Inbound{Protocol: model.MTProto, Settings: `{"routeThroughXray":false}`}, false},
|
|
"absent": {&model.Inbound{Protocol: model.MTProto, Settings: `{}`}, false},
|
|
"non-mtproto": {&model.Inbound{Protocol: model.VLESS, Settings: `{"routeThroughXray":true}`}, false},
|
|
"bad json": {&model.Inbound{Protocol: model.MTProto, Settings: `{nope`}, false},
|
|
"nil": {nil, false},
|
|
}
|
|
for name, c := range cases {
|
|
if got := mtprotoRoutesThroughXray(c.ib); got != c.want {
|
|
t.Fatalf("%s: got %v want %v", name, got, c.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func routeXrayPortOf(t *testing.T, settings string) int {
|
|
t.Helper()
|
|
var parsed map[string]any
|
|
if err := json.Unmarshal([]byte(settings), &parsed); err != nil {
|
|
t.Fatalf("settings not valid JSON: %v\n%s", err, settings)
|
|
}
|
|
return settingsRouteXrayPort(parsed)
|
|
}
|
|
|
|
func TestNormalizeMtprotoXrayPort(t *testing.T) {
|
|
s := &InboundService{}
|
|
|
|
// Non-mtproto inbounds are left alone.
|
|
ib := &model.Inbound{Protocol: model.VLESS, Settings: `{"x":1}`}
|
|
if err := s.normalizeMtprotoXrayPort(ib, ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if ib.Settings != `{"x":1}` {
|
|
t.Fatalf("non-mtproto settings must be untouched, got %s", ib.Settings)
|
|
}
|
|
|
|
// Routing on with no existing port allocates a fresh one.
|
|
ib = &model.Inbound{Protocol: model.MTProto, Settings: `{"routeThroughXray":true}`}
|
|
if err := s.normalizeMtprotoXrayPort(ib, ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if p := routeXrayPortOf(t, ib.Settings); p <= 0 {
|
|
t.Fatalf("a routed inbound must get a port, got %d", p)
|
|
}
|
|
|
|
// On update, the stored port wins over both a missing and a client-echoed
|
|
// value — the backend owns it, so no churn and no client override.
|
|
ib = &model.Inbound{Protocol: model.MTProto, Settings: `{"routeThroughXray":true,"routeXrayPort":99999}`}
|
|
if err := s.normalizeMtprotoXrayPort(ib, `{"routeThroughXray":true,"routeXrayPort":51000}`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if p := routeXrayPortOf(t, ib.Settings); p != 51000 {
|
|
t.Fatalf("stored port must win, got %d", p)
|
|
}
|
|
|
|
// An already-present port (no old settings) is stable and not re-marshaled.
|
|
const stable = `{"routeThroughXray":true,"routeXrayPort":52000}`
|
|
ib = &model.Inbound{Protocol: model.MTProto, Settings: stable}
|
|
if err := s.normalizeMtprotoXrayPort(ib, ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if ib.Settings != stable {
|
|
t.Fatalf("stable settings must pass through untouched, got %s", ib.Settings)
|
|
}
|
|
|
|
// Turning routing off strips both the bridge port and the inert outbound.
|
|
ib = &model.Inbound{Protocol: model.MTProto, Settings: `{"routeThroughXray":false,"routeXrayPort":53000,"outboundTag":"warp"}`}
|
|
if err := s.normalizeMtprotoXrayPort(ib, ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if p := routeXrayPortOf(t, ib.Settings); p != 0 {
|
|
t.Fatalf("disabling routing must drop the port, got %d", p)
|
|
}
|
|
var parsed map[string]any
|
|
if err := json.Unmarshal([]byte(ib.Settings), &parsed); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, ok := parsed["outboundTag"]; ok {
|
|
t.Fatalf("disabling routing must drop the inert outbound tag, got %s", ib.Settings)
|
|
}
|
|
}
|