punchnet-macos/Tun/Punchnet/DNS/DNSCloudClient.swift
2026-04-10 11:19:37 +08:00

108 lines
3.5 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// SDLDNSClient 2.swift
// punchnet
//
// Created by on 2026/4/9.
//
import Foundation
import Network
final class DNSCloudClient {
private var connection: NWConnection?
private let logger: SDLLogger
private let dnsServerAddress: NWEndpoint
// DNS
public let packetFlow: AsyncStream<Data>
private let packetContinuation: AsyncStream<Data>.Continuation
//
private let (closeStream, closeContinuation) = AsyncStream.makeStream(of: Void.self)
/// - Parameter host: sn-server ( "8.8.8.8")
/// - Parameter port: ( 53)
init(host: String, port: UInt16, logger: SDLLogger) {
self.logger = logger
self.dnsServerAddress = .hostPort(host: NWEndpoint.Host(host), port: NWEndpoint.Port(integerLiteral: port))
let (stream, continuation) = AsyncStream.makeStream(of: Data.self, bufferingPolicy: .unbounded)
self.packetFlow = stream
self.packetContinuation = continuation
}
func start() {
// 1.
let parameters = NWParameters.udp
// TUN NE TUN .other
parameters.prohibitedInterfaceTypes = [.other]
// 2. pathSelectionOptions
parameters.multipathServiceType = .handover
// 2.
let connection = NWConnection(to: self.dnsServerAddress, using: parameters)
self.connection = connection
connection.stateUpdateHandler = { [weak self] state in
switch state {
case .ready:
self?.logger.log("[DNSClient] Connection ready", level: .debug)
self?.receiveLoop() //
case .failed(let error):
self?.logger.log("[DNSClient] Connection failed: \(error)", level: .error)
self?.stop()
case .cancelled:
self?.packetContinuation.finish()
self?.closeContinuation.finish()
default:
break
}
}
//
connection.start(queue: .global())
}
public func waitClose() async {
for await _ in closeStream { }
}
///
private func receiveLoop() {
connection?.receiveMessage { [weak self] content, _, isComplete, error in
if let data = content, !data.isEmpty {
// DNS AsyncStream
self?.packetContinuation.yield(data)
}
if error == nil && self?.connection?.state == .ready {
self?.receiveLoop() //
}
}
}
/// DNS TUN IP
func forward(ipPacketData: Data) {
guard let connection = self.connection, connection.state == .ready else {
return
}
connection.send(content: ipPacketData, completion: .contentProcessed { [weak self] error in
if let error = error {
self?.logger.log("[DNSClient] Send error: \(error)", level: .error)
}
})
}
func stop() {
connection?.cancel()
connection = nil
}
deinit {
stop()
}
}