Publish Zonghengjia Windows 0.4.0 client source
This commit is contained in:
@@ -280,6 +280,60 @@ internal sealed class XrayManager : IDisposable, IAsyncDisposable, IHandoverRunt
|
||||
AppPaths.WriteLog(new TransportDiagnostic("connection_phase", "runtime_ready", _nodeSlug).SafeMessage);
|
||||
}
|
||||
|
||||
internal async Task<bool> SwitchRouteModeAsync(string replacementConfigJson, string replacementMode,
|
||||
string nodeSlug, CancellationToken token)
|
||||
{
|
||||
if (replacementMode is not ("smart" or "global"))
|
||||
throw new InvalidOperationException("不支持的代理模式。");
|
||||
SingleNodeClientPolicy.ValidateRuntime(replacementConfigJson);
|
||||
await _lifecycle.WaitAsync(token).ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
if (!IsRunning || _baseConfigJson is null || _boundNetwork is null || _routingApiPort == 0)
|
||||
throw new IOException("当前代理内核未就绪,保留原模式。");
|
||||
if (_nodeSlug != SingleNodeClientPolicy.FixedNodeSlug || nodeSlug != _nodeSlug
|
||||
|| !SingleNodeClientPolicy.SameProxyIdentity(_baseConfigJson, replacementConfigJson))
|
||||
throw new InvalidOperationException("新旧模式不是同一默认公网代理,已拒绝切换。");
|
||||
if (_routeMode == replacementMode) return true;
|
||||
|
||||
var oldBase = _baseConfigJson;
|
||||
var oldDirect = _directFirstConfig ?? oldBase;
|
||||
var oldEffective = _routeMode == "smart" && _networkLearned.Count > 0
|
||||
? LearningPolicy.Merge(oldDirect, new RoutingManifest { Contract = 1,
|
||||
Revision = new string('0', 64), Rules = _networkLearned.Targets.ToList() })
|
||||
: oldDirect;
|
||||
var replacementDirect = replacementMode == "smart"
|
||||
? LearningPolicy.DirectFirst(replacementConfigJson) : replacementConfigJson;
|
||||
var replacementRouting = TunConfigBuilder.BuildLearningRouting(replacementDirect,
|
||||
_boundNetwork.InterfaceName, _learningProbePort, _directProbePort, tailnetInterfaceName: null);
|
||||
var rollbackRouting = TunConfigBuilder.BuildLearningRouting(oldEffective,
|
||||
_boundNetwork.InterfaceName, _learningProbePort, _directProbePort, tailnetInterfaceName: null);
|
||||
|
||||
var changed = await LiveProxyModeSwitch.ApplyAsync(replacementRouting, rollbackRouting,
|
||||
(routing, applyToken) => XrayRoutingUpdater.ApplyAsync(XrayPath, _routingApiPort, routing, applyToken),
|
||||
async verifyToken => await CheckProxyHealthAsync(verifyToken).ConfigureAwait(false)
|
||||
&& await CheckHealthAsync(verifyToken).ConfigureAwait(false), token).ConfigureAwait(false);
|
||||
if (!changed) return false;
|
||||
|
||||
_learningEpoch.Cancel();
|
||||
_learningEpoch.Dispose();
|
||||
_learningEpoch = new CancellationTokenSource();
|
||||
_learningBudget = new LearningProbeBudget();
|
||||
_networkLearned.Clear();
|
||||
CachedLearningRevision = "";
|
||||
_learningCandidates = [];
|
||||
_baseConfigJson = replacementConfigJson;
|
||||
_directFirstConfig = replacementDirect;
|
||||
_routeMode = replacementMode;
|
||||
_tailnetFallbackConfigJson = null;
|
||||
_preferredTailnetConfigJson = null;
|
||||
_tailnetBinding = null;
|
||||
AppPaths.WriteLog(new TransportDiagnostic("route_mode", "hot_switch_verified", _nodeSlug).SafeMessage);
|
||||
return true;
|
||||
}
|
||||
finally { _lifecycle.Release(); }
|
||||
}
|
||||
|
||||
internal async Task StopAsync()
|
||||
{
|
||||
await _lifecycle.WaitAsync().ConfigureAwait(false);
|
||||
|
||||
Reference in New Issue
Block a user