This commit is contained in:
crusader 2017-12-04 18:00:07 +09:00
commit 68a123318d
3 changed files with 39 additions and 8 deletions

View File

@ -84,17 +84,48 @@ func parseDarwinRouteGet(output []byte) (net.IP, string, error) {
// gateway: 192.168.1.1 // gateway: 192.168.1.1
// interface: tun0 // interface: tun0
// flags: <UP,GATEWAY,DONE,STATIC,PRCLONING> // flags: <UP,GATEWAY,DONE,STATIC,PRCLONING>
lines := strings.Split(string(output), "\n") // lines := strings.Split(string(output), "\n")
for _, line := range lines { // for _, line := range lines {
// fields := strings.Fields(line)
// if len(fields) >= 2 && fields[0] == "gateway:" {
// ip := net.ParseIP(fields[1])
// if ip != nil {
// return ip, "", nil
// }
// }
// }
// Darwin route out format is always like this:
// Internet:
// Destination Gateway Flags Refs Use Netif Expire
// default 192.168.10.254 UGSc 194 0 en3
// 127 127.0.0.1 UCS 0 429 lo0
// 127.0.0.1 127.0.0.1 UH 1 587632 lo0
// 169.254 link#7 UCS 0 0 en3
// 192.168.10 link#7 UCS 4 0 en3
// 192.168.10.1 0:11:32:7f:20:61 UHLWIi 1 202 en3 1065
// 224.0.0/4 link#7 UmCS 3 0 en3
// 224.0.0.251 1:0:5e:0:0:fb UHmLWI 0 2325 en3
// 239.192.152.143 1:0:5e:40:98:8f UHmLWI 0 22892 en3
// 239.255.255.250 1:0:5e:7f:ff:fa UHmLWI 0 15988 en3
// 255.255.255.255/32 link#7 UCS 0 0 en3
// Internet6:
// Destination Gateway Flags Netif Expire
// default fe80::%utun0 UGcI utun0
// default fe80::%utun1 UGcI utun1
// default fe80::%utun2 UGcI utun2
// default fe80::%utun3 UGcI utun3
outputLines := strings.Split(string(output), "\n")
for _, line := range outputLines {
fields := strings.Fields(line) fields := strings.Fields(line)
if len(fields) >= 2 && fields[0] == "gateway:" { if len(fields) >= 2 && fields[0] == "default" {
ip := net.ParseIP(fields[1]) ip := net.ParseIP(fields[1])
if ip != nil { if ip != nil {
return ip, "", nil return ip, fields[5], nil
} }
} }
} }
return nil, "", errNoGateway return nil, "", errNoGateway
} }

View File

@ -5,11 +5,11 @@ import (
"os/exec" "os/exec"
) )
func DiscoverGateway() (net.IP, string, error) { func DiscoverGateway() (ip net.IP, iface string, err error) {
routeCmd := exec.Command("netstat", "-rn") routeCmd := exec.Command("netstat", "-rn")
output, err := routeCmd.CombinedOutput() output, err := routeCmd.CombinedOutput()
if err != nil { if err != nil {
return nil, err return nil, "", err
} }
return parseDarwinRouteGet(output) return parseDarwinRouteGet(output)

View File

@ -9,7 +9,7 @@ func DiscoverGateway() (ip net.IP, iface string, err error) {
routeCmd := exec.Command("netstat", "-rn") routeCmd := exec.Command("netstat", "-rn")
output, err := routeCmd.CombinedOutput() output, err := routeCmd.CombinedOutput()
if err != nil { if err != nil {
return nil, err return nil, "", err
} }
return parseBSDSolarisNetstat(output) return parseBSDSolarisNetstat(output)