Skip to content

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

Fixed Windivert Driver not unloaded on app closes via tray, #14 Added Config File option

Sourav Kalal77383dd8e05a
diff --git a/gui/App.axaml.cs b/gui/App.axaml.cs
index 5fd7848..4929a9d 100644
--- a/gui/App.axaml.cs
+++ b/gui/App.axaml.cs
@@ -23,6 +23,15 @@ public class App : Application
{
DataContext = new MainWindowViewModel()
};
+
+ // save config during shutdown
+ desktop.ShutdownRequested += (s, e) =>
+ {
+ if (desktop.MainWindow?.DataContext is MainWindowViewModel vm)
+ {
+ vm.Cleanup();
+ }
+ };
}
base.OnFrameworkInitializationCompleted();
diff --git a/gui/Services/ConfigManager.cs b/gui/Services/ConfigManager.cs
new file mode 100644
index 0000000..d47e127
--- /dev/null
+++ b/gui/Services/ConfigManager.cs
@@ -0,0 +1,90 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Text.Json;
+using System.Text.Json.Serialization;
+using ProxyBridge.GUI.ViewModels;
+
+namespace ProxyBridge.GUI.Services;
+
+public class AppConfig
+{
+ public string ProxyType { get; set; } = "SOCKS5";
+ public string ProxyIp { get; set; } = "";
+ public string ProxyPort { get; set; } = "";
+ public string ProxyUsername { get; set; } = "";
+ public string ProxyPassword { get; set; } = "";
+ public bool DnsViaProxy { get; set; } = true;
+ public List<ProxyRuleConfig> ProxyRules { get; set; } = new();
+}
+
+public class ProxyRuleConfig
+{
+ public string ProcessName { get; set; } = "";
+ public string TargetHosts { get; set; } = "*";
+ public string TargetPorts { get; set; } = "*";
+ public string Protocol { get; set; } = "TCP";
+ public string Action { get; set; } = "PROXY";
+ public bool IsEnabled { get; set; } = true;
+}
+
+[JsonSerializable(typeof(AppConfig))]
+[JsonSerializable(typeof(ProxyRuleConfig))]
+[JsonSerializable(typeof(List<ProxyRuleConfig>))]
+internal partial class AppConfigJsonContext : JsonSerializerContext
+{
+}
+
+public static class ConfigManager
+{
+ private static readonly string ConfigDirectory = Path.Combine(
+ Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
+ "ProxyBridge"
+ );
+ // C:\Users\<username>\AppData\Roaming\ProxyBridge\config.json
+
+ private static readonly string ConfigFilePath = Path.Combine(ConfigDirectory, "config.json");
+
+ public static bool SaveConfig(AppConfig config)
+ {
+ try
+ {
+ if (!Directory.Exists(ConfigDirectory))
+ {
+ Directory.CreateDirectory(ConfigDirectory);
+ }
+
+ var json = JsonSerializer.Serialize(config, AppConfigJsonContext.Default.AppConfig);
+ File.WriteAllText(ConfigFilePath, json);
+ return true;
+ }
+ catch
+ {
+ return false;
+ }
+ }
+
+ public static AppConfig LoadConfig()
+ {
+ try
+ {
+ if (!File.Exists(ConfigFilePath))
+ {
+ return new AppConfig();
+ }
+
+ var json = File.ReadAllText(ConfigFilePath);
+ var config = JsonSerializer.Deserialize(json, AppConfigJsonContext.Default.AppConfig);
+ return config ?? new AppConfig();
+ }
+ catch
+ {
+ return new AppConfig();
+ }
+ }
+
+ public static bool ConfigExists()
+ {
+ return File.Exists(ConfigFilePath);
+ }
+}
diff --git a/gui/Services/ProxyBridgeService.cs b/gui/Services/ProxyBridgeService.cs
index 234b927..13a277e 100644
--- a/gui/Services/ProxyBridgeService.cs
+++ b/gui/Services/ProxyBridgeService.cs
@@ -132,7 +132,30 @@ public class ProxyBridgeService : IDisposable
public void Dispose()
{
- Stop();
+ if (_isRunning)
+ {
+ Stop();
+
+ // WinDivert takes few seconds to stop
+ System.Threading.Thread.Sleep(500);
+
+ // STOP WinDivert kernel driver
+ try
+ {
+ var psi = new System.Diagnostics.ProcessStartInfo
+ {
+ FileName = "sc.exe",
+ Arguments = "stop WinDivert",
+ WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden,
+ CreateNoWindow = true,
+ UseShellExecute = false
+ };
+ var process = System.Diagnostics.Process.Start(psi);
+ process?.WaitForExit(2000);
+ }
+ catch {
+ }
+ }
GC.SuppressFinalize(this);
}
}
diff --git a/gui/ViewModels/MainWindowViewModel.cs b/gui/ViewModels/MainWindowViewModel.cs
index 2eb1b63..b17fe95 100644
--- a/gui/ViewModels/MainWindowViewModel.cs
+++ b/gui/ViewModels/MainWindowViewModel.cs
@@ -44,6 +44,7 @@ public class MainWindowViewModel : ViewModelBase
public void SetMainWindow(Window window)
{
_mainWindow = window;
+ LoadConfiguration();
try
{
@@ -93,12 +94,50 @@ public class MainWindowViewModel : ViewModelBase
};
_connectionLogTimer.Start();
- _proxyService.SetDnsViaProxy(true);
+ // Apply config DNS setting
+ _proxyService.SetDnsViaProxy(_dnsViaProxy);
+
+ // Apply comfig proxy settings
+ if (!string.IsNullOrEmpty(_currentProxyIp) &&
+ !string.IsNullOrEmpty(_currentProxyPort) &&
+ ushort.TryParse(_currentProxyPort, out ushort portNum))
+ {
+ _proxyService.SetProxyConfig(
+ _currentProxyType,
+ _currentProxyIp,
+ portNum,
+ _currentProxyUsername,
+ _currentProxyPassword);
+
+ ActivityLog += $"[{DateTime.Now:HH:mm:ss}] Restored proxy settings: {_currentProxyType} {_currentProxyIp}:{_currentProxyPort}\n";
+ }
// Start the proxy bridge
if (_proxyService.Start())
{
ActivityLog += $"[{DateTime.Now:HH:mm:ss}] ProxyBridge service started successfully\n";
+
+ // Apply config proxy rules, Need to see if this cause UI issues/slow/freeze
+ foreach (var rule in ProxyRules)
+ {
+ uint ruleId = _proxyService.AddRule(
+ rule.ProcessName,
+ rule.TargetHosts,
+ rule.TargetPorts,
+ rule.Protocol,
+ rule.Action);
+
+ if (ruleId > 0)
+ {
+ rule.RuleId = ruleId;
+ rule.Index = ProxyRules.IndexOf(rule) + 1;
+ }
+ }
+
+ if (ProxyRules.Count > 0)
+ {
+ ActivityLog += $"[{DateTime.Now:HH:mm:ss}] Restored {ProxyRules.Count} proxy rule(s)\n";
+ }
}
else
{
@@ -490,8 +529,99 @@ public class MainWindowViewModel : ViewModelBase
public void Cleanup()
{
+ try
+ {
+ var config = new AppConfig
+ {
+ ProxyType = _currentProxyType,
+ ProxyIp = _currentProxyIp,
+ ProxyPort = _currentProxyPort,
+ ProxyUsername = _currentProxyUsername,
+ ProxyPassword = _currentProxyPassword,
+ DnsViaProxy = _dnsViaProxy,
+ ProxyRules = ProxyRules.Select(r => new ProxyRuleConfig
+ {
+ ProcessName = r.ProcessName,
+ TargetHosts = r.TargetHosts,
+ TargetPorts = r.TargetPorts,
+ Protocol = r.Protocol,
+ Action = r.Action,
+ IsEnabled = r.IsEnabled
+ }).ToList()
+ };
+
+ ConfigManager.SaveConfig(config);
+ }
+ catch { }
+
_proxyService?.Dispose();
_proxyService = null;
+ } private void LoadConfiguration()
+ {
+ try
+ {
+ var config = ConfigManager.LoadConfig();
+
+ _currentProxyType = config.ProxyType;
+ _currentProxyIp = config.ProxyIp;
+ _currentProxyPort = config.ProxyPort;
+ _currentProxyUsername = config.ProxyUsername;
+ _currentProxyPassword = config.ProxyPassword;
+
+ DnsViaProxy = config.DnsViaProxy;
+
+ foreach (var ruleConfig in config.ProxyRules)
+ {
+ var rule = new ProxyRule
+ {
+ ProcessName = ruleConfig.ProcessName,
+ TargetHosts = ruleConfig.TargetHosts,
+ TargetPorts = ruleConfig.TargetPorts,
+ Protocol = ruleConfig.Protocol,
+ Action = ruleConfig.Action,
+ IsEnabled = ruleConfig.IsEnabled
+ };
+ ProxyRules.Add(rule);
+ }
+ }
+ catch (Exception ex)
+ {
+ ActivityLog += $"[{DateTime.Now:HH:mm:ss}] Failed to load configuration: {ex.Message}\n";
+ }
+ }
+
+ private void SaveConfiguration()
+ {
+ try
+ {
+ var config = new AppConfig
+ {
+ ProxyType = _currentProxyType,
+ ProxyIp = _currentProxyIp,
+ ProxyPort = _currentProxyPort,
+ ProxyUsername = _currentProxyUsername,
+ ProxyPassword = _currentProxyPassword,
+ DnsViaProxy = _dnsViaProxy,
+ ProxyRules = ProxyRules.Select(r => new ProxyRuleConfig
+ {
+ ProcessName = r.ProcessName,
+ TargetHosts = r.TargetHosts,
+ TargetPorts = r.TargetPorts,
+ Protocol = r.Protocol,
+ Action = r.Action,
+ IsEnabled = r.IsEnabled
+ }).ToList()
+ };
+
+ if (ConfigManager.SaveConfig(config))
+ {
+ ActivityLog += $"[{DateTime.Now:HH:mm:ss}] Configuration saved successfully\n";
+ }
+ }
+ catch (Exception ex)
+ {
+ ActivityLog += $"[{DateTime.Now:HH:mm:ss}] Failed to save configuration: {ex.Message}\n";
+ }
}
}