Use verified company gateway for authorized Tailscale nodes

This commit is contained in:
UCVL Release
2026-11-16 10:37:17 +08:00
parent eb90ef4042
commit 16ac8227ea
7 changed files with 166 additions and 38 deletions
+37
View File
@@ -1,3 +1,4 @@
using System.Net;
using System.Text.Json;
using System.Text.Json.Nodes;
@@ -74,6 +75,42 @@ internal static class TailnetTransportSelfTest
checks["ambiguous_interface_refused"] = TailnetTransport.SelectRoute("100.99.142.38", [route,route with {InterfaceIndex=9}],usable) is null;
checks["noncontiguous_mask_refused"] = TailnetTransport.SelectRoute("100.99.142.38", [route with {Mask="255.0.255.0"}],usable) is null;
checks["unrelated_route_refused"] = TailnetTransport.SelectRoute("100.99.142.38", [route with {Destination="172.16.0.0",Mask="255.255.0.0"}],usable) is null;
const string target = "100.99.142.38";
const int targetPort = 10803;
var native = new TailnetTransport.RouteBinding("Tailscale", 34, IPAddress.Parse("100.65.4.8"));
var office = new TunRouting.PhysicalNetwork("Ethernet", 9, IPAddress.Parse("172.16.1.154"),
24, IPAddress.Parse("172.16.1.1"), [IPAddress.Parse("172.16.1.1")]);
var gateway = new TailnetTransport.RouteBinding(office.InterfaceName, office.InterfaceIndex,
office.Ipv4Address, office.Ipv4Gateway);
TailnetTransport.RouteBinding NoSpecificRoute(string _) => throw new InvalidOperationException();
PhysicalUplink.Reading OfficeUplink() => new(true, office);
var testedBindings = new List<TailnetTransport.RouteBinding>();
Task<bool> Reachable(string address, int port, TailnetTransport.RouteBinding binding, CancellationToken token)
{
token.ThrowIfCancellationRequested();
testedBindings.Add(binding);
return Task.FromResult(address == target && port == targetPort);
}
checks["native_route_precedes_gateway"] = await TailnetTransport.ProbeAuthorizedRouteAsync(target, targetPort,
CancellationToken.None, _ => native, () => throw new InvalidOperationException("Unused uplink"), Reachable) == native
&& testedBindings.SequenceEqual([native]);
testedBindings.Clear();
checks["company_gateway_requires_authorized_port_probe"] = await TailnetTransport.ProbeAuthorizedRouteAsync(target, targetPort,
CancellationToken.None, NoSpecificRoute, OfficeUplink, Reachable) == gateway
&& testedBindings.SequenceEqual([gateway]);
checks["unreachable_company_gateway_falls_back"] = await TailnetTransport.ProbeAuthorizedRouteAsync(target, targetPort,
CancellationToken.None, NoSpecificRoute, OfficeUplink, (_, _, _, _) => Task.FromResult(false)) is null;
checks["no_gateway_no_unbound_probe"] = await TailnetTransport.ProbeAuthorizedRouteAsync(target, targetPort,
CancellationToken.None, NoSpecificRoute, () => new(true, office with { Ipv4Gateway = IPAddress.Any }),
(_, _, _, _) => throw new InvalidOperationException("Unbound probe")) is null;
checks["non_tailnet_address_never_probed"] = await TailnetTransport.ProbeAuthorizedRouteAsync("198.51.100.20", targetPort,
CancellationToken.None, _ => throw new InvalidOperationException("Unexpected route lookup"), OfficeUplink, Reachable) is null;
var routerRuntime = JsonNode.Parse(TunConfigBuilder.Build(prepared, office.InterfaceName, false, includeTun: false,
tailnetInterfaceName: gateway.InterfaceName))!.AsObject();
checks["router_runtime_binds_only_chosen_physical_interface"] =
routerRuntime["outbounds"]![0]!["streamSettings"]!["sockopt"]!["interface"]!.GetValue<string>() == office.InterfaceName
&& Host(prepared) == target;
var preference = new TailnetPreference();
long tick = 100;