mirror of https://github.com/tailscale/tailscale/
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
115 lines
2.7 KiB
Go
115 lines
2.7 KiB
Go
2 years ago
|
// Copyright (c) Tailscale Inc & AUTHORS
|
||
|
// SPDX-License-Identifier: BSD-3-Clause
|
||
4 years ago
|
|
||
7 months ago
|
package netmon
|
||
4 years ago
|
|
||
|
import (
|
||
|
"errors"
|
||
8 months ago
|
"io"
|
||
2 years ago
|
"net/netip"
|
||
4 years ago
|
"os/exec"
|
||
|
"testing"
|
||
|
|
||
|
"go4.org/mem"
|
||
|
"tailscale.com/util/lineread"
|
||
|
"tailscale.com/version"
|
||
|
)
|
||
|
|
||
|
func TestLikelyHomeRouterIPSyscallExec(t *testing.T) {
|
||
11 months ago
|
syscallIP, _, syscallOK := likelyHomeRouterIPBSDFetchRIB()
|
||
2 years ago
|
netstatIP, netstatIf, netstatOK := likelyHomeRouterIPDarwinExec()
|
||
|
|
||
4 years ago
|
if syscallOK != netstatOK || syscallIP != netstatIP {
|
||
|
t.Errorf("syscall() = %v, %v, netstat = %v, %v",
|
||
|
syscallIP, syscallOK,
|
||
|
netstatIP, netstatOK,
|
||
|
)
|
||
|
}
|
||
2 years ago
|
|
||
|
if !syscallOK {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
def, err := defaultRoute()
|
||
|
if err != nil {
|
||
|
t.Errorf("defaultRoute() error: %v", err)
|
||
|
}
|
||
|
|
||
|
if def.InterfaceName != netstatIf {
|
||
|
t.Errorf("syscall default route interface %s differs from netstat %s", def.InterfaceName, netstatIf)
|
||
|
}
|
||
4 years ago
|
}
|
||
|
|
||
|
/*
|
||
2 years ago
|
Parse out 10.0.0.1 and en0 from:
|
||
4 years ago
|
|
||
|
$ 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 !
|
||
|
...
|
||
|
*/
|
||
2 years ago
|
func likelyHomeRouterIPDarwinExec() (ret netip.Addr, netif string, ok bool) {
|
||
4 years ago
|
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"
|
||
|
// This is why we have likelyHomeRouterIPDarwinSyscall.
|
||
2 years ago
|
return ret, "", false
|
||
4 years ago
|
}
|
||
|
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()
|
||
8 months ago
|
defer io.Copy(io.Discard, stdout) // clear the pipe to prevent hangs
|
||
4 years ago
|
|
||
|
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)
|
||
2 years ago
|
if len(f) < 4 || !f[0].EqualString("default") {
|
||
4 years ago
|
return nil
|
||
|
}
|
||
2 years ago
|
ipm, flagsm, netifm := f[1], f[2], f[3]
|
||
4 years ago
|
if !mem.Contains(flagsm, mem.S("G")) {
|
||
|
return nil
|
||
|
}
|
||
2 years ago
|
if mem.Contains(flagsm, mem.S("I")) {
|
||
|
return nil
|
||
|
}
|
||
2 years ago
|
ip, err := netip.ParseAddr(string(mem.Append(nil, ipm)))
|
||
3 years ago
|
if err == nil && ip.IsPrivate() {
|
||
4 years ago
|
ret = ip
|
||
2 years ago
|
netif = netifm.StringCopy()
|
||
4 years ago
|
// We've found what we're looking for.
|
||
|
return errStopReadingNetstatTable
|
||
|
}
|
||
|
return nil
|
||
|
})
|
||
2 years ago
|
return ret, netif, ret.IsValid()
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
func TestFetchRoutingTable(t *testing.T) {
|
||
|
// Issue 1345: this used to be flaky on darwin.
|
||
7 months ago
|
for range 20 {
|
||
4 years ago
|
_, err := fetchRoutingTable()
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
var errStopReadingNetstatTable = errors.New("found private gateway")
|