From cfe1209d61526f9132d476afbff654c04bf07710 Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Fri, 20 Sep 2024 21:57:12 +0700 Subject: [PATCH] cmd/cli: use powershell to get physical interfaces --- cmd/cli/net.go | 34 ---------------------------------- cmd/cli/net_darwin.go | 1 + cmd/cli/net_windows.go | 27 +++++++++++++++++++-------- 3 files changed, 20 insertions(+), 42 deletions(-) delete mode 100644 cmd/cli/net.go diff --git a/cmd/cli/net.go b/cmd/cli/net.go deleted file mode 100644 index 80da827..0000000 --- a/cmd/cli/net.go +++ /dev/null @@ -1,34 +0,0 @@ -package cli - -import "strings" - -// Copied from https://gist.github.com/Ultraporing/fe52981f678be6831f747c206a4861cb - -// Mac Address parts to look for, and identify non-physical devices. There may be more, update me! -var macAddrPartsToFilter = []string{ - "00:03:FF", // Microsoft Hyper-V, Virtual Server, Virtual PC - "0A:00:27", // VirtualBox - "00:00:00:00:00", // Teredo Tunneling Pseudo-Interface - "00:50:56", // VMware ESX 3, Server, Workstation, Player - "00:1C:14", // VMware ESX 3, Server, Workstation, Player - "00:0C:29", // VMware ESX 3, Server, Workstation, Player - "00:05:69", // VMware ESX 3, Server, Workstation, Player - "00:1C:42", // Microsoft Hyper-V, Virtual Server, Virtual PC - "00:0F:4B", // Virtual Iron 4 - "00:16:3E", // Red Hat Xen, Oracle VM, XenSource, Novell Xen - "08:00:27", // Sun xVM VirtualBox - "7A:79", // Hamachi -} - -// Filters the possible physical interface address by comparing it to known popular VM Software addresses -// and Teredo Tunneling Pseudo-Interface. -// -//lint:ignore U1000 use in net_windows.go -func isPhysicalInterface(addr string) bool { - for _, macPart := range macAddrPartsToFilter { - if strings.HasPrefix(strings.ToLower(addr), strings.ToLower(macPart)) { - return false - } - } - return true -} diff --git a/cmd/cli/net_darwin.go b/cmd/cli/net_darwin.go index b58a0bf..ece1862 100644 --- a/cmd/cli/net_darwin.go +++ b/cmd/cli/net_darwin.go @@ -49,6 +49,7 @@ func validInterface(iface *net.Interface, validIfacesMap map[string]struct{}) bo return ok } +// validInterfacesMap returns a set of all valid hardware ports. func validInterfacesMap() map[string]struct{} { b, err := exec.Command("networksetup", "-listallhardwareports").Output() if err != nil { diff --git a/cmd/cli/net_windows.go b/cmd/cli/net_windows.go index 8ec5a5f..dc13b08 100644 --- a/cmd/cli/net_windows.go +++ b/cmd/cli/net_windows.go @@ -1,7 +1,10 @@ package cli import ( + "bufio" + "bytes" "net" + "strings" ) func patchNetIfaceName(iface *net.Interface) error { @@ -11,13 +14,21 @@ func patchNetIfaceName(iface *net.Interface) error { // validInterface reports whether the *net.Interface is a valid one. // On Windows, only physical interfaces are considered valid. func validInterface(iface *net.Interface, validIfacesMap map[string]struct{}) bool { - if iface == nil { - return false - } - if isPhysicalInterface(iface.HardwareAddr.String()) { - return true - } - return false + _, ok := validIfacesMap[iface.Name] + return ok } -func validInterfacesMap() map[string]struct{} { return nil } +// validInterfacesMap returns a set of all physical interfaces. +func validInterfacesMap() map[string]struct{} { + out, err := powershell("Get-NetAdapter -Physical | Select-Object -ExpandProperty Name") + if err != nil { + return nil + } + m := make(map[string]struct{}) + scanner := bufio.NewScanner(bytes.NewReader(out)) + for scanner.Scan() { + ifaceName := strings.TrimSpace(scanner.Text()) + m[ifaceName] = struct{}{} + } + return m +}