Publish reviewed history-free open-source snapshot (2026-10-12)
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
using System.Net;
|
||||
using System.Net.NetworkInformation;
|
||||
using System.Text.Json.Nodes;
|
||||
|
||||
namespace UCVL.VPNPlus;
|
||||
|
||||
// Narrow adapter for the Zero3 web/AI TCP relay. Never changes public-node DNS.
|
||||
internal static class PrivateAuxiliaryRuntime
|
||||
{
|
||||
internal const string Marker = "ucvl_private_auxiliary";
|
||||
|
||||
internal static string Prepare(ConfigResponse response)
|
||||
{
|
||||
var root = JsonNode.Parse(response.Config.GetRawText())!.AsObject();
|
||||
root[Marker] = new JsonObject {
|
||||
["expires_at"] = response.ConfigExpiresAt!.Value.ToString("O"),
|
||||
["address"] = EndpointAddress(root)
|
||||
};
|
||||
return root.ToJsonString();
|
||||
}
|
||||
|
||||
internal static bool IsPrivate(JsonObject root) => root[Marker] is JsonObject;
|
||||
internal static bool IsPrivate(string json) => IsPrivate(JsonNode.Parse(json)!.AsObject());
|
||||
|
||||
private static string EndpointAddress(JsonObject root)
|
||||
{
|
||||
var proxies = root["outbounds"]!.AsArray().Where(o => o?["tag"]?.GetValue<string>() == "proxy").ToArray();
|
||||
if (proxies.Length != 1 || proxies[0]?["settings"]?["servers"] is not JsonArray servers || servers.Count != 1
|
||||
|| servers[0]?["address"]?.GetValue<string>() is not { } address
|
||||
|| !PrivateAuxiliaryRouting.IsRegisteredEndpoint(address)
|
||||
|| address == PrivateAuxiliaryRouting.PublicPhoneAddress
|
||||
|| servers[0]?["port"]?.GetValue<int>() != (address == PrivateAuxiliaryRouting.PublicPhoneHost ? 11080 : 18081))
|
||||
throw new InvalidOperationException("私有节点登记入口不匹配。");
|
||||
return address;
|
||||
}
|
||||
|
||||
internal static string Address(string json) => EndpointAddress(JsonNode.Parse(json)!.AsObject());
|
||||
|
||||
internal static bool SameConnection(string oldJson, string newJson)
|
||||
{
|
||||
var oldRoot = JsonNode.Parse(oldJson)!.AsObject(); var newRoot = JsonNode.Parse(newJson)!.AsObject();
|
||||
if (!IsPrivate(oldRoot) || !IsPrivate(newRoot)) return false;
|
||||
oldRoot.Remove(Marker); newRoot.Remove(Marker);
|
||||
return JsonNode.DeepEquals(oldRoot, newRoot);
|
||||
}
|
||||
|
||||
internal static void ValidateLifetime(string json)
|
||||
{
|
||||
var root = JsonNode.Parse(json)!.AsObject();
|
||||
if (!IsPrivate(root)) return;
|
||||
if (root[Marker]?["address"]?.GetValue<string>() != EndpointAddress(root)
|
||||
|| !DateTimeOffset.TryParse(root[Marker]?["expires_at"]?.GetValue<string>(), out var expiry)
|
||||
|| expiry <= DateTimeOffset.UtcNow || expiry > DateTimeOffset.UtcNow.AddMinutes(15))
|
||||
throw new InvalidOperationException("私有节点设备授权已过期,请重新连接。");
|
||||
}
|
||||
|
||||
internal static void RequireExistingRoute(string address = PrivateAuxiliaryRouting.JinanAddress)
|
||||
{
|
||||
var usable = NetworkInterface.GetAllNetworkInterfaces()
|
||||
.Where(n => n.OperationalStatus == OperationalStatus.Up
|
||||
&& n.NetworkInterfaceType != NetworkInterfaceType.Loopback
|
||||
&& !n.Name.StartsWith(TunRouting.AdapterName, StringComparison.OrdinalIgnoreCase))
|
||||
.Select(n => n.GetIPProperties().GetIPv4Properties()?.Index).Where(n => n.HasValue)
|
||||
.Select(n => n!.Value).ToHashSet();
|
||||
if (!HasPreservedRoute(new WindowsRouteTable().Snapshot(), usable, address))
|
||||
throw new InvalidOperationException("未找到可保留的济南办网络路径,未修改局域网或 Tailscale 路由。");
|
||||
}
|
||||
|
||||
internal static bool HasPreservedRoute(IEnumerable<Ipv4Route> routes, IReadOnlySet<int> usable,
|
||||
string address = PrivateAuxiliaryRouting.JinanAddress)
|
||||
{
|
||||
// A user-supplied route longer than /1 wins over this client's two TUN /1s.
|
||||
// Do NOT manufacture a /32 via the default Ethernet gateway: that breaks
|
||||
// Tailscale reachability and transfers network management away from user.
|
||||
if (!PrivateAuxiliaryRouting.IsRegisteredEndpoint(address)) return false;
|
||||
if (address == PrivateAuxiliaryRouting.PhoneTailnetAddress)
|
||||
return TailnetTransport.SelectRoute(address, routes, usable) is not null;
|
||||
var target = IPAddress.Parse(address).GetAddressBytes();
|
||||
return routes.Any(r => {
|
||||
if (!usable.Contains(r.InterfaceIndex)
|
||||
|| !IPAddress.TryParse(r.Mask, out var mask) || mask.GetAddressBytes().Length != 4
|
||||
|| !IPAddress.TryParse(r.Destination, out var dest) || dest.GetAddressBytes().Length != 4)
|
||||
return false;
|
||||
var m = mask.GetAddressBytes(); var d = dest.GetAddressBytes();
|
||||
var prefix = 0; var sawZero = false;
|
||||
foreach (var b in m)
|
||||
for (var bit = 7; bit >= 0; bit--) {
|
||||
var one = (b & (1 << bit)) != 0;
|
||||
if (one && sawZero) return false;
|
||||
if (one) prefix++; else sawZero = true;
|
||||
}
|
||||
return prefix > 1 && Enumerable.Range(0, 4).All(i => (target[i] & m[i]) == (d[i] & m[i]));
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user