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++; _nodeTrafficPanel.Unavailable("等待当前账号采样", clear: true); _nextNodeTrafficAttempt = DateTimeOffset.MinValue; _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 || _healthChecking || _uplinkChecking || _handoverOperating || _handover.Pending || _handover.Settling || _xray.IsTransitioning || _exiting, _xray.IsRunning, _xray.UsesTailnet, !_currentNodeIsPrivateAuxiliary && _xray.TailnetProbeEndpoint is not null); // The initial connection already tested this address. At ten-minute // intervals repeat only the authorized IP ping; failure on the active path // retries the same node's domain through the normal fresh-grant connector. 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.TailnetProbeEndpoint is { } endpoint) reachable = await TailnetTransport.ProbeEndpointAsync(endpoint.Address, endpoint.Port, _lifetime.Token); } catch (OperationCanceledException) { } catch (Exception) { /* 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.Fallback && _currentNodeSlug is { } slug) { _tailnetPreference.Failed(slug, Environment.TickCount64); _tailnetPreference.CommitDomainFallback(); _transportRetryNodeSlug = slug; _connectionHealth.Invalidate(); AppPaths.WriteLog(new TransportDiagnostic("periodic_recheck", "ping_failed_same_node_domain_fallback", slug).SafeMessage); await ToggleConnectionAsync(suppressDialogs: true, reconnect: true); return; } 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); } }