diff --git a/component/fakeip/pool.go b/component/fakeip/pool.go index 43b9cee1..68484899 100644 --- a/component/fakeip/pool.go +++ b/component/fakeip/pool.go @@ -147,10 +147,10 @@ type Options struct { // New return Pool instance func New(options Options) (*Pool, error) { - min := ipToUint(options.IPNet.IP) + 2 + min := ipToUint(options.IPNet.IP) + 3 ones, bits := options.IPNet.Mask.Size() - total := 1< %s", old, interfaceName) + } +} diff --git a/listener/tun/ipstack/commons/router_darwin.go b/listener/tun/ipstack/commons/router_darwin.go index 97233144..4185a48a 100644 --- a/listener/tun/ipstack/commons/router_darwin.go +++ b/listener/tun/ipstack/commons/router_darwin.go @@ -9,7 +9,7 @@ import ( ) func GetAutoDetectInterface() (string, error) { - return cmd.ExecCmd("bash -c netstat -rnf inet | grep 'default' | awk -F ' ' 'NR==1{print $6}' | xargs echo -n") + return cmd.ExecCmd("bash -c route -n get default | grep 'interface:' | awk -F ' ' 'NR==1{print $2}' | xargs echo -n") } func ConfigInterfaceAddress(dev device.Device, addr netip.Prefix, forceMTU int, autoRoute bool) error { @@ -20,8 +20,8 @@ func ConfigInterfaceAddress(dev device.Device, addr netip.Prefix, forceMTU int, var ( interfaceName = dev.Name() ip = addr.Masked().Addr().Next() - gw = ip - netmask = IPv4MaskString(addr.Bits()) + gw = ip.Next() + netmask = ipv4MaskString(addr.Bits()) ) cmdStr := fmt.Sprintf("ifconfig %s inet %s netmask %s %s", interfaceName, ip, netmask, gw) @@ -31,10 +31,10 @@ func ConfigInterfaceAddress(dev device.Device, addr netip.Prefix, forceMTU int, return err } - // _, err = cmd.ExecCmd(fmt.Sprintf("ipconfig set %s automatic-v6", interfaceName)) - // if err != nil { - // return err - // } + _, err = cmd.ExecCmd(fmt.Sprintf("ipconfig set %s automatic-v6", interfaceName)) + if err != nil { + return err + } if autoRoute { err = configInterfaceRouting(interfaceName, addr) @@ -43,16 +43,20 @@ func ConfigInterfaceAddress(dev device.Device, addr netip.Prefix, forceMTU int, } func configInterfaceRouting(interfaceName string, addr netip.Prefix) error { - routes := append(Routes, addr.String()) + var ( + routes = append(defaultRoutes, addr.String()) + gateway = addr.Masked().Addr().Next() + ) - for _, route := range routes { - if err := execRouterCmd("add", "-inet", route, interfaceName); err != nil { + for _, destination := range routes { + if _, err := cmd.ExecCmd(fmt.Sprintf("route add -net %s %s", destination, gateway)); err != nil { return err } } - // return execRouterCmd("add", "-inet6", "2000::/3", interfaceName) - return nil + go defaultInterfaceChangeMonitor() + + return execRouterCmd("add", "-inet6", "2000::/3", interfaceName) } func execRouterCmd(action, inet, route string, interfaceName string) error { diff --git a/listener/tun/ipstack/commons/router_linux.go b/listener/tun/ipstack/commons/router_linux.go index e27cdcf6..63479bae 100644 --- a/listener/tun/ipstack/commons/router_linux.go +++ b/listener/tun/ipstack/commons/router_linux.go @@ -36,11 +36,14 @@ func ConfigInterfaceAddress(dev device.Device, addr netip.Prefix, forceMTU int, func configInterfaceRouting(interfaceName string, addr netip.Prefix) error { linkIP := addr.Masked().Addr().Next() - for _, route := range Routes { + for _, route := range defaultRoutes { if err := execRouterCmd("add", route, interfaceName, linkIP.String()); err != nil { return err } } + + go defaultInterfaceChangeMonitor() + return nil } diff --git a/listener/tun/ipstack/commons/router_windows.go b/listener/tun/ipstack/commons/router_windows.go index 05ac7fb1..5ddcf709 100644 --- a/listener/tun/ipstack/commons/router_windows.go +++ b/listener/tun/ipstack/commons/router_windows.go @@ -15,6 +15,8 @@ import ( "golang.zx2c4.com/wireguard/windows/tunnel/winipcfg" ) +var wintunInterfaceName string + func GetAutoDetectInterface() (string, error) { ifname, err := getAutoDetectInterfaceByFamily(winipcfg.AddressFamily(windows.AF_INET)) if err == nil { @@ -30,7 +32,7 @@ func ConfigInterfaceAddress(dev device.Device, addr netip.Prefix, forceMTU int, var err error startOver: if tryTimes > 0 { - log.Infoln("Retrying interface configuration after failure because system just booted (T+%v): %v", windows.DurationSinceBoot(), err) + log.Infoln("[TUN] retrying interface configuration after failure because system just booted (T+%v): %v", windows.DurationSinceBoot(), err) time.Sleep(time.Second) retryOnFailure = retryOnFailure && tryTimes < 15 } @@ -199,6 +201,10 @@ startOver: return fmt.Errorf("unable to set DNS %s %s: %w", "198.18.0.2", "nil", err) } + wintunInterfaceName = dev.Name() + + go defaultInterfaceChangeMonitor() + return nil } @@ -221,7 +227,7 @@ func cleanupAddressesOnDisconnectedInterfaces(family winipcfg.AddressFamily, add for address := iface.FirstUnicastAddress; address != nil; address = address.Next { if ip, _ := netip.AddrFromSlice(address.Address.IP()); addrHash[ip] { prefix := netip.PrefixFrom(ip, int(address.OnLinkPrefixLength)) - log.Infoln("Cleaning up stale address %s from interface ā€˜%sā€™", prefix.String(), iface.FriendlyName()) + log.Infoln("[TUN] cleaning up stale address %s from interface ā€˜%sā€™", prefix.String(), iface.FriendlyName()) _ = iface.LUID.DeleteIPAddress(prefix) } } @@ -248,6 +254,10 @@ func getAutoDetectInterfaceByFamily(family winipcfg.AddressFamily) (string, erro ifname := iface.FriendlyName() + if wintunInterfaceName == ifname { + continue + } + for gatewayAddress := iface.FirstGatewayAddress; gatewayAddress != nil; gatewayAddress = gatewayAddress.Next { nextHop, _ := netip.AddrFromSlice(gatewayAddress.Address.IP()) diff --git a/listener/tun/tun_adapter.go b/listener/tun/tun_adapter.go index a54087be..40cf5909 100644 --- a/listener/tun/tun_adapter.go +++ b/listener/tun/tun_adapter.go @@ -26,11 +26,11 @@ import ( // New TunAdapter func New(tunConf *config.Tun, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) (ipstack.Stack, error) { var ( - tunAddress = netip.MustParsePrefix("198.18.0.1/16") - devName = tunConf.Device - stackType = tunConf.Stack - autoRoute = tunConf.AutoRoute - mtu = 9000 + tunAddress, _ = netip.ParsePrefix("198.18.0.1/16") + devName = tunConf.Device + stackType = tunConf.Stack + autoRoute = tunConf.AutoRoute + mtu = 9000 tunDevice device.Device tunStack ipstack.Stack @@ -49,20 +49,14 @@ func New(tunConf *config.Tun, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound. process.AppendLocalIPs(tunAddress.Masked().Addr().Next().AsSlice()) // open tun device - tunDevice, err = parseDevice(devName, uint32(mtu)) - if err != nil { - for i := 1; i < 3; i++ { - time.Sleep(time.Second * 1) - tunDevice, err = parseDevice(devName, uint32(mtu)) - if err == nil { - break - } - } + for i := 1; i < 3; i++ { + time.Sleep(time.Second * 1) + tunDevice, err = parseDevice(devName, uint32(mtu)) if err != nil { return nil, fmt.Errorf("can't open tun: %w", err) } + break } - // new ip stack switch stackType { case C.TunGvisor: @@ -116,7 +110,7 @@ func New(tunConf *config.Tun, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound. func generateDeviceName() string { switch runtime.GOOS { case "darwin": - return tun.Driver + "://utun" + return tun.Driver + "://Meta" case "windows": return tun.Driver + "://Meta" default: