//go:build linux package netdetect import ( "context" "fmt" "os" "os/exec" "time" ) // routeQueryTimeout bounds the external route-table query. const routeQueryTimeout = 2 * time.Second // DefaultRoutes returns the host's IPv4 default routes. It prefers the `ip` // command and falls back to reading /proc/net/route. func DefaultRoutes() ([]Route, error) { ctx, cancel := context.WithTimeout(context.Background(), routeQueryTimeout) defer cancel() out, err := exec.CommandContext(ctx, "ip", "-4", "route", "show", "default").Output() if err == nil { if routes := parseIPRoute(string(out)); len(routes) > 0 { return routes, nil } } data, err := os.ReadFile("/proc/net/route") if err != nil { return nil, fmt.Errorf("reading /proc/net/route: %w", err) } return parseProcNetRoute(string(data)), nil }