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
+1 -1
View File
@@ -143,7 +143,7 @@ final class AppState: ObservableObject {
let configJSON = try response.configJSONString()
if !transport.domainOnly,
let endpoint = ProxyTransportPolicy.authorizedInternalEndpoint(response: response) {
let reachable = await InternalIPProbe.isReachable(endpoint.address)
let reachable = await InternalIPProbe.isReachable(endpoint.address, port: endpoint.port)
try Task.checkCancellation()
if reachable {
message = "正在通过内部 IP 连接 \(response.node.displayName)…"
+1 -1
View File
@@ -179,7 +179,7 @@ private struct RegistrationPrompt: View {
var prompt = AttributedString("还没有账号?")
prompt.foregroundColor = UCVLColors.muted
var link = AttributedString("注册新用户")
link.link = URL(string: "https://vpn.example.invalid/register/")
link.link = URL(string: "https://vpn.ucvl.cn/register/")
link.foregroundColor = UCVLColors.royal
link.font = .footnote.weight(.semibold)
prompt.append(link)
+18 -37
View File
@@ -2,20 +2,20 @@ import Darwin
import Foundation
enum InternalIPProbe {
static func isReachable(_ address: String) async -> Bool {
guard NetworkAddress.isAuthorizedInternalIPv4(address), !Task.isCancelled else { return false }
let attempt = PingAttempt()
static func isReachable(_ address: String, port: Int) async -> Bool {
guard NetworkAddress.isAuthorizedInternalIPv4(address), (1...65535).contains(port), !Task.isCancelled else { return false }
let attempt = EndpointAttempt()
return await withTaskCancellationHandler(operation: {
await withCheckedContinuation { continuation in
DispatchQueue.global(qos: .utility).async {
continuation.resume(returning: attempt.run(address))
continuation.resume(returning: attempt.run(address, port: UInt16(port)))
}
}
}, onCancel: { attempt.cancel() })
}
}
private final class PingAttempt: @unchecked Sendable {
private final class EndpointAttempt: @unchecked Sendable {
private let lock = NSLock()
private var cancelled = false
@@ -31,12 +31,11 @@ private final class PingAttempt: @unchecked Sendable {
return cancelled
}
func run(_ address: String) -> Bool {
let deadline = DispatchTime.now().uptimeNanoseconds + 1_000_000_000
func run(_ address: String, port: UInt16) -> Bool {
let deadline = DispatchTime.now().uptimeNanoseconds + 5_000_000_000
guard !isCancelled else { return false }
// Darwin datagram ICMP is unprivileged. Never run a shell, query a VPN
// service or replace ICMP with a TCP/HTTP test.
let descriptor = Darwin.socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP)
// Connect to the exact authorized endpoint using the system route.
let descriptor = Darwin.socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)
guard descriptor >= 0 else { return false }
defer { Darwin.close(descriptor) }
let flags = fcntl(descriptor, F_GETFL, 0)
@@ -45,19 +44,16 @@ private final class PingAttempt: @unchecked Sendable {
var destination = sockaddr_in()
destination.sin_len = UInt8(MemoryLayout<sockaddr_in>.size)
destination.sin_family = sa_family_t(AF_INET)
destination.sin_port = port.bigEndian
guard address.withCString({ inet_pton(AF_INET, $0, &destination.sin_addr) }) == 1 else { return false }
let connected = withUnsafePointer(to: &destination) {
$0.withMemoryRebound(to: sockaddr.self, capacity: 1) {
Darwin.connect(descriptor, $0, socklen_t(MemoryLayout<sockaddr_in>.size))
}
}
guard connected == 0, !isCancelled else { return false }
let payload = Array(UUID().uuidString.utf8)
let packet = ICMPEchoPacket.request(identifier: UInt16.random(in: 1...UInt16.max), payload: payload)
let sent = packet.withUnsafeBytes { Darwin.write(descriptor, $0.baseAddress, $0.count) }
guard sent == packet.count else { return false }
var descriptorState = pollfd(fd: descriptor, events: Int16(POLLIN), revents: 0)
if connected == 0 { return !isCancelled }
guard errno == EINPROGRESS || errno == EINTR else { return false }
var descriptorState = pollfd(fd: descriptor, events: Int16(POLLOUT), revents: 0)
while !isCancelled {
let now = DispatchTime.now().uptimeNanoseconds
guard now < deadline else { return false }
@@ -68,26 +64,11 @@ private final class PingAttempt: @unchecked Sendable {
return false
}
guard ready > 0 else { continue }
guard descriptorState.revents & Int16(POLLIN) != 0 else { return false }
var buffer = [UInt8](repeating: 0, count: 4096)
var sender = sockaddr_in()
var senderSize = socklen_t(MemoryLayout<sockaddr_in>.size)
let received = withUnsafeMutablePointer(to: &sender) { senderPointer in
senderPointer.withMemoryRebound(to: sockaddr.self, capacity: 1) { socketPointer in
buffer.withUnsafeMutableBytes {
Darwin.recvfrom(descriptor, $0.baseAddress, $0.count, 0, socketPointer, &senderSize)
}
}
}
guard received > 0 else {
if errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR { continue }
return false
}
if sender.sin_family == sa_family_t(AF_INET),
sender.sin_addr.s_addr == destination.sin_addr.s_addr,
ICMPEchoPacket.isReply(Array(buffer.prefix(received)), payload: payload) {
return !isCancelled && DispatchTime.now().uptimeNanoseconds < deadline
}
var socketError: Int32 = 0
var size = socklen_t(MemoryLayout<Int32>.size)
return descriptorState.revents & Int16(POLLOUT) != 0
&& getsockopt(descriptor, SOL_SOCKET, SO_ERROR, &socketError, &size) == 0
&& socketError == 0 && !isCancelled && DispatchTime.now().uptimeNanoseconds < deadline
}
return false
}
+27 -20
View File
@@ -4,7 +4,7 @@ import UIKit
actor PlatformAPI {
static let shared = PlatformAPI()
private let baseURL = URL(string: "https://vpn.example.invalid/")!
private let baseURL = URL(string: "https://vpn.ucvl.cn/")!
private let session: URLSession
private let decoder = JSONDecoder()
private let encoder = JSONEncoder()
@@ -16,7 +16,7 @@ actor PlatformAPI {
configuration.waitsForConnectivity = true
configuration.httpAdditionalHeaders = [
"Accept": "application/json",
"User-Agent": "UCVL-Zonghengjia/1.0.3 iOS",
"User-Agent": "UCVL-Zonghengjia/1.0.4 iOS",
]
session = URLSession(configuration: configuration)
}
@@ -27,7 +27,7 @@ actor PlatformAPI {
"password": password,
"device_id": SecureStore.deviceID(),
"device_name": await MainActor.run { UIDevice.current.name },
"app_version": Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "1.0.3",
"app_version": Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "1.0.4",
"platform": "ios",
]
return try await send(
@@ -66,10 +66,6 @@ actor PlatformAPI {
}
func verifyProxyEgress() async throws {
let endpoints = [
URL(string: "https://api.openai.com/v1/models")!,
URL(string: "https://chatgpt.com/api/auth/session")!,
]
let configuration = URLSessionConfiguration.ephemeral
configuration.timeoutIntervalForRequest = 12
configuration.timeoutIntervalForResource = 15
@@ -78,27 +74,38 @@ actor PlatformAPI {
let verifier = URLSession(configuration: configuration)
defer { verifier.invalidateAndCancel() }
for endpoint in endpoints {
var request = URLRequest(url: endpoint)
request.httpMethod = "GET"
request.setValue("no-cache", forHTTPHeaderField: "Cache-Control")
do {
let (_, response) = try await verifier.data(for: request)
if let http = response as? HTTPURLResponse, (200..<500).contains(http.statusCode) {
return
let reachable = await withTaskGroup(of: Bool.self) { probes in
for endpoint in TunnelConfigurationBuilder.egressProbeURLs {
probes.addTask {
var request = URLRequest(url: endpoint)
request.setValue("no-cache", forHTTPHeaderField: "Cache-Control")
do {
let (_, response) = try await verifier.data(for: request)
guard let response = response as? HTTPURLResponse else { return false }
return response.url?.scheme == "https" && response.url?.host == endpoint.host
&& (100...599).contains(response.statusCode)
} catch {
return false
}
}
} catch {
continue
}
for await success in probes where success {
probes.cancelAll()
return true
}
return false
}
try Task.checkCancellation()
guard reachable else {
throw UCVLError.connection("当前入口未通过代理 HTTPS 出口验证。")
}
throw UCVLError.connection("当前节点未通过 OpenAI/ChatGPT 出口验证,正在切换其它节点。")
}
func resolvePublicIPv4(host: String) async throws -> String {
if NetworkAddress.isPublicIPv4(host) { return host }
// 001/002 是后台节点名称。NPM TCP Stream 依靠 11081/11082 等端口
// 分流,不要求每个节点子域名都单独发布 A 记录;解析不到时使用统一入口。
let candidateHosts = host == "vpn.example.invalid" ? [host] : [host, "vpn.example.invalid"]
let candidateHosts = host == "vpn.ucvl.cn" ? [host] : [host, "vpn.ucvl.cn"]
let resolvers = [
"https://dns.alidns.com/resolve",
"https://doh.pub/resolve",
@@ -131,7 +138,7 @@ actor PlatformAPI {
}
}
}
throw UCVLError.connection("无法取得 NPM 入口 vpn.example.invalid 的真实公网 IPv4;已拒绝 Fake-IP/内网地址。")
throw UCVLError.connection("无法取得 NPM 入口 vpn.ucvl.cn 的真实公网 IPv4;已拒绝 Fake-IP/内网地址。")
}
func logout(token: String) async {
+10 -10
View File
@@ -1,10 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.developer.networking.networkextension</key>
<array>
<string>packet-tunnel-provider</string>
</array>
</dict>
</plist>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.developer.networking.networkextension</key>
<array>
<string>packet-tunnel-provider</string>
</array>
</dict>
</plist>
+1
View File
@@ -1,3 +1,4 @@
import Combine
import Foundation
import NetworkExtension