Skip to content

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

#22 added rule import export in Windows GUI

Sourav Kalal20630cdc16a9
diff --git a/README.md b/README.md
index 2a4ed26..3ecf769 100644
--- a/README.md
+++ b/README.md
@@ -126,6 +126,45 @@ Launch `ProxyBridge.exe` (GUI) with Administrator privileges for an intuitive gr
4. Click **Save Rule** to apply the configuration
+#### Exporting and Importing Rules
+
+ProxyBridge allows you to export selected rules to a JSON file and import rules from previously exported files, making it easy to share configurations between systems or back up your rule sets.
+
+**Export Rules:**
+1. In the **Proxy Rules** window, select one or more rules using the checkboxes in the **Select** column
+2. Click the **Select All** button to select all rules (optional)
+3. Click the **Export** button (only enabled when at least one rule is selected)
+4. Choose a location and save the JSON file
+
+**Import Rules:**
+1. Click the **Import** button
+2. Select a previously exported JSON file
+3. Rules will be imported and added to your existing rules
+
+**JSON Format Example:**
+```json
+[
+ {
+ "processNames": "chrome.exe",
+ "targetHosts": "*",
+ "targetPorts": "*",
+ "protocol": "TCP",
+ "action": "PROXY",
+ "enabled": true
+ },
+ {
+ "processNames": "firefox.exe",
+ "targetHosts": "192.168.*.*",
+ "targetPorts": "80;443",
+ "protocol": "BOTH",
+ "action": "DIRECT",
+ "enabled": true
+ }
+]
+```
+
+**Note:** The JSON format is compatible across platforms, allowing you to export rules from macOS and import them on Windows, and vice versa.
+
#### Activity Monitoring
<p align="center">
diff --git a/Windows/gui/ViewModels/MainWindowViewModel.cs b/Windows/gui/ViewModels/MainWindowViewModel.cs
index 92e6d95..ec40b04 100644
--- a/Windows/gui/ViewModels/MainWindowViewModel.cs
+++ b/Windows/gui/ViewModels/MainWindowViewModel.cs
@@ -693,6 +693,7 @@ public class ProxyRule : ViewModelBase
private string _protocol = "TCP";
private string _action = "PROXY";
private bool _isEnabled = true;
+ private bool _isSelected = false;
private int _index;
private uint _ruleId;
@@ -743,6 +744,12 @@ public class ProxyRule : ViewModelBase
get => _isEnabled;
set => SetProperty(ref _isEnabled, value);
}
+
+ public bool IsSelected
+ {
+ get => _isSelected;
+ set => SetProperty(ref _isSelected, value);
+ }
}
// Simple ICommand implementation
diff --git a/Windows/gui/ViewModels/ProxyRulesViewModel.cs b/Windows/gui/ViewModels/ProxyRulesViewModel.cs
index d688bcd..912532d 100644
--- a/Windows/gui/ViewModels/ProxyRulesViewModel.cs
+++ b/Windows/gui/ViewModels/ProxyRulesViewModel.cs
@@ -1,6 +1,7 @@
using System;
using System.Collections.ObjectModel;
using System.Linq;
+using System.Text.Json.Serialization;
using System.Windows.Input;
using Avalonia.Controls;
using ProxyBridge.GUI.Services;
@@ -81,6 +82,12 @@ public class ProxyRulesViewModel : ViewModelBase
public ICommand BrowseProcessCommand { get; }
public ICommand DeleteRuleCommand { get; }
public ICommand EditRuleCommand { get; }
+ public ICommand ToggleSelectAllCommand { get; }
+ public ICommand ExportRulesCommand { get; }
+ public ICommand ImportRulesCommand { get; }
+
+ public bool HasSelectedRules => ProxyRules.Any(r => r.IsSelected);
+ public bool AllRulesSelected => ProxyRules.Any() && ProxyRules.All(r => r.IsSelected);
public void SetWindow(Window window)
{
@@ -283,6 +290,41 @@ public class ProxyRulesViewModel : ViewModelBase
ProcessNameError = "";
IsAddRuleViewOpen = true;
});
+
+ ToggleSelectAllCommand = new RelayCommand(() =>
+ {
+ bool selectAll = !AllRulesSelected;
+ foreach (var rule in ProxyRules)
+ {
+ rule.IsSelected = selectAll;
+ }
+ OnPropertyChanged(nameof(HasSelectedRules));
+ OnPropertyChanged(nameof(AllRulesSelected));
+ });
+
+ ExportRulesCommand = new RelayCommand(async () =>
+ {
+ try
+ {
+ await ExportSelectedRulesAsync();
+ }
+ catch (Exception ex)
+ {
+ await ShowMessageAsync("Export Failed", $"Failed to export rules: {ex.Message}");
+ }
+ });
+
+ ImportRulesCommand = new RelayCommand(async () =>
+ {
+ try
+ {
+ await ImportRulesAsync();
+ }
+ catch (Exception ex)
+ {
+ await ShowMessageAsync("Import Failed", $"Failed to import rules: {ex.Message}");
+ }
+ });
}
private async System.Threading.Tasks.Task<bool> ShowConfirmDialogAsync(string title, string message)
@@ -365,9 +407,219 @@ public class ProxyRulesViewModel : ViewModelBase
_proxyService.DisableRule(rule.RuleId);
}
}
+ else if (e.PropertyName == nameof(ProxyRule.IsSelected))
+ {
+ OnPropertyChanged(nameof(HasSelectedRules));
+ OnPropertyChanged(nameof(AllRulesSelected));
+ }
+ }
+
+ private async System.Threading.Tasks.Task ExportSelectedRulesAsync()
+ {
+ if (_window == null)
+ return;
+
+ var selectedRules = ProxyRules.Where(r => r.IsSelected).ToList();
+
+ if (!selectedRules.Any())
+ {
+ await ShowMessageAsync("No Rules Selected", "Please select at least one rule to export.");
+ return;
+ }
+
+ var saveDialog = new Avalonia.Platform.Storage.FilePickerSaveOptions
+ {
+ Title = "Export Proxy Rules",
+ SuggestedFileName = "ProxyBridge-Rules.json",
+ FileTypeChoices = new[]
+ {
+ new Avalonia.Platform.Storage.FilePickerFileType("JSON Files")
+ {
+ Patterns = new[] { "*.json" }
+ }
+ }
+ };
+
+ var result = await _window.StorageProvider.SaveFilePickerAsync(saveDialog);
+
+ if (result != null)
+ {
+ var exportData = selectedRules.Select(r => new ProxyRuleExport
+ {
+ ProcessNames = r.ProcessName,
+ TargetHosts = r.TargetHosts,
+ TargetPorts = r.TargetPorts,
+ Protocol = r.Protocol,
+ Action = r.Action,
+ Enabled = r.IsEnabled
+ }).ToList();
+
+ var json = System.Text.Json.JsonSerializer.Serialize(exportData, ProxyRuleJsonContext.Default.ListProxyRuleExport);
+
+ await System.IO.File.WriteAllTextAsync(result.Path.LocalPath, json);
+
+ await ShowMessageAsync("Export Successful", $"Exported {selectedRules.Count} rule(s) to:\n{result.Path.LocalPath}");
+ }
+ }
+
+ private async System.Threading.Tasks.Task ImportRulesAsync()
+ {
+ if (_window == null)
+ return;
+
+ if (_proxyService == null)
+ {
+ await ShowMessageAsync("Import Failed", "Proxy service is not available.");
+ return;
+ }
+
+ var openDialog = new Avalonia.Platform.Storage.FilePickerOpenOptions
+ {
+ Title = "Import Proxy Rules",
+ AllowMultiple = false,
+ FileTypeFilter = new[]
+ {
+ new Avalonia.Platform.Storage.FilePickerFileType("JSON Files")
+ {
+ Patterns = new[] { "*.json" }
+ }
+ }
+ };
+
+ var result = await _window.StorageProvider.OpenFilePickerAsync(openDialog);
+
+ if (result != null && result.Count > 0)
+ {
+ var filePath = result[0].Path.LocalPath;
+
+ var json = await System.IO.File.ReadAllTextAsync(filePath);
+
+ var importedRules = System.Text.Json.JsonSerializer.Deserialize(json, ProxyRuleJsonContext.Default.ListProxyRuleExport);
+
+ if (importedRules != null && importedRules.Count > 0)
+ {
+ int successCount = 0;
+ foreach (var ruleData in importedRules)
+ {
+ var ruleId = _proxyService.AddRule(
+ ruleData.ProcessNames,
+ ruleData.TargetHosts,
+ ruleData.TargetPorts,
+ ruleData.Protocol,
+ ruleData.Action
+ );
+
+ if (ruleId > 0)
+ {
+ var newRule = new ProxyRule
+ {
+ RuleId = ruleId,
+ ProcessName = ruleData.ProcessNames,
+ TargetHosts = ruleData.TargetHosts,
+ TargetPorts = ruleData.TargetPorts,
+ Protocol = ruleData.Protocol,
+ Action = ruleData.Action,
+ IsEnabled = ruleData.Enabled,
+ Index = ProxyRules.Count + 1
+ };
+
+ newRule.PropertyChanged += Rule_PropertyChanged;
+ ProxyRules.Add(newRule);
+
+ if (!ruleData.Enabled)
+ {
+ _proxyService.DisableRule(ruleId);
+ }
+
+ successCount++;
+ }
+ }
+
+ await ShowMessageAsync("Import Successful", $"Imported {successCount} rule(s) from:\n{filePath}");
+ }
+ else
+ {
+ await ShowMessageAsync("Import Failed", "No valid rules found in the selected file.");
+ }
+ }
+ }
+
+ private async System.Threading.Tasks.Task ShowMessageAsync(string title, string message)
+ {
+ if (_window == null)
+ return;
+
+ var messageBox = new Window
+ {
+ Title = title,
+ Width = 450,
+ Height = 180,
+ WindowStartupLocation = WindowStartupLocation.CenterOwner,
+ CanResize = false,
+ Background = new Avalonia.Media.SolidColorBrush(Avalonia.Media.Color.Parse("#FF2D2D30"))
+ };
+
+ var stackPanel = new StackPanel
+ {
+ Margin = new Avalonia.Thickness(20),
+ Spacing = 15
+ };
+
+ stackPanel.Children.Add(new Avalonia.Controls.TextBlock
+ {
+ Text = message,
+ TextWrapping = Avalonia.Media.TextWrapping.Wrap,
+ Foreground = new Avalonia.Media.SolidColorBrush(Avalonia.Media.Colors.White),
+ FontSize = 13
+ });
+
+ var buttonPanel = new StackPanel
+ {
+ Orientation = Avalonia.Layout.Orientation.Horizontal,
+ HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Right,
+ Spacing = 10
+ };
+
+ var okButton = new Button
+ {
+ Content = "OK",
+ Width = 80,
+ Background = new Avalonia.Media.SolidColorBrush(Avalonia.Media.Color.Parse("#FF0E639C")),
+ Foreground = new Avalonia.Media.SolidColorBrush(Avalonia.Media.Colors.White)
+ };
+ okButton.Click += (s, e) => messageBox.Close();
+
+ buttonPanel.Children.Add(okButton);
+ stackPanel.Children.Add(buttonPanel);
+
+ messageBox.Content = stackPanel;
+
+ await messageBox.ShowDialog(_window);
}
}
+// JSON export/import model matching macOS format
+public class ProxyRuleExport
+{
+ public string ProcessNames { get; set; } = "*";
+ public string TargetHosts { get; set; } = "*";
+ public string TargetPorts { get; set; } = "*";
+ public string Protocol { get; set; } = "BOTH";
+ public string Action { get; set; } = "DIRECT";
+ public bool Enabled { get; set; } = true;
+}
+
+// JSON serialization context for NativeAOT compatibility
+[JsonSourceGenerationOptions(
+ WriteIndented = true,
+ PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase,
+ PropertyNameCaseInsensitive = true)]
+[JsonSerializable(typeof(System.Collections.Generic.List<ProxyRuleExport>))]
+[JsonSerializable(typeof(ProxyRuleExport))]
+internal partial class ProxyRuleJsonContext : JsonSerializerContext
+{
+}
+
public class RelayCommandWithParameter<T> : ICommand
{
private readonly Action<T> _execute;
diff --git a/Windows/gui/Views/ProxyRulesWindow.axaml b/Windows/gui/Views/ProxyRulesWindow.axaml
index 07d1d81..0064ece 100644
--- a/Windows/gui/Views/ProxyRulesWindow.axaml
+++ b/Windows/gui/Views/ProxyRulesWindow.axaml
@@ -1,6 +1,7 @@
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="using:ProxyBridge.GUI.ViewModels"
+ xmlns:views="using:ProxyBridge.GUI.Views"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="700" d:DesignHeight="550"
@@ -14,6 +15,11 @@
Background="#FF2D2D30"
WindowStartupLocation="CenterOwner">
+ <Window.Resources>
+ <views:SelectAllTextConverter x:Key="SelectAllTextConverter"/>
+ <views:SelectAllIconConverter x:Key="SelectAllIconConverter"/>
+ </Window.Resources>
+
<Window.Styles>
<Style Selector="TextBlock.label">
<Setter Property="Foreground" Value="#FFE0E0E0"/>
@@ -103,10 +109,42 @@
Foreground="#FFE0E0E0"
VerticalAlignment="Center"/>
</StackPanel>
- <Button Grid.Column="1"
- Content="{Binding Loc.ButtonAdd}"
- Command="{Binding AddRuleCommand}"
- IsVisible="{Binding !IsAddRuleViewOpen}"/>
+ <StackPanel Grid.Column="1"
+ Orientation="Horizontal"
+ Spacing="8"
+ IsVisible="{Binding !IsAddRuleViewOpen}">
+ <Button Content="{Binding AllRulesSelected, Converter={StaticResource SelectAllTextConverter}}"
+ Command="{Binding ToggleSelectAllCommand}"
+ Classes="secondary"
+ Padding="12,8"
+ IsEnabled="{Binding ProxyRules.Count}">
+ <Button.Content>
+ <StackPanel Orientation="Horizontal" Spacing="6">
+ <TextBlock Text="{Binding AllRulesSelected, Converter={StaticResource SelectAllIconConverter}}" FontSize="14"/>
+ <TextBlock Text="{Binding AllRulesSelected, Converter={StaticResource SelectAllTextConverter}}"/>
+ </StackPanel>
+ </Button.Content>
+ </Button>
+ <Button Command="{Binding ExportRulesCommand}"
+ Classes="secondary"
+ Padding="12,8"
+ IsEnabled="{Binding HasSelectedRules}">
+ <StackPanel Orientation="Horizontal" Spacing="6">
+ <TextBlock Text="↑" FontSize="14"/>
+ <TextBlock Text="Export"/>
+ </StackPanel>
+ </Button>
+ <Button Command="{Binding ImportRulesCommand}"
+ Classes="secondary"
+ Padding="12,8">
+ <StackPanel Orientation="Horizontal" Spacing="6">
+ <TextBlock Text="↓" FontSize="14"/>
+ <TextBlock Text="Import"/>
+ </StackPanel>
+ </Button>
+ <Button Content="{Binding Loc.ButtonAdd}"
+ Command="{Binding AddRuleCommand}"/>
+ </StackPanel>
</Grid>
</Border>
@@ -123,50 +161,56 @@
BorderBrush="#FF3F3F46"
BorderThickness="0,1,0,1"
Padding="20,12">
- <Grid ColumnDefinitions="80,140,60,200,150,120,80,*">
+ <Grid ColumnDefinitions="60,80,140,60,200,150,120,80,*">
<TextBlock Grid.Column="0"
- Text="{Binding Loc.EnabledLabel}"
+ Text="Select"
FontSize="13"
FontWeight="SemiBold"
Foreground="#FFB0B0B0"
Margin="0,0,10,0"/>
<TextBlock Grid.Column="1"
- Text="{Binding Loc.ActionsLabel}"
+ Text="{Binding Loc.EnabledLabel}"
FontSize="13"
FontWeight="SemiBold"
Foreground="#FFB0B0B0"
Margin="0,0,10,0"/>
<TextBlock Grid.Column="2"
- Text="{Binding Loc.SRLabel}"
+ Text="{Binding Loc.ActionsLabel}"
FontSize="13"
FontWeight="SemiBold"
Foreground="#FFB0B0B0"
Margin="0,0,10,0"/>
<TextBlock Grid.Column="3"
- Text="{Binding Loc.ProcessLabel}"
+ Text="{Binding Loc.SRLabel}"
FontSize="13"
FontWeight="SemiBold"
Foreground="#FFB0B0B0"
Margin="0,0,10,0"/>
<TextBlock Grid.Column="4"
- Text="{Binding Loc.TargetHostsLabel}"
+ Text="{Binding Loc.ProcessLabel}"
FontSize="13"
FontWeight="SemiBold"
Foreground="#FFB0B0B0"
Margin="0,0,10,0"/>
<TextBlock Grid.Column="5"
- Text="{Binding Loc.TargetPortsLabel}"
+ Text="{Binding Loc.TargetHostsLabel}"
FontSize="13"
FontWeight="SemiBold"
Foreground="#FFB0B0B0"
Margin="0,0,10,0"/>
<TextBlock Grid.Column="6"
- Text="{Binding Loc.ProtocolLabel}"
+ Text="{Binding Loc.TargetPortsLabel}"
FontSize="13"
FontWeight="SemiBold"
Foreground="#FFB0B0B0"
Margin="0,0,10,0"/>
<TextBlock Grid.Column="7"
+ Text="{Binding Loc.ProtocolLabel}"
+ FontSize="13"
+ FontWeight="SemiBold"
+ Foreground="#FFB0B0B0"
+ Margin="0,0,10,0"/>
+ <TextBlock Grid.Column="8"
Text="{Binding Loc.ActionLabel}"
FontSize="13"
FontWeight="SemiBold"
@@ -185,12 +229,16 @@
BorderBrush="#FF3F3F46"
BorderThickness="0,0,0,1"
Padding="20,16">
- <Grid ColumnDefinitions="80,140,60,200,150,120,80,*">
+ <Grid ColumnDefinitions="60,80,140,60,200,150,120,80,*">
<CheckBox Grid.Column="0"
+ IsChecked="{Binding IsSelected}"
+ VerticalAlignment="Center"
+ Margin="0,0,10,0"/>
+ <CheckBox Grid.Column="1"
IsChecked="{Binding IsEnabled}"
VerticalAlignment="Center"
Margin="0,0,10,0"/>
- <StackPanel Grid.Column="1"
+ <StackPanel Grid.Column="2"
Orientation="Horizontal"
Spacing="8"
VerticalAlignment="Center"
@@ -209,13 +257,13 @@
FontSize="12"
Classes="secondary"/>
</StackPanel>
- <TextBlock Grid.Column="2"
+ <TextBlock Grid.Column="3"
Text="{Binding Index}"
FontSize="14"
Foreground="#FF999999"
VerticalAlignment="Center"
Margin="0,0,10,0"/>
- <TextBlock Grid.Column="3"
+ <TextBlock Grid.Column="4"
Text="{Binding ProcessName}"
FontSize="14"
FontWeight="Medium"
@@ -223,27 +271,27 @@
VerticalAlignment="Center"
TextTrimming="CharacterEllipsis"
Margin="0,0,10,0"/>
- <TextBlock Grid.Column="4"
+ <TextBlock Grid.Column="5"
Text="{Binding TargetHosts}"
FontSize="13"
Foreground="#FF999999"
VerticalAlignment="Center"
TextTrimming="CharacterEllipsis"
Margin="0,0,10,0"/>
- <TextBlock Grid.Column="5"
+ <TextBlock Grid.Column="6"
Text="{Binding TargetPorts}"
FontSize="13"
Foreground="#FF999999"
VerticalAlignment="Center"
TextTrimming="CharacterEllipsis"
Margin="0,0,10,0"/>
- <TextBlock Grid.Column="6"
+ <TextBlock Grid.Column="7"
Text="{Binding Protocol}"
FontSize="13"
Foreground="#FF999999"
VerticalAlignment="Center"
Margin="0,0,10,0"/>
- <TextBlock Grid.Column="7"
+ <TextBlock Grid.Column="8"
Text="{Binding Action}"
FontSize="14"
FontWeight="SemiBold"
diff --git a/Windows/gui/Views/ProxyRulesWindow.axaml.cs b/Windows/gui/Views/ProxyRulesWindow.axaml.cs
index dbd1b6d..8ef6814 100644
--- a/Windows/gui/Views/ProxyRulesWindow.axaml.cs
+++ b/Windows/gui/Views/ProxyRulesWindow.axaml.cs
@@ -1,11 +1,39 @@
using System;
using System.ComponentModel;
+using System.Globalization;
using Avalonia.Controls;
+using Avalonia.Data.Converters;
using Avalonia.Interactivity;
using ProxyBridge.GUI.ViewModels;
namespace ProxyBridge.GUI.Views;
+public class SelectAllTextConverter : IValueConverter
+{
+ public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
+ {
+ return value is bool allSelected && allSelected ? "Deselect All" : "Select All";
+ }
+
+ public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
+ {
+ throw new NotImplementedException();
+ }
+}
+
+public class SelectAllIconConverter : IValueConverter
+{
+ public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
+ {
+ return value is bool allSelected && allSelected ? "☑" : "☐";
+ }
+
+ public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
+ {
+ throw new NotImplementedException();
+ }
+}
+
public partial class ProxyRulesWindow : Window
{
private bool _isUpdatingFromViewModel = false;