Publish reviewed history-free open-source snapshot (2026-10-12)
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
namespace UCVL.VPNPlus;
|
||||
|
||||
internal static class NodeTransportConnector
|
||||
{
|
||||
internal static async Task<string> RebindAsync(string current, string? original,
|
||||
Func<string, CancellationToken, Task> start, Func<Task> stop,
|
||||
Func<CancellationToken, Task<bool>> proxyHealth, CancellationToken token)
|
||||
{
|
||||
var tailnet = TailnetTransport.Address(current) is not null;
|
||||
if (tailnet && !TailnetTransport.SameAuthorizedFallback(current, original))
|
||||
throw new InvalidOperationException("缺少可验证的同节点原入口,未尝试切换。");
|
||||
using var deadline = CancellationTokenSource.CreateLinkedTokenSource(token);
|
||||
if (tailnet) deadline.CancelAfter(TimeSpan.FromSeconds(45));
|
||||
try
|
||||
{
|
||||
await start(current, deadline.Token);
|
||||
if (tailnet && !await proxyHealth(deadline.Token)) throw new IOException("Tailscale 切网后出口未通过。");
|
||||
deadline.Token.ThrowIfCancellationRequested();
|
||||
return current;
|
||||
}
|
||||
catch (Exception e) when (tailnet && e is not LocalNetworkException && !token.IsCancellationRequested)
|
||||
{
|
||||
await stop(); // Full cleanup before same-node fallback; failure aborts.
|
||||
token.ThrowIfCancellationRequested();
|
||||
await start(original!, token);
|
||||
return original!;
|
||||
}
|
||||
}
|
||||
|
||||
// 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,
|
||||
Func<string, int, CancellationToken, Task<bool>>? endpointProbe = null)
|
||||
{
|
||||
token.ThrowIfCancellationRequested();
|
||||
var tailnet = preference.MayTry(response.Node.Slug, ticks()) ? TailnetTransport.TryPrepare(response, allowKnownEndpoints) : null;
|
||||
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;
|
||||
status("Tailscale 无独立内网路由,使用同一节点公网入口");
|
||||
}
|
||||
}
|
||||
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;
|
||||
status("Tailscale 端口不可达,使用同一节点公网入口");
|
||||
}
|
||||
}
|
||||
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();
|
||||
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();
|
||||
status("Tailscale 协议或代理出口验证失败,回退同一节点公网入口");
|
||||
}
|
||||
}
|
||||
token.ThrowIfCancellationRequested();
|
||||
await start(response.GetRuntimeConfigJson(), token);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user