2026-10-11 23:23:20 +00:00
|
|
|
using System.Net.NetworkInformation;
|
|
|
|
|
|
|
|
|
|
namespace UCVL.VPNPlus;
|
|
|
|
|
|
|
|
|
|
internal sealed partial class MainForm
|
|
|
|
|
{
|
|
|
|
|
private readonly System.Windows.Forms.Timer _uplinkTimer = new() { Interval = 2_000 };
|
|
|
|
|
private NetworkHandover _handover = null!;
|
|
|
|
|
private CancellationTokenSource? _handoverCancellation;
|
|
|
|
|
private bool _uplinkChecking, _handoverOperating;
|
|
|
|
|
|
|
|
|
|
private void InitializeHandover()
|
|
|
|
|
{
|
|
|
|
|
_handover = new(_xray);
|
|
|
|
|
if (_preview || _uiOnly) return;
|
|
|
|
|
_uplinkTimer.Tick += async (_, _) => await CheckUplinkAsync();
|
|
|
|
|
NetworkChange.NetworkAddressChanged += UplinkChanged;
|
|
|
|
|
NetworkChange.NetworkAvailabilityChanged += UplinkAvailabilityChanged;
|
|
|
|
|
_uplinkTimer.Start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UplinkAvailabilityChanged(object? sender, NetworkAvailabilityEventArgs e) => UplinkChanged(sender, EventArgs.Empty);
|
|
|
|
|
private void UplinkChanged(object? sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (_exiting || IsDisposed || !IsHandleCreated) return;
|
|
|
|
|
try { BeginInvoke(async () => await CheckUplinkAsync()); }
|
|
|
|
|
catch (InvalidOperationException) { }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void StopHandoverMonitoring()
|
|
|
|
|
{
|
|
|
|
|
_uplinkTimer.Stop();
|
|
|
|
|
NetworkChange.NetworkAddressChanged -= UplinkChanged;
|
|
|
|
|
NetworkChange.NetworkAvailabilityChanged -= UplinkAvailabilityChanged;
|
|
|
|
|
_handoverCancellation?.Cancel();
|
|
|
|
|
_handover.Reset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task CheckUplinkAsync()
|
|
|
|
|
{
|
2026-11-06 15:28:10 +00:00
|
|
|
if (_runtimeRepairRequired || _preview || _uiOnly || _exiting || !_wantConnected || _busy || _uplinkChecking || _xray.IsTransitioning || !_xray.CanRebindNetwork) return;
|
|
|
|
|
if (!PreflightClientRuntime(suppressDialogs: true)) return;
|
2026-10-11 23:23:20 +00:00
|
|
|
_uplinkChecking = true;
|
|
|
|
|
var refreshAuthorization = false;
|
|
|
|
|
var operating = false;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var reading = await Task.Run(() => PhysicalUplink.Read(_xray.BoundNetwork?.InterfaceIndex));
|
2026-11-06 15:28:10 +00:00
|
|
|
if (_runtimeRepairRequired || _exiting || !_wantConnected || _busy || _xray.IsTransitioning) return;
|
2026-10-11 23:23:20 +00:00
|
|
|
if (!reading.Success) return;
|
|
|
|
|
if (!_handover.Pending && PhysicalUplink.Fingerprint(reading.Network) == _xray.BoundNetworkFingerprint && reading.Network is not null)
|
|
|
|
|
{
|
|
|
|
|
await _handover.ObserveAsync(reading, Environment.TickCount64, _lifetime.Token);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// Invalidate any health probe already in flight; this is not node failure.
|
|
|
|
|
_connectionHealth.Invalidate();
|
|
|
|
|
_handoverOperating = operating = true;
|
|
|
|
|
_handoverCancellation = CancellationTokenSource.CreateLinkedTokenSource(_lifetime.Token);
|
|
|
|
|
_handoverCancellation.CancelAfter(TimeSpan.FromSeconds(90));
|
|
|
|
|
SetBusy(true, "正在识别新的上网方式,可点击断开取消…");
|
|
|
|
|
_connectButton.Enabled = true;
|
|
|
|
|
_connectButton.Text = "断开";
|
|
|
|
|
var result = await _handover.ObserveAsync(reading, Environment.TickCount64, _handoverCancellation.Token);
|
2026-11-06 15:28:10 +00:00
|
|
|
if (_runtimeRepairRequired || !_wantConnected || _exiting) return;
|
2026-10-11 23:23:20 +00:00
|
|
|
if (result == NetworkHandover.Result.Recovered)
|
|
|
|
|
{
|
|
|
|
|
if (_currentNodeUsesTailnet && !_xray.UsesTailnet)
|
|
|
|
|
{
|
2026-10-27 02:00:33 +00:00
|
|
|
_tailnetPreference.CommitDomainFallback();
|
2026-10-11 23:23:20 +00:00
|
|
|
if (_currentNodeSlug is not null) _tailnetPreference.Failed(_currentNodeSlug, Environment.TickCount64);
|
|
|
|
|
_transportRetryNodeSlug = null;
|
|
|
|
|
}
|
|
|
|
|
UpdateConnectedTransportPresentation(_xray.UsesTailnet);
|
|
|
|
|
_api.ResetTransport();
|
|
|
|
|
_nextReconnect = DateTimeOffset.MinValue;
|
|
|
|
|
_connectionHealth.BeginCore(Environment.TickCount64);
|
|
|
|
|
_currentNodeLabel.Text = _currentNodeName ?? _currentNodeSlug ?? "当前工作节点";
|
|
|
|
|
SetConnectionState(true, "已恢复安全连接", $"{_currentNodeLabel.Text} · {_routeCombo.Text}");
|
|
|
|
|
_notifyIcon.Text = "纵横家 · 已连接";
|
|
|
|
|
AppPaths.WriteLog($"切网恢复成功:{_xray.BoundNetwork?.InterfaceName} IF {_xray.BoundNetwork?.InterfaceIndex};沿用当前节点,未计为节点故障。");
|
|
|
|
|
refreshAuthorization = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
SetConnectionState(false, result == NetworkHandover.Result.Offline ? "等待网络恢复" : "切换网络,正在恢复",
|
|
|
|
|
result == NetworkHandover.Result.Offline ? "已释放本机代理接管;网络恢复后自动连接。" : "正在跟随新的网卡、网关和 DNS;可点击断开取消。");
|
|
|
|
|
DisplayTraffic(null, null, "等待网络恢复");
|
|
|
|
|
_connectButton.Text = "断开";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (OperationCanceledException) { }
|
2026-11-06 15:28:10 +00:00
|
|
|
catch (ClientRuntimeException error) { HandleClientRuntimeFailure(error, suppressDialogs: true); }
|
2026-10-11 23:23:20 +00:00
|
|
|
catch (Exception error)
|
|
|
|
|
{
|
|
|
|
|
AppPaths.WriteError(error);
|
2026-11-06 15:28:10 +00:00
|
|
|
if (!_runtimeRepairRequired && _wantConnected && !_exiting)
|
2026-10-11 23:23:20 +00:00
|
|
|
{
|
|
|
|
|
SetConnectionState(false, "等待网络恢复", "本地网络尚未就绪,将自动重试;可点击断开取消。");
|
|
|
|
|
DisplayTraffic(null, null, "等待网络恢复");
|
|
|
|
|
_connectButton.Text = "断开";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
if (operating)
|
|
|
|
|
{
|
|
|
|
|
_handoverCancellation?.Dispose(); _handoverCancellation = null;
|
|
|
|
|
_handoverOperating = false;
|
|
|
|
|
if (!_exiting) SetBusy(false);
|
|
|
|
|
}
|
|
|
|
|
_uplinkChecking = false;
|
|
|
|
|
}
|
|
|
|
|
// Existing authorization enforcement remains active after transport reset;
|
|
|
|
|
// 401/403 still stops the cached connection and clears the session.
|
|
|
|
|
if (refreshAuthorization && _wantConnected && !_exiting) await RefreshProfileAsync(silent: true);
|
|
|
|
|
}
|
|
|
|
|
}
|