Skip to content

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

added rule toggle method

Sourav Kalal5d5c09389cff
diff --git a/MacOS/ProxyBridge/ProxyBridge/ProxyRulesView.swift b/MacOS/ProxyBridge/ProxyBridge/ProxyRulesView.swift
index 943af9f..26dfb8c 100644
--- a/MacOS/ProxyBridge/ProxyBridge/ProxyRulesView.swift
+++ b/MacOS/ProxyBridge/ProxyBridge/ProxyRulesView.swift
@@ -201,14 +201,9 @@ struct ProxyRulesView: View {
private func toggleRule(_ rule: ProxyRule, enabled: Bool) {
guard let session = viewModel.tunnelSession else { return }
- RuleManager.updateRule(
+ RuleManager.toggleRule(
session: session,
ruleId: rule.id,
- processNames: rule.processNames,
- targetHosts: rule.targetHosts,
- targetPorts: rule.targetPorts,
- protocol: rule.ruleProtocol,
- action: rule.action,
enabled: enabled
) { _, _ in
loadRules()
diff --git a/MacOS/ProxyBridge/ProxyBridge/RuleManager.swift b/MacOS/ProxyBridge/ProxyBridge/RuleManager.swift
index 2cd5d6f..92d7555 100644
--- a/MacOS/ProxyBridge/ProxyBridge/RuleManager.swift
+++ b/MacOS/ProxyBridge/ProxyBridge/RuleManager.swift
@@ -100,6 +100,42 @@ struct RuleManager {
}
}
+
+ static func toggleRule(
+ session: NETunnelProviderSession,
+ ruleId: UInt32,
+ enabled: Bool,
+ completion: @escaping (Bool, String) -> Void
+ ) {
+ let message: [String: Any] = [
+ "action": "toggleRule",
+ "ruleId": ruleId,
+ "enabled": enabled
+ ]
+
+ guard let data = try? JSONSerialization.data(withJSONObject: message) else {
+ completion(false, "Failed to serialize message")
+ return
+ }
+
+ try? session.sendProviderMessage(data) { response in
+ guard let responseData = response,
+ let result = try? JSONSerialization.jsonObject(with: responseData) as? [String: Any],
+ let status = result["status"] as? String else {
+ completion(false, "No response from extension")
+ return
+ }
+
+ if status == "ok" {
+ completion(true, "Rule #\(ruleId) \(enabled ? "enabled" : "disabled")")
+ } else if let message = result["message"] as? String {
+ completion(false, "Error: \(message)")
+ } else {
+ completion(false, "Unknown error")
+ }
+ }
+ }
+
// Send removeRule command
static func removeRule(
session: NETunnelProviderSession,
diff --git a/MacOS/ProxyBridge/extension/AppProxyProvider.swift b/MacOS/ProxyBridge/extension/AppProxyProvider.swift
index 75d8d25..a302d60 100644
--- a/MacOS/ProxyBridge/extension/AppProxyProvider.swift
+++ b/MacOS/ProxyBridge/extension/AppProxyProvider.swift
@@ -339,6 +339,26 @@ class AppProxyProvider: NETransparentProxyProvider {
completionHandler?(try? JSONSerialization.data(withJSONObject: response))
}
+ case "toggleRule":
+ if let ruleId = message["ruleId"] as? UInt32,
+ let enabled = message["enabled"] as? Bool {
+ rulesLock.lock()
+ if let index = rules.firstIndex(where: { $0.ruleId == ruleId }) {
+ rules[index].enabled = enabled
+ rulesLock.unlock()
+ log("Rule #\(ruleId) \(enabled ? "enabled" : "disabled")")
+ let response: [String: Any] = ["status": "ok", "ruleId": ruleId, "enabled": enabled]
+ completionHandler?(try? JSONSerialization.data(withJSONObject: response))
+ } else {
+ rulesLock.unlock()
+ let response = ["status": "error", "message": "Rule not found"]
+ completionHandler?(try? JSONSerialization.data(withJSONObject: response))
+ }
+ } else {
+ let response = ["status": "error", "message": "Missing ruleId or enabled"]
+ completionHandler?(try? JSONSerialization.data(withJSONObject: response))
+ }
+
case "removeRule":
if let ruleId = message["ruleId"] as? UInt32 {
rulesLock.lock()