cmd/cli: only list physical interfaces when listing

Since these are the interfaces that ctrld will manipulate anyway.

While at it, also skipping non-working devices on MacOS, by checking
if the device is present in network service order
This commit is contained in:
Cuong Manh Le
2025-01-16 14:00:34 +07:00
committed by Cuong Manh Le
parent 7833132917
commit 841be069b7
6 changed files with 24 additions and 16 deletions

View File

@@ -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

View File

@@ -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{

View File

@@ -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 {

View File

@@ -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 }

View File

@@ -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.

View File

@@ -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 {