Skip to content

Commit

Permalink
Merge pull request #772 from alexadhy/netutil-fixes
Browse files Browse the repository at this point in the history
Netutil fixes
  • Loading branch information
jdknives authored May 27, 2021
2 parents 944474c + 9beb5a6 commit 4c2c618
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 16 deletions.
13 changes: 13 additions & 0 deletions pkg/util/netutil/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion pkg/util/netutil/net_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 0 additions & 14 deletions pkg/util/netutil/net_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package netutil
import (
"bytes"
"fmt"
"net"
"os/exec"
)

Expand All @@ -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
}
17 changes: 17 additions & 0 deletions pkg/util/netutil/net_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
50 changes: 49 additions & 1 deletion pkg/util/netutil/net_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 4c2c618

Please sign in to comment.