diff --git a/pkg/util/netutil/net.go b/pkg/util/netutil/net.go index 5d046f5d9..f01856f33 100644 --- a/pkg/util/netutil/net.go +++ b/pkg/util/netutil/net.go @@ -90,3 +90,16 @@ func IsPublicIP(IP net.IP) bool { } return false } + +// DefaultNetworkInterfaceIPs returns IP addresses for the default network interface +func DefaultNetworkInterfaceIPs() ([]net.IP, error) { + networkIfc, err := DefaultNetworkInterface() + if err != nil { + return nil, fmt.Errorf("failed to get default network interface: %w", err) + } + localIPs, err := NetworkInterfaceIPs(networkIfc) + if err != nil { + return nil, fmt.Errorf("failed to get IPs of %s: %w", networkIfc, err) + } + return localIPs, nil +} diff --git a/pkg/util/netutil/net_darwin.go b/pkg/util/netutil/net_darwin.go index 4c5a0349f..3f87240bc 100644 --- a/pkg/util/netutil/net_darwin.go +++ b/pkg/util/netutil/net_darwin.go @@ -9,7 +9,7 @@ import ( ) const ( - defaultNetworkInterfaceCMD = "netstat -rn | sed -n '/Internet/,/Internet6/p' | grep default | awk '{print $4}'" + defaultNetworkInterfaceCMD = "route -n get default | awk 'FNR == 5 {print $2}'" ) // DefaultNetworkInterface fetches default network interface name. diff --git a/pkg/util/netutil/net_linux.go b/pkg/util/netutil/net_linux.go index 5b770abdf..55324a711 100644 --- a/pkg/util/netutil/net_linux.go +++ b/pkg/util/netutil/net_linux.go @@ -5,7 +5,6 @@ package netutil import ( "bytes" "fmt" - "net" "os/exec" ) @@ -25,16 +24,3 @@ func DefaultNetworkInterface() (string, error) { return string(outputBytes), nil } - -// DefaultNetworkInterfaceIPs returns IP addresses for the default network interface -func DefaultNetworkInterfaceIPs() ([]net.IP, error) { - networkIfc, err := DefaultNetworkInterface() - if err != nil { - return nil, fmt.Errorf("failed to get default network interface: %w", err) - } - localIPs, err := NetworkInterfaceIPs(networkIfc) - if err != nil { - return nil, fmt.Errorf("failed to get IPs of %s: %w", networkIfc, err) - } - return localIPs, nil -} diff --git a/pkg/util/netutil/net_test.go b/pkg/util/netutil/net_test.go new file mode 100644 index 000000000..3384d7aee --- /dev/null +++ b/pkg/util/netutil/net_test.go @@ -0,0 +1,17 @@ +package netutil_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + + "github.com/skycoin/skywire/pkg/util/netutil" +) + +func TestDefaultNetworkInterfaceIPs(t *testing.T) { + req := require.New(t) + + ifaceIPs, err := netutil.DefaultNetworkInterfaceIPs() + req.NoError(err) + t.Logf("interface IP: %v", ifaceIPs) +} diff --git a/pkg/util/netutil/net_windows.go b/pkg/util/netutil/net_windows.go index c5f6e9410..308121d27 100644 --- a/pkg/util/netutil/net_windows.go +++ b/pkg/util/netutil/net_windows.go @@ -2,7 +2,55 @@ package netutil +import ( + "errors" + "fmt" + "net" + "os/exec" + "regexp" + "strings" +) + +const ( + defaultNetworkInterfaceCMD = `netsh int ip show config | findstr /r "IP Address.*([0-9]{1,3}\.|){4}"` +) + // DefaultNetworkInterface fetches default network interface name. func DefaultNetworkInterface() (string, error) { - return "", errServerMethodsNotSupported + cmd := exec.Command("powershell", defaultNetworkInterfaceCMD) + output, err := cmd.Output() + if err != nil { + return "", err + } + // parse output + splitLines := strings.Split(string(output), "\n") + var ips []string + + if len(splitLines) > 0 { + re := regexp.MustCompile("\\s+") + for i, line := range splitLines { + ipAddr := re.Split(strings.TrimSpace(line), -1) + + if len(ipAddr) > 2 { + ip := net.ParseIP(ipAddr[2]) + if ip != nil && !ip.IsLoopback() { + ips = append(ips, ipAddr[2]) + } + } + } + } + + if len(ips) == 0 { + return "", errors.New("no active ip found") + } + + // get default network interface based on its ip + findInterfaceCmd := fmt.Sprintf("Get-NetIpAddress -IPAddress '%s' | %%{$_.InterfaceAlias}", ips[0]) + cmd = exec.Command("powershell", findInterfaceCmd) + output, err = cmd.Output() + if err != nil { + return "", fmt.Errorf("unable to get default interface: %v", err) + } + + return strings.TrimSpace(string(output)), nil }