diff --git a/Tun/PacketTunnelProvider.swift b/Tun/PacketTunnelProvider.swift index 62ebb1d..950e664 100644 --- a/Tun/PacketTunnelProvider.swift +++ b/Tun/PacketTunnelProvider.swift @@ -28,22 +28,22 @@ class PacketTunnelProvider: NEPacketTunnelProvider { return } - let rsaCipher = try! CCRSACipher(keySize: 1024) - self.runtimeEnv = SDLRuntimeEnvironment(config: config, rsaCipher: rsaCipher, provider: self) - Task { - await self.runtimeEnv?.submitCommand(command: .start(completion: { err in - completionHandler(err) - })) + if self.runtimeEnv == nil { + let rsaCipher = try! CCRSACipher(keySize: 1024) + self.runtimeEnv = SDLRuntimeEnvironment(config: config, rsaCipher: rsaCipher, provider: self) + self.runtimeEnv?.run() } + + self.runtimeEnv?.submitCommand(command: .start(completion: { err in + completionHandler(err) + })) } override func stopTunnel(with reason: NEProviderStopReason, completionHandler: @escaping () -> Void) { // Add code here to start the process of stopping the tunnel. - Task { - await self.runtimeEnv?.submitCommand(command: .stop(completion: { - completionHandler() - })) - } + self.runtimeEnv?.submitCommand(command: .stop(completion: { + completionHandler() + })) } override func handleAppMessage(_ messageData: Data, completionHandler: ((Data?) -> Void)?) { @@ -66,27 +66,23 @@ class PacketTunnelProvider: NEPacketTunnelProvider { override func sleep(completionHandler: @escaping () -> Void) { // Add code here to get ready to sleep. - Task { - await self.runtimeEnv?.submitCommand(command: .stop(completion: { - SDLLogger.log("[PacketTunnelProvider] sleep") - })) - } + self.runtimeEnv?.submitCommand(command: .stop(completion: { + SDLLogger.log("[PacketTunnelProvider] sleep") + })) completionHandler() } override func wake() { SDLLogger.log("[PacketTunnelProvider] wake up!!!!!!!") // Add code here to wake up. - Task { - // 重新启动 - await self.runtimeEnv?.submitCommand(command: .start(completion: { err in - SDLLogger.log("[PacketTunnelProvider] wakeup and try start") - })) - } + // 重新启动 + self.runtimeEnv?.submitCommand(command: .start(completion: { err in + SDLLogger.log("[PacketTunnelProvider] wakeup and try start") + })) } private func handleAppRequest(message: AppRequest) async throws -> Data? { - guard let contextActor = await self.runtimeEnv?.getContextActor() else { + guard let contextActor = self.runtimeEnv?.getContextActor() else { throw TunnelError.invalidContext } diff --git a/Tun/Punchnet/Actors/SDLContextActor.swift b/Tun/Punchnet/Actors/SDLContextActor.swift index 346b2e3..f9cbd04 100644 --- a/Tun/Punchnet/Actors/SDLContextActor.swift +++ b/Tun/Punchnet/Actors/SDLContextActor.swift @@ -34,7 +34,7 @@ actor SDLContextActor { } } - var config: SDLConfiguration + private var config: SDLConfiguration // nat的网络类型 var natType: SDLNATProberActor.NatType = .blocked @@ -119,13 +119,7 @@ actor SDLContextActor { } public func start() async { - await self.startRuntime(resetNotifier: true) - } - - private func startRuntime(resetNotifier: Bool) async { - if resetNotifier { - self.prepareTunnelNotifier() - } + self.prepareTunnelNotifier() // 启动arp的定时清理任务 await self.puncherActor.start() diff --git a/Tun/SDLRuntimeEnvironment.swift b/Tun/SDLRuntimeEnvironment.swift index 4762c1f..74e476d 100644 --- a/Tun/SDLRuntimeEnvironment.swift +++ b/Tun/SDLRuntimeEnvironment.swift @@ -5,12 +5,12 @@ enum SDLRuntimeEnvironmentCommand { case stop(completion: @Sendable () -> Void) } -actor SDLRuntimeEnvironment { +final class SDLRuntimeEnvironment { private enum State { case idle case running } - + private var state: State = .idle private var contextActor: SDLContextActor? @@ -104,14 +104,9 @@ actor SDLRuntimeEnvironment { } } - func shutdown() { + deinit { self.commandCont.finish() self.commandTask?.cancel() self.commandTask = nil } - - deinit { - self.commandCont.finish() - self.commandTask?.cancel() - } }