Force Proxy to any Windows/MacOS/Linux Application TCP and UDP traffic
C ccybersecurityhttp3httpproxylinuxlinux-proxymacosmacos-appmacos-proxyproxifierproxifier-alternativeproxyclienttcpthick-clientudpwindivertwindows
Add "Run at Startup" and "Close to Menu Bar" profile settings; implement logic to manage login item state
AnoF-Cyber57561d60991e
diff --git a/MacOS/ProxyBridge/ProxyBridge/ProxyBridgeGUI.swift b/MacOS/ProxyBridge/ProxyBridge/ProxyBridgeGUI.swiftindex 9bd5fce..65dd42f 100644--- a/MacOS/ProxyBridge/ProxyBridge/ProxyBridgeGUI.swift+++ b/MacOS/ProxyBridge/ProxyBridge/ProxyBridgeGUI.swift@@ -1,5 +1,30 @@import SwiftUIimport UniformTypeIdentifiers+import ServiceManagement++// launch-at-login. the desired state lives in the "runAtStartup" working key so+// it can be part of a profile, and applyToSystem keeps the real login item in sync+enum LoginItem {+ static var isEnabled: Bool { UserDefaults.standard.bool(forKey: "runAtStartup") }++ static func setEnabled(_ enabled: Bool) {+ UserDefaults.standard.set(enabled, forKey: "runAtStartup")+ applyToSystem(enabled)+ }++ static func applyToSystem(_ enabled: Bool) {+ guard #available(macOS 13.0, *) else { return }+ do {+ if enabled, SMAppService.mainApp.status != .enabled {+ try SMAppService.mainApp.register()+ } else if !enabled, SMAppService.mainApp.status == .enabled {+ try SMAppService.mainApp.unregister()+ }+ } catch {+ // registration can fail if the app isn't in /Applications, ignore+ }+ }+}@mainstruct ProxyBridgeGUIApp: App {@@ -84,6 +109,10 @@ struct ProxyBridgeGUIApp: App {CommandMenu("Settings") {Toggle("Close to Menu Bar", isOn: $closeToMenuBar)+ Toggle("Run at Startup", isOn: Binding(+ get: { LoginItem.isEnabled },+ set: { LoginItem.setEnabled($0) }+ ))}CommandGroup(replacing: .help) {@@ -208,6 +237,8 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {static var pendingUpdateInfo: VersionInfo?static weak var shared: AppDelegate?static weak var mainWindow: NSWindow?+ // set at launch, tells WindowAccessor to keep the first window hidden+ static var startHidden = falseprivate var statusItem: NSStatusItem?@@ -216,6 +247,16 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {func applicationDidFinishLaunching(_ notification: Notification) {AppDelegate.shared = selfsetupStatusItem()+ // keep the real login item in sync with the active profile's setting+ LoginItem.applyToSystem(LoginItem.isEnabled)++ // a login-item / resume launch is not a "default" launch. if we got here+ // that way and startup is enabled, come up in the menu bar with no window+ let isDefaultLaunch = notification.userInfo?[NSApplication.launchIsDefaultUserInfoKey] as? Bool ?? true+ if !isDefaultLaunch && LoginItem.isEnabled {+ AppDelegate.startHidden = true+ NSApp.setActivationPolicy(.accessory)+ }}// if close-to-menu-bar is off, quitting on last window close matches the@@ -328,6 +369,11 @@ struct WindowAccessor: NSViewRepresentable {guard let window = view.window else { return }AppDelegate.mainWindow = windowwindow.delegate = AppDelegate.shared+ // launched at login, keep it hidden in the menu bar+ if AppDelegate.startHidden {+ AppDelegate.startHidden = false+ window.orderOut(nil)+ }}return view}diff --git a/MacOS/ProxyBridge/ProxyBridge/ProxyBridgeViewModel.swift b/MacOS/ProxyBridge/ProxyBridge/ProxyBridgeViewModel.swiftindex 0fd5d48..f03091b 100644--- a/MacOS/ProxyBridge/ProxyBridge/ProxyBridgeViewModel.swift+++ b/MacOS/ProxyBridge/ProxyBridge/ProxyBridgeViewModel.swift@@ -168,6 +168,8 @@ class ProxyBridgeViewModel: NSObject, ObservableObject {private func profileConfigsKey(_ name: String) -> String { "profile.\(name).proxyConfigs" }private func profileRulesKey(_ name: String) -> String { "profile.\(name).proxyRules" }private func profileLoggingKey(_ name: String) -> String { "profile.\(name).trafficLoggingEnabled" }+ private func profileCloseKey(_ name: String) -> String { "profile.\(name).closeToMenuBar" }+ private func profileStartupKey(_ name: String) -> String { "profile.\(name).runAtStartup" }private func loadProfiles() {let d = UserDefaults.standard@@ -189,6 +191,8 @@ class ProxyBridgeViewModel: NSObject, ObservableObject {d.set(d.data(forKey: "proxyConfigs"), forKey: profileConfigsKey(name))d.set(d.array(forKey: "proxyRules"), forKey: profileRulesKey(name))d.set(isTrafficLoggingEnabled, forKey: profileLoggingKey(name))+ d.set(d.bool(forKey: "closeToMenuBar"), forKey: profileCloseKey(name))+ d.set(d.bool(forKey: "runAtStartup"), forKey: profileStartupKey(name))}// load a profile's snapshot into the live working keys@@ -201,14 +205,18 @@ class ProxyBridgeViewModel: NSObject, ObservableObject {}d.set(d.array(forKey: profileRulesKey(name)) ?? [], forKey: "proxyRules")// traffic logging is per profile, default on when the snapshot has none- let logging = d.object(forKey: profileLoggingKey(name)) as? Bool ?? true- d.set(logging, forKey: "trafficLoggingEnabled")+ d.set(d.object(forKey: profileLoggingKey(name)) as? Bool ?? true, forKey: "trafficLoggingEnabled")+ // window + startup behaviour are per profile too, default off+ d.set(d.object(forKey: profileCloseKey(name)) as? Bool ?? false, forKey: "closeToMenuBar")+ d.set(d.object(forKey: profileStartupKey(name)) as? Bool ?? false, forKey: "runAtStartup")}// reload in-memory state from the working keys and push it to the extensionprivate func applyActiveProfile() {loadProxyConfig()loadTrafficLoggingSetting()+ // window-close behaviour is read live from the key, only startup needs applying+ LoginItem.applyToSystem(UserDefaults.standard.bool(forKey: "runAtStartup"))if let session = tunnelSession {sendProxyConfigsToExtension(session: session)RuleManager.resyncRules(session: session) { _, _ in }@@ -246,9 +254,13 @@ class ProxyBridgeViewModel: NSObject, ObservableObject {d.set(d.data(forKey: profileConfigsKey(old)), forKey: profileConfigsKey(new))d.set(d.array(forKey: profileRulesKey(old)), forKey: profileRulesKey(new))d.set(d.object(forKey: profileLoggingKey(old)), forKey: profileLoggingKey(new))+ d.set(d.object(forKey: profileCloseKey(old)), forKey: profileCloseKey(new))+ d.set(d.object(forKey: profileStartupKey(old)), forKey: profileStartupKey(new))d.removeObject(forKey: profileConfigsKey(old))d.removeObject(forKey: profileRulesKey(old))d.removeObject(forKey: profileLoggingKey(old))+ d.removeObject(forKey: profileCloseKey(old))+ d.removeObject(forKey: profileStartupKey(old))if let i = profiles.firstIndex(of: old) { profiles[i] = new }d.set(profiles, forKey: "profiles")if activeProfile == old {@@ -267,6 +279,8 @@ class ProxyBridgeViewModel: NSObject, ObservableObject {d.removeObject(forKey: profileConfigsKey(name))d.removeObject(forKey: profileRulesKey(name))d.removeObject(forKey: profileLoggingKey(name))+ d.removeObject(forKey: profileCloseKey(name))+ d.removeObject(forKey: profileStartupKey(name))profiles.removeAll { $0 == name }d.set(profiles, forKey: "profiles")}@@ -282,6 +296,8 @@ class ProxyBridgeViewModel: NSObject, ObservableObject {let dict: [String: Any] = ["name": name,"trafficLoggingEnabled": d.object(forKey: profileLoggingKey(name)) as? Bool ?? true,+ "closeToMenuBar": d.object(forKey: profileCloseKey(name)) as? Bool ?? false,+ "runAtStartup": d.object(forKey: profileStartupKey(name)) as? Bool ?? false,"proxyConfigs": configsJSON,"proxyRules": d.array(forKey: profileRulesKey(name)) ?? []]@@ -307,6 +323,8 @@ class ProxyBridgeViewModel: NSObject, ObservableObject {}d.set(obj["proxyRules"] as? [[String: Any]] ?? [], forKey: profileRulesKey(name))d.set(obj["trafficLoggingEnabled"] as? Bool ?? true, forKey: profileLoggingKey(name))+ d.set(obj["closeToMenuBar"] as? Bool ?? false, forKey: profileCloseKey(name))+ d.set(obj["runAtStartup"] as? Bool ?? false, forKey: profileStartupKey(name))profiles.append(name)d.set(profiles, forKey: "profiles")