fix tun route for ipv6

This commit is contained in:
ginuerzh
2025-08-09 19:19:04 +08:00
parent 4eb006f1b0
commit f3d3d2231e
3 changed files with 60 additions and 18 deletions
+4 -4
View File
@@ -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,
+28 -7
View File
@@ -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()
+28 -7
View File
@@ -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()