78 lines
3.5 KiB
C#
78 lines
3.5 KiB
C#
|
|
namespace UCVL.VPNPlus;
|
||
|
|
|
||
|
|
internal sealed partial class MainForm
|
||
|
|
{
|
||
|
|
private readonly LearningSyncCoordinator _learningSync = new();
|
||
|
|
private int? _learningSyncCore;
|
||
|
|
private readonly TailnetReadinessPolicy _tailnetReadiness = new();
|
||
|
|
private readonly ToolStripMenuItem _tailnetReconnectItem = new("优先内网重连(会重建连接)") { Enabled = false };
|
||
|
|
|
||
|
|
private void SetSessionToken(string? token)
|
||
|
|
{
|
||
|
|
_connectionAttemptCancellation?.Cancel();
|
||
|
|
_accessToken = token;
|
||
|
|
_sessionRevision++;
|
||
|
|
_learningSync.Reset();
|
||
|
|
_learningSyncCore = null;
|
||
|
|
_tailnetReadiness.Reset();
|
||
|
|
_tailnetReconnectItem.Enabled = false;
|
||
|
|
while (_observationQueue.TryDequeue(out _)) { }
|
||
|
|
}
|
||
|
|
|
||
|
|
private bool TryCancelPendingConnection()
|
||
|
|
{
|
||
|
|
if (_connectionAttemptCancellation is not { } pending) return false;
|
||
|
|
_wantConnected = false;
|
||
|
|
pending.Cancel();
|
||
|
|
_connectButton.Enabled = false;
|
||
|
|
_statusDetail.Text = "正在取消连接并恢复本机网络…";
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
private TailnetReadinessPolicy.Context ReadTailnetReadinessContext() => new(
|
||
|
|
_sessionRevision, _xray.CoreId, _wantConnected,
|
||
|
|
_busy || _handoverOperating || _xray.IsTransitioning || _exiting,
|
||
|
|
_xray.IsRunning, _xray.UsesTailnet, _xray.PreferredTailnetEndpoint is not null);
|
||
|
|
|
||
|
|
// Read-only recovery observation. TCP availability is NOT authenticated
|
||
|
|
// proxy proof and never authorizes terminating a healthy public connection.
|
||
|
|
private async Task CheckTailnetReadinessAsync()
|
||
|
|
{
|
||
|
|
if (_uiOnly || _preview) return;
|
||
|
|
if (!_tailnetReadiness.TryBegin(ReadTailnetReadinessContext(), Environment.TickCount64, out var ticket))
|
||
|
|
{
|
||
|
|
_tailnetReconnectItem.Enabled = _tailnetReadiness.IsReady && !_busy && !_exiting;
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
var reachable = false;
|
||
|
|
try
|
||
|
|
{
|
||
|
|
if (_xray.PreferredTailnetEndpoint is { } endpoint)
|
||
|
|
reachable = await TailnetTransport.ProbeEndpointAsync(endpoint.Address, endpoint.Port, _lifetime.Token);
|
||
|
|
}
|
||
|
|
catch (OperationCanceledException) { }
|
||
|
|
catch (Exception) { /* Advisory only; fixed diagnostics below contain no credentials. */ }
|
||
|
|
var result = _tailnetReadiness.Observe(ticket, ReadTailnetReadinessContext(), Environment.TickCount64, reachable);
|
||
|
|
if (_exiting || IsDisposed || Disposing) return;
|
||
|
|
_tailnetReconnectItem.Enabled = _tailnetReadiness.IsReady && !_busy;
|
||
|
|
if (result == TailnetReadinessPolicy.Result.BecameReady)
|
||
|
|
{
|
||
|
|
AppPaths.WriteLog(new TransportDiagnostic("readiness", "tailnet_endpoint_recovered", _currentNodeSlug ?? "").SafeMessage);
|
||
|
|
_notifyIcon.ShowBalloonTip(3500, "Tailscale 内网入口已恢复",
|
||
|
|
"现有连接保持不变。右键托盘可优先内网重连;重连会重建连接。", ToolTipIcon.Info);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private async Task ReconnectThroughPreferredTailnetAsync()
|
||
|
|
{
|
||
|
|
if (_uiOnly || _preview || _exiting || _busy || !_wantConnected || !_xray.IsRunning
|
||
|
|
|| _xray.UsesTailnet || !_tailnetReadiness.IsReady) return;
|
||
|
|
_tailnetReadiness.Reset();
|
||
|
|
_tailnetReconnectItem.Enabled = false;
|
||
|
|
_tailnetPreference.ResetForUserConnect();
|
||
|
|
// Explicit operator action, with fresh management authorization and the
|
||
|
|
// normal same-node/manual constraints. No silent migration of streams.
|
||
|
|
await ToggleConnectionAsync(reconnect: true);
|
||
|
|
}
|
||
|
|
}
|