Skip to content

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

Windows Version 4.0.0

Sourav Kalaled7bc7da080f
diff --git a/Windows/cli/Program.cs b/Windows/cli/Program.cs
index d8aa59d..de922f8 100644
--- a/Windows/cli/Program.cs
+++ b/Windows/cli/Program.cs
@@ -22,6 +22,36 @@ public partial class ProxyRuleJsonContext : JsonSerializerContext
{
}
+static class ParsingHelpers
+{
+ public static ProxyBridgeNative.RuleProtocol ParseProtocol(string protocolStr)
+ {
+ return protocolStr.ToUpper() switch
+ {
+ "TCP" => ProxyBridgeNative.RuleProtocol.TCP,
+ "UDP" => ProxyBridgeNative.RuleProtocol.UDP,
+ "BOTH" => ProxyBridgeNative.RuleProtocol.BOTH,
+ _ => throw new ArgumentException($"Invalid protocol: {protocolStr}. Use TCP, UDP, or BOTH")
+ };
+ }
+
+ public static ProxyBridgeNative.RuleAction ParseAction(string actionStr)
+ {
+ return actionStr.ToUpper() switch
+ {
+ "PROXY" => ProxyBridgeNative.RuleAction.PROXY,
+ "DIRECT" => ProxyBridgeNative.RuleAction.DIRECT,
+ "BLOCK" => ProxyBridgeNative.RuleAction.BLOCK,
+ _ => throw new ArgumentException($"Invalid action: {actionStr}. Use PROXY, DIRECT, or BLOCK")
+ };
+ }
+
+ public static string DefaultIfEmpty(string value, string defaultValue = "*")
+ {
+ return string.IsNullOrWhiteSpace(value) ? defaultValue : value;
+ }
+}
+
class Program
{
private static ProxyBridgeNative.LogCallback? _logCallback;
@@ -294,21 +324,8 @@ class Program
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}")
- };
+ var protocol = ParsingHelpers.ParseProtocol(rule.Protocol);
+ var action = ParsingHelpers.ParseAction(rule.Action);
rules.Add((rule.ProcessNames, rule.TargetHosts, rule.TargetPorts, protocol, action));
}
@@ -369,33 +386,11 @@ class Program
throw new ArgumentException($"Invalid rule format: {rule}\nExpected format: process:hosts:ports:protocol:action");
}
- // Don't trim semicolons - they are valid separators for multiple values
- var processName = parts[0].Trim();
- var targetHosts = parts[1].Trim();
- var targetPorts = parts[2].Trim();
- var protocolStr = parts[3].Trim().ToUpper();
- var actionStr = parts[4].Trim().ToUpper();
-
- // Handle empty fields - use "*" as default
- if (string.IsNullOrWhiteSpace(processName)) processName = "*";
- if (string.IsNullOrWhiteSpace(targetHosts)) targetHosts = "*";
- if (string.IsNullOrWhiteSpace(targetPorts)) targetPorts = "*";
-
- var protocol = protocolStr switch
- {
- "TCP" => ProxyBridgeNative.RuleProtocol.TCP,
- "UDP" => ProxyBridgeNative.RuleProtocol.UDP,
- "BOTH" => ProxyBridgeNative.RuleProtocol.BOTH,
- _ => throw new ArgumentException($"Invalid protocol: {protocolStr}. Use TCP, UDP, or BOTH")
- };
-
- var action = actionStr switch
- {
- "PROXY" => ProxyBridgeNative.RuleAction.PROXY,
- "DIRECT" => ProxyBridgeNative.RuleAction.DIRECT,
- "BLOCK" => ProxyBridgeNative.RuleAction.BLOCK,
- _ => throw new ArgumentException($"Invalid action: {actionStr}. Use PROXY, DIRECT, or BLOCK")
- };
+ var processName = ParsingHelpers.DefaultIfEmpty(parts[0].Trim());
+ var targetHosts = ParsingHelpers.DefaultIfEmpty(parts[1].Trim());
+ var targetPorts = ParsingHelpers.DefaultIfEmpty(parts[2].Trim());
+ var protocol = ParsingHelpers.ParseProtocol(parts[3].Trim());
+ var action = ParsingHelpers.ParseAction(parts[4].Trim());
parsedRules.Add((processName, targetHosts, targetPorts, protocol, action));
}
diff --git a/Windows/compile.ps1 b/Windows/compile.ps1
index 124a097..befe24f 100644
--- a/Windows/compile.ps1
+++ b/Windows/compile.ps1
@@ -195,7 +195,17 @@ if ($success) {
}
Write-Host "`nPublishing GUI..." -ForegroundColor Green
- $publishResult = dotnet publish gui/ProxyBridge.GUI.csproj -c Release -r win-x64 --self-contained -o gui/bin/Release/net10.0-windows/win-x64/publish 2>&1
+ $publishResult = dotnet publish gui/ProxyBridge.GUI.csproj -c Release -r win-x64 --self-contained `
+ /p:PublishTrimmed=true `
+ /p:PublishSingleFile=false `
+ /p:EnableCompressionInSingleFile=true `
+ /p:DebugType=None `
+ /p:DebugSymbols=false `
+ /p:Optimize=true `
+ /p:TieredCompilation=true `
+ /p:TieredCompilationQuickJit=false `
+ /p:ReadyToRun=true `
+ -o gui/bin/Release/net10.0-windows/win-x64/publish 2>&1
if ($LASTEXITCODE -eq 0) {
Write-Host " GUI published successfully" -ForegroundColor Gray
@@ -209,13 +219,27 @@ if ($success) {
Copy-Item $_.FullName -Destination $OutputDir -Force
Write-Host " Copied: $($_.Name)" -ForegroundColor Gray
}
+
+ Write-Host "`nCleaning up GUI build artifacts..." -ForegroundColor Yellow
+ Remove-Item "gui\bin" -Recurse -Force -ErrorAction SilentlyContinue
+ Remove-Item "gui\obj" -Recurse -Force -ErrorAction SilentlyContinue
} else {
Write-Host " GUI publish failed!" -ForegroundColor Red
Write-Host $publishResult
}
Write-Host "`nPublishing CLI..." -ForegroundColor Green
- $publishResult = dotnet publish cli/ProxyBridge.CLI.csproj -c Release -r win-x64 --self-contained -o cli/bin/Release/net10.0-windows/win-x64/publish 2>&1
+ $publishResult = dotnet publish cli/ProxyBridge.CLI.csproj -c Release -r win-x64 --self-contained `
+ /p:PublishTrimmed=true `
+ /p:PublishSingleFile=true `
+ /p:EnableCompressionInSingleFile=true `
+ /p:DebugType=None `
+ /p:DebugSymbols=false `
+ /p:Optimize=true `
+ /p:TieredCompilation=true `
+ /p:TieredCompilationQuickJit=false `
+ /p:ReadyToRun=true `
+ -o cli/bin/Release/net10.0-windows/win-x64/publish 2>&1
if ($LASTEXITCODE -eq 0) {
Write-Host " CLI published successfully" -ForegroundColor Gray
@@ -224,6 +248,10 @@ if ($success) {
Copy-Item "$cliPublishPath\ProxyBridge_CLI.exe" -Destination $OutputDir -Force
Write-Host " Copied: ProxyBridge_CLI.exe" -ForegroundColor Gray
+
+ Write-Host "`nCleaning up CLI build artifacts..." -ForegroundColor Yellow
+ Remove-Item "cli\bin" -Recurse -Force -ErrorAction SilentlyContinue
+ Remove-Item "cli\obj" -Recurse -Force -ErrorAction SilentlyContinue
} else {
Write-Host " CLI publish failed!" -ForegroundColor Red
Write-Host $publishResult