forked from hashicorp/go-sockaddr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
route_info_windows.go
65 lines (54 loc) · 1.71 KB
/
route_info_windows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package sockaddr
import (
"os/exec"
"strings"
)
var cmds map[string][]string = map[string][]string{
"defaultInterface": {"powershell", "Get-NetRoute -DestinationPrefix '0.0.0.0/0' | select -ExpandProperty InterfaceAlias"},
// These commands enable GetDefaultInterfaceNameLegacy and should be removed
// when it is.
"netstat": {"netstat", "-rn"},
"ipconfig": {"ipconfig"},
}
// NewRouteInfo returns a BSD-specific implementation of the RouteInfo
// interface.
func NewRouteInfo() (routeInfo, error) {
return routeInfo{
cmds: cmds,
}, nil
}
// GetDefaultInterfaceName returns the interface name attached to the default
// route on the default interface.
func (ri routeInfo) GetDefaultInterfaceName() (string, error) {
if !hasPowershell() {
// No powershell, fallback to legacy method
return ri.GetDefaultInterfaceNameLegacy()
}
ifNameOut, err := exec.Command(cmds["defaultInterface"][0], cmds["defaultInterface"][1:]...).Output()
if err != nil {
return "", err
}
ifName := strings.TrimSpace(string(ifNameOut[:]))
return ifName, nil
}
// GetDefaultInterfaceNameLegacy provides legacy behavior for GetDefaultInterfaceName
// on Windows machines without powershell.
func (ri routeInfo) GetDefaultInterfaceNameLegacy() (string, error) {
ifNameOut, err := exec.Command(cmds["netstat"][0], cmds["netstat"][1:]...).Output()
if err != nil {
return "", err
}
ipconfigOut, err := exec.Command(cmds["ipconfig"][0], cmds["ipconfig"][1:]...).Output()
if err != nil {
return "", err
}
ifName, err := parseDefaultIfNameWindows(string(ifNameOut), string(ipconfigOut))
if err != nil {
return "", err
}
return ifName, nil
}
func hasPowershell() bool {
_, err := exec.LookPath("powershell")
return (err != nil)
}