Skip to content

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

added Rule EDIT and Delelete

Sourav Kalal171d37120068
diff --git a/gui/Interop/ProxyBridgeNative.cs b/gui/Interop/ProxyBridgeNative.cs
index 81d9f2f..5953257 100644
--- a/gui/Interop/ProxyBridgeNative.cs
+++ b/gui/Interop/ProxyBridgeNative.cs
@@ -69,6 +69,20 @@ public static class ProxyBridgeNative
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ProxyBridge_DisableRule(uint ruleId);
+ [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
+ [return: MarshalAs(UnmanagedType.Bool)]
+ public static extern bool ProxyBridge_DeleteRule(uint ruleId);
+
+ [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
+ [return: MarshalAs(UnmanagedType.Bool)]
+ public static extern bool ProxyBridge_EditRule(
+ uint ruleId,
+ [MarshalAs(UnmanagedType.LPStr)] string processName,
+ [MarshalAs(UnmanagedType.LPStr)] string targetHosts,
+ [MarshalAs(UnmanagedType.LPStr)] string targetPorts,
+ RuleProtocol protocol,
+ RuleAction action);
+
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ProxyBridge_SetProxyConfig(
diff --git a/gui/Services/ProxyBridgeService.cs b/gui/Services/ProxyBridgeService.cs
index 3d26f7f..234b927 100644
--- a/gui/Services/ProxyBridgeService.cs
+++ b/gui/Services/ProxyBridgeService.cs
@@ -88,6 +88,31 @@ public class ProxyBridgeService : IDisposable
return ProxyBridgeNative.ProxyBridge_DisableRule(ruleId);
}
+ public bool DeleteRule(uint ruleId)
+ {
+ return ProxyBridgeNative.ProxyBridge_DeleteRule(ruleId);
+ }
+
+ public bool EditRule(uint ruleId, string processName, string targetHosts, string targetPorts, string protocol, string action)
+ {
+ var ruleAction = action.ToUpper() switch
+ {
+ "DIRECT" => ProxyBridgeNative.RuleAction.DIRECT,
+ "BLOCK" => ProxyBridgeNative.RuleAction.BLOCK,
+ _ => ProxyBridgeNative.RuleAction.PROXY
+ };
+
+ var ruleProtocol = protocol.ToUpper() switch
+ {
+ "UDP" => ProxyBridgeNative.RuleProtocol.UDP,
+ "BOTH" => ProxyBridgeNative.RuleProtocol.BOTH,
+ "TCP+UDP" => ProxyBridgeNative.RuleProtocol.BOTH,
+ _ => ProxyBridgeNative.RuleProtocol.TCP
+ };
+
+ return ProxyBridgeNative.ProxyBridge_EditRule(ruleId, processName, targetHosts, targetPorts, ruleProtocol, ruleAction);
+ }
+
public void SetDnsViaProxy(bool enable)
{
ProxyBridgeNative.ProxyBridge_SetDnsViaProxy(enable);
diff --git a/gui/ViewModels/ProxyRulesViewModel.cs b/gui/ViewModels/ProxyRulesViewModel.cs
index 3785208..07aefe5 100644
--- a/gui/ViewModels/ProxyRulesViewModel.cs
+++ b/gui/ViewModels/ProxyRulesViewModel.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.ObjectModel;
+using System.Linq;
using System.Windows.Input;
using Avalonia.Controls;
using ProxyBridge.GUI.Services;
@@ -9,6 +10,8 @@ namespace ProxyBridge.GUI.ViewModels;
public class ProxyRulesViewModel : ViewModelBase
{
private bool _isAddRuleViewOpen;
+ private bool _isEditMode;
+ private uint _currentEditingRuleId;
private string _newProcessName = "*";
private string _newTargetHosts = "*";
private string _newTargetPorts = "*";
@@ -73,6 +76,8 @@ public class ProxyRulesViewModel : ViewModelBase
public ICommand CancelAddRuleCommand { get; }
public ICommand CloseCommand { get; }
public ICommand BrowseProcessCommand { get; }
+ public ICommand DeleteRuleCommand { get; }
+ public ICommand EditRuleCommand { get; }
public void SetWindow(Window window)
{
@@ -137,19 +142,43 @@ public class ProxyRulesViewModel : ViewModelBase
}
}
- var newRule = new ProxyRule
+ if (_isEditMode && _proxyService != null)
{
- ProcessName = NewProcessName,
- TargetHosts = NewTargetHosts,
- TargetPorts = NewTargetPorts,
- Protocol = NewProtocol,
- Action = NewProxyAction,
- IsEnabled = true
- };
+ // Edit existing rule
+ if (_proxyService.EditRule(_currentEditingRuleId, NewProcessName, NewTargetHosts, NewTargetPorts, NewProtocol, NewProxyAction))
+ {
+ // Update the rule in the ObservableCollection
+ var existingRule = ProxyRules.FirstOrDefault(r => r.RuleId == _currentEditingRuleId);
+ if (existingRule != null)
+ {
+ existingRule.ProcessName = NewProcessName;
+ existingRule.TargetHosts = NewTargetHosts;
+ existingRule.TargetPorts = NewTargetPorts;
+ existingRule.Protocol = NewProtocol;
+ existingRule.Action = NewProxyAction;
+ }
+ }
+
+ _isEditMode = false;
+ _currentEditingRuleId = 0;
+ }
+ else
+ {
+ // Add new rule
+ var newRule = new ProxyRule
+ {
+ ProcessName = NewProcessName,
+ TargetHosts = NewTargetHosts,
+ TargetPorts = NewTargetPorts,
+ Protocol = NewProtocol,
+ Action = NewProxyAction,
+ IsEnabled = true
+ };
- newRule.PropertyChanged += Rule_PropertyChanged;
+ newRule.PropertyChanged += Rule_PropertyChanged;
- _onAddRule?.Invoke(newRule);
+ _onAddRule?.Invoke(newRule);
+ }
IsAddRuleViewOpen = false;
@@ -218,6 +247,106 @@ public class ProxyRulesViewModel : ViewModelBase
}
}
});
+
+ DeleteRuleCommand = new RelayCommandWithParameter<ProxyRule>(async (rule) =>
+ {
+ if (rule == null || _proxyService == null || _window == null)
+ return;
+
+ var result = await ShowConfirmDialogAsync("Delete Rule",
+ $"Are you sure you want to delete the rule for process '{rule.ProcessName}'?");
+
+ if (result)
+ {
+ if (_proxyService.DeleteRule(rule.RuleId))
+ {
+ ProxyRules.Remove(rule);
+ }
+ }
+ });
+
+ EditRuleCommand = new RelayCommandWithParameter<ProxyRule>((rule) =>
+ {
+ if (rule == null)
+ return;
+
+ _isEditMode = true;
+ _currentEditingRuleId = rule.RuleId;
+ NewProcessName = rule.ProcessName;
+ NewTargetHosts = rule.TargetHosts;
+ NewTargetPorts = rule.TargetPorts;
+ NewProtocol = rule.Protocol;
+ NewProxyAction = rule.Action;
+ ProcessNameError = "";
+ IsAddRuleViewOpen = true;
+ });
+ }
+
+ private async System.Threading.Tasks.Task<bool> ShowConfirmDialogAsync(string title, string message)
+ {
+ if (_window == null)
+ return false;
+
+ var messageBox = new Window
+ {
+ Title = title,
+ Width = 400,
+ Height = 150,
+ WindowStartupLocation = WindowStartupLocation.CenterOwner,
+ CanResize = false
+ };
+
+ bool result = false;
+
+ var stackPanel = new StackPanel
+ {
+ Margin = new Avalonia.Thickness(20),
+ Spacing = 10
+ };
+
+ stackPanel.Children.Add(new Avalonia.Controls.TextBlock
+ {
+ Text = message,
+ TextWrapping = Avalonia.Media.TextWrapping.Wrap
+ });
+
+ var buttonPanel = new StackPanel
+ {
+ Orientation = Avalonia.Layout.Orientation.Horizontal,
+ HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Right,
+ Spacing = 10
+ };
+
+ var yesButton = new Button
+ {
+ Content = "Yes",
+ Width = 80
+ };
+ yesButton.Click += (s, e) =>
+ {
+ result = true;
+ messageBox.Close();
+ };
+
+ var noButton = new Button
+ {
+ Content = "No",
+ Width = 80
+ };
+ noButton.Click += (s, e) =>
+ {
+ result = false;
+ messageBox.Close();
+ };
+
+ buttonPanel.Children.Add(yesButton);
+ buttonPanel.Children.Add(noButton);
+ stackPanel.Children.Add(buttonPanel);
+
+ messageBox.Content = stackPanel;
+
+ await messageBox.ShowDialog(_window);
+ return result;
}
private void Rule_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
@@ -235,3 +364,32 @@ public class ProxyRulesViewModel : ViewModelBase
}
}
}
+
+public class RelayCommandWithParameter<T> : ICommand
+{
+ private readonly Action<T> _execute;
+ private readonly Func<T, bool>? _canExecute;
+
+ public RelayCommandWithParameter(Action<T> execute, Func<T, bool>? canExecute = null)
+ {
+ _execute = execute ?? throw new ArgumentNullException(nameof(execute));
+ _canExecute = canExecute;
+ }
+
+ public event EventHandler? CanExecuteChanged;
+
+ public bool CanExecute(object? parameter)
+ {
+ if (parameter is T typedParameter)
+ return _canExecute?.Invoke(typedParameter) ?? true;
+ return false;
+ }
+
+ public void Execute(object? parameter)
+ {
+ if (parameter is T typedParameter)
+ _execute(typedParameter);
+ }
+
+ public void RaiseCanExecuteChanged() => CanExecuteChanged?.Invoke(this, EventArgs.Empty);
+}
diff --git a/gui/Views/ProxyRulesWindow.axaml b/gui/Views/ProxyRulesWindow.axaml
index dc4501a..326b8ee 100644
--- a/gui/Views/ProxyRulesWindow.axaml
+++ b/gui/Views/ProxyRulesWindow.axaml
@@ -9,8 +9,8 @@
x:CompileBindings="True"
Title="Process Rules"
Icon="/Assets/logo.ico"
- Width="700" Height="550"
- MinWidth="600" MinHeight="400"
+ Width="1000" Height="550"
+ MinWidth="900" MinHeight="400"
Background="#FF2D2D30"
WindowStartupLocation="CenterOwner">
@@ -123,7 +123,7 @@
BorderBrush="#FF3F3F46"
BorderThickness="0,1,0,1"
Padding="20,12">
- <Grid ColumnDefinitions="80,60,200,150,120,80,*">
+ <Grid ColumnDefinitions="80,140,60,200,150,120,80,*">
<TextBlock Grid.Column="0"
Text="Enabled"
FontSize="13"
@@ -131,36 +131,42 @@
Foreground="#FFB0B0B0"
Margin="0,0,10,0"/>
<TextBlock Grid.Column="1"
- Text="SR"
+ Text="Actions"
FontSize="13"
FontWeight="SemiBold"
Foreground="#FFB0B0B0"
Margin="0,0,10,0"/>
<TextBlock Grid.Column="2"
- Text="Process"
+ Text="SR"
FontSize="13"
FontWeight="SemiBold"
Foreground="#FFB0B0B0"
Margin="0,0,10,0"/>
<TextBlock Grid.Column="3"
- Text="Target Hosts"
+ Text="Process"
FontSize="13"
FontWeight="SemiBold"
Foreground="#FFB0B0B0"
Margin="0,0,10,0"/>
<TextBlock Grid.Column="4"
- Text="Target Ports"
+ Text="Target Hosts"
FontSize="13"
FontWeight="SemiBold"
Foreground="#FFB0B0B0"
Margin="0,0,10,0"/>
<TextBlock Grid.Column="5"
- Text="Protocol"
+ Text="Target Ports"
FontSize="13"
FontWeight="SemiBold"
Foreground="#FFB0B0B0"
Margin="0,0,10,0"/>
<TextBlock Grid.Column="6"
+ Text="Protocol"
+ FontSize="13"
+ FontWeight="SemiBold"
+ Foreground="#FFB0B0B0"
+ Margin="0,0,10,0"/>
+ <TextBlock Grid.Column="7"
Text="Action"
FontSize="13"
FontWeight="SemiBold"
@@ -179,18 +185,37 @@
BorderBrush="#FF3F3F46"
BorderThickness="0,0,0,1"
Padding="20,16">
- <Grid ColumnDefinitions="80,60,200,150,120,80,*">
+ <Grid ColumnDefinitions="80,140,60,200,150,120,80,*">
<CheckBox Grid.Column="0"
IsChecked="{Binding IsEnabled}"
VerticalAlignment="Center"
Margin="0,0,10,0"/>
- <TextBlock Grid.Column="1"
+ <StackPanel Grid.Column="1"
+ Orientation="Horizontal"
+ Spacing="8"
+ VerticalAlignment="Center"
+ Margin="0,0,10,0">
+ <Button Content="✏ Edit"
+ Command="{Binding $parent[ItemsControl].((vm:ProxyRulesViewModel)DataContext).EditRuleCommand}"
+ CommandParameter="{Binding}"
+ Padding="8,4"
+ MinWidth="60"
+ FontSize="12"/>
+ <Button Content="🗑 Delete"
+ Command="{Binding $parent[ItemsControl].((vm:ProxyRulesViewModel)DataContext).DeleteRuleCommand}"
+ CommandParameter="{Binding}"
+ Padding="8,4"
+ MinWidth="60"
+ FontSize="12"
+ Classes="secondary"/>
+ </StackPanel>
+ <TextBlock Grid.Column="2"
Text="{Binding Index}"
FontSize="14"
Foreground="#FF999999"
VerticalAlignment="Center"
Margin="0,0,10,0"/>
- <TextBlock Grid.Column="2"
+ <TextBlock Grid.Column="3"
Text="{Binding ProcessName}"
FontSize="14"
FontWeight="Medium"
@@ -198,27 +223,27 @@
VerticalAlignment="Center"
TextTrimming="CharacterEllipsis"
Margin="0,0,10,0"/>
- <TextBlock Grid.Column="3"
+ <TextBlock Grid.Column="4"
Text="{Binding TargetHosts}"
FontSize="13"
Foreground="#FF999999"
VerticalAlignment="Center"
TextTrimming="CharacterEllipsis"
Margin="0,0,10,0"/>
- <TextBlock Grid.Column="4"
+ <TextBlock Grid.Column="5"
Text="{Binding TargetPorts}"
FontSize="13"
Foreground="#FF999999"
VerticalAlignment="Center"
TextTrimming="CharacterEllipsis"
Margin="0,0,10,0"/>
- <TextBlock Grid.Column="5"
+ <TextBlock Grid.Column="6"
Text="{Binding Protocol}"
FontSize="13"
Foreground="#FF999999"
VerticalAlignment="Center"
Margin="0,0,10,0"/>
- <TextBlock Grid.Column="6"
+ <TextBlock Grid.Column="7"
Text="{Binding Action}"
FontSize="14"
FontWeight="SemiBold"
diff --git a/src/ProxyBridge.c b/src/ProxyBridge.c
index 3c183d7..e6b64fa 100644
--- a/src/ProxyBridge.c
+++ b/src/ProxyBridge.c
@@ -1923,6 +1923,70 @@ PROXYBRIDGE_API BOOL ProxyBridge_DisableRule(UINT32 rule_id)
return FALSE;
}
+PROXYBRIDGE_API BOOL ProxyBridge_DeleteRule(UINT32 rule_id)
+{
+ if (rule_id == 0)
+ return FALSE;
+
+ PROCESS_RULE *rule = rules_list;
+ PROCESS_RULE *prev = NULL;
+
+ while (rule != NULL)
+ {
+ if (rule->rule_id == rule_id)
+ {
+ if (prev == NULL)
+ rules_list = rule->next;
+ else
+ prev->next = rule->next;
+
+ if (rule->target_hosts != NULL)
+ free(rule->target_hosts);
+ if (rule->target_ports != NULL)
+ free(rule->target_ports);
+ free(rule);
+
+ log_message("Deleted rule ID: %u", rule_id);
+ return TRUE;
+ }
+ prev = rule;
+ rule = rule->next;
+ }
+ return FALSE;
+}
+
+PROXYBRIDGE_API BOOL ProxyBridge_EditRule(UINT32 rule_id, const char* process_name, const char* target_hosts, const char* target_ports, RuleProtocol protocol, RuleAction action)
+{
+ if (rule_id == 0 || process_name == NULL || target_hosts == NULL || target_ports == NULL)
+ return FALSE;
+
+ PROCESS_RULE *rule = rules_list;
+ while (rule != NULL)
+ {
+ if (rule->rule_id == rule_id)
+ {
+ strncpy(rule->process_name, process_name, MAX_PROCESS_NAME - 1);
+ rule->process_name[MAX_PROCESS_NAME - 1] = '\0';
+
+ if (rule->target_hosts != NULL)
+ free(rule->target_hosts);
+ rule->target_hosts = _strdup(target_hosts);
+
+ if (rule->target_ports != NULL)
+ free(rule->target_ports);
+ rule->target_ports = _strdup(target_ports);
+
+ rule->protocol = protocol;
+ rule->action = action;
+
+ log_message("Updated rule ID: %u", rule_id);
+ return TRUE;
+ }
+ rule = rule->next;
+ }
+ return FALSE;
+}
+
PROXYBRIDGE_API BOOL ProxyBridge_SetProxyConfig(ProxyType type, const char* proxy_ip, UINT16 proxy_port, const char* username, const char* password)
{
if (proxy_ip == NULL || proxy_ip[0] == '\0' || proxy_port == 0)
diff --git a/src/ProxyBridge.h b/src/ProxyBridge.h
index c3e0af1..70f5b09 100644
--- a/src/ProxyBridge.h
+++ b/src/ProxyBridge.h
@@ -36,6 +36,8 @@ typedef enum {
PROXYBRIDGE_API UINT32 ProxyBridge_AddRule(const char* process_name, const char* target_hosts, const char* target_ports, RuleProtocol protocol, RuleAction action);
PROXYBRIDGE_API BOOL ProxyBridge_EnableRule(UINT32 rule_id);
PROXYBRIDGE_API BOOL ProxyBridge_DisableRule(UINT32 rule_id);
+PROXYBRIDGE_API BOOL ProxyBridge_DeleteRule(UINT32 rule_id);
+PROXYBRIDGE_API BOOL ProxyBridge_EditRule(UINT32 rule_id, const char* process_name, const char* target_hosts, const char* target_ports, RuleProtocol protocol, RuleAction action);
PROXYBRIDGE_API BOOL ProxyBridge_SetProxyConfig(ProxyType type, const char* proxy_ip, UINT16 proxy_port, const char* username, const char* password);
PROXYBRIDGE_API void ProxyBridge_SetDnsViaProxy(BOOL enable);
PROXYBRIDGE_API void ProxyBridge_SetLogCallback(LogCallback callback);