Skip to content

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

#60 - readded fixed rule priority

Sourav Kalal47063b9cc344
diff --git a/Windows/gui/Interop/ProxyBridgeNative.cs b/Windows/gui/Interop/ProxyBridgeNative.cs
index 4c1c514..4451f82 100644
--- a/Windows/gui/Interop/ProxyBridgeNative.cs
+++ b/Windows/gui/Interop/ProxyBridgeNative.cs
@@ -83,6 +83,13 @@ public static class ProxyBridgeNative
RuleProtocol protocol,
RuleAction action);
+ [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
+ public static extern uint ProxyBridge_GetRulePosition(uint ruleId);
+
+ [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
+ [return: MarshalAs(UnmanagedType.Bool)]
+ public static extern bool ProxyBridge_MoveRuleToPosition(uint ruleId, uint newPosition);
+
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ProxyBridge_SetProxyConfig(
diff --git a/Windows/gui/Services/ProxyBridgeService.cs b/Windows/gui/Services/ProxyBridgeService.cs
index 6ddd5cc..be93ae3 100644
--- a/Windows/gui/Services/ProxyBridgeService.cs
+++ b/Windows/gui/Services/ProxyBridgeService.cs
@@ -113,6 +113,16 @@ public class ProxyBridgeService : IDisposable
return ProxyBridgeNative.ProxyBridge_EditRule(ruleId, processName, targetHosts, targetPorts, ruleProtocol, ruleAction);
}
+ public uint GetRulePosition(uint ruleId)
+ {
+ return ProxyBridgeNative.ProxyBridge_GetRulePosition(ruleId);
+ }
+
+ public bool MoveRuleToPosition(uint ruleId, uint newPosition)
+ {
+ return ProxyBridgeNative.ProxyBridge_MoveRuleToPosition(ruleId, newPosition);
+ }
+
public void SetDnsViaProxy(bool enable)
{
ProxyBridgeNative.ProxyBridge_SetDnsViaProxy(enable);
diff --git a/Windows/gui/ViewModels/ProxyRulesViewModel.cs b/Windows/gui/ViewModels/ProxyRulesViewModel.cs
index 61ebb77..811b10e 100644
--- a/Windows/gui/ViewModels/ProxyRulesViewModel.cs
+++ b/Windows/gui/ViewModels/ProxyRulesViewModel.cs
@@ -96,6 +96,14 @@ public class ProxyRulesViewModel : ViewModelBase
_window = window;
}
+ public bool MoveRuleToPosition(uint ruleId, uint newPosition)
+ {
+ if (_proxyService == null)
+ return false;
+
+ return _proxyService.MoveRuleToPosition(ruleId, newPosition);
+ }
+
private void ResetRuleForm()
{
NewProcessName = "*";
diff --git a/Windows/gui/Views/ProxyRulesWindow.axaml b/Windows/gui/Views/ProxyRulesWindow.axaml
index 620a051..7da1fc1 100644
--- a/Windows/gui/Views/ProxyRulesWindow.axaml
+++ b/Windows/gui/Views/ProxyRulesWindow.axaml
@@ -151,7 +151,9 @@
<Border Background="#FF252526"
BorderBrush="#FF3F3F46"
BorderThickness="0,0,0,1"
- Padding="20,16">
+ Padding="20,16"
+ PointerPressed="Rule_PointerPressed"
+ DragDrop.AllowDrop="True">
<Grid ColumnDefinitions="60,80,140,60,200,150,120,80,*">
<CheckBox Grid.Column="0"
IsChecked="{Binding IsSelected}"
diff --git a/Windows/gui/Views/ProxyRulesWindow.axaml.cs b/Windows/gui/Views/ProxyRulesWindow.axaml.cs
index 8ef6814..f25266d 100644
--- a/Windows/gui/Views/ProxyRulesWindow.axaml.cs
+++ b/Windows/gui/Views/ProxyRulesWindow.axaml.cs
@@ -4,6 +4,7 @@ using System.Globalization;
using Avalonia.Controls;
using Avalonia.Data.Converters;
using Avalonia.Interactivity;
+using Avalonia.Input;
using ProxyBridge.GUI.ViewModels;
namespace ProxyBridge.GUI.Views;
@@ -48,6 +49,12 @@ public partial class ProxyRulesWindow : Window
}
this.DataContextChanged += ProxyRulesWindow_DataContextChanged;
+
+ if (this.FindControl<ItemsControl>("RulesItemsControl") is ItemsControl itemsControl)
+ {
+ itemsControl.AddHandler(DragDrop.DropEvent, Rules_Drop);
+ itemsControl.AddHandler(DragDrop.DragOverEvent, Rules_DragOver);
+ }
}
private void ProxyRulesWindow_DataContextChanged(object? sender, EventArgs e)
@@ -151,4 +158,70 @@ public partial class ProxyRulesWindow : Window
vm.NewProtocol = tag;
}
}
+
+ private async void Rule_PointerPressed(object? sender, PointerPressedEventArgs e)
+ {
+ if (sender is not Border border || border.DataContext is not ProxyRule rule)
+ return;
+
+ var dragData = new DataObject();
+ dragData.Set("DraggedRule", rule);
+
+ var result = await DragDrop.DoDragDrop(e, dragData, DragDropEffects.Move);
+
+ if (result == DragDropEffects.Move && DataContext is ProxyRulesViewModel vm)
+ {
+ // refsh indices after drag completes
+ for (int i = 0; i < vm.ProxyRules.Count; i++)
+ {
+ vm.ProxyRules[i].Index = i + 1;
+ }
+ }
+ }
+
+ private void Rules_DragOver(object? sender, DragEventArgs e)
+ {
+ e.DragEffects = DragDropEffects.Move;
+ }
+
+ private void Rules_Drop(object? sender, DragEventArgs e)
+ {
+ if (DataContext is not ProxyRulesViewModel vm)
+ return;
+
+ if (e.Data.Get("DraggedRule") is not ProxyRule draggedRule)
+ return;
+
+ if (e.Source is Control control)
+ {
+ var current = control;
+ while (current != null && current is not Border)
+ {
+ current = current.Parent as Control;
+ }
+
+ if (current is Border border && border.DataContext is ProxyRule targetRule)
+ {
+ if (draggedRule.RuleId == targetRule.RuleId)
+ return;
+
+ int draggedIndex = vm.ProxyRules.IndexOf(draggedRule);
+ int targetIndex = vm.ProxyRules.IndexOf(targetRule);
+
+ if (draggedIndex == -1 || targetIndex == -1 || draggedIndex == targetIndex)
+ return;
+
+ uint newPosition = (uint)(targetIndex + 1);
+ if (vm.MoveRuleToPosition(draggedRule.RuleId, newPosition))
+ {
+ vm.ProxyRules.Move(draggedIndex, targetIndex);
+
+ for (int i = 0; i < vm.ProxyRules.Count; i++)
+ {
+ vm.ProxyRules[i].Index = i + 1;
+ }
+ }
+ }
+ }
+ }
}
\ No newline at end of file
diff --git a/Windows/src/ProxyBridge.c b/Windows/src/ProxyBridge.c
index 8fadece..c44434e 100644
--- a/Windows/src/ProxyBridge.c
+++ b/Windows/src/ProxyBridge.c
@@ -2393,6 +2393,92 @@ PROXYBRIDGE_API BOOL ProxyBridge_EditRule(UINT32 rule_id, const char* process_na
return FALSE;
}
+PROXYBRIDGE_API UINT32 ProxyBridge_GetRulePosition(UINT32 rule_id)
+{
+ if (rule_id == 0)
+ return 0;
+
+ UINT32 position = 1;
+ PROCESS_RULE *rule = rules_list;
+ while (rule != NULL)
+ {
+ if (rule->rule_id == rule_id)
+ return position;
+ position++;
+ rule = rule->next;
+ }
+ return 0;
+}
+
+PROXYBRIDGE_API BOOL ProxyBridge_MoveRuleToPosition(UINT32 rule_id, UINT32 new_position)
+{
+ if (rule_id == 0 || new_position == 0)
+ return FALSE;
+
+ // first rule and remove it from current position
+ PROCESS_RULE *rule = rules_list;
+ PROCESS_RULE *prev = NULL;
+
+ while (rule != NULL)
+ {
+ if (rule->rule_id == rule_id)
+ break;
+ prev = rule;
+ rule = rule->next;
+ }
+
+ if (rule == NULL)
+ return FALSE;
+
+ // Remove from current position
+ if (prev == NULL)
+ {
+ rules_list = rule->next;
+ }
+ else
+ {
+ prev->next = rule->next;
+ }
+
+ // Insert at new position
+ if (new_position == 1)
+ {
+ // Insert at head
+ rule->next = rules_list;
+ rules_list = rule;
+ }
+ else
+ {
+ // taken from stackflow
+ PROCESS_RULE *current = rules_list;
+ UINT32 pos = 1;
+
+ while (current != NULL && pos < new_position - 1)
+ {
+ current = current->next;
+ pos++;
+ }
+
+ if (current == NULL)
+ {
+ // position is beyond list end we can append to tail
+ current = rules_list;
+ while (current->next != NULL)
+ current = current->next;
+ current->next = rule;
+ rule->next = NULL;
+ }
+ else
+ {
+ rule->next = current->next;
+ current->next = rule;
+ }
+ }
+
+ log_message("Moved rule ID %u to position %u", rule_id, new_position);
+ return TRUE;
+}
+
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/Windows/src/ProxyBridge.h b/Windows/src/ProxyBridge.h
index 6f7f1c1..ad6cd31 100644
--- a/Windows/src/ProxyBridge.h
+++ b/Windows/src/ProxyBridge.h
@@ -38,6 +38,8 @@ 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_MoveRuleToPosition(UINT32 rule_id, UINT32 new_position); // Move rule to specific position (1=first, 2=second, etc)
+PROXYBRIDGE_API UINT32 ProxyBridge_GetRulePosition(UINT32 rule_id); // Get current position of rule in list (1-based)
PROXYBRIDGE_API BOOL ProxyBridge_SetProxyConfig(ProxyType type, const char* proxy_ip, UINT16 proxy_port, const char* username, const char* password); // proxy_ip can be IP address or hostname
PROXYBRIDGE_API void ProxyBridge_SetDnsViaProxy(BOOL enable);
PROXYBRIDGE_API void ProxyBridge_SetLogCallback(LogCallback callback);