change to tls

This commit is contained in:
anlicheng 2026-05-03 20:43:44 +08:00
parent 9817e5a5ca
commit 5b10f7de61
3 changed files with 23 additions and 14 deletions

View File

@ -15,8 +15,6 @@ enum TunnelError: Error {
class PacketTunnelProvider: NEPacketTunnelProvider { class PacketTunnelProvider: NEPacketTunnelProvider {
private var runtimeEnv: SDLRuntimeEnvironment? private var runtimeEnv: SDLRuntimeEnvironment?
override func startTunnel(options: [String: NSObject]?, completionHandler: @escaping (Error?) -> Void) { override func startTunnel(options: [String: NSObject]?, completionHandler: @escaping (Error?) -> Void) {
// //
guard self.runtimeEnv == nil else { guard self.runtimeEnv == nil else {
@ -29,8 +27,6 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
return return
} }
self.runtimeEnv = SDLRuntimeEnvironment(options: options) self.runtimeEnv = SDLRuntimeEnvironment(options: options)
Task { Task {
do { do {
@ -78,11 +74,12 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
} }
override func wake() { override func wake() {
SDLLogger.log("[PacketTunnelProvider] wake up!!!!!!!")
// monitor // monitor
let monitor = SDLPathMonitor() let monitor = SDLPathMonitor()
// //
monitor.start() monitor.start()
SDLLogger.log("[SDLContext] monitor started") SDLLogger.log("[PacketTunnelProvider] monitor started")
// Add code here to wake up. // Add code here to wake up.
Task { Task {
@ -93,7 +90,7 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
// //
_ = await monitor.statusStream().first {$0 == .satisfied} _ = await monitor.statusStream().first {$0 == .satisfied}
SDLLogger.log("[SDLContext] network is satisfied") SDLLogger.log("[PacketTunnelProvider] network is satisfied")
// //
try await self.runtimeEnv?.start(provider: self) try await self.runtimeEnv?.start(provider: self)
} }
@ -161,6 +158,7 @@ private class SDLRuntimeEnvironment {
func stop() async { func stop() async {
await self.contextActor?.stop() await self.contextActor?.stop()
self.contextActor = nil
} }
} }

View File

@ -192,7 +192,7 @@ actor SDLContextActor {
SDLLogger.log("[SDLContext] try start quicClient", for: .debug) SDLLogger.log("[SDLContext] try start quicClient", for: .debug)
// monitor // monitor
let quicClient = SDLQUICClient(host: self.config.serverHost, port: 443) let quicClient = SDLQUICClient(host: self.config.serverHost, port: 1443)
self.quicClient = quicClient self.quicClient = quicClient
quicClient.start() quicClient.start()
@ -275,6 +275,7 @@ actor SDLContextActor {
let error = NSError(domain: "com.jihe.punchnet.tun", code: -3) let error = NSError(domain: "com.jihe.punchnet.tun", code: -3)
self.provider.cancelTunnelWithError(error) self.provider.cancelTunnelWithError(error)
} catch { } catch {
SDLLogger.log("[SDLContext] registerTask: cancel")
return return
} }
} }

View File

@ -46,7 +46,13 @@ final class SDLQUICClient {
private let queue = DispatchQueue(label: "com.sdl.QUICClient.queue") // 线 private let queue = DispatchQueue(label: "com.sdl.QUICClient.queue") // 线
init(host: String, port: UInt16, maxBufferSize: Int = 2 * 1024 * 1024) { init(host: String, port: UInt16, maxBufferSize: Int = 2 * 1024 * 1024) {
let options = NWProtocolQUIC.Options(alpn: ["punchnet/1.0"]) let options = NWProtocolTLS.Options()
sec_protocol_options_add_tls_application_protocol(
options.securityProtocolOptions,
"punchnet/1.0"
)
self.frameParser = SDLQUICFrameParser(maxBufferSize: maxBufferSize) self.frameParser = SDLQUICFrameParser(maxBufferSize: maxBufferSize)
(self.eventStream, self.eventCont) = AsyncStream.makeStream(of: SDLQUICEvent.self) (self.eventStream, self.eventCont) = AsyncStream.makeStream(of: SDLQUICEvent.self)
@ -57,12 +63,16 @@ final class SDLQUICClient {
options.securityProtocolOptions, options.securityProtocolOptions,
{ metadata, trust, complete in { metadata, trust, complete in
// //
complete(QUICVerifier.verify(trust: trust, host: host)) complete(TLSVerifier.verify(trust: trust, host: host))
}, },
self.queue self.queue
) )
SDLLogger.log("[SDLQUICClient] start with tls protocol", for: .debug)
let params = NWParameters(tls: options)
// Network.framework
params.preferNoProxies = true
let params = NWParameters(quic: options)
self.connection = NWConnection(host: .init(host), port: .init(rawValue: port)!, using: params) self.connection = NWConnection(host: .init(host), port: .init(rawValue: port)!, using: params)
} }
@ -266,10 +276,10 @@ extension SDLQUICClient {
} }
} }
// --MARK: quic // --MARK: tls
extension SDLQUICClient { extension SDLQUICClient {
enum QUICVerifier { enum TLSVerifier {
// Base64 // Base64
static let pinnedPublicKeyHashes = [ static let pinnedPublicKeyHashes = [
"Q41r6hbMWEVyxo6heNAH4Wx/TH5NNOWlNif9bewcJ3E=" "Q41r6hbMWEVyxo6heNAH4Wx/TH5NNOWlNif9bewcJ3E="