Skip to content

Commit

Permalink
Improve service stop
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Mar 15, 2024
1 parent 95e9bc5 commit 7098a84
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 16 deletions.
2 changes: 1 addition & 1 deletion ApplicationLibrary/Views/Dashboard/OverviewView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions ApplicationLibrary/Views/Dashboard/StartStopButton.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Libbox
import Library
import NetworkExtension
import SwiftUI
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions Library/Network/ExtensionPlatformInterface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
29 changes: 15 additions & 14 deletions Library/Network/ExtensionProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,22 @@ 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())

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()
}

Expand All @@ -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))
}

Expand All @@ -86,15 +82,15 @@ 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
}
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 {
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)")
Expand Down
2 changes: 1 addition & 1 deletion MacLibrary/MenuView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public struct MenuView: View {
}

private nonisolated func serviceReload() async throws {
try LibboxNewStandaloneCommandClient()?.serviceReload()
try LibboxNewStandaloneCommandClient()!.serviceReload()
}
}
}

0 comments on commit 7098a84

Please sign in to comment.