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>
191 lines
5.3 KiB
Go
191 lines
5.3 KiB
Go
package main
|
|
|
|
// WAN-mode / failover profiles (catalog §7, T2). A `config profile` defines
|
|
// conditions (active default-route interface, a connectivity probe up/down, a
|
|
// time window — all AND'd) and overrides (force-enable/disable named rules,
|
|
// override the default catch-all target/egress). The highest-priority profile
|
|
// whose conditions all hold is "active"; its overrides are applied at gen time.
|
|
// Evaluated on every reconcile; the cron re-applies when the active profile
|
|
// changes (its name feeds scheduleSignature).
|
|
|
|
import (
|
|
"net/http"
|
|
"os/exec"
|
|
"sort"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// strSet builds a lookup set from a string slice.
|
|
func strSet(ss []string) map[string]bool {
|
|
m := map[string]bool{}
|
|
for _, s := range ss {
|
|
m[s] = true
|
|
}
|
|
return m
|
|
}
|
|
|
|
// 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 ""
|
|
}
|
|
best, bestMetric := "", 1<<30
|
|
for _, line := range strings.Split(string(out), "\n") {
|
|
f := strings.Fields(line)
|
|
dev, metric := "", 0
|
|
for i := 0; i < len(f); i++ {
|
|
if f[i] == "dev" && i+1 < len(f) {
|
|
dev = f[i+1]
|
|
}
|
|
if f[i] == "metric" && i+1 < len(f) {
|
|
metric = atoiOr(f[i+1], 0)
|
|
}
|
|
}
|
|
if dev != "" && metric < bestMetric {
|
|
best, bestMetric = dev, metric
|
|
}
|
|
}
|
|
return best
|
|
}
|
|
|
|
// profileProbeUp reports whether an HTTP(S) probe to url succeeds (2xx/3xx).
|
|
func profileProbeUp(url string) bool {
|
|
if !strings.Contains(url, "://") {
|
|
url = "http://" + url
|
|
}
|
|
c := &http.Client{Timeout: 6 * time.Second}
|
|
resp, err := c.Get(url)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
defer resp.Body.Close()
|
|
return resp.StatusCode < 400
|
|
}
|
|
|
|
// profileConditionsHold checks a profile's specified conditions (all must hold).
|
|
// iface and now are passed in so a full evaluation dials the probe at most once.
|
|
func profileConditionsHold(p Profile, iface string, now time.Time) bool {
|
|
if len(p.MatchIface) > 0 {
|
|
hit := false
|
|
for _, want := range p.MatchIface {
|
|
if strings.EqualFold(strings.TrimSpace(want), iface) {
|
|
hit = true
|
|
break
|
|
}
|
|
}
|
|
if !hit {
|
|
return false
|
|
}
|
|
}
|
|
if p.ProbeURL != "" {
|
|
up := profileProbeUp(p.ProbeURL)
|
|
if strings.EqualFold(p.ProbeMode, "down") {
|
|
if up {
|
|
return false // want probe down, but it's up
|
|
}
|
|
} else if !up {
|
|
return false // want probe up, but it's down
|
|
}
|
|
}
|
|
if p.SchedStart != "" || p.SchedEnd != "" || len(p.SchedDays) > 0 {
|
|
r := Rule{SchedEnabled: true, SchedDays: p.SchedDays, SchedStart: p.SchedStart, SchedEnd: p.SchedEnd, SchedTZ: p.SchedTZ}
|
|
if !ruleActiveNow(r, now) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// activeWanProfile returns the highest-priority enabled profile whose conditions
|
|
// all hold, or nil. A profile with NO conditions never activates (avoids a
|
|
// permanent override with an empty match set).
|
|
func activeWanProfile(m *Model, now time.Time) *Profile {
|
|
var candidates []Profile
|
|
for _, p := range m.Profiles {
|
|
if !p.Enabled {
|
|
continue
|
|
}
|
|
if len(p.MatchIface) == 0 && p.ProbeURL == "" && p.SchedStart == "" && p.SchedEnd == "" && len(p.SchedDays) == 0 {
|
|
continue // no conditions -> never auto-activates
|
|
}
|
|
candidates = append(candidates, p)
|
|
}
|
|
sort.SliceStable(candidates, func(i, j int) bool { return candidates[i].Priority > candidates[j].Priority })
|
|
iface := ""
|
|
needIface := false
|
|
for _, p := range candidates {
|
|
if len(p.MatchIface) > 0 {
|
|
needIface = true
|
|
}
|
|
}
|
|
if needIface {
|
|
iface = defaultIface()
|
|
}
|
|
for i := range candidates {
|
|
if profileConditionsHold(candidates[i], iface, now) {
|
|
p := candidates[i]
|
|
return &p
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ProfileStatusJSON reports the currently-active WAN-mode profile + evaluation.
|
|
func ProfileStatusJSON() ([]byte, error) {
|
|
m, err := ReadUCI()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
now := timeNow()
|
|
iface := defaultIface()
|
|
active := activeWanProfile(m, now)
|
|
type row struct {
|
|
Name string `json:"name"`
|
|
Enabled bool `json:"enabled"`
|
|
Active bool `json:"active"`
|
|
}
|
|
var rows []row
|
|
for _, p := range m.Profiles {
|
|
rows = append(rows, row{p.Name, p.Enabled, active != nil && active.Name == p.Name})
|
|
}
|
|
res := map[string]any{
|
|
"default_iface": iface,
|
|
"active": "",
|
|
"profiles": rows,
|
|
}
|
|
if active != nil {
|
|
res["active"] = active.Name
|
|
}
|
|
return jsonMarshalIndent(res)
|
|
}
|