2026-10-11 23:23:20 +00:00
|
|
|
import Foundation
|
|
|
|
|
|
|
|
|
|
enum JSONValue: Codable, Sendable, Equatable {
|
|
|
|
|
case string(String)
|
|
|
|
|
case integer(Int64)
|
|
|
|
|
case number(Double)
|
|
|
|
|
case object([String: JSONValue])
|
|
|
|
|
case array([JSONValue])
|
|
|
|
|
case bool(Bool)
|
|
|
|
|
case null
|
|
|
|
|
|
|
|
|
|
init(from decoder: Decoder) throws {
|
|
|
|
|
let container = try decoder.singleValueContainer()
|
|
|
|
|
if container.decodeNil() {
|
|
|
|
|
self = .null
|
|
|
|
|
} else if let value = try? container.decode(Bool.self) {
|
|
|
|
|
self = .bool(value)
|
|
|
|
|
} else if let value = try? container.decode(Int64.self) {
|
|
|
|
|
self = .integer(value)
|
|
|
|
|
} else if let value = try? container.decode(Double.self) {
|
|
|
|
|
self = .number(value)
|
|
|
|
|
} else if let value = try? container.decode(String.self) {
|
|
|
|
|
self = .string(value)
|
|
|
|
|
} else if let value = try? container.decode([String: JSONValue].self) {
|
|
|
|
|
self = .object(value)
|
|
|
|
|
} else if let value = try? container.decode([JSONValue].self) {
|
|
|
|
|
self = .array(value)
|
|
|
|
|
} else {
|
|
|
|
|
throw DecodingError.dataCorruptedError(in: container, debugDescription: "Unsupported JSON value")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func encode(to encoder: Encoder) throws {
|
|
|
|
|
var container = encoder.singleValueContainer()
|
|
|
|
|
switch self {
|
|
|
|
|
case .string(let value): try container.encode(value)
|
|
|
|
|
case .integer(let value): try container.encode(value)
|
|
|
|
|
case .number(let value): try container.encode(value)
|
|
|
|
|
case .object(let value): try container.encode(value)
|
|
|
|
|
case .array(let value): try container.encode(value)
|
|
|
|
|
case .bool(let value): try container.encode(value)
|
|
|
|
|
case .null: try container.encodeNil()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct LoginResponse: Decodable, Sendable {
|
|
|
|
|
let accessToken: String
|
|
|
|
|
let expiresAt: String
|
|
|
|
|
let profile: ClientProfile
|
|
|
|
|
|
|
|
|
|
enum CodingKeys: String, CodingKey {
|
|
|
|
|
case accessToken = "access_token"
|
|
|
|
|
case expiresAt = "expires_at"
|
|
|
|
|
case profile
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct ProfileEnvelope: Decodable, Sendable {
|
|
|
|
|
let profile: ClientProfile
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct ClientProfile: Decodable, Sendable {
|
|
|
|
|
let username: String
|
|
|
|
|
let displayName: String
|
|
|
|
|
let email: String
|
|
|
|
|
let identityType: String
|
|
|
|
|
let subscription: SubscriptionProfile
|
|
|
|
|
let nodes: [VPNNode]
|
|
|
|
|
let routeModes: [RouteMode]
|
|
|
|
|
|
|
|
|
|
enum CodingKeys: String, CodingKey {
|
|
|
|
|
case username
|
|
|
|
|
case displayName = "display_name"
|
|
|
|
|
case email
|
|
|
|
|
case identityType = "identity_type"
|
|
|
|
|
case subscription
|
|
|
|
|
case nodes
|
|
|
|
|
case routeModes = "route_modes"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct SubscriptionProfile: Decodable, Sendable {
|
|
|
|
|
let status: String
|
|
|
|
|
let plan: String
|
|
|
|
|
let usedBytes: Int64
|
|
|
|
|
let trafficLimitBytes: Int64
|
|
|
|
|
let expiresAt: String?
|
|
|
|
|
let deviceLimit: Int
|
|
|
|
|
|
|
|
|
|
enum CodingKeys: String, CodingKey {
|
|
|
|
|
case status
|
|
|
|
|
case plan
|
|
|
|
|
case usedBytes = "used_bytes"
|
|
|
|
|
case trafficLimitBytes = "traffic_limit_bytes"
|
|
|
|
|
case expiresAt = "expires_at"
|
|
|
|
|
case deviceLimit = "device_limit"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct VPNNode: Decodable, Identifiable, Hashable, Sendable {
|
|
|
|
|
let slug: String
|
|
|
|
|
let name: String
|
|
|
|
|
let region: String
|
|
|
|
|
let status: String
|
|
|
|
|
|
2026-10-27 02:46:08 +00:00
|
|
|
enum CodingKeys: String, CodingKey { case slug, name, region, status }
|
|
|
|
|
|
|
|
|
|
init(from decoder: Decoder) throws {
|
|
|
|
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
|
|
|
|
slug = try container.decode(String.self, forKey: .slug)
|
|
|
|
|
name = try container.decode(String.self, forKey: .name)
|
|
|
|
|
region = try container.decode(String.self, forKey: .region)
|
|
|
|
|
// Config responses omit the profile-only node status. Absence is not
|
|
|
|
|
// evidence of offline/online state and must not break a valid config.
|
|
|
|
|
status = try container.decodeIfPresent(String.self, forKey: .status) ?? "unknown"
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-11 23:23:20 +00:00
|
|
|
var id: String { slug }
|
|
|
|
|
var displayName: String { region.isEmpty ? name : "\(name) · \(region)" }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct RouteMode: Decodable, Identifiable, Hashable, Sendable {
|
|
|
|
|
let id: String
|
|
|
|
|
let name: String
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct ClientConfigResponse: Decodable, Sendable {
|
|
|
|
|
struct Gateway: Decodable, Sendable {
|
|
|
|
|
let host: String
|
|
|
|
|
let ipv4: String?
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let configContract: Int?
|
|
|
|
|
let gateway: Gateway?
|
2026-10-27 02:46:08 +00:00
|
|
|
let tailscaleEndpoint: ProxyTransportEndpoint?
|
|
|
|
|
let connectionScope: String?
|
2026-10-11 23:23:20 +00:00
|
|
|
let node: VPNNode
|
|
|
|
|
let routeMode: String
|
|
|
|
|
let config: [String: JSONValue]
|
|
|
|
|
|
|
|
|
|
enum CodingKeys: String, CodingKey {
|
|
|
|
|
case configContract = "config_contract"
|
|
|
|
|
case gateway
|
2026-10-27 02:46:08 +00:00
|
|
|
case tailscaleEndpoint = "tailscale_endpoint"
|
|
|
|
|
case connectionScope = "connection_scope"
|
2026-10-11 23:23:20 +00:00
|
|
|
case node
|
|
|
|
|
case routeMode = "route_mode"
|
|
|
|
|
case config
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-27 02:46:08 +00:00
|
|
|
init(from decoder: Decoder) throws {
|
|
|
|
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
|
|
|
|
configContract = try container.decodeIfPresent(Int.self, forKey: .configContract)
|
|
|
|
|
gateway = try container.decodeIfPresent(Gateway.self, forKey: .gateway)
|
|
|
|
|
node = try container.decode(VPNNode.self, forKey: .node)
|
|
|
|
|
routeMode = try container.decode(String.self, forKey: .routeMode)
|
|
|
|
|
config = try container.decode([String: JSONValue].self, forKey: .config)
|
|
|
|
|
|
|
|
|
|
// Optional transport hints never make a valid public configuration fail.
|
|
|
|
|
// A malformed scope also disables the hint: it is not a legacy absence.
|
|
|
|
|
let scopeIsNull = (try? container.decodeNil(forKey: .connectionScope)) ?? true
|
|
|
|
|
let scopePresent = container.contains(.connectionScope) && !scopeIsNull
|
|
|
|
|
let decodedScope = try? container.decode(String.self, forKey: .connectionScope)
|
|
|
|
|
connectionScope = decodedScope
|
|
|
|
|
if scopePresent && decodedScope == nil {
|
|
|
|
|
tailscaleEndpoint = nil
|
|
|
|
|
} else {
|
|
|
|
|
tailscaleEndpoint = try? container.decode(ProxyTransportEndpoint.self, forKey: .tailscaleEndpoint)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-11 23:23:20 +00:00
|
|
|
func configJSONString() throws -> String {
|
|
|
|
|
let encoder = JSONEncoder()
|
|
|
|
|
encoder.outputFormatting = [.sortedKeys, .withoutEscapingSlashes]
|
|
|
|
|
return String(decoding: try encoder.encode(config), as: UTF8.self)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct APIErrorEnvelope: Decodable {
|
|
|
|
|
let error: String?
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enum UCVLError: LocalizedError {
|
|
|
|
|
case api(status: Int, message: String)
|
|
|
|
|
case invalidResponse
|
|
|
|
|
case invalidConfiguration(String)
|
|
|
|
|
case vpnPermission(String)
|
|
|
|
|
case connection(String)
|
|
|
|
|
|
|
|
|
|
var errorDescription: String? {
|
|
|
|
|
switch self {
|
|
|
|
|
case .api(_, let message), .invalidConfiguration(let message), .vpnPermission(let message), .connection(let message):
|
|
|
|
|
return message
|
|
|
|
|
case .invalidResponse:
|
|
|
|
|
return "平台返回了无法识别的数据。"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|