-
Notifications
You must be signed in to change notification settings - Fork 0
/
gateway.go
67 lines (56 loc) · 1.4 KB
/
gateway.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
66
67
package gateway
import (
"errors"
"net"
"os/exec"
"runtime"
)
func DefaultWithGOOS() (net.IP, error) {
return Default(runtime.GOOS)
}
// I'm not sure if it actually needs to be exported.
func Default(os string) (net.IP, error) {
switch os {
case "darwin":
return darwinGatewayDiscover()
case "freebsd":
return freebsdGatewayDiscover()
case "linux":
return linuxGatewayDiscover()
case "windows":
return windowsGatewayDiscover()
}
return nil, errors.New("not supported OS: " + os)
}
func darwinGatewayDiscover() (net.IP, error) {
route := exec.Command("sh", "-c", "/sbin/route -n get 0.0.0.0 | grep gateway | awk '{print $2}'")
b, err := route.CombinedOutput()
if err != nil {
return nil, err
}
return darwinParser(b)
}
func freebsdGatewayDiscover() (net.IP, error) {
route := exec.Command("sh", "-c", "netstat -rn | grep default | awk '{print $2}'")
b, err := route.CombinedOutput()
if err != nil {
return nil, err
}
return freebsdParser(b)
}
func linuxGatewayDiscover() (net.IP, error) {
route := exec.Command("sh", "-c", "cat /proc/net/route | awk '{print $3}'")
b, err := route.CombinedOutput()
if err != nil {
return nil, err
}
return linuxParser(b)
}
func windowsGatewayDiscover() (net.IP, error) {
route := exec.Command("sh", "-c", "route print 0.0.0.0 | findstr 0.0.0.0")
b, err := route.CombinedOutput()
if err != nil {
return nil, err
}
return windowsParser(b)
}