Publish reviewed history-free open-source snapshot (2026-10-12)
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
import Foundation
|
||||
|
||||
enum ConnectionAttemptDecision: Equatable {
|
||||
case retryNode
|
||||
case stop
|
||||
case revokeAuthorization
|
||||
}
|
||||
|
||||
enum ConnectionAttemptPolicy {
|
||||
static func decide(error: Error, hasAssignedNode: Bool) -> ConnectionAttemptDecision {
|
||||
if error is CancellationError { return .stop }
|
||||
if let failure = error as? UCVLError {
|
||||
switch failure {
|
||||
case .api(let status, _):
|
||||
return status == 401 || status == 403 ? .revokeAuthorization : .stop
|
||||
case .vpnPermission, .invalidConfiguration, .invalidResponse:
|
||||
// Permission, malformed local configuration and control-plane
|
||||
// failures must not mark every otherwise healthy node as failed.
|
||||
return .stop
|
||||
case .connection:
|
||||
break
|
||||
}
|
||||
}
|
||||
return hasAssignedNode ? .retryNode : .stop
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,162 @@
|
||||
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 "平台返回了无法识别的数据。"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import Foundation
|
||||
|
||||
enum NetworkAddress {
|
||||
static func isPublicIPv4(_ value: String) -> Bool {
|
||||
let parts = value.split(separator: ".", omittingEmptySubsequences: false)
|
||||
guard parts.count == 4,
|
||||
let a = UInt8(parts[0]),
|
||||
let b = UInt8(parts[1]),
|
||||
let c = UInt8(parts[2]),
|
||||
UInt8(parts[3]) != nil
|
||||
else { return false }
|
||||
|
||||
if a == 0 || a == 10 || a == 127 || a >= 224 { return false }
|
||||
if a == 100 && (64...127).contains(b) { return false }
|
||||
if a == 169 && b == 254 { return false }
|
||||
if a == 172 && (16...31).contains(b) { return false }
|
||||
if a == 192 && b == 168 { return false }
|
||||
if a == 192 && b == 0 && (c == 0 || c == 2) { return false }
|
||||
if a == 198 && (b == 18 || b == 19) { return false }
|
||||
if a == 198 && b == 51 && c == 100 { return false }
|
||||
if a == 203 && b == 0 && c == 113 { return false }
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
import Foundation
|
||||
|
||||
enum TunnelConfigurationBuilder {
|
||||
static func makeConfiguration(
|
||||
rawJSON: String,
|
||||
tunnelFileDescriptor: Int32,
|
||||
assetDirectory: String,
|
||||
resolvedRemoteAddress: String? = nil
|
||||
) throws -> String {
|
||||
guard let data = rawJSON.data(using: .utf8),
|
||||
var root = try JSONSerialization.jsonObject(with: data) as? [String: Any]
|
||||
else {
|
||||
throw UCVLError.invalidConfiguration("线路配置不是有效的 Xray JSON。")
|
||||
}
|
||||
|
||||
var environment = root["env"] as? [String: Any] ?? [:]
|
||||
environment["xray.tun.fd"] = String(tunnelFileDescriptor)
|
||||
environment["xray.location.asset"] = assetDirectory
|
||||
root["env"] = environment
|
||||
root["metrics"] = NSNull()
|
||||
if let resolvedRemoteAddress {
|
||||
try replaceProxyRemoteAddress(in: &root, with: resolvedRemoteAddress)
|
||||
}
|
||||
root["inbounds"] = [[
|
||||
"tag": "tun-in",
|
||||
"port": 0,
|
||||
"protocol": "tun",
|
||||
"settings": [
|
||||
"name": "ucvl-tun",
|
||||
"mtu": 1500,
|
||||
"userLevel": 0,
|
||||
],
|
||||
"sniffing": [
|
||||
"enabled": true,
|
||||
"destOverride": ["http", "tls", "quic"],
|
||||
"routeOnly": true,
|
||||
],
|
||||
]]
|
||||
|
||||
guard JSONSerialization.isValidJSONObject(root) else {
|
||||
throw UCVLError.invalidConfiguration("无法生成 Apple Packet Tunnel 配置。")
|
||||
}
|
||||
let output = try JSONSerialization.data(withJSONObject: root, options: [.sortedKeys])
|
||||
return String(decoding: output, as: UTF8.self)
|
||||
}
|
||||
|
||||
static func remoteAddress(from rawJSON: String) -> String? {
|
||||
guard let data = rawJSON.data(using: .utf8),
|
||||
let root = try? JSONSerialization.jsonObject(with: data) as? [String: Any],
|
||||
let outbounds = root["outbounds"] as? [[String: Any]],
|
||||
let proxy = outbounds.first(where: { ($0["tag"] as? String) == "proxy" }),
|
||||
let settings = proxy["settings"] as? [String: Any],
|
||||
let vnext = settings["vnext"] as? [[String: Any]],
|
||||
let address = vnext.first?["address"] as? String,
|
||||
!address.isEmpty
|
||||
else { return nil }
|
||||
return address
|
||||
}
|
||||
|
||||
private static func replaceProxyRemoteAddress(in root: inout [String: Any], with address: String) throws {
|
||||
guard var outbounds = root["outbounds"] as? [[String: Any]],
|
||||
let proxyIndex = outbounds.firstIndex(where: { ($0["tag"] as? String) == "proxy" }),
|
||||
var settings = outbounds[proxyIndex]["settings"] as? [String: Any],
|
||||
var vnext = settings["vnext"] as? [[String: Any]],
|
||||
!vnext.isEmpty
|
||||
else {
|
||||
throw UCVLError.invalidConfiguration("线路配置缺少代理服务器地址。")
|
||||
}
|
||||
vnext[0]["address"] = address
|
||||
settings["vnext"] = vnext
|
||||
outbounds[proxyIndex]["settings"] = settings
|
||||
root["outbounds"] = outbounds
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user