From 1c5bc23a25d71fd436e318c44d8fb855f56b91ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Tue, 9 Apr 2024 12:55:11 +0800 Subject: [PATCH] Fix always on --- .../Views/Dashboard/StartStopButton.swift | 12 +----------- IntentsExtension/Intents.swift | 8 ++++---- Library/Network/ExtensionProfile.swift | 11 +++++++++-- MacLibrary/MenuView.swift | 2 +- 4 files changed, 15 insertions(+), 18 deletions(-) diff --git a/ApplicationLibrary/Views/Dashboard/StartStopButton.swift b/ApplicationLibrary/Views/Dashboard/StartStopButton.swift index 6593817..dd781b9 100644 --- a/ApplicationLibrary/Views/Dashboard/StartStopButton.swift +++ b/ApplicationLibrary/Views/Dashboard/StartStopButton.swift @@ -1,4 +1,3 @@ -import Libbox import Library import NetworkExtension import SwiftUI @@ -81,16 +80,7 @@ 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 - } + try await profile.stop() } } catch { await MainActor.run { diff --git a/IntentsExtension/Intents.swift b/IntentsExtension/Intents.swift index 11a36c8..7f00471 100644 --- a/IntentsExtension/Intents.swift +++ b/IntentsExtension/Intents.swift @@ -38,7 +38,7 @@ struct StartServiceIntent: AppIntent { } try LibboxNewStandaloneCommandClient()!.serviceReload() } else if extensionProfile.status.isConnected { - extensionProfile.stop() + try await extensionProfile.stop() try await Task.sleep(nanoseconds: UInt64(100 * Double(NSEC_PER_MSEC))) try await extensionProfile.start() } else { @@ -65,7 +65,7 @@ struct RestartServiceIntent: AppIntent { if extensionProfile.status == .connected { try LibboxNewStandaloneCommandClient()!.serviceReload() } else if extensionProfile.status.isConnected { - extensionProfile.stop() + try await extensionProfile.stop() try await Task.sleep(nanoseconds: UInt64(100 * Double(NSEC_PER_MSEC))) try await extensionProfile.start() } else { @@ -89,7 +89,7 @@ struct StopServiceIntent: AppIntent { guard let extensionProfile = try await (ExtensionProfile.load()) else { return .result() } - extensionProfile.stop() + try await extensionProfile.stop() return .result() } } @@ -109,7 +109,7 @@ struct ToggleServiceIntent: AppIntent { return .result(value: false) } if extensionProfile.status.isConnected { - extensionProfile.stop() + try await extensionProfile.stop() return .result(value: false) } else { diff --git a/Library/Network/ExtensionProfile.swift b/Library/Network/ExtensionProfile.swift index 0a043e0..e0f82f3 100644 --- a/Library/Network/ExtensionProfile.swift +++ b/Library/Network/ExtensionProfile.swift @@ -1,4 +1,5 @@ import Foundation +import Libbox import NetworkExtension public class ExtensionProfile: ObservableObject { @@ -75,8 +76,14 @@ public class ExtensionProfile: ObservableObject { try manager.connection.startVPNTunnel() } - public func stop() { - manager.isOnDemandEnabled = false + public func stop() async throws { + if manager.isOnDemandEnabled { + manager.isOnDemandEnabled = false + try await manager.saveToPreferences() + } + do { + try LibboxNewStandaloneCommandClient()!.serviceClose() + } catch {} manager.connection.stopVPNTunnel() } diff --git a/MacLibrary/MenuView.swift b/MacLibrary/MenuView.swift index b28e5f3..ea9764d 100644 --- a/MacLibrary/MenuView.swift +++ b/MacLibrary/MenuView.swift @@ -96,7 +96,7 @@ public struct MenuView: View { if isEnabled { try await profile.start() } else { - profile.stop() + try await profile.stop() } } catch { alert = Alert(error)