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 } }