Publish Zonghengjia Windows 0.3.13 client source
This commit is contained in:
+22
-60
@@ -134,10 +134,6 @@ internal static class TunRouting
|
||||
throw new InvalidOperationException("Tailscale 节点旁路目标不匹配。");
|
||||
TailnetTransport.RequireExistingRoute(preserveTailnetAddress);
|
||||
}
|
||||
var bypassAddresses = proxyIpv4Addresses
|
||||
.Concat(physicalNetwork.Ipv4DnsServers)
|
||||
.Distinct()
|
||||
.ToList();
|
||||
var tunDnsServer = physicalNetwork.Ipv4DnsServers.FirstOrDefault();
|
||||
if (tunDnsServer is null)
|
||||
{
|
||||
@@ -156,67 +152,33 @@ internal static class TunRouting
|
||||
{
|
||||
if (preservePrivateNodeRoute) PrivateAuxiliaryRuntime.RequireExistingRoute(proxyIpv4Addresses[0].ToString());
|
||||
if (preserveTailnetAddress is not null) TailnetTransport.RequireExistingRoute(preserveTailnetAddress);
|
||||
var routes = physicalNetwork.Ipv4DnsServers.Select(address => new Ipv4Route(
|
||||
address.ToString(), "255.255.255.255",
|
||||
IsOnLink(address, physicalNetwork.Ipv4Address, physicalNetwork.Ipv4PrefixLength)
|
||||
? "0.0.0.0" : physicalNetwork.Ipv4Gateway.ToString(), physicalNetwork.InterfaceIndex)).ToList();
|
||||
routes.Add(new("0.0.0.0", "128.0.0.0", "0.0.0.0", index, 5));
|
||||
routes.Add(new("128.0.0.0", "128.0.0.0", "0.0.0.0", index, 5));
|
||||
// Only journal our new routes. User's existing .111/LAN/Tailscale
|
||||
// entries are neither added to the journal nor removed on recovery.
|
||||
await new NetworkRouteLease(AppPaths.TunStateFile, new WindowsRouteTable()).ApplyAsync(routes, cancellationToken);
|
||||
await RunAsync("ipconfig.exe", "/flushdns", cancellationToken);
|
||||
return;
|
||||
}
|
||||
var state = new TunState
|
||||
{
|
||||
InterfaceIndex = index,
|
||||
PhysicalInterfaceIndex = physicalNetwork.InterfaceIndex,
|
||||
};
|
||||
File.WriteAllText(AppPaths.TunStateFile, JsonSerializer.Serialize(state));
|
||||
|
||||
foreach (var address in bypassAddresses)
|
||||
{
|
||||
var nextHop = IsOnLink(address, physicalNetwork.Ipv4Address, physicalNetwork.Ipv4PrefixLength)
|
||||
|| physicalNetwork.Ipv4Gateway.Equals(IPAddress.Any)
|
||||
? IPAddress.Any
|
||||
: physicalNetwork.Ipv4Gateway;
|
||||
await RunAsync(
|
||||
"route.exe",
|
||||
$"ADD {address} MASK 255.255.255.255 {nextHop} IF {physicalNetwork.InterfaceIndex} METRIC 1",
|
||||
cancellationToken);
|
||||
state.BypassIpv4Addresses.Add(address.ToString());
|
||||
File.WriteAllText(AppPaths.TunStateFile, JsonSerializer.Serialize(state));
|
||||
var purpose = proxyIpv4Addresses.Contains(address) ? "节点" : "DNS";
|
||||
AppPaths.WriteLog($"{purpose}旁路已建立:{address} -> {physicalNetwork.InterfaceName}");
|
||||
}
|
||||
|
||||
await RunAsync("route.exe", $"ADD 0.0.0.0 MASK 128.0.0.0 0.0.0.0 IF {index} METRIC 5", cancellationToken);
|
||||
await RunAsync("route.exe", $"ADD 128.0.0.0 MASK 128.0.0.0 0.0.0.0 IF {index} METRIC 5", cancellationToken);
|
||||
state = new TunState
|
||||
{
|
||||
InterfaceIndex = index,
|
||||
PhysicalInterfaceIndex = physicalNetwork.InterfaceIndex,
|
||||
BypassIpv4Addresses = state.BypassIpv4Addresses,
|
||||
Ipv4RoutesAdded = true,
|
||||
};
|
||||
File.WriteAllText(AppPaths.TunStateFile, JsonSerializer.Serialize(state));
|
||||
// IPv6 stays on the original network in 0.2.1. Capturing it without
|
||||
// per-interface IPv6 gateway and DNS discovery can break Tailscale,
|
||||
// mobile hotspots and dual-stack Wi-Fi even when IPv4 is healthy.
|
||||
state = new TunState
|
||||
{
|
||||
InterfaceIndex = index,
|
||||
PhysicalInterfaceIndex = physicalNetwork.InterfaceIndex,
|
||||
BypassIpv4Addresses = state.BypassIpv4Addresses,
|
||||
Ipv4RoutesAdded = true,
|
||||
Ipv6RoutesAdded = false,
|
||||
};
|
||||
File.WriteAllText(AppPaths.TunStateFile, JsonSerializer.Serialize(state));
|
||||
var routes = CreateOwnedRoutes(physicalNetwork, proxyIpv4Addresses, index,
|
||||
preservePrivateNodeRoute || preserveTailnetAddress is not null);
|
||||
// The journal records additions before applying them and skips exact
|
||||
// pre-existing routes. The user's LAN and Tailnet routes stay in place.
|
||||
await new NetworkRouteLease(AppPaths.TunStateFile, new WindowsRouteTable()).ApplyAsync(routes, cancellationToken);
|
||||
AppPaths.WriteLog($"青岛管理旁路已建立:{PlatformBootstrapTransport.ManagementAddress} -> {physicalNetwork.InterfaceName}");
|
||||
await RunAsync("ipconfig.exe", "/flushdns", cancellationToken);
|
||||
AppPaths.WriteLog("Windows DNS 缓存已刷新,新的网卡与 DNS 路由已生效。");
|
||||
}
|
||||
|
||||
internal static IReadOnlyList<Ipv4Route> CreateOwnedRoutes(PhysicalNetwork physicalNetwork,
|
||||
IReadOnlyCollection<IPAddress> proxyIpv4Addresses, int tunIndex, bool preserveProxyRoute)
|
||||
{
|
||||
var bypassAddresses = physicalNetwork.Ipv4DnsServers.Append(PlatformBootstrapTransport.ManagementAddress);
|
||||
if (!preserveProxyRoute) bypassAddresses = proxyIpv4Addresses.Concat(bypassAddresses);
|
||||
var routes = bypassAddresses.Distinct().Select(address =>
|
||||
{
|
||||
var nextHop = IsOnLink(address, physicalNetwork.Ipv4Address, physicalNetwork.Ipv4PrefixLength)
|
||||
? IPAddress.Any : physicalNetwork.Ipv4Gateway;
|
||||
return new Ipv4Route(address.ToString(), "255.255.255.255", nextHop.ToString(), physicalNetwork.InterfaceIndex);
|
||||
}).ToList();
|
||||
routes.Add(new("0.0.0.0", "128.0.0.0", "0.0.0.0", tunIndex, 5));
|
||||
routes.Add(new("128.0.0.0", "128.0.0.0", "0.0.0.0", tunIndex, 5));
|
||||
return routes;
|
||||
}
|
||||
|
||||
private static bool IsOnLink(IPAddress address, IPAddress localAddress, int prefixLength)
|
||||
{
|
||||
if (address.AddressFamily != AddressFamily.InterNetwork
|
||||
|
||||
Reference in New Issue
Block a user