From 488c3b75f6fd9666bc55fa8ace7ec4be104d3660 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Wed, 12 Oct 2022 16:22:54 +0800 Subject: [PATCH] Remove strict route on windows --- tun_windows.go | 63 +++++++++++++++----------------------------------- 1 file changed, 19 insertions(+), 44 deletions(-) diff --git a/tun_windows.go b/tun_windows.go index 12cfb40..7d2c23e 100644 --- a/tun_windows.go +++ b/tun_windows.go @@ -83,58 +83,33 @@ func (t *NativeTun) configure() error { } } if t.options.AutoRoute { - if t.options.StrictRoute { - if len(t.options.Inet4Address) > 0 { - for _, prefix := range []netip.Prefix{ - netip.MustParsePrefix("0.0.0.0/1"), - netip.MustParsePrefix("128.0.0.0/1"), - } { - err := luid.AddRoute(prefix, netip.IPv4Unspecified(), 0) + if len(t.options.Inet4Address) > 0 { + if len(t.options.Inet4RouteAddress) > 0 { + for _, addr := range t.options.Inet4RouteAddress { + err := luid.AddRoute(addr, netip.IPv4Unspecified(), 0) if err != nil { - return E.Cause(err, "set ipv4 route") + return E.Cause(err, "add ipv4 route: ", addr) } } - } - if len(t.options.Inet6Address) > 0 { - for _, prefix := range []netip.Prefix{ - netip.MustParsePrefix("::/1"), - netip.MustParsePrefix("8000::/1"), - } { - err := luid.AddRoute(prefix, netip.IPv6Unspecified(), 0) - if err != nil { - return E.Cause(err, "set ipv6 route") - } + } else { + err := luid.AddRoute(netip.PrefixFrom(netip.IPv4Unspecified(), 0), netip.IPv4Unspecified(), 0) + if err != nil { + return E.Cause(err, "set ipv4 route") } } - } else { - if len(t.options.Inet4Address) > 0 { - if len(t.options.Inet4RouteAddress) > 0 { - for _, addr := range t.options.Inet4RouteAddress { - err := luid.AddRoute(addr, netip.IPv4Unspecified(), 0) - if err != nil { - return E.Cause(err, "add ipv4 route: ", addr) - } - } - } else { - err := luid.AddRoute(netip.PrefixFrom(netip.IPv4Unspecified(), 0), netip.IPv4Unspecified(), 0) + } + if len(t.options.Inet6Address) > 0 { + if len(t.options.Inet6RouteAddress) > 0 { + for _, addr := range t.options.Inet6RouteAddress { + err := luid.AddRoute(addr, netip.IPv6Unspecified(), 0) if err != nil { - return E.Cause(err, "set ipv4 route") + return E.Cause(err, "add ipv6 route: ", addr) } } - } - if len(t.options.Inet6Address) > 0 { - if len(t.options.Inet6RouteAddress) > 0 { - for _, addr := range t.options.Inet6RouteAddress { - err := luid.AddRoute(addr, netip.IPv6Unspecified(), 0) - if err != nil { - return E.Cause(err, "add ipv6 route: ", addr) - } - } - } else { - err := luid.AddRoute(netip.PrefixFrom(netip.IPv6Unspecified(), 0), netip.IPv6Unspecified(), 0) - if err != nil { - return E.Cause(err, "set ipv6 route") - } + } else { + err := luid.AddRoute(netip.PrefixFrom(netip.IPv6Unspecified(), 0), netip.IPv6Unspecified(), 0) + if err != nil { + return E.Cause(err, "set ipv6 route") } } }