80 lines
3.6 KiB
C#
80 lines
3.6 KiB
C#
|
|
namespace UCVL.VPNPlus;
|
||
|
|
|
||
|
|
internal sealed partial class MainForm
|
||
|
|
{
|
||
|
|
private bool _bindingRouteModes;
|
||
|
|
private bool _switchingRouteMode;
|
||
|
|
private string? _activeRouteModeId;
|
||
|
|
|
||
|
|
private void InitializeLiveProxyModeSwitch()
|
||
|
|
{
|
||
|
|
_routeCombo.SelectedIndexChanged += async (_, _) =>
|
||
|
|
{
|
||
|
|
if (_bindingRouteModes || _switchingRouteMode || !_selectionUiConnected || !_wantConnected
|
||
|
|
|| !_xray.IsRunning || _routeCombo.SelectedItem is not RouteModeInfo selected
|
||
|
|
|| selected.Id == _activeRouteModeId) return;
|
||
|
|
await SwitchProxyModeAsync(selected);
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
private async Task SwitchProxyModeAsync(RouteModeInfo selected)
|
||
|
|
{
|
||
|
|
if (_accessToken is null || _activeRouteModeId is null || _switchingRouteMode) return;
|
||
|
|
var previous = _routeCombo.Items.Cast<RouteModeInfo>()
|
||
|
|
.FirstOrDefault(mode => mode.Id == _activeRouteModeId);
|
||
|
|
if (previous is null) return;
|
||
|
|
_switchingRouteMode = true;
|
||
|
|
SetBusy(true, "正在切换代理模式,现有连接保持工作…");
|
||
|
|
_statusTitle.Text = "正在切换代理模式";
|
||
|
|
_statusDetail.Text = $"{previous.Name} → {selected.Name};不切换线路";
|
||
|
|
RefreshConnectionAppearance();
|
||
|
|
try
|
||
|
|
{
|
||
|
|
var response = await _api.GetConfigAsync(_accessToken, SingleNodeClientPolicy.FixedNodeSlug,
|
||
|
|
selected.Id, _lifetime.Token);
|
||
|
|
var runtime = SingleNodeClientPolicy.Validate(response, selected.Id);
|
||
|
|
var changed = await _xray.SwitchRouteModeAsync(runtime, selected.Id,
|
||
|
|
SingleNodeClientPolicy.FixedNodeSlug, _lifetime.Token);
|
||
|
|
if (!changed) throw new IOException("新模式未通过验证,已自动恢复原模式。");
|
||
|
|
_activeRouteModeId = selected.Id;
|
||
|
|
SetConnectionState(true, "已安全连接", selected.Name);
|
||
|
|
_notifyIcon.ShowBalloonTip(1600, "代理模式已切换", selected.Name, ToolTipIcon.Info);
|
||
|
|
}
|
||
|
|
catch (RouteModeRollbackFailedException exception)
|
||
|
|
{
|
||
|
|
AppPaths.WriteError(exception);
|
||
|
|
await DisconnectAsync(cancelPending: false);
|
||
|
|
ShowRouteModeRollbackFailure(previous);
|
||
|
|
if (!_exiting && !IsDisposed)
|
||
|
|
MessageBox.Show("代理模式回滚未通过,连接已安全断开。请重新连接。",
|
||
|
|
"连接已安全断开", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
|
|
}
|
||
|
|
catch (Exception exception) when (exception is not OperationCanceledException || !_lifetime.IsCancellationRequested)
|
||
|
|
{
|
||
|
|
AppPaths.WriteError(exception);
|
||
|
|
_bindingRouteModes = true;
|
||
|
|
try { _routeCombo.SelectedItem = previous; }
|
||
|
|
finally { _bindingRouteModes = false; }
|
||
|
|
SetConnectionState(true, "已安全连接", $"{previous.Name} · 切换失败,已保留原模式");
|
||
|
|
if (!_exiting && !IsDisposed)
|
||
|
|
MessageBox.Show(exception.Message, "代理模式未切换", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||
|
|
}
|
||
|
|
finally
|
||
|
|
{
|
||
|
|
_switchingRouteMode = false;
|
||
|
|
SetBusy(false);
|
||
|
|
RefreshSelectionControls();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private void ShowRouteModeRollbackFailure(RouteModeInfo previous)
|
||
|
|
{
|
||
|
|
_wantConnected = false;
|
||
|
|
_activeRouteModeId = null;
|
||
|
|
_bindingRouteModes = true;
|
||
|
|
try { _routeCombo.SelectedItem = previous; }
|
||
|
|
finally { _bindingRouteModes = false; }
|
||
|
|
SetConnectionState(false, "连接状态异常", "代理模式回滚未通过,连接已安全断开;请重新连接");
|
||
|
|
}
|
||
|
|
}
|