Use verified company gateway for authorized Tailscale nodes
This commit is contained in:
+36
-12
@@ -32,7 +32,7 @@ internal sealed class XrayManager : IDisposable, IAsyncDisposable, IHandoverRunt
|
||||
// A public fallback is sticky: automatic physical-network recovery must
|
||||
// never promote it to this cached internal alternative.
|
||||
private string? _preferredTailnetConfigJson;
|
||||
private string? _tailnetInterfaceName;
|
||||
private TailnetTransport.RouteBinding? _tailnetBinding;
|
||||
internal bool UsesTailnet => _baseConfigJson is not null && TailnetTransport.Address(_baseConfigJson) is not null;
|
||||
internal (string Address, int Port)? TailnetProbeEndpoint
|
||||
{
|
||||
@@ -56,6 +56,18 @@ internal sealed class XrayManager : IDisposable, IAsyncDisposable, IHandoverRunt
|
||||
return (TailnetTransport.Address(preferred)!, TailnetTransport.Port(preferred));
|
||||
}
|
||||
}
|
||||
internal async Task<bool> ProbeCurrentOrPreferredTailnetAsync(CancellationToken token)
|
||||
{
|
||||
if (TailnetProbeEndpoint is not { } endpoint) return false;
|
||||
if (!UsesTailnet)
|
||||
return await TailnetTransport.ProbeAuthorizedRouteAsync(endpoint.Address, endpoint.Port, token) is not null;
|
||||
|
||||
var binding = _tailnetBinding;
|
||||
// For an active session, verify the path used by Xray. A healthy
|
||||
// alternate interface must not conceal failure of the current one.
|
||||
return binding is not null
|
||||
&& await TailnetTransport.ProbeBoundEndpointAsync(endpoint.Address, endpoint.Port, binding, token);
|
||||
}
|
||||
private string _routeMode = "smart";
|
||||
private string _nodeSlug = "";
|
||||
private string XrayPath => Path.Combine(_xrayDirectory, "xray.exe");
|
||||
@@ -89,12 +101,20 @@ internal sealed class XrayManager : IDisposable, IAsyncDisposable, IHandoverRunt
|
||||
replacementStarted = true;
|
||||
await StopCoreAsync(); // Restore old routes/DNS BEFORE any DNS/network requests.
|
||||
token.ThrowIfCancellationRequested();
|
||||
TailnetTransport.RouteBinding? binding = null;
|
||||
_baseConfigJson = await NodeTransportConnector.RebindAsync(_baseConfigJson!, _tailnetFallbackConfigJson,
|
||||
async (config, rebindToken) => { _baseConfigJson = config; await StartCoreAsync(rebindToken); },
|
||||
async (config, rebindToken) =>
|
||||
{
|
||||
_baseConfigJson = config;
|
||||
await StartCoreAsync(rebindToken, tailnetBinding: TailnetTransport.Address(config) is null ? null : binding);
|
||||
},
|
||||
StopCoreAsync, CheckProxyHealthAsync, token,
|
||||
preferredTailnetConfig: _preferredTailnetConfigJson,
|
||||
requireRoute: address => { TailnetTransport.RequireExistingRoute(address); },
|
||||
endpointProbe: TailnetTransport.ProbeEndpointAsync,
|
||||
endpointProbe: async (address, port, probeToken) =>
|
||||
{
|
||||
binding = await TailnetTransport.ProbeAuthorizedRouteAsync(address, port, probeToken);
|
||||
return binding is not null;
|
||||
},
|
||||
diagnostic: entry => AppPaths.WriteLog((entry with { Node = _nodeSlug }).SafeMessage),
|
||||
verifyPublicEgress: _routeMode is "smart" or "global" && !PrivateAuxiliaryRuntime.IsPrivate(_baseConfigJson!));
|
||||
}
|
||||
@@ -209,7 +229,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)
|
||||
string? tailnetFallbackConfigJson = null, string? preferredTailnetConfigJson = null,
|
||||
TailnetTransport.RouteBinding? tailnetBinding = null)
|
||||
{
|
||||
// Fail locally before ownership/recovery or stopping a healthy core.
|
||||
ValidateRuntime();
|
||||
@@ -237,7 +258,7 @@ internal sealed class XrayManager : IDisposable, IAsyncDisposable, IHandoverRunt
|
||||
_preferredTailnetConfigJson = preferred;
|
||||
_routeMode = routeMode;
|
||||
_nodeSlug = nodeSlug;
|
||||
await StartCoreAsync(cancellationToken, routeMode == "smart" ? beginLearning : null);
|
||||
await StartCoreAsync(cancellationToken, routeMode == "smart" ? beginLearning : null, tailnetBinding);
|
||||
}
|
||||
catch
|
||||
{
|
||||
@@ -264,7 +285,7 @@ internal sealed class XrayManager : IDisposable, IAsyncDisposable, IHandoverRunt
|
||||
_baseConfigJson = null;
|
||||
_tailnetFallbackConfigJson = null;
|
||||
_preferredTailnetConfigJson = null;
|
||||
_tailnetInterfaceName = null;
|
||||
_tailnetBinding = null;
|
||||
_nodeSlug = "";
|
||||
_boundNetwork = null;
|
||||
}
|
||||
@@ -289,7 +310,7 @@ internal sealed class XrayManager : IDisposable, IAsyncDisposable, IHandoverRunt
|
||||
}
|
||||
|
||||
private async Task StartCoreAsync(CancellationToken cancellationToken,
|
||||
Action<CancellationToken>? beginStartupLearning = null)
|
||||
Action<CancellationToken>? beginStartupLearning = null, TailnetTransport.RouteBinding? tailnetBinding = null)
|
||||
{
|
||||
ValidateRuntime();
|
||||
if (string.IsNullOrWhiteSpace(_baseConfigJson)) throw new InvalidOperationException("线路配置为空。");
|
||||
@@ -299,10 +320,12 @@ internal sealed class XrayManager : IDisposable, IAsyncDisposable, IHandoverRunt
|
||||
var preservePrivateRoute = privateAuxiliary && PrivateAuxiliaryRuntime.Address(_baseConfigJson) != PrivateAuxiliaryRouting.PublicPhoneHost;
|
||||
if (preservePrivateRoute) PrivateAuxiliaryRuntime.RequireExistingRoute(PrivateAuxiliaryRuntime.Address(_baseConfigJson));
|
||||
var tailnetAddress = TailnetTransport.Address(_baseConfigJson);
|
||||
var tailnetInterface = tailnetAddress is null ? null : TailnetTransport.RequireExistingRoute(tailnetAddress);
|
||||
_tailnetInterfaceName = tailnetInterface;
|
||||
var proxyServerAddresses = await ResolveProxyServerAddressesAsync(_baseConfigJson, cancellationToken);
|
||||
var physicalNetwork = TunRouting.GetPhysicalNetwork(proxyServerAddresses);
|
||||
_tailnetBinding = tailnetAddress is null ? null : TailnetTransport.RequireCurrentBinding(tailnetAddress, tailnetBinding, physicalNetwork);
|
||||
var tailnetInterface = _tailnetBinding?.InterfaceName;
|
||||
if (_tailnetBinding?.Gateway is not null)
|
||||
AppPaths.WriteLog(new TransportDiagnostic("tailnet_route", "authorized_port_reached_via_gateway", _nodeSlug).SafeMessage);
|
||||
_learningEpoch = new CancellationTokenSource();
|
||||
_learningBudget = new LearningProbeBudget();
|
||||
_networkLearned.Clear();
|
||||
@@ -389,7 +412,8 @@ internal sealed class XrayManager : IDisposable, IAsyncDisposable, IHandoverRunt
|
||||
|
||||
try
|
||||
{
|
||||
await TunRouting.EnableAsync(physicalNetwork, proxyServerAddresses, cancellationToken, preservePrivateRoute, tailnetAddress);
|
||||
await TunRouting.EnableAsync(physicalNetwork, proxyServerAddresses, cancellationToken,
|
||||
preservePrivateRoute, tailnetAddress, _tailnetBinding);
|
||||
// Apply 0.2.5 routing first, then wait until Windows has completed
|
||||
// DAD before applications can use the tunnel's DNS/IPv4 path.
|
||||
await TunRouting.WaitForAdapterAsync(cancellationToken, requireIpv4Ready: true);
|
||||
@@ -534,7 +558,7 @@ internal sealed class XrayManager : IDisposable, IAsyncDisposable, IHandoverRunt
|
||||
var config = LearningPolicy.Merge(_directFirstConfig, new RoutingManifest { Contract = 1,
|
||||
Revision = new string('0', 64), Rules = _networkLearned.Targets.ToList() });
|
||||
var routing = TunConfigBuilder.BuildLearningRouting(config, _boundNetwork.InterfaceName,
|
||||
_learningProbePort, _directProbePort, _tailnetInterfaceName);
|
||||
_learningProbePort, _directProbePort, _tailnetBinding?.InterfaceName);
|
||||
await XrayRoutingUpdater.ApplyAsync(XrayPath, _routingApiPort, routing, token);
|
||||
_networkLearned.Applied();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user