mirror of
https://github.com/tailscale/tailscale.git
synced 2025-06-07 16:28:37 +00:00
net/interfaces: use windows API to get the default route instead of parsing route print
output
Fixes: #1470 Signed-off-by: Aleksandar Pesic <peske.nis@gmail.com>
This commit is contained in:
parent
439d70dce2
commit
7b57310966
@ -50,7 +50,7 @@ tailscale.com/cmd/tailscale dependencies: (generated by github.com/tailscale/dep
|
|||||||
tailscale.com/types/wgkey from tailscale.com/types/netmap+
|
tailscale.com/types/wgkey from tailscale.com/types/netmap+
|
||||||
tailscale.com/util/dnsname from tailscale.com/cmd/tailscale/cli+
|
tailscale.com/util/dnsname from tailscale.com/cmd/tailscale/cli+
|
||||||
W tailscale.com/util/endian from tailscale.com/net/netns
|
W tailscale.com/util/endian from tailscale.com/net/netns
|
||||||
LW tailscale.com/util/lineread from tailscale.com/net/interfaces
|
L tailscale.com/util/lineread from tailscale.com/net/interfaces
|
||||||
tailscale.com/version from tailscale.com/cmd/tailscale/cli+
|
tailscale.com/version from tailscale.com/cmd/tailscale/cli+
|
||||||
tailscale.com/version/distro from tailscale.com/cmd/tailscale/cli
|
tailscale.com/version/distro from tailscale.com/cmd/tailscale/cli
|
||||||
tailscale.com/wgengine/filter from tailscale.com/types/netmap
|
tailscale.com/wgengine/filter from tailscale.com/types/netmap
|
||||||
|
@ -126,7 +126,7 @@ tailscale.com/cmd/tailscaled dependencies: (generated by github.com/tailscale/de
|
|||||||
tailscale.com/types/wgkey from tailscale.com/control/controlclient+
|
tailscale.com/types/wgkey from tailscale.com/control/controlclient+
|
||||||
tailscale.com/util/dnsname from tailscale.com/wgengine/tsdns+
|
tailscale.com/util/dnsname from tailscale.com/wgengine/tsdns+
|
||||||
LW tailscale.com/util/endian from tailscale.com/net/netns+
|
LW tailscale.com/util/endian from tailscale.com/net/netns+
|
||||||
LW tailscale.com/util/lineread from tailscale.com/control/controlclient+
|
L tailscale.com/util/lineread from tailscale.com/control/controlclient+
|
||||||
tailscale.com/util/pidowner from tailscale.com/ipn/ipnserver
|
tailscale.com/util/pidowner from tailscale.com/ipn/ipnserver
|
||||||
tailscale.com/util/racebuild from tailscale.com/logpolicy
|
tailscale.com/util/racebuild from tailscale.com/logpolicy
|
||||||
tailscale.com/util/systemd from tailscale.com/control/controlclient+
|
tailscale.com/util/systemd from tailscale.com/control/controlclient+
|
||||||
|
@ -7,17 +7,19 @@ package interfaces
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"net"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os/exec"
|
|
||||||
"syscall"
|
"syscall"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
"go4.org/mem"
|
|
||||||
"golang.org/x/sys/windows"
|
"golang.org/x/sys/windows"
|
||||||
"golang.zx2c4.com/wireguard/windows/tunnel/winipcfg"
|
"golang.zx2c4.com/wireguard/windows/tunnel/winipcfg"
|
||||||
"inet.af/netaddr"
|
"inet.af/netaddr"
|
||||||
"tailscale.com/tsconst"
|
"tailscale.com/tsconst"
|
||||||
"tailscale.com/util/lineread"
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
fallbackInterfaceMetric = uint32(0) // Used if we cannot get the actual interface metric
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -25,58 +27,76 @@ func init() {
|
|||||||
getPAC = getPACWindows
|
getPAC = getPACWindows
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
Parse out 10.0.0.1 from:
|
|
||||||
|
|
||||||
Z:\>route print -4
|
|
||||||
===========================================================================
|
|
||||||
Interface List
|
|
||||||
15...aa 15 48 ff 1c 72 ......Red Hat VirtIO Ethernet Adapter
|
|
||||||
5...........................Tailscale Tunnel
|
|
||||||
1...........................Software Loopback Interface 1
|
|
||||||
===========================================================================
|
|
||||||
|
|
||||||
IPv4 Route Table
|
|
||||||
===========================================================================
|
|
||||||
Active Routes:
|
|
||||||
Network Destination Netmask Gateway Interface Metric
|
|
||||||
0.0.0.0 0.0.0.0 10.0.0.1 10.0.28.63 5
|
|
||||||
10.0.0.0 255.255.0.0 On-link 10.0.28.63 261
|
|
||||||
10.0.28.63 255.255.255.255 On-link 10.0.28.63 261
|
|
||||||
10.0.42.0 255.255.255.0 100.103.42.106 100.103.42.106 5
|
|
||||||
10.0.255.255 255.255.255.255 On-link 10.0.28.63 261
|
|
||||||
34.193.248.174 255.255.255.255 100.103.42.106 100.103.42.106 5
|
|
||||||
|
|
||||||
*/
|
|
||||||
func likelyHomeRouterIPWindows() (ret netaddr.IP, ok bool) {
|
func likelyHomeRouterIPWindows() (ret netaddr.IP, ok bool) {
|
||||||
cmd := exec.Command("route", "print", "-4")
|
rs, err := winipcfg.GetIPForwardTable2(windows.AF_INET)
|
||||||
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
|
|
||||||
stdout, err := cmd.StdoutPipe()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Printf("routerIP/GetIPForwardTable2 error: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err := cmd.Start(); err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer cmd.Wait()
|
|
||||||
|
|
||||||
var f []mem.RO
|
var ifaceMetricCache map[winipcfg.LUID]uint32
|
||||||
lineread.Reader(stdout, func(lineb []byte) error {
|
|
||||||
line := mem.B(lineb)
|
getIfaceMetric := func(luid winipcfg.LUID) (metric uint32) {
|
||||||
if !mem.Contains(line, mem.S("0.0.0.0")) {
|
if ifaceMetricCache == nil {
|
||||||
return nil
|
ifaceMetricCache = make(map[winipcfg.LUID]uint32)
|
||||||
|
} else if m, ok := ifaceMetricCache[luid]; ok {
|
||||||
|
return m
|
||||||
}
|
}
|
||||||
f = mem.AppendFields(f[:0], line)
|
|
||||||
if len(f) < 3 || !f[0].EqualString("0.0.0.0") || !f[1].EqualString("0.0.0.0") {
|
if iface, err := luid.IPInterface(windows.AF_INET); err == nil {
|
||||||
return nil
|
metric = iface.Metric
|
||||||
|
} else {
|
||||||
|
log.Printf("routerIP/luid.IPInterface error: %v", err)
|
||||||
|
metric = fallbackInterfaceMetric
|
||||||
}
|
}
|
||||||
ipm := f[2]
|
|
||||||
ip, err := netaddr.ParseIP(string(mem.Append(nil, ipm)))
|
ifaceMetricCache[luid] = metric
|
||||||
if err == nil && isPrivateIP(ip) {
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
unspec := net.IPv4(0, 0, 0, 0)
|
||||||
|
var best *winipcfg.MibIPforwardRow2 // best (lowest metric) found so far, or nil
|
||||||
|
|
||||||
|
for i := range rs {
|
||||||
|
r := &rs[i]
|
||||||
|
if r.Loopback || r.DestinationPrefix.PrefixLength != 0 || !r.DestinationPrefix.Prefix.IP().Equal(unspec) {
|
||||||
|
// Not a default route, so skip
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
ip, ok := netaddr.FromStdIP(r.NextHop.IP())
|
||||||
|
if !ok {
|
||||||
|
// Not a valid gateway, so skip (won't happen though)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if best == nil {
|
||||||
|
best = r
|
||||||
|
ret = ip
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// We can get here only if there are multiple default gateways defined (rare case),
|
||||||
|
// in which case we need to calculate the effective metric.
|
||||||
|
// Effective metric is sum of interface metric and route metric offset
|
||||||
|
if ifaceMetricCache == nil {
|
||||||
|
// If we're here it means that previous route still isn't updated, so update it
|
||||||
|
best.Metric += getIfaceMetric(best.InterfaceLUID)
|
||||||
|
}
|
||||||
|
r.Metric += getIfaceMetric(r.InterfaceLUID)
|
||||||
|
|
||||||
|
if best.Metric > r.Metric || best.Metric == r.Metric && ret.Compare(ip) > 0 {
|
||||||
|
// Pick the route with lower metric, or lower IP if metrics are equal
|
||||||
|
best = r
|
||||||
ret = ip
|
ret = ip
|
||||||
}
|
}
|
||||||
return nil
|
}
|
||||||
})
|
|
||||||
|
if !ret.IsZero() && !isPrivateIP(ret) {
|
||||||
|
// Default route has a non-private gateway
|
||||||
|
return netaddr.IP{}, false
|
||||||
|
}
|
||||||
|
|
||||||
return ret, !ret.IsZero()
|
return ret, !ret.IsZero()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user