Windows 0.3.1 clean monitor source delta; no private history or other platform changes
This commit is contained in:
+22
-25
@@ -99,29 +99,23 @@ internal static class TailnetTransport
|
||||
|
||||
internal static int Port(string json) => JsonNode.Parse(json)![Marker]!["port"]!.GetValue<int>();
|
||||
|
||||
// Before starting/replacing a core, test only this socket through the existing
|
||||
// user-owned route. ICMP success isn't enough; TCP success still isn't auth.
|
||||
// Selection checks only ICMP through the existing OS network. It never
|
||||
// queries or changes Tailscale; authenticated proxy health is checked after
|
||||
// starting the chosen authorized transport, separately from reachability.
|
||||
internal static async Task<bool> ProbeEndpointAsync(string address, int port, CancellationToken token)
|
||||
{
|
||||
token.ThrowIfCancellationRequested();
|
||||
if (!IsTailnetAddress(address) || port is < 1 or > 65535) return false;
|
||||
using var deadline = CancellationTokenSource.CreateLinkedTokenSource(token);
|
||||
deadline.CancelAfter(TimeSpan.FromSeconds(3));
|
||||
deadline.CancelAfter(TimeSpan.FromSeconds(1));
|
||||
try
|
||||
{
|
||||
var name = RequireExistingRoute(address);
|
||||
var nic = NetworkInterface.GetAllNetworkInterfaces().Single(n => n.Name == name);
|
||||
var index = nic.GetIPProperties().GetIPv4Properties()!.Index;
|
||||
using var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
// Windows IP_UNICAST_IF=31 is not named by .NET 8's enum.
|
||||
// https://learn.microsoft.com/en-us/windows/win32/winsock/ipproto-ip-socket-options
|
||||
const SocketOptionName ipUnicastInterface = (SocketOptionName)31;
|
||||
socket.SetSocketOption(SocketOptionLevel.IP, ipUnicastInterface, IPAddress.HostToNetworkOrder(index));
|
||||
await socket.ConnectAsync(new IPEndPoint(IPAddress.Parse(address), port), deadline.Token);
|
||||
return true;
|
||||
using var ping = new Ping();
|
||||
var reply = await ping.SendPingAsync(IPAddress.Parse(address), 1000).WaitAsync(deadline.Token).ConfigureAwait(false);
|
||||
return reply.Status == IPStatus.Success;
|
||||
}
|
||||
catch (OperationCanceledException) when (!token.IsCancellationRequested) { return false; }
|
||||
catch (Exception e) when (e is SocketException or NetworkInformationException or InvalidOperationException) { return false; }
|
||||
catch (Exception e) when (e is PingException or SocketException or NetworkInformationException or InvalidOperationException) { return false; }
|
||||
}
|
||||
|
||||
internal static bool SameAuthorizedFallback(string current, string? fallback)
|
||||
@@ -142,20 +136,21 @@ internal static class TailnetTransport
|
||||
}
|
||||
|
||||
// Read only. No CLI/process/service dependency: user owns the underlying network.
|
||||
// Require an existing more-specific route; never manufacture a /32 through the
|
||||
// phone/default gateway. Bind the proxy socket to that interface so a lost
|
||||
// tailnet route cannot silently recurse into our own TUN.
|
||||
// Ask Windows which existing interface owns this IP path, including a valid
|
||||
// default route. No adapter-name, service or route-prefix prerequisite.
|
||||
// This UDP Connect sends no payload. Refuse our own TUN to avoid recursion;
|
||||
// no /32 or user route is manufactured or removed.
|
||||
internal static string RequireExistingRoute(string address)
|
||||
{
|
||||
if (!IsTailnetAddress(address)) throw new InvalidOperationException("Tailscale 地址无效。");
|
||||
var interfaces = NetworkInterface.GetAllNetworkInterfaces()
|
||||
using var route = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
|
||||
route.Connect(new IPEndPoint(IPAddress.Parse(address), 443));
|
||||
var local = (route.LocalEndPoint as IPEndPoint)?.Address;
|
||||
var owner = NetworkInterface.GetAllNetworkInterfaces()
|
||||
.Where(n => n.OperationalStatus == OperationalStatus.Up && n.NetworkInterfaceType != NetworkInterfaceType.Loopback
|
||||
&& !n.Name.StartsWith(TunRouting.AdapterName, StringComparison.OrdinalIgnoreCase))
|
||||
.Select(n => new { n.Name, Index = n.GetIPProperties().GetIPv4Properties()?.Index })
|
||||
.Where(n => n.Index.HasValue).ToDictionary(n => n.Index!.Value, n => n.Name);
|
||||
var index = SelectRoute(address, new WindowsRouteTable().Snapshot(), interfaces.Keys.ToHashSet());
|
||||
return index is { } found ? interfaces[found]
|
||||
: throw new InvalidOperationException("Tailscale 节点路径暂不可用,保留原入口作为回退。");
|
||||
.FirstOrDefault(n => n.GetIPProperties().UnicastAddresses.Any(a => a.Address.Equals(local)));
|
||||
return owner?.Name ?? throw new InvalidOperationException("代理内网 IP 路径暂不可用,保留域名入口作为回退。");
|
||||
}
|
||||
|
||||
internal static int? SelectRoute(string address, IEnumerable<Ipv4Route> routes, IReadOnlySet<int> usable)
|
||||
@@ -191,9 +186,11 @@ internal static class TailnetTransport
|
||||
internal sealed class TailnetPreference
|
||||
{
|
||||
private readonly Dictionary<string, long> _cooldowns = new(StringComparer.Ordinal);
|
||||
internal bool DomainOnly { get; private set; }
|
||||
// Only an explicit user connect action resets failures. Automatic recovery
|
||||
// continues respecting the existing monotonic cooldown.
|
||||
internal void ResetForUserConnect() => _cooldowns.Clear();
|
||||
internal bool MayTry(string slug, long now) => !_cooldowns.TryGetValue(slug, out var expiry) || now >= expiry;
|
||||
internal void ResetForUserConnect() { _cooldowns.Clear(); DomainOnly = false; }
|
||||
internal void CommitDomainFallback() => DomainOnly = true;
|
||||
internal bool MayTry(string slug, long now) => !DomainOnly && (!_cooldowns.TryGetValue(slug, out var expiry) || now >= expiry);
|
||||
internal void Failed(string slug, long now) => _cooldowns[slug] = now + 5 * 60_000;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user