Skip to content

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

fixed proxy rule id

Sourav Kalal46234b9b344b
diff --git a/MacOS/ProxyBridge/ProxyBridge/ProxyBridgeApp.swift b/MacOS/ProxyBridge/ProxyBridge/ProxyBridgeApp.swift
index 89c070b..a5d9683 100644
--- a/MacOS/ProxyBridge/ProxyBridge/ProxyBridgeApp.swift
+++ b/MacOS/ProxyBridge/ProxyBridge/ProxyBridgeApp.swift
@@ -100,7 +100,6 @@ class ProxyBridgeApp: NSObject {
}
func setupTestProxyAndRule(session: NETunnelProviderSession) {
- // Set hardcoded SOCKS5 proxy
let proxyConfig: [String: Any] = [
"action": "setProxyConfig",
"proxyType": "socks5",
@@ -111,12 +110,23 @@ class ProxyBridgeApp: NSObject {
guard let configData = try? JSONSerialization.data(withJSONObject: proxyConfig) else { return }
try? session.sendProviderMessage(configData) { response in
- print("✓ Proxy configured: socks5://192.168.1.4:4444")
+ if let responseData = response,
+ let json = try? JSONSerialization.jsonObject(with: responseData) as? [String: Any],
+ let status = json["status"] as? String {
+ if status == "ok" {
+ print("✓ Proxy configured: socks5://192.168.1.4:4444")
+ print(" Extension response: \(json)")
+ } else {
+ print("✗ Proxy config failed: \(json["message"] as? String ?? "unknown error")")
+ print(" Extension response: \(json)")
+ }
+ } else {
+ print("✓ Proxy configured: socks5://192.168.1.4:4444")
+ print(" Extension response: No data")
+ }
- // After proxy is set, add test rule for curl
let rule: [String: Any] = [
"action": "addRule",
- "ruleId": 1,
"processNames": "com.apple.curl",
"targetHosts": "*",
"targetPorts": "*",
@@ -128,7 +138,21 @@ class ProxyBridgeApp: NSObject {
guard let ruleData = try? JSONSerialization.data(withJSONObject: rule) else { return }
try? session.sendProviderMessage(ruleData) { response in
- print("✓ Test rule added: com.apple.curl -> * -> * -> PROXY")
+ if let responseData = response,
+ let json = try? JSONSerialization.jsonObject(with: responseData) as? [String: Any],
+ let status = json["status"] as? String {
+ if status == "ok" {
+ let ruleId = json["ruleId"] as? UInt32 ?? 0
+ print("✓ Test rule added with ID #\(ruleId): com.apple.curl -> * -> * -> PROXY")
+ print(" Extension response: \(json)")
+ } else {
+ print("✗ Add rule failed: \(json["message"] as? String ?? "unknown error")")
+ print(" Extension response: \(json)")
+ }
+ } else {
+ print("✓ Test rule added: com.apple.curl -> * -> * -> PROXY")
+ print(" Extension response: No data")
+ }
print("")
}
}
diff --git a/MacOS/ProxyBridge/extension/AppProxyProvider.swift b/MacOS/ProxyBridge/extension/AppProxyProvider.swift
index 613585b..22128c8 100644
--- a/MacOS/ProxyBridge/extension/AppProxyProvider.swift
+++ b/MacOS/ProxyBridge/extension/AppProxyProvider.swift
@@ -22,7 +22,7 @@ enum RuleAction: String, Codable {
}
struct ProxyRule: Codable {
- let ruleId: UInt32
+ var ruleId: UInt32
let processNames: String
let targetHosts: String
let targetPorts: String
@@ -40,6 +40,27 @@ struct ProxyRule: Codable {
case enabled
}
+ init(from decoder: Decoder) throws {
+ let container = try decoder.container(keyedBy: CodingKeys.self)
+ self.ruleId = try container.decodeIfPresent(UInt32.self, forKey: .ruleId) ?? 0
+ self.processNames = try container.decode(String.self, forKey: .processNames)
+ self.targetHosts = try container.decode(String.self, forKey: .targetHosts)
+ self.targetPorts = try container.decode(String.self, forKey: .targetPorts)
+ self.ruleProtocol = try container.decode(RuleProtocol.self, forKey: .ruleProtocol)
+ self.action = try container.decode(RuleAction.self, forKey: .action)
+ self.enabled = try container.decodeIfPresent(Bool.self, forKey: .enabled) ?? true
+ }
+
+ init(ruleId: UInt32, processNames: String, targetHosts: String, targetPorts: String, ruleProtocol: RuleProtocol, action: RuleAction, enabled: Bool) {
+ self.ruleId = ruleId
+ self.processNames = processNames
+ self.targetHosts = targetHosts
+ self.targetPorts = targetPorts
+ self.ruleProtocol = ruleProtocol
+ self.action = action
+ self.enabled = enabled
+ }
+
func matchesProcess(_ processPath: String) -> Bool {
return Self.matchProcessList(processNames, processPath: processPath)
}
@@ -178,11 +199,10 @@ class AppProxyProvider: NETransparentProxyProvider {
private var logQueue: [[String: String]] = []
private let queueLock = NSLock()
- // Rule storage
private var rules: [ProxyRule] = []
private let rulesLock = NSLock()
+ private var nextRuleId: UInt32 = 1
- // Proxy configuration
private var proxyType: String?
private var proxyHost: String?
private var proxyPort: Int?
@@ -255,14 +275,25 @@ class AppProxyProvider: NETransparentProxyProvider {
case "addRule":
if let ruleData = try? JSONSerialization.data(withJSONObject: message),
- let rule = try? JSONDecoder().decode(ProxyRule.self, from: ruleData) {
+ var rule = try? JSONDecoder().decode(ProxyRule.self, from: ruleData) {
rulesLock.lock()
- // Remove existing rule with same ID
- rules.removeAll { $0.ruleId == rule.ruleId }
+ rule.ruleId = nextRuleId
+ nextRuleId += 1
rules.append(rule)
rulesLock.unlock()
+
logger.info("Added rule #\(rule.ruleId): \(rule.processNames) -> \(rule.action.rawValue)")
- let response = ["status": "ok"]
+
+ let response: [String: Any] = [
+ "status": "ok",
+ "ruleId": rule.ruleId,
+ "processNames": rule.processNames,
+ "targetHosts": rule.targetHosts,
+ "targetPorts": rule.targetPorts,
+ "protocol": rule.ruleProtocol.rawValue,
+ "action": rule.action.rawValue,
+ "enabled": rule.enabled
+ ]
completionHandler?(try? JSONSerialization.data(withJSONObject: response))
} else {
let response = ["status": "error", "message": "Invalid rule format"]