Skip to content

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

added inital GUI proxy configs

Sourav Kalalec2101268cc9
diff --git a/MacOS/ProxyBridge/ProxyBridge/ProxyBridgeGUI.swift b/MacOS/ProxyBridge/ProxyBridge/ProxyBridgeGUI.swift
index d2ac6ea..123bd6f 100644
--- a/MacOS/ProxyBridge/ProxyBridge/ProxyBridgeGUI.swift
+++ b/MacOS/ProxyBridge/ProxyBridge/ProxyBridgeGUI.swift
@@ -29,12 +29,12 @@ struct ProxyBridgeGUIApp: App {
CommandMenu("Proxy") {
Button("Proxy Settings...") {
- NSApp.sendAction(#selector(AppDelegate.openProxySettings), to: nil, from: nil)
+ openProxySettingsWindow()
}
.keyboardShortcut(",", modifiers: .command)
Button("Proxy Rules...") {
- NSApp.sendAction(#selector(AppDelegate.openProxyRules), to: nil, from: nil)
+ openProxyRulesWindow()
}
.keyboardShortcut("r", modifiers: .command)
}
@@ -42,7 +42,6 @@ struct ProxyBridgeGUIApp: App {
Window("Proxy Settings", id: "proxy-settings") {
ProxySettingsView(viewModel: viewModel)
- .frame(width: 500, height: 400)
}
.windowResizability(.contentSize)
.defaultPosition(.center)
@@ -54,6 +53,14 @@ struct ProxyBridgeGUIApp: App {
.windowResizability(.contentSize)
.defaultPosition(.center)
}
+
+ private func openProxySettingsWindow() {
+ NSApp.sendAction(#selector(AppDelegate.openProxySettings), to: nil, from: nil)
+ }
+
+ private func openProxyRulesWindow() {
+ NSApp.sendAction(#selector(AppDelegate.openProxyRules), to: nil, from: nil)
+ }
}
class AppDelegate: NSObject, NSApplicationDelegate {
@@ -64,10 +71,32 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}
@objc func openProxySettings() {
- // Window opening handled by SwiftUI
+ if let window = NSApplication.shared.windows.first(where: { $0.title == "Proxy Settings" }) {
+ window.makeKeyAndOrderFront(nil)
+ } else {
+ // Open new window
+ let controller = NSHostingController(rootView: ProxySettingsView(viewModel: AppDelegate.viewModel!))
+ let window = NSWindow(contentViewController: controller)
+ window.title = "Proxy Settings"
+ window.setContentSize(NSSize(width: 600, height: 500))
+ window.styleMask = [.titled, .closable]
+ window.center()
+ window.makeKeyAndOrderFront(nil)
+ }
}
@objc func openProxyRules() {
- // Window opening handled by SwiftUI
+ if let window = NSApplication.shared.windows.first(where: { $0.title == "Proxy Rules" }) {
+ window.makeKeyAndOrderFront(nil)
+ } else {
+ // Open new window
+ let controller = NSHostingController(rootView: ProxyRulesView(viewModel: AppDelegate.viewModel!))
+ let window = NSWindow(contentViewController: controller)
+ window.title = "Proxy Rules"
+ window.setContentSize(NSSize(width: 1000, height: 600))
+ window.styleMask = [.titled, .closable, .resizable]
+ window.center()
+ window.makeKeyAndOrderFront(nil)
+ }
}
}
diff --git a/MacOS/ProxyBridge/ProxyBridge/ProxyBridgeViewModel.swift b/MacOS/ProxyBridge/ProxyBridge/ProxyBridgeViewModel.swift
index 658f678..02515ab 100644
--- a/MacOS/ProxyBridge/ProxyBridge/ProxyBridgeViewModel.swift
+++ b/MacOS/ProxyBridge/ProxyBridge/ProxyBridgeViewModel.swift
@@ -15,9 +15,9 @@ class ProxyBridgeViewModel: NSObject, ObservableObject {
@Published var activityLogs: [ActivityLog] = []
@Published var isProxyActive = false
- private var tunnelSession: NETunnelProviderSession?
+ var tunnelSession: NETunnelProviderSession?
private var logTimer: Timer?
- private var proxyConfig: ProxyConfig?
+ private(set) var proxyConfig: ProxyConfig?
struct ProxyConfig {
let type: String
@@ -46,9 +46,45 @@ class ProxyBridgeViewModel: NSObject, ObservableObject {
override init() {
super.init()
+ loadProxyConfig()
installAndStartProxy()
}
+ private func loadProxyConfig() {
+ if let type = UserDefaults.standard.string(forKey: "proxyType"),
+ let host = UserDefaults.standard.string(forKey: "proxyHost"),
+ let port = UserDefaults.standard.object(forKey: "proxyPort") as? Int {
+ let username = UserDefaults.standard.string(forKey: "proxyUsername")
+ let password = UserDefaults.standard.string(forKey: "proxyPassword")
+
+ proxyConfig = ProxyConfig(
+ type: type,
+ host: host,
+ port: port,
+ username: username,
+ password: password
+ )
+ }
+ }
+
+ private func saveProxyConfig(_ config: ProxyConfig) {
+ UserDefaults.standard.set(config.type, forKey: "proxyType")
+ UserDefaults.standard.set(config.host, forKey: "proxyHost")
+ UserDefaults.standard.set(config.port, forKey: "proxyPort")
+
+ if let username = config.username {
+ UserDefaults.standard.set(username, forKey: "proxyUsername")
+ } else {
+ UserDefaults.standard.removeObject(forKey: "proxyUsername")
+ }
+
+ if let password = config.password {
+ UserDefaults.standard.set(password, forKey: "proxyPassword")
+ } else {
+ UserDefaults.standard.removeObject(forKey: "proxyPassword")
+ }
+ }
+
private func installAndStartProxy() {
print("Installing system extension")
let request = OSSystemExtensionRequest.activationRequest(
@@ -194,6 +230,7 @@ class ProxyBridgeViewModel: NSObject, ObservableObject {
func setProxyConfig(_ config: ProxyConfig) {
proxyConfig = config
+ saveProxyConfig(config)
guard let session = tunnelSession else {
addLog("ERROR", "Extension not connected")
diff --git a/MacOS/ProxyBridge/ProxyBridge/ProxyRulesView.swift b/MacOS/ProxyBridge/ProxyBridge/ProxyRulesView.swift
index b97b2ad..a51a5a3 100644
--- a/MacOS/ProxyBridge/ProxyBridge/ProxyRulesView.swift
+++ b/MacOS/ProxyBridge/ProxyBridge/ProxyRulesView.swift
@@ -6,25 +6,359 @@
//
import SwiftUI
+import NetworkExtension
+
+struct ProxyRule: Identifiable {
+ let id: UInt32
+ let processNames: String
+ let targetHosts: String
+ let targetPorts: String
+ let ruleProtocol: String
+ let action: String
+ var enabled: Bool
+
+ var ruleId: UInt32 { id }
+}
struct ProxyRulesView: View {
@ObservedObject var viewModel: ProxyBridgeViewModel
+ @State private var rules: [ProxyRule] = []
+ @State private var showAddRule = false
+ @State private var editingRule: ProxyRule?
+ @State private var isLoading = false
var body: some View {
- VStack {
- Text("Proxy Rules")
+ VStack(spacing: 0) {
+ HStack {
+ Text("Process Rules")
+ .font(.title2)
+ .fontWeight(.semibold)
+
+ Spacer()
+
+ Button(action: { showAddRule = true }) {
+ HStack {
+ Image(systemName: "plus")
+ Text("Add Rule")
+ }
+ .padding(.horizontal, 16)
+ .padding(.vertical, 8)
+ }
+ .buttonStyle(.borderedProminent)
+ }
+ .padding()
+
+ if isLoading {
+ Spacer()
+ ProgressView()
+ .scaleEffect(1.5)
+ Spacer()
+ } else if rules.isEmpty {
+ Spacer()
+ VStack(spacing: 12) {
+ Image(systemName: "list.bullet.rectangle")
+ .font(.system(size: 48))
+ .foregroundColor(.gray)
+ Text("No rules configured")
+ .font(.title3)
+ .foregroundColor(.gray)
+ Text("Click 'Add Rule' to create your first rule")
+ .font(.subheadline)
+ .foregroundColor(.secondary)
+ }
+ Spacer()
+ } else {
+ Table(rules) {
+ TableColumn("Enabled") { rule in
+ Toggle("", isOn: binding(for: rule))
+ .toggleStyle(.switch)
+ .labelsHidden()
+ }
+ .width(60)
+
+ TableColumn("Actions") { rule in
+ HStack(spacing: 8) {
+ Button(action: { editingRule = rule }) {
+ HStack(spacing: 4) {
+ Image(systemName: "pencil")
+ Text("Edit")
+ }
+ }
+ .buttonStyle(.borderless)
+ .foregroundColor(.blue)
+
+ Button(action: { deleteRule(rule) }) {
+ HStack(spacing: 4) {
+ Image(systemName: "trash")
+ Text("Delete")
+ }
+ }
+ .buttonStyle(.borderless)
+ .foregroundColor(.red)
+ }
+ }
+ .width(140)
+
+ TableColumn("SR") { rule in
+ Text("\(rule.id)")
+ }
+ .width(50)
+
+ TableColumn("Process") { rule in
+ Text(rule.processNames.isEmpty ? "Any" : rule.processNames)
+ }
+ .width(150)
+
+ TableColumn("Target Hosts") { rule in
+ Text(rule.targetHosts.isEmpty ? "Any" : rule.targetHosts)
+ }
+ .width(180)
+
+ TableColumn("Target Ports") { rule in
+ Text(rule.targetPorts.isEmpty ? "Any" : rule.targetPorts)
+ }
+ .width(120)
+
+ TableColumn("Protocol") { rule in
+ Text(rule.ruleProtocol)
+ }
+ .width(80)
+
+ TableColumn("Action") { rule in
+ Text(rule.action)
+ .foregroundColor(actionColor(rule.action))
+ .fontWeight(.semibold)
+ }
+ .width(80)
+ }
+ .padding()
+ }
+ }
+ .frame(width: 1000, height: 600)
+ .onAppear {
+ loadRules()
+ }
+ .sheet(isPresented: $showAddRule) {
+ RuleEditorView(viewModel: viewModel, onSave: { loadRules() })
+ }
+ .sheet(item: $editingRule) { rule in
+ RuleEditorView(viewModel: viewModel, existingRule: rule, onSave: { loadRules() })
+ }
+ }
+
+ private func binding(for rule: ProxyRule) -> Binding<Bool> {
+ Binding(
+ get: { rule.enabled },
+ set: { newValue in
+ toggleRule(rule, enabled: newValue)
+ }
+ )
+ }
+
+ private func actionColor(_ action: String) -> Color {
+ switch action {
+ case "PROXY": return .green
+ case "BLOCK": return .red
+ case "DIRECT": return .blue
+ default: return .primary
+ }
+ }
+
+ private func loadRules() {
+ guard let session = viewModel.tunnelSession else { return }
+
+ isLoading = true
+ RuleManager.listRules(session: session) { success, rulesList in
+ DispatchQueue.main.async {
+ isLoading = false
+ if success {
+ rules = rulesList.map { dict in
+ ProxyRule(
+ id: dict["ruleId"] as? UInt32 ?? 0,
+ processNames: dict["processNames"] as? String ?? "",
+ targetHosts: dict["targetHosts"] as? String ?? "",
+ targetPorts: dict["targetPorts"] as? String ?? "",
+ ruleProtocol: dict["protocol"] as? String ?? "BOTH",
+ action: dict["action"] as? String ?? "DIRECT",
+ enabled: dict["enabled"] as? Bool ?? true
+ )
+ }
+ }
+ }
+ }
+ }
+
+ private func deleteRule(_ rule: ProxyRule) {
+ guard let session = viewModel.tunnelSession else { return }
+
+ RuleManager.removeRule(session: session, ruleId: rule.id) { success, message in
+ if success {
+ loadRules()
+ }
+ }
+ }
+
+ 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()
+ }
+ }
+ }
+ }
+}
+
+struct RuleEditorView: View {
+ @ObservedObject var viewModel: ProxyBridgeViewModel
+ var existingRule: ProxyRule?
+ var onSave: () -> Void
+
+ @Environment(\.dismiss) private var dismiss
+
+ @State private var processNames: String
+ @State private var targetHosts: String
+ @State private var targetPorts: String
+ @State private var selectedProtocol: String
+ @State private var selectedAction: String
+
+ init(viewModel: ProxyBridgeViewModel, existingRule: ProxyRule? = nil, onSave: @escaping () -> Void) {
+ self.viewModel = viewModel
+ self.existingRule = existingRule
+ self.onSave = onSave
+
+ _processNames = State(initialValue: existingRule?.processNames ?? "*")
+ _targetHosts = State(initialValue: existingRule?.targetHosts ?? "*")
+ _targetPorts = State(initialValue: existingRule?.targetPorts ?? "*")
+ _selectedProtocol = State(initialValue: existingRule?.ruleProtocol ?? "TCP")
+ _selectedAction = State(initialValue: existingRule?.action ?? "PROXY")
+ }
+
+ var body: some View {
+ VStack(spacing: 20) {
+ Text(existingRule == nil ? "Add Rule" : "Edit Rule")
.font(.title2)
.fontWeight(.semibold)
- .padding()
- Spacer()
+ Form {
+ Section {
+ VStack(alignment: .leading, spacing: 8) {
+ Text("Applications")
+ .fontWeight(.medium)
+ TextField("*", text: $processNames)
+ .textFieldStyle(.roundedBorder)
+ Text("Example: iexplore.exe; \"C:\\some app.exe\"; fire*.exe; *.bin")
+ .font(.caption)
+ .foregroundColor(.secondary)
+ }
+
+ VStack(alignment: .leading, spacing: 8) {
+ Text("Target hosts")
+ .fontWeight(.medium)
+ TextField("*", text: $targetHosts)
+ .textFieldStyle(.roundedBorder)
+ Text("Example: 127.0.0.1; *.example.com; 192.168.1.*; 10.1.0.0-10.5.255.255")
+ .font(.caption)
+ .foregroundColor(.secondary)
+ }
+
+ VStack(alignment: .leading, spacing: 8) {
+ Text("Target ports")
+ .fontWeight(.medium)
+ TextField("*", text: $targetPorts)
+ .textFieldStyle(.roundedBorder)
+ Text("Example: 80; 8000-9000; 3128")
+ .font(.caption)
+ .foregroundColor(.secondary)
+ }
+
+ VStack(alignment: .leading, spacing: 8) {
+ Text("Protocol")
+ .fontWeight(.medium)
+ Picker("", selection: $selectedProtocol) {
+ Text("TCP").tag("TCP")
+ Text("UDP").tag("UDP")
+ Text("BOTH").tag("BOTH")
+ }
+ .pickerStyle(.segmented)
+ }
+
+ VStack(alignment: .leading, spacing: 8) {
+ Text("Action")
+ .fontWeight(.medium)
+ Picker("", selection: $selectedAction) {
+ Text("PROXY").tag("PROXY")
+ Text("DIRECT").tag("DIRECT")
+ Text("BLOCK").tag("BLOCK")
+ }
+ .pickerStyle(.segmented)
+ }
+ }
+ }
+ .formStyle(.grouped)
- Text("Coming soon...")
- .font(.title3)
- .foregroundColor(.gray)
-
- Spacer()
+ HStack {
+ Button("Cancel") {
+ dismiss()
+ }
+ .keyboardShortcut(.cancelAction)
+
+ Spacer()
+
+ Button("Save Rule") {
+ saveRule()
+ }
+ .keyboardShortcut(.defaultAction)
+ .buttonStyle(.borderedProminent)
+ }
+ .padding(.horizontal)
+ }
+ .padding()
+ .frame(width: 600, height: 550)
+ }
+
+ 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)
+ }
+ } 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()
+ }
+ }
}
- .frame(width: 700, height: 500)
}
}
diff --git a/MacOS/ProxyBridge/ProxyBridge/ProxySettingsView.swift b/MacOS/ProxyBridge/ProxyBridge/ProxySettingsView.swift
index 2d45e0b..c74ad6c 100644
--- a/MacOS/ProxyBridge/ProxyBridge/ProxySettingsView.swift
+++ b/MacOS/ProxyBridge/ProxyBridge/ProxySettingsView.swift
@@ -9,57 +9,138 @@ import SwiftUI
struct ProxySettingsView: View {
@ObservedObject var viewModel: ProxyBridgeViewModel
- @State private var proxyType = "socks5"
+ @Environment(\.dismiss) private var dismiss
+
+ @State private var proxyType = ""
@State private var proxyHost = ""
- @State private var proxyPort = "1080"
+ @State private var proxyPort = ""
@State private var username = ""
@State private var password = ""
- let proxyTypes = ["socks5", "http"]
+ let proxyTypes = ["http", "socks5"]
var body: some View {
- VStack(spacing: 20) {
- Text("Proxy Settings")
- .font(.title2)
- .fontWeight(.semibold)
- .padding(.top)
+ VStack(spacing: 0) {
+ // Header
+ HStack {
+ Image(systemName: "network")
+ .font(.title2)
+ .foregroundColor(.accentColor)
+ Text("Proxy Settings")
+ .font(.title2)
+ .fontWeight(.semibold)
+ Spacer()
+ }
+ .padding()
+ .background(Color(NSColor.controlBackgroundColor))
+ // Form Content
Form {
- Picker("Proxy Type:", selection: $proxyType) {
- ForEach(proxyTypes, id: \.self) { type in
- Text(type.uppercased()).tag(type)
+ Section {
+ VStack(alignment: .leading, spacing: 8) {
+ HStack {
+ Text("Proxy Type")
+ .fontWeight(.medium)
+ Text("*")
+ .foregroundColor(.red)
+ }
+ Picker("Select proxy type", selection: $proxyType) {
+ Text("Select proxy type").tag("")
+ ForEach(proxyTypes, id: \.self) { type in
+ Text(type.uppercased()).tag(type)
+ }
+ }
+ .pickerStyle(.menu)
+ }
+ .padding(.vertical, 8)
+
+ VStack(alignment: .leading, spacing: 8) {
+ HStack {
+ Text("Proxy IP Address")
+ .fontWeight(.medium)
+ Text("*")
+ .foregroundColor(.red)
+ }
+ TextField("127.0.0.1", text: $proxyHost)
+ .textFieldStyle(.roundedBorder)
+ }
+ .padding(.vertical, 8)
+
+ VStack(alignment: .leading, spacing: 8) {
+ HStack {
+ Text("Proxy Port")
+ .fontWeight(.medium)
+ Text("*")
+ .foregroundColor(.red)
+ }
+ TextField("8080", text: $proxyPort)
+ .textFieldStyle(.roundedBorder)
}
+ .padding(.vertical, 8)
+
+ VStack(alignment: .leading, spacing: 8) {
+ Text("Username (Optional)")
+ .fontWeight(.medium)
+ TextField("Leave empty if no auth required", text: $username)
+ .textFieldStyle(.roundedBorder)
+ }
+ .padding(.vertical, 8)
+
+ VStack(alignment: .leading, spacing: 8) {
+ Text("Password (Optional)")
+ .fontWeight(.medium)
+ SecureField("Leave empty if no auth required", text: $password)
+ .textFieldStyle(.roundedBorder)
+ }
+ .padding(.vertical, 8)
+
+ HStack {
+ Text("* Required fields")
+ .font(.caption)
+ .foregroundColor(.secondary)
+ Spacer()
+ }
+ .padding(.top, 4)
}
- .pickerStyle(.segmented)
-
- TextField("Proxy Host:", text: $proxyHost)
- .textFieldStyle(.roundedBorder)
-
- TextField("Proxy Port:", text: $proxyPort)
- .textFieldStyle(.roundedBorder)
-
- TextField("Username (optional):", text: $username)
- .textFieldStyle(.roundedBorder)
-
- SecureField("Password (optional):", text: $password)
- .textFieldStyle(.roundedBorder)
}
- .padding()
+ .formStyle(.grouped)
- HStack {
+ Divider()
+
+ // Footer Buttons
+ HStack(spacing: 12) {
Spacer()
+ Button("Cancel") {
+ dismiss()
+ }
+ .keyboardShortcut(.cancelAction)
+
Button("Save Changes") {
saveSettings()
+ dismiss()
}
.buttonStyle(.borderedProminent)
- .disabled(proxyHost.isEmpty || proxyPort.isEmpty)
-
- Spacer()
+ .keyboardShortcut(.defaultAction)
+ .disabled(proxyType.isEmpty || proxyHost.isEmpty || proxyPort.isEmpty)
}
- .padding(.bottom)
+ .padding()
+ .background(Color(NSColor.controlBackgroundColor))
+ }
+ .frame(width: 600, height: 500)
+ .onAppear {
+ loadCurrentSettings()
+ }
+ }
+
+ private func loadCurrentSettings() {
+ if let config = viewModel.proxyConfig {
+ proxyType = config.type
+ proxyHost = config.host
+ proxyPort = String(config.port)
+ username = config.username ?? ""
+ password = config.password ?? ""
}
- .frame(width: 500, height: 400)
}
private func saveSettings() {
diff --git a/MacOS/ProxyBridge/ProxyBridge/RuleManager.swift b/MacOS/ProxyBridge/ProxyBridge/RuleManager.swift
index d46b814..d1b19ce 100644
--- a/MacOS/ProxyBridge/ProxyBridge/RuleManager.swift
+++ b/MacOS/ProxyBridge/ProxyBridge/RuleManager.swift
@@ -29,7 +29,7 @@ struct RuleManager {
"targetHosts": targetHosts,
"targetPorts": targetPorts,
"ruleProtocol": `protocol`,
- "action": action,
+ "ruleAction": action,
"enabled": enabled
]