fix(profiles): pick the active uplink via ip route get (metric/policy-aware)
build / aarch64_cortex-a53 (push) Successful in 3m29s
build / x86_64 (push) Successful in 3m17s

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) <noreply@anthropic.com>
This commit is contained in:
omarandClaude Opus 4.8 committed 2026-07-09 17:16:16 +03:00
1 parent 6c01d8db41
commit f1e4f16a9f
4 files changed
+46 -4

No files matched your search

+1 -1
View File
@@ -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 <maqrota@icloud.com>
PKG_LICENSE:=GPL-2.0-or-later
+1 -1
View File
@@ -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
+29 -2
View File
@@ -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 <x>` 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 ""
+15
View File
@@ -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")