Files
ucvl-zonghengjia-open-source/windows/ApiModels.cs

209 lines
9.9 KiB
C#

using System.Net;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace UCVL.VPNPlus;
internal sealed class LoginRequest
{
[JsonPropertyName("login")] public string Login { get; init; } = "";
[JsonPropertyName("password")] public string Password { get; init; } = "";
[JsonPropertyName("device_id")] public string DeviceId { get; init; } = "";
[JsonPropertyName("device_name")] public string DeviceName { get; init; } = "";
[JsonPropertyName("app_version")] public string AppVersion { get; init; } = "";
}
internal sealed class LoginResponse
{
[JsonPropertyName("access_token")] public string AccessToken { get; init; } = "";
[JsonPropertyName("expires_at")] public DateTimeOffset ExpiresAt { get; init; }
[JsonPropertyName("profile")] public Profile Profile { get; init; } = new();
}
internal sealed class ProfileEnvelope
{
[JsonPropertyName("profile")] public Profile Profile { get; init; } = new();
}
internal sealed class Profile
{
[JsonPropertyName("can_view_node_traffic")] public bool CanViewNodeTraffic { get; init; }
[JsonPropertyName("usage_history_ranges")] public Dictionary<string, UsageHistorySeries>? UsageHistoryRanges { get; init; }
[JsonPropertyName("usage_history")] public List<DailyUsage> UsageHistory { get; init; } = [];
[JsonPropertyName("username")] public string Username { get; init; } = "";
[JsonPropertyName("display_name")] public string DisplayName { get; init; } = "";
[JsonPropertyName("email")] public string Email { get; init; } = "";
[JsonPropertyName("identity_type")] public string IdentityType { get; init; } = "";
[JsonPropertyName("subscription")] public SubscriptionInfo Subscription { get; init; } = new();
[JsonPropertyName("nodes")] public List<NodeInfo> Nodes { get; init; } = [];
// A separate, authenticated grant list; never merge these into public AI dispatch.
// Older servers omit the field, which leaves the released public path unchanged.
[JsonPropertyName("private_auxiliary_nodes")] public List<PrivateAuxiliaryNode> PrivateAuxiliaryNodes { get; init; } = [];
[JsonPropertyName("route_modes")] public List<RouteModeInfo> RouteModes { get; init; } = [];
}
internal sealed class DailyUsage
{
[JsonPropertyName("day")] public string Day { get; init; } = "";
[JsonPropertyName("upload_bytes")] public long UploadBytes { get; init; }
[JsonPropertyName("download_bytes")] public long DownloadBytes { get; init; }
}
internal sealed class SubscriptionInfo
{
[JsonPropertyName("status")] public string Status { get; init; } = "";
[JsonPropertyName("plan")] public string Plan { get; init; } = "";
[JsonPropertyName("used_bytes")] public long UsedBytes { get; init; }
[JsonPropertyName("traffic_limit_bytes")] public long TrafficLimitBytes { get; init; }
[JsonPropertyName("expires_at")] public DateTimeOffset? ExpiresAt { get; init; }
[JsonPropertyName("device_limit")] public int DeviceLimit { get; init; }
}
internal class NodeInfo
{
[JsonPropertyName("slug")] public string Slug { get; init; } = "";
[JsonPropertyName("name")] public string Name { get; init; } = "";
[JsonPropertyName("region")] public string Region { get; init; } = "";
[JsonPropertyName("status")] public string Status { get; init; } = "";
public override string ToString() => $"{Name} · {Region}";
}
internal sealed class PrivateAuxiliaryNode : NodeInfo
{
[JsonPropertyName("probe_ipv4")] public string ProbeIpv4 { get; init; } = "";
[JsonPropertyName("probe_port")] public int ProbePort { get; init; } = 18081;
[JsonPropertyName("connect_address")] public string ConnectAddress { get; init; } = "";
[JsonPropertyName("connection_endpoints")] public List<PrivateConnectionEndpoint> ConnectionEndpoints { get; init; } = [];
internal string EndpointHost => string.IsNullOrEmpty(ConnectAddress) ? ProbeIpv4 : ConnectAddress;
internal PrivateAuxiliaryNode WithEndpoint(PrivateConnectionEndpoint endpoint) => new() {
Slug = Slug, Name = Name, Region = Region, Status = Status, Contract = Contract,
ExpiresAt = ExpiresAt, TlsLeafSha256 = TlsLeafSha256,
ProbeIpv4 = endpoint.ProbeIpv4, ProbePort = endpoint.Port, ConnectAddress = endpoint.Address
};
[JsonPropertyName("tls_leaf_sha256")] public string TlsLeafSha256 { get; init; } = "";
[JsonPropertyName("contract")] public int Contract { get; init; }
[JsonPropertyName("expires_at")] public DateTimeOffset ExpiresAt { get; init; }
public override string ToString() => $"{Name} · 私有辅助";
}
internal sealed class PrivateConnectionEndpoint
{
[JsonPropertyName("address")] public string Address { get; init; } = "";
[JsonPropertyName("probe_ipv4")] public string ProbeIpv4 { get; init; } = "";
[JsonPropertyName("port")] public int Port { get; init; }
}
internal sealed class RouteModeInfo
{
[JsonPropertyName("id")] public string Id { get; init; } = "";
[JsonPropertyName("name")] public string Name { get; init; } = "";
public override string ToString() => Name;
}
internal sealed class ConfigResponse
{
private TailnetEndpoint? _tailscaleEndpoint;
[JsonIgnore] internal bool HasTailscaleEndpoint { get; private set; }
// Missing means an older backend; explicit null is an administrator opt-out.
[JsonPropertyName("tailscale_endpoint")] public TailnetEndpoint? TailscaleEndpoint
{
get => _tailscaleEndpoint;
init { _tailscaleEndpoint = value; HasTailscaleEndpoint = true; }
}
[JsonPropertyName("config_expires_at")] public DateTimeOffset? ConfigExpiresAt { get; init; }
[JsonPropertyName("connection_scope")] public string ConnectionScope { get; init; } = "";
[JsonPropertyName("config_contract")] public int? ConfigContract { get; init; }
[JsonPropertyName("gateway")] public GatewayInfo? Gateway { get; init; }
[JsonPropertyName("node")] public NodeInfo Node { get; init; } = new();
[JsonPropertyName("route_mode")] public string RouteMode { get; init; } = "";
[JsonPropertyName("config")] public JsonElement Config { get; init; }
internal string GetRuntimeConfigJson() =>
ConnectionScope == PrivateAuxiliaryRouting.Scope ? PrivateAuxiliaryRuntime.Prepare(this)
: TunConfigBuilder.OverrideProxyServerAddress(Config.GetRawText(), Gateway?.PreferredAddress());
}
internal sealed class TailnetEndpoint
{
[JsonPropertyName("contract")] public int Contract { get; init; }
[JsonPropertyName("node_slug")] public string NodeSlug { get; init; } = "";
[JsonPropertyName("address")] public string Address { get; init; } = "";
[JsonPropertyName("port")] public int Port { get; init; }
}
internal sealed class GatewayInfo
{
[JsonPropertyName("host")] public string Host { get; init; } = "";
[JsonPropertyName("ipv4")] public string? Ipv4 { get; init; }
internal string? PreferredAddress()
{
if (IPAddress.TryParse(Ipv4, out var address)
&& address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork
&& !IPAddress.IsLoopback(address)
&& IsPublicIpv4(address))
{
return address.ToString();
}
return string.IsNullOrWhiteSpace(Host) ? null : Host.Trim();
}
private static bool IsPublicIpv4(IPAddress address)
{
var bytes = address.GetAddressBytes();
var first = bytes[0];
var second = bytes[1];
if (first == 0 || first == 10 || first == 127 || first >= 224) return false;
if (first == 100 && second >= 64 && second <= 127) return false;
if (first == 169 && second == 254) return false;
if (first == 172 && second >= 16 && second <= 31) return false;
if (first == 192 && second == 168) return false;
if (first == 198 && (second == 18 || second == 19)) return false;
return true;
}
}
internal sealed class ErrorResponse
{
[JsonPropertyName("error")] public string Error { get; init; } = "";
}
internal sealed class ClientUpdateInfo
{
[JsonPropertyName("available")] public bool Available { get; init; }
[JsonPropertyName("version")] public string Version { get; init; } = "";
[JsonPropertyName("download_url")] public string DownloadUrl { get; init; } = "";
[JsonPropertyName("sha256")] public string Sha256 { get; init; } = "";
[JsonPropertyName("size")] public long Size { get; init; }
[JsonPropertyName("notes")] public string Notes { get; init; } = "";
[JsonPropertyName("mandatory")] public bool Mandatory { get; init; }
}
internal sealed class LearningObservation
{
[JsonPropertyName("report_id")] public string ReportId { get; init; } = Guid.NewGuid().ToString("D");
[JsonPropertyName("target_type")] public string TargetType { get; init; } = "domain";
[JsonPropertyName("target")] public string Target { get; init; } = "";
[JsonPropertyName("port")] public int Port { get; init; }
[JsonPropertyName("access_kind")] public string AccessKind { get; init; } = "socket";
[JsonPropertyName("network")] public string Network { get; init; } = "tcp";
[JsonPropertyName("outcome")] public string Outcome { get; init; } = "direct_failed";
[JsonPropertyName("failure_kind")] public string FailureKind { get; init; } = "other";
[JsonPropertyName("scope")] public string Scope { get; init; } = "public";
[JsonPropertyName("evidence_kind")] public string EvidenceKind { get; init; } = "";
[JsonPropertyName("related_report_id")] public string? RelatedReportId { get; init; }
[JsonPropertyName("node")] public string Node { get; init; } = "";
[JsonPropertyName("client_version")] public string ClientVersion { get; init; } = Application.ProductVersion;
}
internal sealed class ObservationEnvelope
{
[JsonPropertyName("observations")] public List<LearningObservation> Observations { get; init; } = [];
}
internal sealed class ApiException : Exception
{
internal int StatusCode { get; }
internal ApiException(int statusCode, string message) : base(message) => StatusCode = statusCode;
}