diff --git a/ApplicationLibrary/Views/Dashboard/OverviewView.swift b/ApplicationLibrary/Views/Dashboard/OverviewView.swift index 7258ebf..059fd75 100644 --- a/ApplicationLibrary/Views/Dashboard/OverviewView.swift +++ b/ApplicationLibrary/Views/Dashboard/OverviewView.swift @@ -98,7 +98,7 @@ public struct OverviewView: View { } private nonisolated func serviceReload() async throws { - try LibboxNewStandaloneCommandClient()?.serviceReload() + try LibboxNewStandaloneCommandClient()!.serviceReload() } private nonisolated func setSystemProxyEnabled(_ isEnabled: Bool) async { diff --git a/ApplicationLibrary/Views/Dashboard/StartStopButton.swift b/ApplicationLibrary/Views/Dashboard/StartStopButton.swift index b42a253..6593817 100644 --- a/ApplicationLibrary/Views/Dashboard/StartStopButton.swift +++ b/ApplicationLibrary/Views/Dashboard/StartStopButton.swift @@ -1,3 +1,4 @@ +import Libbox import Library import NetworkExtension import SwiftUI @@ -80,7 +81,16 @@ public struct StartStopButton: View { try await profile.start() await environments.logClient.connect() } else { + var err: Error? + do { + try LibboxNewStandaloneCommandClient()!.serviceClose() + } catch { + err = error + } await profile.stop() + if let err { + throw err + } } } catch { await MainActor.run { diff --git a/Library/Network/ExtensionPlatformInterface.swift b/Library/Network/ExtensionPlatformInterface.swift index 97f6d8e..2623e4b 100644 --- a/Library/Network/ExtensionPlatformInterface.swift +++ b/Library/Network/ExtensionPlatformInterface.swift @@ -262,6 +262,11 @@ public class ExtensionPlatformInterface: NSObject, LibboxPlatformInterfaceProtoc } } + public func postServiceClose() { + reset() + tunnel.postServiceClose() + } + public func getSystemProxyStatus() -> LibboxSystemProxyStatus? { let status = LibboxSystemProxyStatus() guard let networkSettings else { diff --git a/Library/Network/ExtensionProvider.swift b/Library/Network/ExtensionProvider.swift index 764100b..41e11fd 100644 --- a/Library/Network/ExtensionProvider.swift +++ b/Library/Network/ExtensionProvider.swift @@ -31,7 +31,7 @@ open class ExtensionProvider: NEPacketTunnelProvider { var error: NSError? LibboxRedirectStderr(FilePath.cacheDirectory.appendingPathComponent("stderr.log").relativePath, &error) if let error { - writeError("(packet-tunnel) redirect stderr error: \(error.localizedDescription)") + writeFatalError("(packet-tunnel) redirect stderr error: \(error.localizedDescription)") } await LibboxSetMemoryLimit(!SharedPreferences.ignoreMemoryLimit.get()) @@ -39,14 +39,14 @@ open class ExtensionProvider: NEPacketTunnelProvider { if platformInterface == nil { platformInterface = ExtensionPlatformInterface(self) } - commandServer = try await LibboxNewCommandServer(platformInterface, Int32(SharedPreferences.maxLogLines.get())) + commandServer = await LibboxNewCommandServer(platformInterface, Int32(SharedPreferences.maxLogLines.get())) do { try commandServer.start() } catch { writeFatalError("(packet-tunnel): log server start error: \(error.localizedDescription)") return } - writeMessage("(packet-tunnel) log server started") + writeMessage("(packet-tunnel): Here I stand") await startService() } @@ -58,17 +58,13 @@ open class ExtensionProvider: NEPacketTunnelProvider { } } - func writeError(_ message: String) { - writeMessage(message) - var error: NSError? - LibboxWriteServiceError(message, &error) - } - public func writeFatalError(_ message: String) { #if DEBUG NSLog(message) #endif - writeError(message) + writeMessage(message) + var error: NSError? + LibboxWriteServiceError(message, &error) cancelTunnelWithError(NSError(domain: message, code: 0)) } @@ -86,7 +82,7 @@ open class ExtensionProvider: NEPacketTunnelProvider { } let configContent: String do { - configContent = try await profile.read() + configContent = try profile.read() } catch { writeFatalError("(packet-tunnel) error: read config file \(profile.path): \(error.localizedDescription)") return @@ -94,7 +90,7 @@ open class ExtensionProvider: NEPacketTunnelProvider { var error: NSError? let service = LibboxNewService(configContent, platformInterface, &error) if let error { - writeError("(packet-tunnel) error: create service: \(error.localizedDescription)") + writeFatalError("(packet-tunnel) error: create service: \(error.localizedDescription)") return } guard let service else { @@ -105,7 +101,7 @@ open class ExtensionProvider: NEPacketTunnelProvider { try service.start() } catch { commandServer.setService(nil) - writeError("(packet-tunnel) error: start service: \(error.localizedDescription)") + writeFatalError("(packet-tunnel) error: start service: \(error.localizedDescription)") return } boxService = service @@ -119,7 +115,7 @@ open class ExtensionProvider: NEPacketTunnelProvider { do { try service.close() } catch { - writeError("(packet-tunnel) error: stop service: \(error.localizedDescription)") + writeMessage("(packet-tunnel) error: stop service: \(error.localizedDescription)") } boxService = nil commandServer.setService(nil) @@ -136,8 +132,13 @@ open class ExtensionProvider: NEPacketTunnelProvider { reasserting = false } stopService() + commandServer.resetLog() await startService() } + + func postServiceClose() { + boxService = nil + } override open func stopTunnel(with reason: NEProviderStopReason) async { writeMessage("(packet-tunnel) stopping, reason: \(reason)") diff --git a/MacLibrary/MenuView.swift b/MacLibrary/MenuView.swift index ee35ff5..b28e5f3 100644 --- a/MacLibrary/MenuView.swift +++ b/MacLibrary/MenuView.swift @@ -201,7 +201,7 @@ public struct MenuView: View { } private nonisolated func serviceReload() async throws { - try LibboxNewStandaloneCommandClient()?.serviceReload() + try LibboxNewStandaloneCommandClient()!.serviceReload() } } }