Detect interfaces per platform; add macOS and single-interface support (closes #2)

Linux runs exactly as before when both bridge interfaces exist. When they do
not, the lone default-route interface is monitored, and a single interface now
draws a single UI pane instead of an empty second one.

macOS is newly supported: a running VPN tunnel (utun) is monitored as the
primary pane alongside the physical default-route interface, or the physical
interface alone when no VPN is up.

Detection lives in internal/netdetect: interface/route data types, pure
selection logic keyed on OS name, and route parsers, all unit-tested on Linux
for both platforms. Only the real route query and the per-platform TCP dial
binding (source address on Linux, IP_BOUND_IF on macOS) are build-tagged. Ping
argument construction is a pure, OS-keyed function. NewMonitor now takes a list
of interfaces.

Model: opus-4-8
This commit is contained in:
2026-09-21 06:53:39 +00:00
parent cf9dc39053
commit ef3c373a12
18 changed files with 933 additions and 85 deletions
+54 -23
View File
@@ -1,6 +1,3 @@
//go:build linux
// +build linux
package monitor
import (
@@ -11,6 +8,7 @@ import (
"net"
"os"
"os/exec"
"runtime"
"strconv"
"strings"
"sync"
@@ -53,9 +51,8 @@ type Monitor struct {
packetLossHosts []string
tcpHosts []string
// Interfaces
interfaceA *InterfaceStatus
interfaceB *InterfaceStatus
// Interfaces to monitor (one or two)
interfaces []*InterfaceStatus
// Logging
logFile string
@@ -68,9 +65,16 @@ type Monitor struct {
mu sync.RWMutex
}
// NewMonitor creates a new Monitor instance with default settings
func NewMonitor(ifaceA, labelA, ifaceB, labelB, logFile string) *Monitor {
return &Monitor{
// IfaceSpec names one interface to monitor and its display label.
type IfaceSpec struct {
Name string
Label string
}
// NewMonitor creates a new Monitor instance with default settings, monitoring
// the given interfaces (one or two).
func NewMonitor(ifaces []IfaceSpec, logFile string) *Monitor {
m := &Monitor{
// Default timing configuration
ICMPTimeout: 500 * time.Millisecond,
TCPTimeout: 500 * time.Millisecond,
@@ -94,15 +98,17 @@ func NewMonitor(ifaceA, labelA, ifaceB, labelB, logFile string) *Monitor {
packetLossHosts: []string{},
tcpHosts: []string{},
// Initialize interfaces
interfaceA: NewInterfaceStatus(ifaceA, labelA),
interfaceB: NewInterfaceStatus(ifaceB, labelB),
// Logging
logFile: logFile,
startTime: time.Now(),
}
for _, spec := range ifaces {
m.interfaces = append(m.interfaces, NewInterfaceStatus(spec.Name, spec.Label))
}
return m
}
// AddReachabilityHost adds a host for reachability monitoring
@@ -159,12 +165,11 @@ func (m *Monitor) Run(ctx context.Context) error {
tcpHosts := append([]string{}, m.tcpHosts...)
m.mu.RUnlock()
go m.reachLoop(ctx, m.interfaceA, reachHosts)
go m.reachLoop(ctx, m.interfaceB, reachHosts)
go m.lossLoop(ctx, m.interfaceA, lossHosts)
go m.lossLoop(ctx, m.interfaceB, lossHosts)
go m.tcpLoop(ctx, m.interfaceA, tcpHosts)
go m.tcpLoop(ctx, m.interfaceB, tcpHosts)
for _, st := range m.interfaces {
go m.reachLoop(ctx, st, reachHosts)
go m.lossLoop(ctx, st, lossHosts)
go m.tcpLoop(ctx, st, tcpHosts)
}
// Run UI loop
m.logf("Starting UI loop")
@@ -252,19 +257,45 @@ func fetchIPInfo(iface string) string {
return fmt.Sprintf("%s [%s] %s", r.IP, r.Hostname, r.Org)
}
// pingArgs builds the arguments for a single reachability ping on the given
// OS. Linux binds the interface with -I and takes -W in seconds; macOS binds
// with -b and takes -W in milliseconds.
func pingArgs(goos, iface, host string) []string {
if goos == "darwin" {
return []string{"-b", iface, "-c1", "-W1000", host}
}
return []string{"-I", iface, "-c1", "-W1", host}
}
// lossArgs builds the arguments for a packet-loss ping burst of count pings.
func lossArgs(goos, iface, host string, count int) []string {
c := strconv.Itoa(count)
if goos == "darwin" {
return []string{"-q", "-i", "0.05", "-c", c, "-W1000", "-b", iface, host}
}
return []string{"-q", "-i", "0.05", "-c", c, "-W1", "-I", iface, host}
}
// pingOnce performs a single ping
func (m *Monitor) pingOnce(iface, host string) bool {
ctx, cancel := context.WithTimeout(context.Background(), m.ICMPTimeout)
defer cancel()
return exec.CommandContext(ctx, "ping", "-I", iface, "-c1", "-W1", host).Run() == nil
args := pingArgs(runtime.GOOS, iface, host)
return exec.CommandContext(ctx, "ping", args...).Run() == nil
}
// lossPercent calculates packet loss percentage
func (m *Monitor) lossPercent(iface, host string) float64 {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
out, err := exec.CommandContext(ctx, "ping", "-q", "-i", "0.05",
"-c", fmt.Sprint(m.PacketLossPings), "-W1", "-I", iface, host).CombinedOutput()
args := lossArgs(runtime.GOOS, iface, host, m.PacketLossPings)
out, err := exec.CommandContext(ctx, "ping", args...).CombinedOutput()
if err != nil {
return 1.0
}
@@ -302,7 +333,7 @@ func (m *Monitor) tcpDuration(iface, hp string) time.Duration {
if err != nil {
return m.TCPTimeout
}
d := net.Dialer{Timeout: m.TCPTimeout, LocalAddr: la}
d := net.Dialer{Timeout: m.TCPTimeout, LocalAddr: la, Control: bindControl(iface)}
st := time.Now()
c, err := d.Dial("tcp", hp)
if err != nil {