From f1e4f16a9f9f4f7139800409bc4f4739abd57994 Mon Sep 17 00:00:00 2001 From: omar Date: Thu, 9 Jul 2026 17:16:16 +0300 Subject: [PATCH] fix(profiles): pick the active uplink via `ip route get` (metric/policy-aware) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit defaultIface() now asks the kernel authoritatively which interface it would egress a public destination through (`ip route get 8.8.8.8`/`1.1.1.1`) instead of parsing `ip route show default` by metric. This is correct for multi-WAN / mwan3 / policy- routing setups where the active uplink is chosen by metric AND ip rules — the query carries no fwmark so it reflects the real WAN egress, not the tproxy loopback table. Falls back to the lowest-metric default route if `ip route get` is unavailable. Clarifies the two orderings: which INTERFACE is active = kernel route metric; which PROFILE applies = profile priority among those whose conditions all hold. Verified live: `ip route get 8.8.8.8` -> eth1; a profile matching dum0 with HIGHER priority stays inactive (its iface condition fails) while the eth1 profile (lower priority, condition holds) is active — priority never overrides an unmet condition. - profile_wanmode.go: defaultIface -> ip route get + devFromRouteGet + lowestMetricDefaultIface fallback. - test: TestDevFromRouteGet. r17. Co-Authored-By: Claude Opus 4.8 (1M context) --- shater-core/Makefile | 2 +- xrayctl/Makefile | 2 +- xrayctl/profile_wanmode.go | 31 +++++++++++++++++++++++++++++-- xrayctl/profile_wanmode_test.go | 15 +++++++++++++++ 4 files changed, 46 insertions(+), 4 deletions(-) diff --git a/shater-core/Makefile b/shater-core/Makefile index 43ef98af..21f1b929 100644 --- a/shater-core/Makefile +++ b/shater-core/Makefile @@ -14,7 +14,7 @@ include $(TOPDIR)/rules.mk PKG_NAME:=shater-core PKG_VERSION:=0.1.0 -PKG_RELEASE:=16 +PKG_RELEASE:=17 PKG_MAINTAINER:=Shater PKG_LICENSE:=GPL-2.0-or-later diff --git a/xrayctl/Makefile b/xrayctl/Makefile index 919d5c18..c7401ec2 100644 --- a/xrayctl/Makefile +++ b/xrayctl/Makefile @@ -10,7 +10,7 @@ include $(TOPDIR)/rules.mk PKG_NAME:=xrayctl PKG_VERSION:=0.1.0 -PKG_RELEASE:=16 +PKG_RELEASE:=17 PKG_MAINTAINER:=shater PKG_LICENSE:=MIT diff --git a/xrayctl/profile_wanmode.go b/xrayctl/profile_wanmode.go index 5c3d2543..6b7a95ee 100644 --- a/xrayctl/profile_wanmode.go +++ b/xrayctl/profile_wanmode.go @@ -25,9 +25,36 @@ func strSet(ss []string) map[string]bool { return m } -// defaultIface returns the interface of the current default route (lowest metric -// first), or "" if none / unavailable. +// defaultIface returns the interface the kernel would actually egress a public +// destination through — i.e. which of possibly-several active uplinks is the +// current default. It asks the kernel authoritatively via `ip route get` (which +// honours metric AND policy/mwan3 rules), and falls back to the lowest-metric +// default route. Our own fwmark policy rule is not matched (this query carries no +// mark), so it reflects the real WAN egress, not the tproxy loopback table. func defaultIface() string { + for _, dst := range []string{"8.8.8.8", "1.1.1.1"} { + if out, err := exec.Command("ip", "route", "get", dst).Output(); err == nil { + if dev := devFromRouteGet(string(out)); dev != "" { + return dev + } + } + } + return lowestMetricDefaultIface() +} + +// devFromRouteGet extracts the `dev ` from `ip route get` output. +func devFromRouteGet(out string) string { + f := strings.Fields(out) + for i := 0; i < len(f); i++ { + if f[i] == "dev" && i+1 < len(f) { + return f[i+1] + } + } + return "" +} + +// lowestMetricDefaultIface picks the dev of the lowest-metric default route. +func lowestMetricDefaultIface() string { out, err := exec.Command("ip", "route", "show", "default").Output() if err != nil { return "" diff --git a/xrayctl/profile_wanmode_test.go b/xrayctl/profile_wanmode_test.go index 7689c708..8f71ee2e 100644 --- a/xrayctl/profile_wanmode_test.go +++ b/xrayctl/profile_wanmode_test.go @@ -5,6 +5,21 @@ import ( "time" ) +func TestDevFromRouteGet(t *testing.T) { + // authoritative kernel answer for the active egress + out := "8.8.8.8 via 10.0.2.2 dev eth1 src 10.0.2.15 uid 0 \n cache " + if devFromRouteGet(out) != "eth1" { + t.Fatalf("got %q", devFromRouteGet(out)) + } + // a directly-connected dest: "1.2.3.4 dev wwan0 src ..." + if devFromRouteGet("1.2.3.4 dev wwan0 src 1.2.3.5") != "wwan0" { + t.Fatal("wwan0 not parsed") + } + if devFromRouteGet("unreachable 1.2.3.4") != "" { + t.Fatal("expected empty for no dev") + } +} + func TestProfileConditionsIface(t *testing.T) { p := Profile{Name: "sim", Enabled: true, MatchIface: []string{"wwan0", "usb0"}} now := at(t, "2026-07-09T12:00:00Z")