fix tun route for ipv6

This commit is contained in:
ginuerzh committed 2025-08-09 19:19:04 +08:00
1 parent 4eb006f1b0
commit f3d3d2231e
3 files changed
+60 -18

No files matched your search

+4 -4
View File
@@ -115,9 +115,9 @@ func (h *transportHandler) handleTCPConn(originConn adapter.TCPConn) {
Service: h.opts.Service, Service: h.opts.Service,
Network: "tcp", Network: "tcp",
RemoteAddr: remoteAddr.String(), RemoteAddr: remoteAddr.String(),
DstAddr: dstAddr.String(), DstAddr: dstAddr.String(),
Host: dstAddr.String(), Host: dstAddr.String(),
ClientAddr: remoteAddr.String(), ClientAddr: remoteAddr.String(),
Time: start, Time: start,
SID: sid, SID: sid,
} }
@@ -287,8 +287,8 @@ func (h *transportHandler) handleUDPConn(uc adapter.UDPConn) {
Network: "udp", Network: "udp",
Service: h.opts.Service, Service: h.opts.Service,
RemoteAddr: remoteAddr.String(), RemoteAddr: remoteAddr.String(),
DstAddr: dstAddr.String(), DstAddr: dstAddr.String(),
ClientAddr: remoteAddr.String(), ClientAddr: remoteAddr.String(),
Host: dstAddr.String(), Host: dstAddr.String(),
SID: sid, SID: sid,
Time: start, Time: start,
+28 -7
View File
@@ -7,6 +7,7 @@ import (
"os/exec" "os/exec"
"strings" "strings"
"github.com/go-gost/core/router"
"golang.org/x/sys/windows" "golang.org/x/sys/windows"
"golang.zx2c4.com/wireguard/tun" "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 "+ cmd := fmt.Sprintf("netsh interface ip set address name=%s "+
"source=static addr=%s mask=%s gateway=none", "source=static addr=%s mask=%s gateway=none",
name, ipNet.IP.String(), ipMask(ipNet.Mask)) 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) l.log.Debug(cmd)
args := strings.Split(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 { 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) l.log.Debug(cmd)
args := strings.Split(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 { func (l *tunListener) addRoutes(ifName string, gw net.IP) error {
for _, route := range l.routes { 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", network := "ip"
route.Net.String(), ifName) 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 { if gw != nil {
cmd += " nexthop=" + gw.String() cmd += " nexthop=" + gw.String()
} }
@@ -102,9 +115,17 @@ func (l *tunListener) addRoutes(ifName string, gw net.IP) error {
return nil return nil
} }
func (l *tunListener) deleteRoute(ifName string, route string) error { func (l *tunListener) deleteRoute(ifName string, route *router.Route) error {
cmd := fmt.Sprintf("netsh interface ip delete route prefix=%s interface=%s store=active", if ifName == "" || route == nil {
route, ifName) 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) l.log.Debug(cmd)
args := strings.Split(cmd, " ") args := strings.Split(cmd, " ")
return exec.Command(args[0], args[1:]...).Run() return exec.Command(args[0], args[1:]...).Run()
+28 -7
View File
@@ -7,6 +7,7 @@ import (
"os/exec" "os/exec"
"strings" "strings"
"github.com/go-gost/core/router"
"golang.org/x/sys/windows" "golang.org/x/sys/windows"
"golang.zx2c4.com/wireguard/tun" "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 "+ cmd := fmt.Sprintf("netsh interface ip set address name=%s "+
"source=static addr=%s mask=%s", "source=static addr=%s mask=%s",
name, ipNet.IP.String(), ipMask(ipNet.Mask)) 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) l.log.Debug(cmd)
args := strings.Split(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 { 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) l.log.Debug(cmd)
args := strings.Split(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 { func (l *tunListener) addRoutes(ifName string, gw net.IP) error {
for _, route := range l.routes { 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", network := "ip"
route.Net.String(), ifName) 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 { if gw != nil {
cmd += " nexthop=" + gw.String() cmd += " nexthop=" + gw.String()
} }
@@ -118,9 +131,17 @@ func (l *tunListener) addRoutes(ifName string, gw net.IP) error {
return nil return nil
} }
func (l *tunListener) deleteRoute(ifName string, route string) error { func (l *tunListener) deleteRoute(ifName string, route *router.Route) error {
cmd := fmt.Sprintf("netsh interface ip delete route prefix=%s interface=%s store=active", if ifName == "" || route == nil {
route, ifName) 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) l.log.Debug(cmd)
args := strings.Split(cmd, " ") args := strings.Split(cmd, " ")
output, er := exec.Command(args[0], args[1:]...).CombinedOutput() output, er := exec.Command(args[0], args[1:]...).CombinedOutput()