diff --git a/cmd/cli/cli.go b/cmd/cli/cli.go index 39a5977..7565517 100644 --- a/cmd/cli/cli.go +++ b/cmd/cli/cli.go @@ -764,7 +764,7 @@ func netInterface(ifaceName string) (*net.Interface, error) { if iface == nil { return nil, errors.New("interface not found") } - if err := patchNetIfaceName(iface); err != nil { + if _, err := patchNetIfaceName(iface); err != nil { return nil, err } return iface, err diff --git a/cmd/cli/commands.go b/cmd/cli/commands.go index bae0cf1..9845093 100644 --- a/cmd/cli/commands.go +++ b/cmd/cli/commands.go @@ -9,7 +9,6 @@ import ( "io" "net" "net/http" - "net/netip" "os" "os/exec" "path/filepath" @@ -25,7 +24,6 @@ import ( "github.com/olekukonko/tablewriter" "github.com/spf13/cobra" "github.com/spf13/pflag" - "tailscale.com/net/netmon" "github.com/Control-D-Inc/ctrld" "github.com/Control-D-Inc/ctrld/internal/clientinfo" @@ -903,7 +901,7 @@ func initInterfacesCmd() *cobra.Command { Short: "List network interfaces of the host", Args: cobra.NoArgs, Run: func(cmd *cobra.Command, args []string) { - err := netmon.ForeachInterface(func(i netmon.Interface, prefixes []netip.Prefix) { + withEachPhysicalInterfaces("", "", func(i *net.Interface) error { fmt.Printf("Index : %d\n", i.Index) fmt.Printf("Name : %s\n", i.Name) addrs, _ := i.Addrs() @@ -914,7 +912,14 @@ func initInterfacesCmd() *cobra.Command { } fmt.Printf(" %v\n", ipaddr) } - for i, dns := range currentDNS(i.Interface) { + nss, err := currentStaticDNS(i) + if err != nil { + mainLog.Load().Warn().Err(err).Msg("failed to get DNS") + } + if len(nss) == 0 { + nss = currentDNS(i) + } + for i, dns := range nss { if i == 0 { fmt.Printf("DNS : %s\n", dns) continue @@ -922,10 +927,8 @@ func initInterfacesCmd() *cobra.Command { fmt.Printf(" : %s\n", dns) } println() + return nil }) - if err != nil { - mainLog.Load().Error().Msg(err.Error()) - } }, } interfacesCmd := &cobra.Command{ diff --git a/cmd/cli/net_darwin.go b/cmd/cli/net_darwin.go index ece1862..ec7e517 100644 --- a/cmd/cli/net_darwin.go +++ b/cmd/cli/net_darwin.go @@ -9,17 +9,19 @@ import ( "strings" ) -func patchNetIfaceName(iface *net.Interface) error { +func patchNetIfaceName(iface *net.Interface) (bool, error) { b, err := exec.Command("networksetup", "-listnetworkserviceorder").Output() if err != nil { - return err + return false, err } + patched := false if name := networkServiceName(iface.Name, bytes.NewReader(b)); name != "" { iface.Name = name mainLog.Load().Debug().Str("network_service", name).Msg("found network service name for interface") + patched = true } - return nil + return patched, nil } func networkServiceName(ifaceName string, r io.Reader) string { diff --git a/cmd/cli/net_others.go b/cmd/cli/net_others.go index 5a66e82..edd89ec 100644 --- a/cmd/cli/net_others.go +++ b/cmd/cli/net_others.go @@ -4,7 +4,7 @@ package cli import "net" -func patchNetIfaceName(iface *net.Interface) error { return nil } +func patchNetIfaceName(iface *net.Interface) (bool, error) { return true, nil } func validInterface(iface *net.Interface, validIfacesMap map[string]struct{}) bool { return true } diff --git a/cmd/cli/net_windows.go b/cmd/cli/net_windows.go index f46a93f..5b6e6b4 100644 --- a/cmd/cli/net_windows.go +++ b/cmd/cli/net_windows.go @@ -13,8 +13,8 @@ import ( "github.com/microsoft/wmi/pkg/hardware/network/netadapter" ) -func patchNetIfaceName(iface *net.Interface) error { - return nil +func patchNetIfaceName(iface *net.Interface) (bool, error) { + return true, nil } // validInterface reports whether the *net.Interface is a valid one. diff --git a/cmd/cli/prog.go b/cmd/cli/prog.go index b1fb18b..6aa95b1 100644 --- a/cmd/cli/prog.go +++ b/cmd/cli/prog.go @@ -1119,7 +1119,7 @@ func canBeLocalUpstream(addr string) bool { func withEachPhysicalInterfaces(excludeIfaceName, context string, f func(i *net.Interface) error) { validIfacesMap := validInterfacesMap() netmon.ForeachInterface(func(i netmon.Interface, prefixes []netip.Prefix) { - // Skip loopback/virtual interface. + // Skip loopback/virtual/down interface. if i.IsLoopback() || len(i.HardwareAddr) == 0 { return } @@ -1128,9 +1128,12 @@ func withEachPhysicalInterfaces(excludeIfaceName, context string, f func(i *net. return } netIface := i.Interface - if err := patchNetIfaceName(netIface); err != nil { + if patched, err := patchNetIfaceName(netIface); err != nil { mainLog.Load().Debug().Err(err).Msg("failed to patch net interface name") return + } else if !patched { + // The interface is not functional, skipping. + return } // Skip excluded interface. if netIface.Name == excludeIfaceName {