Skip to content

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

fixed rule update

Sourav Kalalca0799bc8931
diff --git a/MacOS/ProxyBridge/ProxyBridge/ProxyRulesView.swift b/MacOS/ProxyBridge/ProxyBridge/ProxyRulesView.swift
index a51a5a3..943af9f 100644
--- a/MacOS/ProxyBridge/ProxyBridge/ProxyRulesView.swift
+++ b/MacOS/ProxyBridge/ProxyBridge/ProxyRulesView.swift
@@ -201,21 +201,17 @@ struct ProxyRulesView: View {
private func toggleRule(_ rule: ProxyRule, enabled: Bool) {
guard let session = viewModel.tunnelSession else { return }
- RuleManager.removeRule(session: session, ruleId: rule.id) { success, _ in
- if success {
- RuleManager.addRule(
- session: session,
- ruleId: rule.id,
- processNames: rule.processNames,
- targetHosts: rule.targetHosts,
- targetPorts: rule.targetPorts,
- protocol: rule.ruleProtocol,
- action: rule.action,
- enabled: enabled
- ) { _, _ in
- loadRules()
- }
- }
+ RuleManager.updateRule(
+ session: session,
+ ruleId: rule.id,
+ processNames: rule.processNames,
+ targetHosts: rule.targetHosts,
+ targetPorts: rule.targetPorts,
+ protocol: rule.ruleProtocol,
+ action: rule.action,
+ enabled: enabled
+ ) { _, _ in
+ loadRules()
}
}
}
@@ -331,32 +327,40 @@ struct RuleEditorView: View {
private func saveRule() {
guard let session = viewModel.tunnelSession else { return }
- let ruleId = existingRule?.id ?? UInt32.random(in: 1...999999)
-
if let existing = existingRule {
- RuleManager.removeRule(session: session, ruleId: existing.id) { _, _ in
- addNewRule(session: session, ruleId: ruleId)
+ // Edit existing rule - use update
+ RuleManager.updateRule(
+ session: session,
+ ruleId: existing.id,
+ processNames: processNames,
+ targetHosts: targetHosts,
+ targetPorts: targetPorts,
+ protocol: selectedProtocol,
+ action: selectedAction,
+ enabled: true
+ ) { success, _ in
+ if success {
+ DispatchQueue.main.async {
+ onSave()
+ dismiss()
+ }
+ }
}
} else {
- addNewRule(session: session, ruleId: ruleId)
- }
- }
-
- private func addNewRule(session: NETunnelProviderSession, ruleId: UInt32) {
- RuleManager.addRule(
- session: session,
- ruleId: ruleId,
- processNames: processNames,
- targetHosts: targetHosts,
- targetPorts: targetPorts,
- protocol: selectedProtocol,
- action: selectedAction,
- enabled: true
- ) { success, _ in
- if success {
- DispatchQueue.main.async {
- onSave()
- dismiss()
+ RuleManager.addRule(
+ session: session,
+ processNames: processNames,
+ targetHosts: targetHosts,
+ targetPorts: targetPorts,
+ protocol: selectedProtocol,
+ action: selectedAction,
+ enabled: true
+ ) { success, _, _ in
+ if success {
+ DispatchQueue.main.async {
+ onSave()
+ dismiss()
+ }
}
}
}
diff --git a/MacOS/ProxyBridge/ProxyBridge/RuleManager.swift b/MacOS/ProxyBridge/ProxyBridge/RuleManager.swift
index d1b19ce..2cd5d6f 100644
--- a/MacOS/ProxyBridge/ProxyBridge/RuleManager.swift
+++ b/MacOS/ProxyBridge/ProxyBridge/RuleManager.swift
@@ -12,6 +12,50 @@ struct RuleManager {
// Send addRule command to extension
static func addRule(
+ session: NETunnelProviderSession,
+ processNames: String,
+ targetHosts: String,
+ targetPorts: String,
+ protocol: String,
+ action: String,
+ enabled: Bool = true,
+ completion: @escaping (Bool, String, UInt32?) -> Void
+ ) {
+ let message: [String: Any] = [
+ "action": "addRule",
+ "processNames": processNames,
+ "targetHosts": targetHosts,
+ "targetPorts": targetPorts,
+ "ruleProtocol": `protocol`,
+ "ruleAction": action,
+ "enabled": enabled
+ ]
+
+ guard let data = try? JSONSerialization.data(withJSONObject: message) else {
+ completion(false, "Failed to serialize rule", nil)
+ 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", nil)
+ return
+ }
+
+ if status == "ok" {
+ let ruleId = result["ruleId"] as? UInt32
+ completion(true, "Rule added successfully", ruleId)
+ } else if let message = result["message"] as? String {
+ completion(false, "Error: \(message)", nil)
+ } else {
+ completion(false, "Unknown error", nil)
+ }
+ }
+ }
+
+ static func updateRule(
session: NETunnelProviderSession,
ruleId: UInt32,
processNames: String,
@@ -23,7 +67,7 @@ struct RuleManager {
completion: @escaping (Bool, String) -> Void
) {
let message: [String: Any] = [
- "action": "addRule",
+ "action": "updateRule",
"ruleId": ruleId,
"processNames": processNames,
"targetHosts": targetHosts,
@@ -47,7 +91,7 @@ struct RuleManager {
}
if status == "ok" {
- completion(true, "Rule #\(ruleId) added successfully")
+ completion(true, "Rule #\(ruleId) updated successfully")
} else if let message = result["message"] as? String {
completion(false, "Error: \(message)")
} else {
diff --git a/MacOS/ProxyBridge/extension/AppProxyProvider.swift b/MacOS/ProxyBridge/extension/AppProxyProvider.swift
index 4a244d1..75d8d25 100644
--- a/MacOS/ProxyBridge/extension/AppProxyProvider.swift
+++ b/MacOS/ProxyBridge/extension/AppProxyProvider.swift
@@ -316,6 +316,29 @@ class AppProxyProvider: NETransparentProxyProvider {
completionHandler?(try? JSONSerialization.data(withJSONObject: response))
}
+ case "updateRule":
+ if let ruleId = message["ruleId"] as? UInt32,
+ let ruleData = try? JSONSerialization.data(withJSONObject: message),
+ let updatedRule = try? JSONDecoder().decode(ProxyRule.self, from: ruleData) {
+ rulesLock.lock()
+ if let index = rules.firstIndex(where: { $0.ruleId == ruleId }) {
+ var rule = updatedRule
+ rule.ruleId = ruleId
+ rules[index] = rule
+ rulesLock.unlock()
+ log("Updated rule #\(ruleId): \(rule.processNames) -> \(rule.action.rawValue)")
+ let response: [String: Any] = ["status": "ok", "ruleId": ruleId]
+ 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": "Invalid update data"]
+ completionHandler?(try? JSONSerialization.data(withJSONObject: response))
+ }
+
case "removeRule":
if let ruleId = message["ruleId"] as? UInt32 {
rulesLock.lock()