diff --git a/install/windows-inno/windows.iss b/install/windows-inno/windows.iss index b71090c9..1aedd7f8 100644 --- a/install/windows-inno/windows.iss +++ b/install/windows-inno/windows.iss @@ -57,7 +57,7 @@ Name: "{group}\{cm:UninstallProgram,{#MyAppName}}"; Filename: "{uninstallexe}"; ; 添加 bin 目录到系统 PATH Filename: "powershell.exe"; Parameters: "-NoProfile -ExecutionPolicy Bypass -Command ""$path = [Environment]::GetEnvironmentVariable('Path', 'Machine'); if ($path -notlike '*{app}\bin*') {{ [Environment]::SetEnvironmentVariable('Path', $path + ';{app}\bin', 'Machine') }"""; Flags: runhidden waituntilterminated ; 创建环境变量配置文件 -Filename: "powershell.exe"; Parameters: "-NoProfile -ExecutionPolicy Bypass -Command ""if (!(Test-Path '{app}\v2rayA_env.txt')) {{ Set-Content -Path '{app}\v2rayA_env.txt' -Value 'V2RAYA_V2RAY_ASSETSDIR=''{app}\data''','V2RAYA_LOG_FILE='""${env:TEMP}\v2raya.log"" -Encoding UTF8 }"""; Flags: runhidden waituntilterminated +Filename: "powershell.exe"; Parameters: "-NoProfile -ExecutionPolicy Bypass -Command ""if (!(Test-Path '{app}\v2rayA_env.txt')) {{ Set-Content -Path '{app}\v2rayA_env.txt' -Value 'V2RAYA_V2RAY_ASSETSDIR=''{app}\data''','V2RAYA_LOG_FILE='""${{env:TEMP}\v2raya.log"" -Encoding UTF8 }"""; Flags: runhidden waituntilterminated ; 设置环境变量指向配置文件 Filename: "powershell.exe"; Parameters: "-NoProfile -ExecutionPolicy Bypass -Command ""[Environment]::SetEnvironmentVariable('V2RAYA_WIN_ENVFILE', '{app}\v2rayA_env.txt', 'Machine')"""; Flags: runhidden waituntilterminated ; 创建服务 diff --git a/service/core/tun/route_other.go b/service/core/tun/route_other.go index 14b407e6..945cfbad 100644 --- a/service/core/tun/route_other.go +++ b/service/core/tun/route_other.go @@ -14,16 +14,44 @@ import ( ) var excludedRoutes []netip.Prefix +var tunDefaultRouteAdded bool -// SetupTunRouteRules is a no-op on non-Linux systems (fwmark not supported) +// SetupTunRouteRules sets up TUN default route on Windows +// This is needed because sing-tun's AutoRoute doesn't work properly on Windows func SetupTunRouteRules() error { - // Policy routing with fwmark is only supported on Linux + if runtime.GOOS != "windows" { + return nil + } + + // Add default route via TUN interface gateway with lower metric than physical interface + // TUN gateway is 172.19.0.2, metric should be lower than physical interface (typically 35) + cmd := exec.Command("route", "add", "0.0.0.0", "mask", "0.0.0.0", "172.19.0.2", "metric", "1") + output, err := cmd.CombinedOutput() + if err != nil { + // Check if route already exists + if !strings.Contains(string(output), "对象已存在") && !strings.Contains(string(output), "already exists") { + log.Warn("SetupTunRouteRules: failed to add default route via TUN: %v, output: %s", err, string(output)) + return err + } + } + tunDefaultRouteAdded = true + log.Info("[TUN] Added default route 0.0.0.0/0 via 172.19.0.2 metric 1") return nil } -// CleanupTunRouteRules is a no-op on non-Linux systems +// CleanupTunRouteRules removes TUN default route on Windows func CleanupTunRouteRules() error { - // Policy routing with fwmark is only supported on Linux + if runtime.GOOS != "windows" || !tunDefaultRouteAdded { + return nil + } + + cmd := exec.Command("route", "delete", "0.0.0.0") + if output, err := cmd.CombinedOutput(); err != nil { + log.Warn("CleanupTunRouteRules: failed to delete TUN default route: %v, output: %s", err, string(output)) + } else { + log.Info("[TUN] Removed default route via TUN") + } + tunDefaultRouteAdded = false return nil } @@ -124,9 +152,9 @@ func getDefaultGateway() (string, error) { var cmd *exec.Cmd if runtime.GOOS == "windows" { - // Windows: Use PowerShell to get gateway + // Windows: Use PowerShell to get gateway, excluding TUN interface by InterfaceMetric cmd = exec.Command("powershell", "-Command", - "(Get-NetRoute -DestinationPrefix '0.0.0.0/0' | Select-Object -First 1).NextHop") + "(Get-NetRoute -DestinationPrefix '0.0.0.0/0' | Where-Object {$_.InterfaceMetric -gt 0} | Sort-Object InterfaceMetric | Select-Object -First 1).NextHop") } else { // macOS/BSD: route -n get default cmd = exec.Command("sh", "-c", "route -n get default | grep gateway | awk '{print $2}'") diff --git a/service/core/tun/singTun.go b/service/core/tun/singTun.go index 76149fec..a834970a 100644 --- a/service/core/tun/singTun.go +++ b/service/core/tun/singTun.go @@ -35,6 +35,8 @@ const ( var ( prefix4 = netip.MustParsePrefix("172.19.0.1/30") prefix6 = netip.MustParsePrefix("fdfe:dcba:9876::1/126") + route4 = netip.MustParsePrefix("0.0.0.0/0") + route6 = netip.MustParsePrefix("::/0") defaultLogger = logger.NOP() @@ -203,6 +205,13 @@ func (t *singTun) Start(stack Stack) error { autoRoute := true strictRoute := t.strictRoute + // On Windows, disable sing-tun's AutoRoute as it adds metric=0 route + // We will add manual default route with proper metric instead + if runtime.GOOS == "windows" { + autoRoute = false + log.Info("[TUN] Windows: Disabling AutoRoute, will use manual routing") + } + log.Info("[TUN] Starting with StrictRoute=%t, AutoRoute=%t", strictRoute, autoRoute) log.Info("[TUN] Total exclusions: %d IPv4, %d IPv6", len(inet4Exclude), len(inet6Exclude)) if runtime.GOOS == "windows" && strictRoute { @@ -220,6 +229,7 @@ func (t *singTun) Start(stack Stack) error { Name: tun.CalculateInterfaceName(""), MTU: 9000, Inet4Address: []netip.Prefix{prefix4}, + Inet4RouteAddress: []netip.Prefix{route4}, Inet4RouteExcludeAddress: inet4Exclude, // Exclude loopback + server IPs AutoRoute: autoRoute, StrictRoute: strictRoute, @@ -228,6 +238,7 @@ func (t *singTun) Start(stack Stack) error { // Enable IPv6 if requested if t.useIPv6 { tunOptions.Inet6Address = []netip.Prefix{prefix6} + tunOptions.Inet6RouteAddress = []netip.Prefix{route6} // Exclude IPv6 loopback (::1/128) loopback6 := netip.MustParsePrefix("::1/128") inet6Exclude = append([]netip.Prefix{loopback6}, inet6Exclude...)