iOS 1.0.3 source candidate only; no Apple build or installation acceptance
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
import Foundation
|
||||
|
||||
// A nonce in the payload binds a reply to this request. The kernel may rewrite
|
||||
// the identifier on an unprivileged datagram ICMP socket.
|
||||
enum ICMPEchoPacket {
|
||||
static func checksum(_ bytes: [UInt8]) -> UInt16 {
|
||||
var sum: UInt32 = 0
|
||||
var index = 0
|
||||
while index + 1 < bytes.count {
|
||||
sum += (UInt32(bytes[index]) << 8) | UInt32(bytes[index + 1])
|
||||
index += 2
|
||||
}
|
||||
if index < bytes.count { sum += UInt32(bytes[index]) << 8 }
|
||||
while sum >> 16 != 0 { sum = (sum & 0xffff) + (sum >> 16) }
|
||||
return ~UInt16(sum)
|
||||
}
|
||||
|
||||
static func request(identifier: UInt16, payload: [UInt8]) -> [UInt8] {
|
||||
var bytes: [UInt8] = [8, 0, 0, 0, UInt8(identifier >> 8), UInt8(identifier & 0xff), 0, 1]
|
||||
bytes += payload
|
||||
let value = checksum(bytes)
|
||||
bytes[2] = UInt8(value >> 8)
|
||||
bytes[3] = UInt8(value & 0xff)
|
||||
return bytes
|
||||
}
|
||||
|
||||
static func isReply(_ received: [UInt8], payload: [UInt8]) -> Bool {
|
||||
var offset = 0
|
||||
if let first = received.first, first >> 4 == 4 {
|
||||
offset = Int(first & 0x0f) * 4
|
||||
guard offset >= 20 else { return false }
|
||||
}
|
||||
guard received.count == offset + 8 + payload.count else { return false }
|
||||
let reply = Array(received.dropFirst(offset))
|
||||
return reply[0] == 0 && reply[1] == 0 && reply[6] == 0 && reply[7] == 1
|
||||
&& Array(reply.dropFirst(8)) == payload && checksum(reply) == 0
|
||||
}
|
||||
}
|
||||
@@ -104,6 +104,18 @@ struct VPNNode: Decodable, Identifiable, Hashable, Sendable {
|
||||
let region: String
|
||||
let status: String
|
||||
|
||||
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"
|
||||
}
|
||||
|
||||
var id: String { slug }
|
||||
var displayName: String { region.isEmpty ? name : "\(name) · \(region)" }
|
||||
}
|
||||
@@ -121,6 +133,8 @@ struct ClientConfigResponse: Decodable, Sendable {
|
||||
|
||||
let configContract: Int?
|
||||
let gateway: Gateway?
|
||||
let tailscaleEndpoint: ProxyTransportEndpoint?
|
||||
let connectionScope: String?
|
||||
let node: VPNNode
|
||||
let routeMode: String
|
||||
let config: [String: JSONValue]
|
||||
@@ -128,11 +142,34 @@ struct ClientConfigResponse: Decodable, Sendable {
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case configContract = "config_contract"
|
||||
case gateway
|
||||
case tailscaleEndpoint = "tailscale_endpoint"
|
||||
case connectionScope = "connection_scope"
|
||||
case node
|
||||
case routeMode = "route_mode"
|
||||
case config
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
func configJSONString() throws -> String {
|
||||
let encoder = JSONEncoder()
|
||||
encoder.outputFormatting = [.sortedKeys, .withoutEscapingSlashes]
|
||||
|
||||
@@ -1,6 +1,20 @@
|
||||
import Foundation
|
||||
|
||||
enum NetworkAddress {
|
||||
static func isAuthorizedInternalIPv4(_ value: String) -> Bool {
|
||||
let parts = value.split(separator: ".", omittingEmptySubsequences: false)
|
||||
guard parts.count == 4 else { return false }
|
||||
let octets = parts.compactMap { UInt8($0) }
|
||||
guard octets.count == 4,
|
||||
octets.map({ String($0) }).joined(separator: ".") == value,
|
||||
octets[0] == 100,
|
||||
(64...127).contains(octets[1])
|
||||
else { return false }
|
||||
return value != "100.64.0.0"
|
||||
&& value != "100.127.255.255"
|
||||
&& value != "100.100.100.100"
|
||||
}
|
||||
|
||||
static func isPublicIPv4(_ value: String) -> Bool {
|
||||
let parts = value.split(separator: ".", omittingEmptySubsequences: false)
|
||||
guard parts.count == 4,
|
||||
|
||||
@@ -0,0 +1,124 @@
|
||||
import Foundation
|
||||
|
||||
struct ProxyTransportEndpoint: Codable, Equatable, Sendable {
|
||||
let contract: Int
|
||||
let nodeSlug: String
|
||||
let address: String
|
||||
let port: Int
|
||||
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case contract
|
||||
case nodeSlug = "node_slug"
|
||||
case address
|
||||
case port
|
||||
}
|
||||
}
|
||||
|
||||
// One value belongs to one explicit user connection, including its node retries.
|
||||
// Once public ingress is chosen, automatic recovery has no promotion operation.
|
||||
struct ProxyTransportSession {
|
||||
private(set) var domainOnly = false
|
||||
|
||||
mutating func commitPublicFallback() {
|
||||
domainOnly = true
|
||||
}
|
||||
}
|
||||
|
||||
enum ProxyTransportPolicy {
|
||||
static func authorizedInternalEndpoint(response: ClientConfigResponse) -> ProxyTransportEndpoint? {
|
||||
guard let rawJSON = try? response.configJSONString() else { return nil }
|
||||
return authorizedInternalEndpoint(
|
||||
endpoint: response.tailscaleEndpoint,
|
||||
nodeSlug: response.node.slug,
|
||||
configContract: response.configContract,
|
||||
connectionScope: response.connectionScope,
|
||||
routeMode: response.routeMode,
|
||||
configJSON: rawJSON
|
||||
)
|
||||
}
|
||||
|
||||
// The Packet Tunnel repeats the same gates over its saved original config.
|
||||
static func authorizedInternalEndpoint(
|
||||
endpoint: ProxyTransportEndpoint?,
|
||||
nodeSlug: String,
|
||||
configContract: Int?,
|
||||
connectionScope: String?,
|
||||
routeMode: String,
|
||||
configJSON: String
|
||||
) -> ProxyTransportEndpoint? {
|
||||
guard let endpoint,
|
||||
configContract == 2,
|
||||
connectionScope == nil || connectionScope == "" || connectionScope == "public",
|
||||
routeMode == "smart" || routeMode == "global",
|
||||
!nodeSlug.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty,
|
||||
endpoint.nodeSlug == nodeSlug,
|
||||
isValidEndpoint(endpoint),
|
||||
let root = configurationRoot(configJSON),
|
||||
proxyIndex(in: root) != nil
|
||||
else { return nil }
|
||||
return endpoint
|
||||
}
|
||||
|
||||
// The caller keeps rawJSON for same-node public fallback. Nothing else in
|
||||
// the authorization, Reality identity or routing is reconstructed here.
|
||||
static func internalConfigJSON(rawJSON: String, endpoint: ProxyTransportEndpoint) throws -> String {
|
||||
guard isValidEndpoint(endpoint),
|
||||
var root = configurationRoot(rawJSON),
|
||||
let index = proxyIndex(in: root),
|
||||
var outbounds = root["outbounds"] as? [[String: Any]],
|
||||
var settings = outbounds[index]["settings"] as? [String: Any],
|
||||
var servers = settings["vnext"] as? [[String: Any]]
|
||||
else {
|
||||
throw UCVLError.invalidConfiguration("授权内部入口与节点代理配置不匹配。")
|
||||
}
|
||||
servers[0]["address"] = endpoint.address
|
||||
servers[0]["port"] = endpoint.port
|
||||
settings["vnext"] = servers
|
||||
outbounds[index]["settings"] = settings
|
||||
root["outbounds"] = outbounds
|
||||
let output = try JSONSerialization.data(withJSONObject: root, options: [.sortedKeys])
|
||||
return String(decoding: output, as: UTF8.self)
|
||||
}
|
||||
|
||||
private static func isValidEndpoint(_ endpoint: ProxyTransportEndpoint) -> Bool {
|
||||
endpoint.contract == 1
|
||||
&& !endpoint.nodeSlug.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
|
||||
&& NetworkAddress.isAuthorizedInternalIPv4(endpoint.address)
|
||||
&& (1...65535).contains(endpoint.port)
|
||||
}
|
||||
|
||||
private static func configurationRoot(_ rawJSON: String) -> [String: Any]? {
|
||||
guard let data = rawJSON.data(using: .utf8),
|
||||
let root = try? JSONSerialization.jsonObject(with: data) as? [String: Any],
|
||||
root["ucvl_private_auxiliary"] == nil,
|
||||
root["ucvl_tailnet_transport"] == nil
|
||||
else { return nil }
|
||||
return root
|
||||
}
|
||||
|
||||
private static func proxyIndex(in root: [String: Any]) -> Int? {
|
||||
guard let outbounds = root["outbounds"] as? [[String: Any]] else { return nil }
|
||||
let matches = outbounds.indices.filter { outbounds[$0]["tag"] as? String == "proxy" }
|
||||
guard matches.count == 1, let index = matches.first else { return nil }
|
||||
let proxy = outbounds[index]
|
||||
guard proxy["protocol"] as? String == "vless",
|
||||
let settings = proxy["settings"] as? [String: Any],
|
||||
let servers = settings["vnext"] as? [[String: Any]], servers.count == 1,
|
||||
let users = servers[0]["users"] as? [[String: Any]], !users.isEmpty,
|
||||
users.allSatisfy({ nonemptyString($0["id"]) }),
|
||||
let stream = proxy["streamSettings"] as? [String: Any],
|
||||
stream["security"] as? String == "reality",
|
||||
let network = stream["network"] as? String,
|
||||
network == "tcp" || network == "raw",
|
||||
let reality = stream["realitySettings"] as? [String: Any],
|
||||
nonemptyString(reality["serverName"]),
|
||||
nonemptyString(reality["password"]) || nonemptyString(reality["publicKey"])
|
||||
else { return nil }
|
||||
return index
|
||||
}
|
||||
|
||||
private static func nonemptyString(_ value: Any?) -> Bool {
|
||||
guard let value = value as? String else { return false }
|
||||
return !value.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user