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