mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-11-19 03:25:01 +03:00
feat(mtproto): route Telegram egress through Xray routing rules
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.
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
||||
xuilogger "github.com/mhsanaei/3x-ui/v3/internal/logger"
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/util/json_util"
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/xray"
|
||||
@@ -271,3 +272,99 @@ func TestInjectPanelEgress_BadRoutingSkips(t *testing.T) {
|
||||
t.Fatal("unparsable routing must be left untouched")
|
||||
}
|
||||
}
|
||||
|
||||
func mtprotoInbound(tag string, settings string) *model.Inbound {
|
||||
return &model.Inbound{Tag: tag, Protocol: model.MTProto, Enable: true, Settings: settings}
|
||||
}
|
||||
|
||||
func TestInjectMtprotoEgress_WithOutbound(t *testing.T) {
|
||||
cfg := egressTestConfig()
|
||||
injectMtprotoEgress(cfg, mtprotoInbound("inbound-443",
|
||||
`{"routeThroughXray":true,"routeXrayPort":50000,"outboundTag":"warp"}`))
|
||||
|
||||
if len(cfg.InboundConfigs) != 2 {
|
||||
t.Fatalf("expected the bridge inbound to be appended, got %d", len(cfg.InboundConfigs))
|
||||
}
|
||||
ib := cfg.InboundConfigs[1]
|
||||
if ib.Tag != "inbound-443" || ib.Protocol != "socks" || ib.Port != 50000 {
|
||||
t.Fatalf("unexpected bridge inbound: %+v", ib)
|
||||
}
|
||||
if string(ib.Listen) != `"127.0.0.1"` {
|
||||
t.Fatalf("bridge must listen on loopback, got %s", ib.Listen)
|
||||
}
|
||||
|
||||
var routing egressRouting
|
||||
if err := json.Unmarshal(cfg.RouterConfig, &routing); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(routing.Rules) != 2 {
|
||||
t.Fatalf("expected the egress rule prepended to the existing rule, got %+v", routing.Rules)
|
||||
}
|
||||
first := routing.Rules[0]
|
||||
if first.Type != "field" || first.OutboundTag != "warp" ||
|
||||
len(first.InboundTag) != 1 || first.InboundTag[0] != "inbound-443" {
|
||||
t.Fatalf("egress rule must bind the inbound tag to the outbound, got %+v", first)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInjectMtprotoEgress_NoOutboundLeavesRouting(t *testing.T) {
|
||||
cfg := egressTestConfig()
|
||||
before := string(cfg.RouterConfig)
|
||||
injectMtprotoEgress(cfg, mtprotoInbound("inbound-443",
|
||||
`{"routeThroughXray":true,"routeXrayPort":50001}`))
|
||||
|
||||
if len(cfg.InboundConfigs) != 2 || cfg.InboundConfigs[1].Port != 50001 {
|
||||
t.Fatalf("bridge must still be appended without an outbound, got %+v", cfg.InboundConfigs)
|
||||
}
|
||||
if string(cfg.RouterConfig) != before {
|
||||
t.Fatalf("no outbound means no rule change, got %s", cfg.RouterConfig)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInjectMtprotoEgress_BalancerTag(t *testing.T) {
|
||||
cfg := egressTestConfig()
|
||||
cfg.RouterConfig = json_util.RawMessage(`{"rules":[],"balancers":[{"tag":"lb","selector":["warp"]}]}`)
|
||||
injectMtprotoEgress(cfg, mtprotoInbound("inbound-443",
|
||||
`{"routeThroughXray":true,"routeXrayPort":50002,"outboundTag":"lb"}`))
|
||||
|
||||
var routing struct {
|
||||
Rules []struct {
|
||||
OutboundTag string `json:"outboundTag"`
|
||||
BalancerTag string `json:"balancerTag"`
|
||||
} `json:"rules"`
|
||||
}
|
||||
if err := json.Unmarshal(cfg.RouterConfig, &routing); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(routing.Rules) != 1 || routing.Rules[0].BalancerTag != "lb" || routing.Rules[0].OutboundTag != "" {
|
||||
t.Fatalf("a balancer tag must target balancerTag, got %+v", routing.Rules)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInjectMtprotoEgress_Disabled(t *testing.T) {
|
||||
// Not routed, and routed-but-portless, are both no-ops.
|
||||
for _, settings := range []string{
|
||||
`{"routeThroughXray":false,"routeXrayPort":50000}`,
|
||||
`{"routeThroughXray":true}`,
|
||||
`{"routeThroughXray":true,"routeXrayPort":0}`,
|
||||
} {
|
||||
cfg := egressTestConfig()
|
||||
before := string(cfg.RouterConfig)
|
||||
injectMtprotoEgress(cfg, mtprotoInbound("inbound-443", settings))
|
||||
if len(cfg.InboundConfigs) != 1 || string(cfg.RouterConfig) != before {
|
||||
t.Fatalf("settings %s must be a no-op, got %d inbounds", settings, len(cfg.InboundConfigs))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestInjectMtprotoEgress_TagCollisionSkips(t *testing.T) {
|
||||
cfg := egressTestConfig()
|
||||
cfg.InboundConfigs = append(cfg.InboundConfigs,
|
||||
xray.InboundConfig{Port: 443, Protocol: "vless", Tag: "inbound-443"})
|
||||
before := string(cfg.RouterConfig)
|
||||
injectMtprotoEgress(cfg, mtprotoInbound("inbound-443",
|
||||
`{"routeThroughXray":true,"routeXrayPort":50003,"outboundTag":"warp"}`))
|
||||
if len(cfg.InboundConfigs) != 2 || string(cfg.RouterConfig) != before {
|
||||
t.Fatal("a real inbound already owning the tag must make the bridge a no-op")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user