Skip to content

Force Proxy to any Windows/MacOS/Linux Application TCP and UDP traffic

#51 - Fixed system extension keeps memory after GUI closes

Sourav Kalal80c5bd50cad4
diff --git a/MacOS/ProxyBridge/ProxyBridge/ProxyBridgeGUI.swift b/MacOS/ProxyBridge/ProxyBridge/ProxyBridgeGUI.swift
index 69c2905..52590be 100644
--- a/MacOS/ProxyBridge/ProxyBridge/ProxyBridgeGUI.swift
+++ b/MacOS/ProxyBridge/ProxyBridge/ProxyBridgeGUI.swift
@@ -112,8 +112,16 @@ class AppDelegate: NSObject, NSApplicationDelegate {
static var viewModel: ProxyBridgeViewModel?
static var pendingUpdateInfo: VersionInfo?
- func applicationWillTerminate(_ notification: Notification) {
+ func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply {
+ // Clear extension memory before app quits
AppDelegate.viewModel?.stopProxy()
+
+ // Give time for memory clearing to complete
+ DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
+ NSApp.reply(toApplicationShouldTerminate: true)
+ }
+
+ return .terminateLater
}
@objc func openProxySettings() {
diff --git a/MacOS/ProxyBridge/ProxyBridge/ProxyBridgeViewModel.swift b/MacOS/ProxyBridge/ProxyBridge/ProxyBridgeViewModel.swift
index f420695..0ba66ec 100644
--- a/MacOS/ProxyBridge/ProxyBridge/ProxyBridgeViewModel.swift
+++ b/MacOS/ProxyBridge/ProxyBridge/ProxyBridgeViewModel.swift
@@ -189,15 +189,45 @@ class ProxyBridgeViewModel: NSObject, ObservableObject {
}
func stopProxy() {
- NETransparentProxyManager.loadAllFromPreferences { [weak self] managers, error in
+ guard let session = tunnelSession else {
+ isProxyActive = false
+ logTimer?.invalidate()
+ logTimer = nil
+ return
+ }
+
+ // Clear all data from extension memory before stopping
+ clearExtensionMemory(session: session) { [weak self] in
guard let self = self else { return }
- if let manager = managers?.first {
- (manager.connection as? NETunnelProviderSession)?.stopTunnel()
- self.isProxyActive = false
- self.logTimer?.invalidate()
- self.logTimer = nil
- self.addLog("INFO", "Proxy stopped")
+ NETransparentProxyManager.loadAllFromPreferences { managers, error in
+ if let manager = managers?.first {
+ (manager.connection as? NETunnelProviderSession)?.stopTunnel()
+ self.isProxyActive = false
+ self.logTimer?.invalidate()
+ self.logTimer = nil
+ self.tunnelSession = nil
+ self.addLog("INFO", "Proxy stopped and extension memory cleared")
+ }
+ }
+ }
+ }
+
+ private func clearExtensionMemory(session: NETunnelProviderSession, completion: @escaping () -> Void) {
+ // clear rules auto fix the #51 - and proxy rules become inactive after It closes
+ RuleManager.clearRules(session: session) { success, message in
+ //clear proxy config as well keep meory usage low for extesion
+ let clearConfigMessage: [String: Any] = [
+ "action": "clearConfig"
+ ]
+
+ guard let data = try? JSONSerialization.data(withJSONObject: clearConfigMessage) else {
+ completion()
+ return
+ }
+
+ try? session.sendProviderMessage(data) { _ in
+ completion()
}
}
}