Separate router discovery, evidence and authorized control from device runtime. Add Node/Core contracts and reusable operator surfaces.
114 lines
2.9 KiB
Go
114 lines
2.9 KiB
Go
package routers
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"net"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type route struct {
|
|
Gateway string `json:"gateway"`
|
|
Dev string `json:"dev"`
|
|
PrefSrc string `json:"prefsrc"`
|
|
}
|
|
|
|
func commandJSON(ctx context.Context, args []string, output any) bool {
|
|
ctx, cancel := context.WithTimeout(ctx, time.Second)
|
|
defer cancel()
|
|
data, err := exec.CommandContext(ctx, "/usr/sbin/ip", args...).Output()
|
|
return err == nil && len(data) <= 65536 && json.Unmarshal(data, output) == nil
|
|
}
|
|
|
|
func physical(name string) bool {
|
|
if name == "" || filepath.Base(name) != name {
|
|
return false
|
|
}
|
|
_, err := os.Stat(filepath.Join("/sys/class/net", name, "device"))
|
|
return err == nil
|
|
}
|
|
|
|
func onLink(iface, address string) (string, bool) {
|
|
i, err := net.InterfaceByName(iface)
|
|
if err != nil || i.Flags&net.FlagUp == 0 {
|
|
return "", false
|
|
}
|
|
addrs, err := i.Addrs()
|
|
if err != nil {
|
|
return "", false
|
|
}
|
|
target := net.ParseIP(address)
|
|
if target == nil || target.To4() == nil || !target.IsPrivate() {
|
|
return "", false
|
|
}
|
|
for _, addr := range addrs {
|
|
ip, subnet, err := net.ParseCIDR(addr.String())
|
|
if err == nil && ip.To4() != nil && !ip.Equal(target) && subnet.Contains(target) {
|
|
return ip.String(), true
|
|
}
|
|
}
|
|
return "", false
|
|
}
|
|
|
|
// Discovery never scans subnets or follows a Tailscale/default VPN gateway.
|
|
// Remembered addresses are eligible only on a currently connected physical LAN.
|
|
func discover(ctx context.Context, remembered []Candidate) []Candidate {
|
|
var routes []route
|
|
if !commandJSON(ctx, []string{"-j", "-4", "route", "show", "table", "main"}, &routes) {
|
|
return nil
|
|
}
|
|
for _, r := range routes {
|
|
if r.Gateway != "" {
|
|
remembered = append(remembered, Candidate{Address: r.Gateway, Interface: r.Dev})
|
|
}
|
|
}
|
|
seen := map[string]bool{}
|
|
var found []Candidate
|
|
for _, c := range remembered {
|
|
if len(found) >= maxDevices || seen[c.Address] || !physical(c.Interface) {
|
|
continue
|
|
}
|
|
local, ok := onLink(c.Interface, c.Address)
|
|
if !ok {
|
|
continue
|
|
}
|
|
var selected []route
|
|
if !commandJSON(ctx, []string{"-j", "-4", "route", "get", c.Address}, &selected) || len(selected) != 1 || selected[0].Dev != c.Interface || selected[0].Gateway != "" {
|
|
continue
|
|
}
|
|
c.LocalIP, c.MAC = local, neighbor(c.Interface, c.Address)
|
|
seen[c.Address] = true
|
|
found = append(found, c)
|
|
}
|
|
return found
|
|
}
|
|
|
|
func neighbor(iface, ip string) string {
|
|
data, err := os.ReadFile("/proc/net/arp")
|
|
if err != nil || len(data) > 65536 {
|
|
return ""
|
|
}
|
|
for _, line := range strings.Split(string(data), "\n") {
|
|
f := strings.Fields(line)
|
|
if len(f) != 6 || f[0] != ip || f[5] != iface || f[2] != "0x2" {
|
|
continue
|
|
}
|
|
mac, err := net.ParseMAC(f[3])
|
|
if err == nil && len(mac) == 6 && mac[0]&1 == 0 && f[3] != "00:00:00:00:00:00" {
|
|
return mac.String()
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func identity(mac string) string {
|
|
sum := sha256.Sum256([]byte("dexaflow:lan:" + mac))
|
|
return "router_" + hex.EncodeToString(sum[:12])
|
|
}
|