Skip to content

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

added Import rule in Windows CLI #22

Sourav Kalald4a24a2a0474
diff --git a/README.md b/README.md
index 3ecf769..83b8878 100644
--- a/README.md
+++ b/README.md
@@ -225,7 +225,7 @@ ProxyBridge_CLI.exe -h
| |_) | '__/ _ \ \/ / | | | | _ \| '__| |/ _` |/ _` |/ _ \
| __/| | | (_) > <| |_| | | |_) | | | | (_| | (_| | __/
|_| |_| \___/_/\_\\__, | |____/|_| |_|\__,_|\__, |\___|
- |___/ |___/ V2.0.1
+ |___/ |___/ V3.0.0
Universal proxy client for Windows applications
@@ -254,6 +254,17 @@ Options:
chrome.exe:*:*:TCP:PROXY
*:*:53:UDP:PROXY
firefox.exe:*:80,443:TCP:DIRECT
+ --rule-file <rule-file> Path to JSON file containing proxy rules
+ JSON format (same as GUI export):
+ [{
+ "processNames": "chrome.exe",
+ "targetHosts": "*",
+ "targetPorts": "*",
+ "protocol": "TCP",
+ "action": "PROXY",
+ "enabled": true
+ }]
+ Example: --rule-file C:\\rules.json []
--dns-via-proxy Route DNS queries through proxy (default: true) [default: True]
--verbose <verbose> Logging verbosity level
0 - No logs (default)
diff --git a/Windows/cli/Program.cs b/Windows/cli/Program.cs
index 13a97e5..bb94c6f 100644
--- a/Windows/cli/Program.cs
+++ b/Windows/cli/Program.cs
@@ -1,9 +1,27 @@
using System.CommandLine;
using System.Security.Principal;
using System.Runtime.Versioning;
+using System.Text.Json.Serialization;
namespace ProxyBridge.CLI;
+public class ProxyRuleImport
+{
+ public string ProcessNames { get; set; } = string.Empty;
+ public string TargetHosts { get; set; } = string.Empty;
+ public string TargetPorts { get; set; } = string.Empty;
+ public string Protocol { get; set; } = string.Empty;
+ public string Action { get; set; } = string.Empty;
+ public bool Enabled { get; set; } = true;
+}
+
+// JSON Source Generator for NativeAOT compatibility
+[JsonSerializable(typeof(List<ProxyRuleImport>))]
+[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)]
+public partial class ProxyRuleJsonContext : JsonSerializerContext
+{
+}
+
class Program
{
private static ProxyBridgeNative.LogCallback? _logCallback;
@@ -40,6 +58,21 @@ class Program
Arity = ArgumentArity.ZeroOrMore
};
+ var ruleFileOption = new Option<string?>(
+ name: "--rule-file",
+ description: "Path to JSON file containing proxy rules\n" +
+ "JSON format (same as GUI export):\n" +
+ "[{\n" +
+ " \"processNames\": \"chrome.exe\",\n" +
+ " \"targetHosts\": \"*\",\n" +
+ " \"targetPorts\": \"*\",\n" +
+ " \"protocol\": \"TCP\",\n" +
+ " \"action\": \"PROXY\",\n" +
+ " \"enabled\": true\n" +
+ "}]\n" +
+ "Example: --rule-file C:\\\\rules.json",
+ getDefaultValue: () => null);
+
var dnsViaProxyOption = new Option<bool>(
name: "--dns-via-proxy",
description: "Route DNS queries through proxy (default: true)",
@@ -60,6 +93,7 @@ class Program
{
proxyOption,
ruleOption,
+ ruleFileOption,
dnsViaProxyOption,
verboseOption
};
@@ -71,10 +105,10 @@ class Program
await CheckAndUpdate();
});
- rootCommand.SetHandler(async (proxyUrl, rules, dnsViaProxy, verbose) =>
+ rootCommand.SetHandler(async (proxyUrl, rules, ruleFile, dnsViaProxy, verbose) =>
{
- await RunProxyBridge(proxyUrl, rules, dnsViaProxy, verbose);
- }, proxyOption, ruleOption, dnsViaProxyOption, verboseOption);
+ await RunProxyBridge(proxyUrl, rules, ruleFile, dnsViaProxy, verbose);
+ }, proxyOption, ruleOption, ruleFileOption, dnsViaProxyOption, verboseOption);
if (args.Contains("--help") || args.Contains("-h") || args.Contains("-?"))
{
@@ -84,7 +118,7 @@ class Program
return await rootCommand.InvokeAsync(args);
}
- private static async Task<int> RunProxyBridge(string proxyUrl, string[] rules, bool dnsViaProxy, int verboseLevel)
+ private static async Task<int> RunProxyBridge(string proxyUrl, string[] rules, string? ruleFile, bool dnsViaProxy, int verboseLevel)
{
_verboseLevel = verboseLevel;
ShowBanner();
@@ -103,6 +137,12 @@ class Program
var proxyInfo = ParseProxyConfig(proxyUrl);
var parsedRules = ParseRules(rules);
+ if (!string.IsNullOrEmpty(ruleFile))
+ {
+ var fileRules = await LoadRulesFromFile(ruleFile);
+ parsedRules.AddRange(fileRules);
+ }
+
_logCallback = OnLog;
_connectionCallback = OnConnection;
@@ -205,6 +245,65 @@ class Program
}
}
+ private static async Task<List<(string ProcessName, string TargetHosts, string TargetPorts, ProxyBridgeNative.RuleProtocol Protocol, ProxyBridgeNative.RuleAction Action)>> LoadRulesFromFile(string filePath)
+ {
+ var rules = new List<(string, string, string, ProxyBridgeNative.RuleProtocol, ProxyBridgeNative.RuleAction)>();
+
+ if (!File.Exists(filePath))
+ {
+ throw new FileNotFoundException($"Rule file not found: {filePath}");
+ }
+
+ try
+ {
+ var json = await File.ReadAllTextAsync(filePath);
+ var importedRules = System.Text.Json.JsonSerializer.Deserialize(json, ProxyRuleJsonContext.Default.ListProxyRuleImport);
+
+ if (importedRules == null || importedRules.Count == 0)
+ {
+ Console.ForegroundColor = ConsoleColor.Yellow;
+ Console.WriteLine($"WARNING: No rules found in file: {filePath}");
+ Console.ResetColor();
+ return rules;
+ }
+
+ Console.WriteLine($"Loaded {importedRules.Count} rule(s) from: {filePath}");
+
+ foreach (var rule in importedRules)
+ {
+ if (!rule.Enabled)
+ {
+ Console.WriteLine($" Skipping disabled rule: {rule.ProcessNames}");
+ continue;
+ }
+
+ var protocol = rule.Protocol.ToUpper() switch
+ {
+ "TCP" => ProxyBridgeNative.RuleProtocol.TCP,
+ "UDP" => ProxyBridgeNative.RuleProtocol.UDP,
+ "BOTH" => ProxyBridgeNative.RuleProtocol.BOTH,
+ _ => throw new ArgumentException($"Invalid protocol in rule file: {rule.Protocol}")
+ };
+
+ var action = rule.Action.ToUpper() switch
+ {
+ "PROXY" => ProxyBridgeNative.RuleAction.PROXY,
+ "DIRECT" => ProxyBridgeNative.RuleAction.DIRECT,
+ "BLOCK" => ProxyBridgeNative.RuleAction.BLOCK,
+ _ => throw new ArgumentException($"Invalid action in rule file: {rule.Action}")
+ };
+
+ rules.Add((rule.ProcessNames, rule.TargetHosts, rule.TargetPorts, protocol, action));
+ }
+
+ return rules;
+ }
+ catch (System.Text.Json.JsonException ex)
+ {
+ throw new ArgumentException($"Invalid JSON format in rule file: {ex.Message}");
+ }
+ }
+
private static (ProxyBridgeNative.ProxyType Type, string Host, ushort Port, string? Username, string? Password) ParseProxyConfig(string proxyUrl)
{
string? username = null;