Fix always on

This commit is contained in:
世界
2024-04-10 20:48:15 +08:00
parent debd0ca1a2
commit 1c5bc23a25
4 changed files with 15 additions and 18 deletions
@@ -1,4 +1,3 @@
import Libbox
import Library import Library
import NetworkExtension import NetworkExtension
import SwiftUI import SwiftUI
@@ -81,16 +80,7 @@ public struct StartStopButton: View {
try await profile.start() try await profile.start()
await environments.logClient.connect() await environments.logClient.connect()
} else { } else {
var err: Error? try await profile.stop()
do {
try LibboxNewStandaloneCommandClient()!.serviceClose()
} catch {
err = error
}
await profile.stop()
if let err {
throw err
}
} }
} catch { } catch {
await MainActor.run { await MainActor.run {
+4 -4
View File
@@ -38,7 +38,7 @@ struct StartServiceIntent: AppIntent {
} }
try LibboxNewStandaloneCommandClient()!.serviceReload() try LibboxNewStandaloneCommandClient()!.serviceReload()
} else if extensionProfile.status.isConnected { } else if extensionProfile.status.isConnected {
extensionProfile.stop() try await extensionProfile.stop()
try await Task.sleep(nanoseconds: UInt64(100 * Double(NSEC_PER_MSEC))) try await Task.sleep(nanoseconds: UInt64(100 * Double(NSEC_PER_MSEC)))
try await extensionProfile.start() try await extensionProfile.start()
} else { } else {
@@ -65,7 +65,7 @@ struct RestartServiceIntent: AppIntent {
if extensionProfile.status == .connected { if extensionProfile.status == .connected {
try LibboxNewStandaloneCommandClient()!.serviceReload() try LibboxNewStandaloneCommandClient()!.serviceReload()
} else if extensionProfile.status.isConnected { } else if extensionProfile.status.isConnected {
extensionProfile.stop() try await extensionProfile.stop()
try await Task.sleep(nanoseconds: UInt64(100 * Double(NSEC_PER_MSEC))) try await Task.sleep(nanoseconds: UInt64(100 * Double(NSEC_PER_MSEC)))
try await extensionProfile.start() try await extensionProfile.start()
} else { } else {
@@ -89,7 +89,7 @@ struct StopServiceIntent: AppIntent {
guard let extensionProfile = try await (ExtensionProfile.load()) else { guard let extensionProfile = try await (ExtensionProfile.load()) else {
return .result() return .result()
} }
extensionProfile.stop() try await extensionProfile.stop()
return .result() return .result()
} }
} }
@@ -109,7 +109,7 @@ struct ToggleServiceIntent: AppIntent {
return .result(value: false) return .result(value: false)
} }
if extensionProfile.status.isConnected { if extensionProfile.status.isConnected {
extensionProfile.stop() try await extensionProfile.stop()
return .result(value: false) return .result(value: false)
} else { } else {
+9 -2
View File
@@ -1,4 +1,5 @@
import Foundation import Foundation
import Libbox
import NetworkExtension import NetworkExtension
public class ExtensionProfile: ObservableObject { public class ExtensionProfile: ObservableObject {
@@ -75,8 +76,14 @@ public class ExtensionProfile: ObservableObject {
try manager.connection.startVPNTunnel() try manager.connection.startVPNTunnel()
} }
public func stop() { public func stop() async throws {
manager.isOnDemandEnabled = false if manager.isOnDemandEnabled {
manager.isOnDemandEnabled = false
try await manager.saveToPreferences()
}
do {
try LibboxNewStandaloneCommandClient()!.serviceClose()
} catch {}
manager.connection.stopVPNTunnel() manager.connection.stopVPNTunnel()
} }
+1 -1
View File
@@ -96,7 +96,7 @@ public struct MenuView: View {
if isEnabled { if isEnabled {
try await profile.start() try await profile.start()
} else { } else {
profile.stop() try await profile.stop()
} }
} catch { } catch {
alert = Alert(error) alert = Alert(error)