From f3d3d2231ef5b57066af0b958af68667c82f2baf Mon Sep 17 00:00:00 2001 From: ginuerzh Date: Sat, 9 Aug 2025 19:19:04 +0800 Subject: [PATCH] fix tun route for ipv6 --- handler/tungo/stack.go | 8 ++++---- listener/tun/tun_windows.go | 35 ++++++++++++++++++++++++++++------- listener/tungo/tun_windows.go | 35 ++++++++++++++++++++++++++++------- 3 files changed, 60 insertions(+), 18 deletions(-) diff --git a/handler/tungo/stack.go b/handler/tungo/stack.go index 50329b5e..7d95d0f8 100644 --- a/handler/tungo/stack.go +++ b/handler/tungo/stack.go @@ -115,9 +115,9 @@ func (h *transportHandler) handleTCPConn(originConn adapter.TCPConn) { Service: h.opts.Service, Network: "tcp", RemoteAddr: remoteAddr.String(), - DstAddr: dstAddr.String(), + DstAddr: dstAddr.String(), Host: dstAddr.String(), - ClientAddr: remoteAddr.String(), + ClientAddr: remoteAddr.String(), Time: start, SID: sid, } @@ -287,8 +287,8 @@ func (h *transportHandler) handleUDPConn(uc adapter.UDPConn) { Network: "udp", Service: h.opts.Service, RemoteAddr: remoteAddr.String(), - DstAddr: dstAddr.String(), - ClientAddr: remoteAddr.String(), + DstAddr: dstAddr.String(), + ClientAddr: remoteAddr.String(), Host: dstAddr.String(), SID: sid, Time: start, diff --git a/listener/tun/tun_windows.go b/listener/tun/tun_windows.go index a0440708..9a0b4dda 100644 --- a/listener/tun/tun_windows.go +++ b/listener/tun/tun_windows.go @@ -7,6 +7,7 @@ import ( "os/exec" "strings" + "github.com/go-gost/core/router" "golang.org/x/sys/windows" "golang.zx2c4.com/wireguard/tun" ) @@ -56,6 +57,10 @@ func (l *tunListener) createTun() (ifce io.ReadWriteCloser, name string, ip net. cmd := fmt.Sprintf("netsh interface ip set address name=%s "+ "source=static addr=%s mask=%s gateway=none", name, ipNet.IP.String(), ipMask(ipNet.Mask)) + if ipNet.IP.To4() == nil { // ipv6 + cmd = fmt.Sprintf("netsh interface ipv6 set address %s %s", + name, ipNet.IP.String()) + } l.log.Debug(cmd) args := strings.Split(cmd, " ") @@ -71,7 +76,11 @@ func (l *tunListener) createTun() (ifce io.ReadWriteCloser, name string, ip net. } for _, dns := range l.md.config.DNS { - cmd := fmt.Sprintf("netsh interface ip add dnsservers name=%s address=%s validate=no", name, dns.String()) + network := "ip" + if dns.To4() == nil { + network = "ipv6" + } + cmd := fmt.Sprintf("netsh interface %s add dnsservers name=%s address=%s validate=no", network, name, dns.String()) l.log.Debug(cmd) args := strings.Split(cmd, " ") @@ -86,10 +95,14 @@ func (l *tunListener) createTun() (ifce io.ReadWriteCloser, name string, ip net. func (l *tunListener) addRoutes(ifName string, gw net.IP) error { for _, route := range l.routes { - l.deleteRoute(ifName, route.Net.String()) + l.deleteRoute(ifName, route) - cmd := fmt.Sprintf("netsh interface ip add route prefix=%s interface=%s store=active", - route.Net.String(), ifName) + network := "ip" + if route.Net.IP.To4() == nil { + network = "ipv6" + } + cmd := fmt.Sprintf("netsh interface %s add route prefix=%s interface=%s store=active", + network, route.Net.String(), ifName) if gw != nil { cmd += " nexthop=" + gw.String() } @@ -102,9 +115,17 @@ func (l *tunListener) addRoutes(ifName string, gw net.IP) error { return nil } -func (l *tunListener) deleteRoute(ifName string, route string) error { - cmd := fmt.Sprintf("netsh interface ip delete route prefix=%s interface=%s store=active", - route, ifName) +func (l *tunListener) deleteRoute(ifName string, route *router.Route) error { + if ifName == "" || route == nil { + return nil + } + + network := "ip" + if route.Net.IP.To4() == nil { + network = "ipv6" + } + cmd := fmt.Sprintf("netsh interface %s delete route prefix=%s interface=%s store=active", + network, route.Net.String(), ifName) l.log.Debug(cmd) args := strings.Split(cmd, " ") return exec.Command(args[0], args[1:]...).Run() diff --git a/listener/tungo/tun_windows.go b/listener/tungo/tun_windows.go index 402be418..50333a65 100644 --- a/listener/tungo/tun_windows.go +++ b/listener/tungo/tun_windows.go @@ -7,6 +7,7 @@ import ( "os/exec" "strings" + "github.com/go-gost/core/router" "golang.org/x/sys/windows" "golang.zx2c4.com/wireguard/tun" ) @@ -60,6 +61,10 @@ func (l *tunListener) createTun() (ifce io.ReadWriteCloser, name string, ip net. cmd := fmt.Sprintf("netsh interface ip set address name=%s "+ "source=static addr=%s mask=%s", name, ipNet.IP.String(), ipMask(ipNet.Mask)) + if ipNet.IP.To4() == nil { // ipv6 + cmd = fmt.Sprintf("netsh interface ipv6 set address %s %s", + name, ipNet.IP.String()) + } l.log.Debug(cmd) args := strings.Split(cmd, " ") @@ -79,7 +84,11 @@ func (l *tunListener) createTun() (ifce io.ReadWriteCloser, name string, ip net. } for _, dns := range l.md.config.DNS { - cmd := fmt.Sprintf("netsh interface ip add dnsservers name=%s address=%s validate=no", name, dns.String()) + network := "ip" + if dns.To4() == nil { + network = "ipv6" + } + cmd := fmt.Sprintf("netsh interface %s add dnsservers name=%s address=%s validate=no", network, name, dns.String()) l.log.Debug(cmd) args := strings.Split(cmd, " ") @@ -98,10 +107,14 @@ func (l *tunListener) createTun() (ifce io.ReadWriteCloser, name string, ip net. func (l *tunListener) addRoutes(ifName string, gw net.IP) error { for _, route := range l.routes { - l.deleteRoute(ifName, route.Net.String()) + l.deleteRoute(ifName, route) - cmd := fmt.Sprintf("netsh interface ip add route prefix=%s interface=%s store=active", - route.Net.String(), ifName) + network := "ip" + if route.Net.IP.To4() == nil { + network = "ipv6" + } + cmd := fmt.Sprintf("netsh interface %s add route prefix=%s interface=%s store=active", + network, route.Net.String(), ifName) if gw != nil { cmd += " nexthop=" + gw.String() } @@ -118,9 +131,17 @@ func (l *tunListener) addRoutes(ifName string, gw net.IP) error { return nil } -func (l *tunListener) deleteRoute(ifName string, route string) error { - cmd := fmt.Sprintf("netsh interface ip delete route prefix=%s interface=%s store=active", - route, ifName) +func (l *tunListener) deleteRoute(ifName string, route *router.Route) error { + if ifName == "" || route == nil { + return nil + } + + network := "ip" + if route.Net.IP.To4() == nil { + network = "ipv6" + } + cmd := fmt.Sprintf("netsh interface %s delete route prefix=%s interface=%s store=active", + network, route.Net.String(), ifName) l.log.Debug(cmd) args := strings.Split(cmd, " ") output, er := exec.Command(args[0], args[1:]...).CombinedOutput()