Sync Zonghengjia iPhone and iPad 1.0.4 source candidate

This commit is contained in:
2026-11-15 14:04:42 +00:00
parent 295799b1ab
commit cecd21fedf
20 changed files with 184 additions and 209 deletions
-38
View File
@@ -1,38 +0,0 @@
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
}
}
+10
View File
@@ -1,6 +1,16 @@
import Foundation
enum NetworkAddress {
static func tunnelUnderlayIPv4(_ remoteAddress: String) -> [String] {
let managementAddress = "47.104.134.190"
if isAuthorizedInternalIPv4(remoteAddress) {
// Preserve the management gateway and the three company relays.
return [remoteAddress, managementAddress, "8.216.53.107", "47.254.94.29", "192.236.188.22"]
}
guard isPublicIPv4(remoteAddress), remoteAddress != managementAddress else { return [managementAddress] }
return [remoteAddress, managementAddress]
}
static func isAuthorizedInternalIPv4(_ value: String) -> Bool {
let parts = value.split(separator: ".", omittingEmptySubsequences: false)
guard parts.count == 4 else { return false }
@@ -1,6 +1,12 @@
import Foundation
enum TunnelConfigurationBuilder {
static let egressProbeURLs = [
URL(string: "https://cp.cloudflare.com/generate_204")!,
URL(string: "https://api.openai.com/v1/models")!,
URL(string: "https://chatgpt.com/api/auth/session")!,
]
static func makeConfiguration(
rawJSON: String,
tunnelFileDescriptor: Int32,
@@ -21,6 +27,7 @@ enum TunnelConfigurationBuilder {
if let resolvedRemoteAddress {
try replaceProxyRemoteAddress(in: &root, with: resolvedRemoteAddress)
}
try addTransportRules(to: &root)
root["inbounds"] = [[
"tag": "tun-in",
"port": 0,
@@ -44,6 +51,33 @@ enum TunnelConfigurationBuilder {
return String(decoding: output, as: UTF8.self)
}
private static func addTransportRules(to root: inout [String: Any]) throws {
guard let outbounds = root["outbounds"] as? [[String: Any]],
outbounds.contains(where: { $0["tag"] as? String == "proxy" }),
var routing = root["routing"] as? [String: Any],
var rules = routing["rules"] as? [[String: Any]]
else {
throw UCVLError.invalidConfiguration("线路配置缺少用于代理出口验证的路由。")
}
// A request to any probe host must use the authorized outbound even
// when the platform's smart routing rules would otherwise send it direct.
rules.insert([
"type": "field",
"domain": egressProbeURLs.compactMap { $0.host }.map { "full:" + $0 },
"port": "443",
"outboundTag": "proxy",
], at: 0)
if outbounds.contains(where: { $0["tag"] as? String == "direct" }) {
rules.insert([
"type": "field",
"domain": [#"regexp:(?i)(^|\.)(tailscale\.com|ts\.net)\.?$"#],
"outboundTag": "direct",
], at: 0)
}
routing["rules"] = rules
root["routing"] = routing
}
static func remoteAddress(from rawJSON: String) -> String? {
guard let data = rawJSON.data(using: .utf8),
let root = try? JSONSerialization.jsonObject(with: data) as? [String: Any],