163 lines
4.6 KiB
Swift
163 lines
4.6 KiB
Swift
|
|
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
|
||
|
|
|
||
|
|
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?
|
||
|
|
let node: VPNNode
|
||
|
|
let routeMode: String
|
||
|
|
let config: [String: JSONValue]
|
||
|
|
|
||
|
|
enum CodingKeys: String, CodingKey {
|
||
|
|
case configContract = "config_contract"
|
||
|
|
case gateway
|
||
|
|
case node
|
||
|
|
case routeMode = "route_mode"
|
||
|
|
case config
|
||
|
|
}
|
||
|
|
|
||
|
|
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 "平台返回了无法识别的数据。"
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|