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-09-22 22:20:01 +01:00
|
|
|
"fmt"
|
2020-10-01 23:33:37 +01:00
|
|
|
"log"
|
2020-07-06 18:34:52 +01:00
|
|
|
"os/exec"
|
2020-07-15 20:43:48 +01:00
|
|
|
"syscall"
|
2020-10-01 23:33:37 +01:00
|
|
|
"unsafe"
|
2020-07-06 18:34:52 +01:00
|
|
|
|
|
|
|
"go4.org/mem"
|
2020-10-01 23:33:37 +01:00
|
|
|
"golang.org/x/sys/windows"
|
2020-09-26 03:11:05 +01:00
|
|
|
"golang.zx2c4.com/wireguard/windows/tunnel/winipcfg"
|
2020-07-06 18:34:52 +01:00
|
|
|
"inet.af/netaddr"
|
2020-09-10 19:40:02 +01:00
|
|
|
"tailscale.com/tsconst"
|
2020-07-06 18:34:52 +01:00
|
|
|
"tailscale.com/util/lineread"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
likelyHomeRouterIP = likelyHomeRouterIPWindows
|
2020-10-01 23:33:37 +01:00
|
|
|
getPAC = getPACWindows
|
2020-07-06 18:34:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
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) {
|
|
|
|
cmd := exec.Command("route", "print", "-4")
|
2020-07-15 20:43:48 +01:00
|
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
|
2020-07-06 18:34:52 +01:00
|
|
|
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("0.0.0.0")) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
f = mem.AppendFields(f[:0], line)
|
|
|
|
if len(f) < 3 || !f[0].EqualString("0.0.0.0") || !f[1].EqualString("0.0.0.0") {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ipm := f[2]
|
|
|
|
ip, err := netaddr.ParseIP(string(mem.Append(nil, ipm)))
|
|
|
|
if err == nil && isPrivateIP(ip) {
|
|
|
|
ret = ip
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return ret, !ret.IsZero()
|
|
|
|
}
|
2020-09-10 19:40:02 +01:00
|
|
|
|
|
|
|
// NonTailscaleMTUs returns a map of interface LUID to interface MTU,
|
|
|
|
// for all interfaces except Tailscale tunnels.
|
2020-09-26 03:11:05 +01:00
|
|
|
func NonTailscaleMTUs() (map[winipcfg.LUID]uint32, error) {
|
|
|
|
mtus := map[winipcfg.LUID]uint32{}
|
2020-09-22 22:20:01 +01:00
|
|
|
ifs, err := NonTailscaleInterfaces()
|
|
|
|
for luid, iface := range ifs {
|
2020-09-26 03:11:05 +01:00
|
|
|
mtus[luid] = iface.MTU
|
2020-09-22 22:20:01 +01:00
|
|
|
}
|
|
|
|
return mtus, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// NonTailscaleInterfaces returns a map of interface LUID to interface
|
|
|
|
// for all interfaces except Tailscale tunnels.
|
2020-09-26 03:11:05 +01:00
|
|
|
func NonTailscaleInterfaces() (map[winipcfg.LUID]*winipcfg.IPAdapterAddresses, error) {
|
|
|
|
ifs, err := winipcfg.GetAdaptersAddresses(windows.AF_UNSPEC, winipcfg.GAAFlagIncludeAllInterfaces)
|
2020-09-10 19:40:02 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-09-26 03:11:05 +01:00
|
|
|
ret := map[winipcfg.LUID]*winipcfg.IPAdapterAddresses{}
|
2020-09-10 19:40:02 +01:00
|
|
|
for _, iface := range ifs {
|
2020-09-26 03:11:05 +01:00
|
|
|
if iface.Description() == tsconst.WintunInterfaceDesc {
|
2020-09-10 19:40:02 +01:00
|
|
|
continue
|
|
|
|
}
|
2020-09-26 03:11:05 +01:00
|
|
|
ret[iface.LUID] = iface
|
2020-09-10 19:40:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
2020-09-22 22:20:01 +01:00
|
|
|
|
|
|
|
// GetWindowsDefault returns the interface that has the non-Tailscale
|
|
|
|
// default route for the given address family.
|
|
|
|
//
|
|
|
|
// It returns (nil, nil) if no interface is found.
|
2020-09-26 03:11:05 +01:00
|
|
|
func GetWindowsDefault(family winipcfg.AddressFamily) (*winipcfg.IPAdapterAddresses, error) {
|
2020-09-22 22:20:01 +01:00
|
|
|
ifs, err := NonTailscaleInterfaces()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-09-26 03:11:05 +01:00
|
|
|
routes, err := winipcfg.GetIPForwardTable2(family)
|
2020-09-22 22:20:01 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
bestMetric := ^uint32(0)
|
2020-09-26 03:11:05 +01:00
|
|
|
var bestIface *winipcfg.IPAdapterAddresses
|
2020-09-22 22:20:01 +01:00
|
|
|
for _, route := range routes {
|
2020-09-26 03:11:05 +01:00
|
|
|
iface := ifs[route.InterfaceLUID]
|
2020-09-22 22:20:01 +01:00
|
|
|
if route.DestinationPrefix.PrefixLength != 0 || iface == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if iface.OperStatus == winipcfg.IfOperStatusUp && route.Metric < bestMetric {
|
|
|
|
bestMetric = route.Metric
|
|
|
|
bestIface = iface
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bestIface, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func DefaultRouteInterface() (string, error) {
|
2020-09-26 03:11:05 +01:00
|
|
|
iface, err := GetWindowsDefault(windows.AF_INET)
|
2020-09-22 22:20:01 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if iface == nil {
|
|
|
|
return "(none)", nil
|
|
|
|
}
|
2020-09-26 03:11:05 +01:00
|
|
|
return fmt.Sprintf("%s (%s)", iface.FriendlyName(), iface.Description()), nil
|
2020-09-22 22:20:01 +01:00
|
|
|
}
|
2020-10-01 23:33:37 +01:00
|
|
|
|
|
|
|
var (
|
|
|
|
winHTTP = windows.NewLazySystemDLL("winhttp.dll")
|
|
|
|
detectAutoProxyConfigURL = winHTTP.NewProc("WinHttpDetectAutoProxyConfigUrl")
|
|
|
|
|
|
|
|
kernel32 = windows.NewLazySystemDLL("kernel32.dll")
|
|
|
|
globalFree = kernel32.NewProc("GlobalFree")
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
winHTTP_AUTO_DETECT_TYPE_DHCP = 0x00000001
|
|
|
|
winHTTP_AUTO_DETECT_TYPE_DNS_A = 0x00000002
|
|
|
|
)
|
|
|
|
|
|
|
|
func getPACWindows() string {
|
|
|
|
var res *uint16
|
2020-10-02 06:02:39 +01:00
|
|
|
r, _, e := detectAutoProxyConfigURL.Call(
|
2020-10-01 23:33:37 +01:00
|
|
|
winHTTP_AUTO_DETECT_TYPE_DHCP|winHTTP_AUTO_DETECT_TYPE_DNS_A,
|
|
|
|
uintptr(unsafe.Pointer(&res)),
|
|
|
|
)
|
2020-10-02 06:02:39 +01:00
|
|
|
if r == 1 {
|
|
|
|
if res == nil {
|
|
|
|
log.Printf("getPACWindows: unexpected success with nil result")
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
defer globalFree.Call(uintptr(unsafe.Pointer(res)))
|
|
|
|
return windows.UTF16PtrToString(res)
|
|
|
|
}
|
|
|
|
const (
|
|
|
|
ERROR_WINHTTP_AUTODETECTION_FAILED = 12180
|
|
|
|
)
|
|
|
|
if e == syscall.Errno(ERROR_WINHTTP_AUTODETECTION_FAILED) {
|
|
|
|
// Common case on networks without advertised PAC.
|
|
|
|
return ""
|
2020-10-01 23:33:37 +01:00
|
|
|
}
|
2020-10-02 06:02:39 +01:00
|
|
|
log.Printf("getPACWindows: %T=%v", e, e) // syscall.Errno=0x....
|
|
|
|
return ""
|
2020-10-01 23:33:37 +01:00
|
|
|
}
|