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:
@@ -0,0 +1,36 @@
|
||||
//go:build darwin
|
||||
|
||||
package monitor
|
||||
|
||||
import (
|
||||
"net"
|
||||
"syscall"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
// bindControl returns a socket control hook that pins the connection to the
|
||||
// given interface with IP_BOUND_IF. On macOS a bound source address is not
|
||||
// enough: without this the kernel still routes the packets over the VPN's
|
||||
// default route, so traffic would not leave the interface we are measuring.
|
||||
func bindControl(iface string) func(network, address string, c syscall.RawConn) error {
|
||||
ifi, err := net.InterfaceByName(iface)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
idx := ifi.Index
|
||||
|
||||
return func(_, _ string, c syscall.RawConn) error {
|
||||
var setErr error
|
||||
|
||||
ctrlErr := c.Control(func(fd uintptr) {
|
||||
setErr = unix.SetsockoptInt(int(fd), unix.IPPROTO_IP, unix.IP_BOUND_IF, idx)
|
||||
})
|
||||
if ctrlErr != nil {
|
||||
return ctrlErr
|
||||
}
|
||||
|
||||
return setErr
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
//go:build linux
|
||||
|
||||
package monitor
|
||||
|
||||
import "syscall"
|
||||
|
||||
// bindControl returns no socket control hook on Linux: binding the dialer's
|
||||
// local source address (as tcpDuration already does) is enough to send
|
||||
// traffic out of the chosen interface.
|
||||
func bindControl(_ string) func(network, address string, c syscall.RawConn) error {
|
||||
return nil
|
||||
}
|
||||
@@ -1,6 +1,3 @@
|
||||
//go:build linux
|
||||
// +build linux
|
||||
|
||||
package monitor
|
||||
|
||||
import (
|
||||
|
||||
+54
-23
@@ -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 {
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
//go:build linux
|
||||
// +build linux
|
||||
|
||||
package monitor
|
||||
|
||||
import (
|
||||
@@ -10,27 +7,26 @@ import (
|
||||
// TestNewMonitor tests the creation of a new Monitor
|
||||
func TestNewMonitor(t *testing.T) {
|
||||
// Test that we can create a monitor without errors
|
||||
mon := NewMonitor("test0", "Test Interface A", "test1", "Test Interface B", "/tmp/test.log")
|
||||
mon := NewMonitor([]IfaceSpec{
|
||||
{Name: "test0", Label: "Test Interface A"},
|
||||
{Name: "test1", Label: "Test Interface B"},
|
||||
}, "/tmp/test.log")
|
||||
|
||||
if mon == nil {
|
||||
t.Fatal("NewMonitor returned nil")
|
||||
}
|
||||
|
||||
// Check interfaces
|
||||
if mon.interfaceA == nil {
|
||||
t.Fatal("interfaceA is nil")
|
||||
if len(mon.interfaces) != 2 {
|
||||
t.Fatalf("Expected 2 interfaces, got %d", len(mon.interfaces))
|
||||
}
|
||||
|
||||
if mon.interfaceB == nil {
|
||||
t.Fatal("interfaceB is nil")
|
||||
if mon.interfaces[0].Name != "test0" {
|
||||
t.Errorf("Expected interfaces[0].Name to be 'test0', got '%s'", mon.interfaces[0].Name)
|
||||
}
|
||||
|
||||
if mon.interfaceA.Name != "test0" {
|
||||
t.Errorf("Expected interfaceA.Name to be 'test0', got '%s'", mon.interfaceA.Name)
|
||||
}
|
||||
|
||||
if mon.interfaceB.Name != "test1" {
|
||||
t.Errorf("Expected interfaceB.Name to be 'test1', got '%s'", mon.interfaceB.Name)
|
||||
if mon.interfaces[1].Name != "test1" {
|
||||
t.Errorf("Expected interfaces[1].Name to be 'test1', got '%s'", mon.interfaces[1].Name)
|
||||
}
|
||||
|
||||
// Check default configuration
|
||||
@@ -56,9 +52,25 @@ func TestNewMonitor(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestNewMonitorSingle verifies a monitor can be built with one interface.
|
||||
func TestNewMonitorSingle(t *testing.T) {
|
||||
mon := NewMonitor([]IfaceSpec{{Name: "eth0", Label: "default route"}}, "")
|
||||
|
||||
if len(mon.interfaces) != 1 {
|
||||
t.Fatalf("Expected 1 interface, got %d", len(mon.interfaces))
|
||||
}
|
||||
|
||||
if mon.interfaces[0].Name != "eth0" {
|
||||
t.Errorf("Expected interfaces[0].Name to be 'eth0', got '%s'", mon.interfaces[0].Name)
|
||||
}
|
||||
}
|
||||
|
||||
// TestAddHosts tests adding hosts to the monitor
|
||||
func TestAddHosts(t *testing.T) {
|
||||
mon := NewMonitor("test0", "Test A", "test1", "Test B", "")
|
||||
mon := NewMonitor([]IfaceSpec{
|
||||
{Name: "test0", Label: "Test A"},
|
||||
{Name: "test1", Label: "Test B"},
|
||||
}, "")
|
||||
|
||||
// Test adding reachability hosts
|
||||
mon.AddReachabilityHost("8.8.8.8")
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
package monitor
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPingArgs(t *testing.T) {
|
||||
tests := []struct {
|
||||
goos string
|
||||
want []string
|
||||
}{
|
||||
{"linux", []string{"-I", "eth0", "-c1", "-W1", "8.8.8.8"}},
|
||||
{"darwin", []string{"-b", "eth0", "-c1", "-W1000", "8.8.8.8"}},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.goos, func(t *testing.T) {
|
||||
got := pingArgs(tt.goos, "eth0", "8.8.8.8")
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("pingArgs(%q) = %v, want %v", tt.goos, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLossArgs(t *testing.T) {
|
||||
tests := []struct {
|
||||
goos string
|
||||
want []string
|
||||
}{
|
||||
{"linux", []string{
|
||||
"-q", "-i", "0.05", "-c", "20", "-W1", "-I", "eth0", "8.8.8.8",
|
||||
}},
|
||||
{"darwin", []string{
|
||||
"-q", "-i", "0.05", "-c", "20", "-W1000", "-b", "eth0", "8.8.8.8",
|
||||
}},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.goos, func(t *testing.T) {
|
||||
got := lossArgs(tt.goos, "eth0", "8.8.8.8", 20)
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("lossArgs(%q) = %v, want %v", tt.goos, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,3 @@
|
||||
//go:build linux
|
||||
// +build linux
|
||||
|
||||
package monitor
|
||||
|
||||
import tcell "github.com/gdamore/tcell/v2"
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
//go:build linux
|
||||
// +build linux
|
||||
|
||||
package monitor
|
||||
|
||||
import (
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
//go:build linux
|
||||
// +build linux
|
||||
|
||||
package monitor
|
||||
|
||||
import (
|
||||
@@ -345,10 +342,11 @@ func (m *Monitor) uiLoop(ctx context.Context) {
|
||||
// Draw the runtime
|
||||
Put(m.screen, 0, 3, "Runtime: "+time.Since(m.startTime).Round(time.Second).String(), CDefault)
|
||||
|
||||
// Draw interfaces
|
||||
// Draw interfaces (one pane each; a single interface draws one pane)
|
||||
y := 5
|
||||
y = m.DrawInterface(m.screen, y, w, m.interfaceA)
|
||||
_ = m.DrawInterface(m.screen, y, w, m.interfaceB)
|
||||
for _, st := range m.interfaces {
|
||||
y = m.DrawInterface(m.screen, y, w, st)
|
||||
}
|
||||
|
||||
// Show the screen
|
||||
m.screen.Show()
|
||||
|
||||
Reference in New Issue
Block a user