2020-07-06 18:34:52 +01:00
|
|
|
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package interfaces
|
|
|
|
|
|
|
|
import (
|
2020-11-19 17:05:32 +00:00
|
|
|
"errors"
|
2020-07-06 18:34:52 +01:00
|
|
|
"os/exec"
|
|
|
|
|
|
|
|
"go4.org/mem"
|
|
|
|
"inet.af/netaddr"
|
|
|
|
"tailscale.com/util/lineread"
|
2020-07-31 18:35:15 +01:00
|
|
|
"tailscale.com/version"
|
2020-07-06 18:34:52 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
Parse out 10.0.0.1 from:
|
|
|
|
|
|
|
|
$ netstat -r -n -f inet
|
|
|
|
Routing tables
|
|
|
|
|
|
|
|
Internet:
|
|
|
|
Destination Gateway Flags Netif Expire
|
|
|
|
default 10.0.0.1 UGSc en0
|
|
|
|
default link#14 UCSI utun2
|
|
|
|
10/16 link#4 UCS en0 !
|
|
|
|
10.0.0.1/32 link#4 UCS en0 !
|
|
|
|
...
|
|
|
|
|
|
|
|
*/
|
2020-08-04 22:20:38 +01:00
|
|
|
func likelyHomeRouterIPDarwinExec() (ret netaddr.IP, ok bool) {
|
2020-07-31 18:35:15 +01:00
|
|
|
if version.IsMobile() {
|
|
|
|
// Don't try to do subprocesses on iOS. Ends up with log spam like:
|
|
|
|
// kernel: "Sandbox: IPNExtension(86580) deny(1) process-fork"
|
2020-08-04 22:20:38 +01:00
|
|
|
// This is why we have likelyHomeRouterIPDarwinSyscall.
|
2020-07-31 18:35:15 +01:00
|
|
|
return ret, false
|
|
|
|
}
|
2020-07-06 18:34:52 +01:00
|
|
|
cmd := exec.Command("/usr/sbin/netstat", "-r", "-n", "-f", "inet")
|
|
|
|
stdout, err := cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer cmd.Wait()
|
|
|
|
|
|
|
|
var f []mem.RO
|
|
|
|
lineread.Reader(stdout, func(lineb []byte) error {
|
|
|
|
line := mem.B(lineb)
|
|
|
|
if !mem.Contains(line, mem.S("default")) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
f = mem.AppendFields(f[:0], line)
|
|
|
|
if len(f) < 3 || !f[0].EqualString("default") {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ipm, flagsm := f[1], f[2]
|
|
|
|
if !mem.Contains(flagsm, mem.S("G")) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ip, err := netaddr.ParseIP(string(mem.Append(nil, ipm)))
|
|
|
|
if err == nil && isPrivateIP(ip) {
|
|
|
|
ret = ip
|
2020-11-19 17:05:32 +00:00
|
|
|
// We've found what we're looking for.
|
2020-12-14 19:43:09 +00:00
|
|
|
return errStopReadingNetstatTable
|
2020-07-06 18:34:52 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return ret, !ret.IsZero()
|
|
|
|
}
|
2020-11-19 17:05:32 +00:00
|
|
|
|
2020-12-14 19:43:09 +00:00
|
|
|
var errStopReadingNetstatTable = errors.New("found private gateway")
|