2026-10-11 23:23:20 +00:00
|
|
|
namespace UCVL.VPNPlus;
|
|
|
|
|
|
2026-10-22 05:43:39 +00:00
|
|
|
// Contains only bounded identifiers and fixed reason codes, never config JSON,
|
|
|
|
|
// credentials, exception messages, URLs, or a management access token.
|
|
|
|
|
internal sealed record TransportDiagnostic(string Event, string Reason, string Node)
|
|
|
|
|
{
|
|
|
|
|
private static string Identifier(string value) => new(value.Take(100)
|
|
|
|
|
.Select(c => char.IsAsciiLetterOrDigit(c) || c is '-' or '_' or '.' ? c : '_').ToArray());
|
|
|
|
|
internal string SafeMessage => $"transport event={Identifier(Event)} reason={Identifier(Reason)} node={Identifier(Node)}";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal sealed class TailnetUnavailableException(string reason) : IOException("当前授权节点的 Tailscale 入口未通过验证。")
|
|
|
|
|
{
|
|
|
|
|
internal string Reason { get; } = reason;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-11 23:23:20 +00:00
|
|
|
internal static class NodeTransportConnector
|
|
|
|
|
{
|
|
|
|
|
internal static async Task<string> RebindAsync(string current, string? original,
|
|
|
|
|
Func<string, CancellationToken, Task> start, Func<Task> stop,
|
2026-10-22 05:43:39 +00:00
|
|
|
Func<CancellationToken, Task<bool>> proxyHealth, CancellationToken token,
|
|
|
|
|
string? preferredTailnetConfig = null, Action<string>? requireRoute = null,
|
|
|
|
|
Func<string, int, CancellationToken, Task<bool>>? endpointProbe = null,
|
|
|
|
|
Action<TransportDiagnostic>? diagnostic = null)
|
2026-10-11 23:23:20 +00:00
|
|
|
{
|
2026-10-22 05:43:39 +00:00
|
|
|
token.ThrowIfCancellationRequested();
|
2026-10-11 23:23:20 +00:00
|
|
|
var tailnet = TailnetTransport.Address(current) is not null;
|
|
|
|
|
if (tailnet && !TailnetTransport.SameAuthorizedFallback(current, original))
|
|
|
|
|
throw new InvalidOperationException("缺少可验证的同节点原入口,未尝试切换。");
|
2026-10-22 05:43:39 +00:00
|
|
|
var selected = current;
|
|
|
|
|
var fallback = original;
|
|
|
|
|
if (!tailnet && preferredTailnetConfig is not null)
|
|
|
|
|
{
|
|
|
|
|
// Only a confirmed physical-network recovery calls this method.
|
|
|
|
|
// Re-check the cached alternate of this SAME authorized config; a
|
|
|
|
|
// previous-network cooldown must not make public ingress sticky.
|
|
|
|
|
if (!TailnetTransport.SameAuthorizedFallback(preferredTailnetConfig, current))
|
|
|
|
|
throw new InvalidOperationException("Tailscale 恢复入口与当前授权线路不匹配。");
|
|
|
|
|
if (requireRoute is null || endpointProbe is null)
|
|
|
|
|
throw new InvalidOperationException("Tailscale 恢复入口缺少独立路由或端口验证。");
|
|
|
|
|
var recheckReason = "route_unavailable";
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var address = TailnetTransport.Address(preferredTailnetConfig)!;
|
|
|
|
|
requireRoute(address);
|
|
|
|
|
token.ThrowIfCancellationRequested();
|
|
|
|
|
recheckReason = "endpoint_unreachable";
|
|
|
|
|
if (!await endpointProbe(address, TailnetTransport.Port(preferredTailnetConfig), token))
|
|
|
|
|
diagnostic?.Invoke(new("rebind_skipped", "endpoint_unreachable", ""));
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
token.ThrowIfCancellationRequested();
|
|
|
|
|
selected = preferredTailnetConfig;
|
|
|
|
|
fallback = current;
|
|
|
|
|
tailnet = true;
|
|
|
|
|
diagnostic?.Invoke(new("rebind_attempt", "tailnet_recovered", ""));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e) when (e is not OperationCanceledException && e is not LocalNetworkException)
|
|
|
|
|
{ diagnostic?.Invoke(new("rebind_skipped", recheckReason, "")); }
|
|
|
|
|
}
|
|
|
|
|
token.ThrowIfCancellationRequested();
|
2026-10-11 23:23:20 +00:00
|
|
|
using var deadline = CancellationTokenSource.CreateLinkedTokenSource(token);
|
|
|
|
|
if (tailnet) deadline.CancelAfter(TimeSpan.FromSeconds(45));
|
|
|
|
|
try
|
|
|
|
|
{
|
2026-10-22 05:43:39 +00:00
|
|
|
await start(selected, deadline.Token);
|
2026-10-11 23:23:20 +00:00
|
|
|
if (tailnet && !await proxyHealth(deadline.Token)) throw new IOException("Tailscale 切网后出口未通过。");
|
|
|
|
|
deadline.Token.ThrowIfCancellationRequested();
|
2026-10-22 05:43:39 +00:00
|
|
|
diagnostic?.Invoke(new("rebind_selected", tailnet ? "tailnet_verified" : "public_ingress", ""));
|
|
|
|
|
return selected;
|
2026-10-11 23:23:20 +00:00
|
|
|
}
|
|
|
|
|
catch (Exception e) when (tailnet && e is not LocalNetworkException && !token.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
await stop(); // Full cleanup before same-node fallback; failure aborts.
|
|
|
|
|
token.ThrowIfCancellationRequested();
|
2026-10-22 05:43:39 +00:00
|
|
|
diagnostic?.Invoke(new("rebind_fallback", "proxy_health_or_start_failed", ""));
|
|
|
|
|
await start(fallback!, token);
|
|
|
|
|
token.ThrowIfCancellationRequested();
|
|
|
|
|
return fallback!;
|
2026-10-11 23:23:20 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Called only as part of an explicit connect or confirmed-failure recovery.
|
|
|
|
|
// No background race or periodic optimization may replace a healthy session.
|
|
|
|
|
internal static async Task<bool> ConnectAsync(ConfigResponse response, TailnetPreference preference,
|
|
|
|
|
Func<string, CancellationToken, Task> start, Func<Task> stop, Func<CancellationToken, Task<bool>> proxyHealth,
|
|
|
|
|
Action<string> requireRoute, Action<string> status, Func<long> ticks,
|
|
|
|
|
CancellationToken token, bool allowKnownEndpoints = false,
|
2026-10-22 05:43:39 +00:00
|
|
|
Func<string, int, CancellationToken, Task<bool>>? endpointProbe = null,
|
|
|
|
|
bool tailnetOnly = false, Action<TransportDiagnostic>? diagnostic = null)
|
2026-10-11 23:23:20 +00:00
|
|
|
{
|
|
|
|
|
token.ThrowIfCancellationRequested();
|
2026-10-22 05:43:39 +00:00
|
|
|
var reason = "cooldown_active";
|
|
|
|
|
var tailnet = preference.MayTry(response.Node.Slug, ticks())
|
|
|
|
|
? TailnetTransport.TryPrepare(response, out reason, allowKnownEndpoints) : null;
|
|
|
|
|
void Report(string action, string code) => diagnostic?.Invoke(new(action, code, response.Node.Slug));
|
|
|
|
|
if (tailnet is null) Report("tailnet_skipped", reason);
|
|
|
|
|
else Report("tailnet_candidate", reason);
|
2026-10-11 23:23:20 +00:00
|
|
|
if (tailnet is not null)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
requireRoute(TailnetTransport.Address(tailnet)!);
|
|
|
|
|
token.ThrowIfCancellationRequested();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e) when (e is not OperationCanceledException)
|
|
|
|
|
{
|
|
|
|
|
preference.Failed(response.Node.Slug, ticks());
|
|
|
|
|
tailnet = null;
|
2026-10-22 05:43:39 +00:00
|
|
|
reason = "route_unavailable";
|
|
|
|
|
Report("tailnet_skipped", reason);
|
|
|
|
|
status(tailnetOnly ? "Tailscale 无独立内网路由,继续检测其他授权内网节点" : "Tailscale 无独立内网路由,使用同一节点公网入口");
|
2026-10-11 23:23:20 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (tailnet is not null && endpointProbe is not null)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
status("正在检测 Tailscale 入口(最多 3 秒)");
|
|
|
|
|
if (!await endpointProbe(TailnetTransport.Address(tailnet)!, TailnetTransport.Port(tailnet), token))
|
|
|
|
|
throw new IOException("Tailscale 端口不可达。");
|
|
|
|
|
token.ThrowIfCancellationRequested();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e) when (e is not OperationCanceledException)
|
|
|
|
|
{
|
|
|
|
|
preference.Failed(response.Node.Slug, ticks());
|
|
|
|
|
tailnet = null;
|
2026-10-22 05:43:39 +00:00
|
|
|
reason = "endpoint_unreachable";
|
|
|
|
|
Report("tailnet_skipped", reason);
|
|
|
|
|
status(tailnetOnly ? "Tailscale 端口不可达,继续检测其他授权内网节点" : "Tailscale 端口不可达,使用同一节点公网入口");
|
2026-10-11 23:23:20 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (tailnet is not null)
|
|
|
|
|
{
|
|
|
|
|
using var deadline = CancellationTokenSource.CreateLinkedTokenSource(token);
|
|
|
|
|
deadline.CancelAfter(TimeSpan.FromSeconds(45));
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
token.ThrowIfCancellationRequested();
|
|
|
|
|
status("正在验证 Tailscale 节点入口");
|
|
|
|
|
await start(tailnet, deadline.Token);
|
|
|
|
|
deadline.Token.ThrowIfCancellationRequested();
|
|
|
|
|
// Plain TCP/ping, or a successful DIRECT smart request, is not proof.
|
|
|
|
|
if (!await proxyHealth(deadline.Token)) throw new InvalidOperationException("Tailscale 代理出口验证失败。");
|
|
|
|
|
deadline.Token.ThrowIfCancellationRequested();
|
2026-10-22 05:43:39 +00:00
|
|
|
Report("selected", "tailnet_verified");
|
2026-10-11 23:23:20 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (LocalNetworkException) { throw; }
|
|
|
|
|
catch (OperationCanceledException) when (token.IsCancellationRequested) { throw; }
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
preference.Failed(response.Node.Slug, ticks());
|
|
|
|
|
// A failed restore must propagate: never stack a second connection.
|
|
|
|
|
await stop();
|
2026-10-22 05:43:39 +00:00
|
|
|
reason = "proxy_health_or_start_failed";
|
|
|
|
|
Report("tailnet_failed", reason);
|
|
|
|
|
status(tailnetOnly ? "Tailscale 代理出口验证失败,继续检测其他授权内网节点" : "Tailscale 协议或代理出口验证失败,回退同一节点公网入口");
|
2026-10-11 23:23:20 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
token.ThrowIfCancellationRequested();
|
2026-10-22 05:43:39 +00:00
|
|
|
// Automatic selection can test ALL authorized Tailnet nodes first. A
|
|
|
|
|
// failed alternate must not launch public ingress before that pass ends.
|
|
|
|
|
if (tailnetOnly) throw new TailnetUnavailableException(reason);
|
|
|
|
|
Report("selected", "public_ingress");
|
2026-10-11 23:23:20 +00:00
|
|
|
await start(response.GetRuntimeConfigJson(), token);
|
2026-10-22 05:43:39 +00:00
|
|
|
token.ThrowIfCancellationRequested();
|
2026-10-11 23:23:20 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|