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>
142 lines
4.9 KiB
Go
142 lines
4.9 KiB
Go
package main
|
|
|
|
import (
|
|
"testing"
|
|
"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")
|
|
if !profileConditionsHold(p, "wwan0", now) {
|
|
t.Fatal("should hold on wwan0")
|
|
}
|
|
if profileConditionsHold(p, "eth0", now) {
|
|
t.Fatal("should not hold on eth0")
|
|
}
|
|
}
|
|
|
|
func TestProfileConditionsSchedule(t *testing.T) {
|
|
p := Profile{Name: "night", Enabled: true, SchedTZ: "UTC", SchedStart: "22:00", SchedEnd: "23:00"}
|
|
if !profileConditionsHold(p, "", at(t, "2026-07-09T22:30:00Z")) {
|
|
t.Fatal("should hold at 22:30")
|
|
}
|
|
if profileConditionsHold(p, "", at(t, "2026-07-09T12:00:00Z")) {
|
|
t.Fatal("should not hold at 12:00")
|
|
}
|
|
}
|
|
|
|
func TestProfileConditionsAND(t *testing.T) {
|
|
// iface AND schedule: both must hold
|
|
p := Profile{Name: "x", Enabled: true, MatchIface: []string{"wwan0"}, SchedTZ: "UTC", SchedStart: "22:00", SchedEnd: "23:00"}
|
|
if profileConditionsHold(p, "wwan0", at(t, "2026-07-09T12:00:00Z")) {
|
|
t.Fatal("iface ok but out of window -> must not hold")
|
|
}
|
|
if profileConditionsHold(p, "eth0", at(t, "2026-07-09T22:30:00Z")) {
|
|
t.Fatal("in window but wrong iface -> must not hold")
|
|
}
|
|
if !profileConditionsHold(p, "wwan0", at(t, "2026-07-09T22:30:00Z")) {
|
|
t.Fatal("both hold -> must hold")
|
|
}
|
|
}
|
|
|
|
func TestActiveWanProfilePriority(t *testing.T) {
|
|
m := &Model{Globals: defaultGlobals()}
|
|
m.Profiles = []Profile{
|
|
{Name: "lo", Enabled: true, Priority: 1, SchedTZ: "UTC", SchedStart: "00:00", SchedEnd: "23:59"},
|
|
{Name: "hi", Enabled: true, Priority: 9, SchedTZ: "UTC", SchedStart: "00:00", SchedEnd: "23:59"},
|
|
}
|
|
p := activeWanProfile(m, at(t, "2026-07-09T12:00:00Z"))
|
|
if p == nil || p.Name != "hi" {
|
|
t.Fatalf("expected 'hi' by priority, got %v", p)
|
|
}
|
|
}
|
|
|
|
func TestActiveWanProfileNoConditionsNeverActive(t *testing.T) {
|
|
m := &Model{Globals: defaultGlobals()}
|
|
m.Profiles = []Profile{{Name: "empty", Enabled: true}}
|
|
if activeWanProfile(m, at(t, "2026-07-09T12:00:00Z")) != nil {
|
|
t.Fatal("a profile with no conditions must never auto-activate")
|
|
}
|
|
}
|
|
|
|
func TestProfileOverrideEnableDisable(t *testing.T) {
|
|
saved := timeNow
|
|
defer func() { timeNow = saved }()
|
|
timeNow = func() time.Time { return at(t, "2026-07-09T22:30:00Z") }
|
|
|
|
m := &Model{Globals: defaultGlobals()}
|
|
m.Nodes = []Node{{Name: "n1", Enabled: true, URI: vlessReality}}
|
|
m.Rules = []Rule{
|
|
{Name: "off-by-default", Enabled: false, Order: 10, DstDomain: []string{"ru.example"}, Target: "node:n1"},
|
|
{Name: "on-by-default", Enabled: true, Order: 11, DstDomain: []string{"keep.example"}, Target: "node:n1"},
|
|
}
|
|
m.Profiles = []Profile{{
|
|
Name: "night", Enabled: true, SchedTZ: "UTC", SchedStart: "22:00", SchedEnd: "23:00",
|
|
EnableRules: []string{"off-by-default"}, DisableRules: []string{"on-by-default"},
|
|
}}
|
|
cfg, err := BuildConfig(m)
|
|
if err != nil {
|
|
t.Fatalf("BuildConfig: %v", err)
|
|
}
|
|
if ruleWithDomain(cfg, "ru.example") == nil {
|
|
t.Fatal("profile should have enabled 'off-by-default'")
|
|
}
|
|
if ruleWithDomain(cfg, "keep.example") != nil {
|
|
t.Fatal("profile should have disabled 'on-by-default'")
|
|
}
|
|
}
|
|
|
|
func TestProfileDefaultTargetInjected(t *testing.T) {
|
|
saved := timeNow
|
|
defer func() { timeNow = saved }()
|
|
timeNow = func() time.Time { return at(t, "2026-07-09T22:30:00Z") }
|
|
|
|
m := &Model{Globals: defaultGlobals()}
|
|
m.Nodes = []Node{{Name: "ru", Enabled: true, URI: vlessReality}}
|
|
m.Profiles = []Profile{{
|
|
Name: "whitelist", Enabled: true, SchedTZ: "UTC", SchedStart: "22:00", SchedEnd: "23:00",
|
|
DefaultTarget: "node:ru",
|
|
}}
|
|
cfg, err := BuildConfig(m)
|
|
if err != nil {
|
|
t.Fatalf("BuildConfig: %v", err)
|
|
}
|
|
if routingHasTarget(cfg, "node_ru") == nil {
|
|
t.Fatal("profile default target should inject a catch-all to node_ru")
|
|
}
|
|
}
|
|
|
|
func TestProfileInactiveNoOverride(t *testing.T) {
|
|
saved := timeNow
|
|
defer func() { timeNow = saved }()
|
|
timeNow = func() time.Time { return at(t, "2026-07-09T12:00:00Z") } // outside window
|
|
|
|
m := &Model{Globals: defaultGlobals()}
|
|
m.Nodes = []Node{{Name: "n1", Enabled: true, URI: vlessReality}}
|
|
m.Rules = []Rule{{Name: "off-by-default", Enabled: false, Order: 10, DstDomain: []string{"ru.example"}, Target: "node:n1"}}
|
|
m.Profiles = []Profile{{
|
|
Name: "night", Enabled: true, SchedTZ: "UTC", SchedStart: "22:00", SchedEnd: "23:00",
|
|
EnableRules: []string{"off-by-default"},
|
|
}}
|
|
cfg, _ := BuildConfig(m)
|
|
if ruleWithDomain(cfg, "ru.example") != nil {
|
|
t.Fatal("inactive profile must not enable the rule")
|
|
}
|
|
}
|