Windows 0.3.6 runtime preflight and update download reliability source delta; connection fixes retained; no private history or other platform changes

This commit is contained in:
2026-11-06 15:28:10 +00:00
parent 23a87387b8
commit ac40dd3a6c
18 changed files with 599 additions and 58 deletions
+16 -18
View File
@@ -75,10 +75,14 @@ internal sealed class XrayManager : IDisposable, IAsyncDisposable, IHandoverRunt
public async Task RebindNetworkAsync(CancellationToken token)
{
ValidateRuntime();
await _lifecycle.WaitAsync(token);
var replacementStarted = false;
try
{
ValidateRuntime();
if (!CanRebindNetwork) throw new InvalidOperationException("没有可恢复的已授权线路。");
replacementStarted = true;
await StopCoreAsync(); // Restore old routes/DNS BEFORE any DNS/network requests.
token.ThrowIfCancellationRequested();
_baseConfigJson = await NodeTransportConnector.RebindAsync(_baseConfigJson!, _tailnetFallbackConfigJson,
@@ -89,7 +93,7 @@ internal sealed class XrayManager : IDisposable, IAsyncDisposable, IHandoverRunt
endpointProbe: TailnetTransport.ProbeEndpointAsync,
diagnostic: entry => AppPaths.WriteLog((entry with { Node = _nodeSlug }).SafeMessage));
}
catch { await StopCoreAsync(); throw; }
catch { if (replacementStarted) await StopCoreAsync(); throw; }
finally { _lifecycle.Release(); }
}
@@ -202,6 +206,8 @@ internal sealed class XrayManager : IDisposable, IAsyncDisposable, IHandoverRunt
internal async Task StartAsync(string configJson, string routeMode, string nodeSlug, CancellationToken cancellationToken,
string? tailnetFallbackConfigJson = null, string? preferredTailnetConfigJson = null)
{
// Fail locally before ownership/recovery or stopping a healthy core.
ValidateRuntime();
if (TailnetTransport.Address(configJson) is not null
&& !TailnetTransport.SameAuthorizedFallback(configJson, tailnetFallbackConfigJson))
throw new InvalidOperationException("Tailscale 原入口与已授权线路不匹配。");
@@ -214,8 +220,12 @@ internal sealed class XrayManager : IDisposable, IAsyncDisposable, IHandoverRunt
await InitializeAsync(cancellationToken);
await StartupLearningWarmup.RunAsync(async beginLearning => {
await _lifecycle.WaitAsync(cancellationToken);
var replacementStarted = false;
try
{
// Recheck after waiting for the lifecycle; a file may change.
ValidateRuntime();
replacementStarted = true;
await StopCoreAsync();
_baseConfigJson = configJson;
_tailnetFallbackConfigJson = preferred is not null ? original : tailnetFallbackConfigJson;
@@ -226,7 +236,7 @@ internal sealed class XrayManager : IDisposable, IAsyncDisposable, IHandoverRunt
}
catch
{
await StopCoreAsync().ConfigureAwait(false);
if (replacementStarted) await StopCoreAsync().ConfigureAwait(false);
throw;
}
finally { _lifecycle.Release(); }
@@ -276,7 +286,7 @@ internal sealed class XrayManager : IDisposable, IAsyncDisposable, IHandoverRunt
private async Task StartCoreAsync(CancellationToken cancellationToken,
Action<CancellationToken>? beginStartupLearning = null)
{
EnsureRuntime();
ValidateRuntime();
if (string.IsNullOrWhiteSpace(_baseConfigJson)) throw new InvalidOperationException("线路配置为空。");
PrivateAuxiliaryRuntime.ValidateLifetime(_baseConfigJson);
var privateAuxiliary = PrivateAuxiliaryRuntime.IsPrivate(_baseConfigJson);
@@ -354,7 +364,7 @@ internal sealed class XrayManager : IDisposable, IAsyncDisposable, IHandoverRunt
_process.ErrorDataReceived += (_, eventArgs) => AppendXrayLog(eventArgs.Data);
var startedProcess = _process;
_process.Exited += (_, _) => _ = HandleCoreExitAsync(startedProcess);
if (!_process.Start()) throw new InvalidOperationException("Xray 内核无法启动。");
if (!ClientRuntime.Start(_process)) throw new ClientRuntimeException(XrayPath, ClientRuntimeFault.CannotStart);
_process.BeginOutputReadLine();
_process.BeginErrorReadLine();
File.WriteAllText(AppPaths.PidFile, _process.Id.ToString());
@@ -557,7 +567,7 @@ internal sealed class XrayManager : IDisposable, IAsyncDisposable, IHandoverRunt
StandardErrorEncoding = Encoding.UTF8,
}
};
process.Start();
ClientRuntime.Start(process);
var stdout = process.StandardOutput.ReadToEndAsync(cancellationToken);
var stderr = process.StandardError.ReadToEndAsync(cancellationToken);
await process.WaitForExitAsync(cancellationToken);
@@ -593,19 +603,7 @@ internal sealed class XrayManager : IDisposable, IAsyncDisposable, IHandoverRunt
return addresses;
}
private void EnsureRuntime()
{
foreach (var file in new[]
{
XrayPath,
Path.Combine(_xrayDirectory, "geoip.dat"),
Path.Combine(_xrayDirectory, "geosite.dat"),
Path.Combine(_xrayDirectory, "wintun.dll"),
})
{
if (!File.Exists(file)) throw new FileNotFoundException($"客户端组件缺失:{Path.GetFileName(file)}");
}
}
internal void ValidateRuntime() => ClientRuntime.Validate(AppContext.BaseDirectory);
private async Task RecoverStaleStateAsync(CancellationToken cancellationToken)
{