diff --git a/net/gateway/gateway_common.go b/net/gateway/gateway_common.go index de301ed..532ee32 100644 --- a/net/gateway/gateway_common.go +++ b/net/gateway/gateway_common.go @@ -84,17 +84,48 @@ func parseDarwinRouteGet(output []byte) (net.IP, string, error) { // gateway: 192.168.1.1 // interface: tun0 // flags: - lines := strings.Split(string(output), "\n") - for _, line := range lines { + // lines := strings.Split(string(output), "\n") + // 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) - if len(fields) >= 2 && fields[0] == "gateway:" { + if len(fields) >= 2 && fields[0] == "default" { ip := net.ParseIP(fields[1]) if ip != nil { - return ip, "", nil + return ip, fields[5], nil } } } - return nil, "", errNoGateway }