Skip to content

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

#156

AnoF-Cyber29bd4b18fd65
diff --git a/Windows/gui/Resources/Resources.resx b/Windows/gui/Resources/Resources.resx
index 5f0db8b..fc8e62e 100644
--- a/Windows/gui/Resources/Resources.resx
+++ b/Windows/gui/Resources/Resources.resx
@@ -33,10 +33,10 @@
<value>Log Filters...</value>
</data>
<data name="LogFiltersDesc1" xml:space="preserve">
- <value>Only connections matching all your filters will appear in the log. Connections that don't match are silently ignored and never stored.</value>
+ <value>Each rule can match on any combination of fields leave a field empty or set to "All" to ignore it. Include rules show only matching connections. Exclude rules hide matching connections. Excludes run first.</value>
</data>
<data name="LogFiltersDesc2" xml:space="preserve">
- <value>Use * as a wildcard anywhere in text fields: chrome* = starts with, *.exe = ends with, *ch* = contains anywhere. Action and Protocol use a dropdown choose "All" to ignore that field.</value>
+ <value>Use * as a wildcard in text fields: chrome* = starts with, *.exe = ends with, *ch* = contains anywhere.</value>
</data>
<data name="MenuAutoClearLogs" xml:space="preserve">
<value>Auto Clear Logs</value>
diff --git a/Windows/gui/Resources/Resources.zh.resx b/Windows/gui/Resources/Resources.zh.resx
index 7414cf2..26d5ad8 100644
--- a/Windows/gui/Resources/Resources.zh.resx
+++ b/Windows/gui/Resources/Resources.zh.resx
@@ -33,10 +33,10 @@
<value>日志过滤...</value>
</data>
<data name="LogFiltersDesc1" xml:space="preserve">
- <value>只有符合所有过滤条件的连接才会显示在日志中。不符合的连接将被忽略,不会被记录。</value>
+ <value>每条规则可同时匹配多个字段 留空或选"全部"表示忽略该字段。Include规则只显示匹配的连接,Exclude规则隐藏匹配的连接,排除优先执行。</value>
</data>
<data name="LogFiltersDesc2" xml:space="preserve">
- <value>文本字段支持 * 通配符:chrome* = 以此开头,*.exe = 以此结尾,*ch* = 包含此内容。操作和协议使用下拉菜单 选择"全部"可忽略该字段。</value>
+ <value>文本字段支持 * 通配符:chrome* = 以此开头,*.exe = 以此结尾,*ch* = 包含此内容。</value>
</data>
<data name="MenuAutoClearLogs" xml:space="preserve">
<value>自动清除日志</value>
diff --git a/Windows/gui/Services/ProfileManager.cs b/Windows/gui/Services/ProfileManager.cs
index 60bb565..7792b9c 100644
--- a/Windows/gui/Services/ProfileManager.cs
+++ b/Windows/gui/Services/ProfileManager.cs
@@ -9,9 +9,12 @@ namespace ProxyBridge.GUI.Services;
public class LogFilterEntry
{
- public string Field { get; set; } = "Process Name"; // Process Name, IP, Port, Protocol, Action
- public string Operator { get; set; } = "Contains"; // Contains, Not Contains, Equals, Not Equals, Starts With
- public string Value { get; set; } = "";
+ public string Mode { get; set; } = "Include"; // Include or Exclude
+ public string ProcessName { get; set; } = ""; // empty = any
+ public string Ip { get; set; } = ""; // empty = any
+ public string Port { get; set; } = ""; // empty = any
+ public string Protocol { get; set; } = "All"; // All, TCP, UDP
+ public string Action { get; set; } = "All"; // All, Proxy, Direct, Blocked
}
public class ProxyConfigEntry
diff --git a/Windows/gui/ViewModels/LogFiltersViewModel.cs b/Windows/gui/ViewModels/LogFiltersViewModel.cs
index 7243303..648f038 100644
--- a/Windows/gui/ViewModels/LogFiltersViewModel.cs
+++ b/Windows/gui/ViewModels/LogFiltersViewModel.cs
@@ -9,76 +9,29 @@ using ProxyBridge.GUI.Services;
namespace ProxyBridge.GUI.ViewModels;
-public class LogFilterRowViewModel : ViewModelBase
+public class LogFilterRuleViewModel : ViewModelBase
{
- private static readonly string[] _textOperators =
- { "Contains", "Not Contains", "Equals", "Not Equals", "Starts With" };
- private static readonly string[] _enumOperators =
- { "Equals", "Not Equals" };
- private static readonly string[] _actionValues =
- { "All", "Proxy", "Direct", "Blocked" };
- private static readonly string[] _protocolValues =
- { "All", "TCP", "UDP" };
- private static readonly string[] _noOptions = Array.Empty<string>();
-
- public static readonly string[] AvailableFields =
- { "Process Name", "IP", "Port", "Protocol", "Action" };
-
- private string _field = "Process Name";
- private string _operator = "Contains";
- private string _value = "";
-
- public string[] Fields => AvailableFields;
-
- // Dynamic — depends on selected field
- public string[] AvailableOperators =>
- IsValueDropdown ? _enumOperators : _textOperators;
-
- // Non-empty only for enum fields (Protocol, Action)
- public string[] ValueOptions => _field switch
- {
- "Action" => _actionValues,
- "Protocol" => _protocolValues,
- _ => _noOptions
- };
-
- public bool IsValueDropdown => _field is "Action" or "Protocol";
-
- public string ValuePlaceholder => _field switch
- {
- "Process Name" => "e.g. chrome*, *.exe, *ch*",
- "IP" => "e.g. 192.168.*, *.1, 10.*",
- "Port" => "e.g. 443, 8080",
- _ => ""
- };
-
- public string Field
- {
- get => _field;
- set
- {
- if (!SetProperty(ref _field, value)) return;
- OnPropertyChanged(nameof(AvailableOperators));
- OnPropertyChanged(nameof(ValueOptions));
- OnPropertyChanged(nameof(IsValueDropdown));
- OnPropertyChanged(nameof(ValuePlaceholder));
- // reset operator and value to sensible defaults for the new field
- Operator = IsValueDropdown ? _enumOperators[0] : _textOperators[0];
- Value = ValueOptions.Length > 0 ? ValueOptions[0] : "";
- }
- }
-
- public string Operator
- {
- get => _operator;
- set => SetProperty(ref _operator, value);
- }
-
- public string Value
- {
- get => _value;
- set => SetProperty(ref _value, value);
- }
+ public static readonly string[] Modes = { "Include", "Exclude" };
+ public static readonly string[] ProtocolOptions = { "All", "TCP", "UDP" };
+ public static readonly string[] ActionOptions = { "All", "Proxy", "Direct", "Blocked" };
+
+ private string _mode = "Include";
+ private string _processName = "";
+ private string _ip = "";
+ private string _port = "";
+ private string _protocol = "All";
+ private string _action = "All";
+
+ public string[] ModeList => Modes;
+ public string[] ProtocolList => ProtocolOptions;
+ public string[] ActionList => ActionOptions;
+
+ public string Mode { get => _mode; set => SetProperty(ref _mode, value); }
+ public string ProcessName { get => _processName; set => SetProperty(ref _processName, value); }
+ public string Ip { get => _ip; set => SetProperty(ref _ip, value); }
+ public string Port { get => _port; set => SetProperty(ref _port, value); }
+ public string Protocol { get => _protocol; set => SetProperty(ref _protocol, value); }
+ public string Action { get => _action; set => SetProperty(ref _action, value); }
}
public class LogFiltersViewModel : ViewModelBase
@@ -88,13 +41,13 @@ public class LogFiltersViewModel : ViewModelBase
public Loc Loc => Loc.Instance;
- public ObservableCollection<LogFilterRowViewModel> FilterRows { get; } = new();
+ public ObservableCollection<LogFilterRuleViewModel> Rules { get; } = new();
- public ICommand AddFilterCommand { get; }
- public ICommand RemoveFilterRowCommand { get; }
- public ICommand ClearAllCommand { get; }
- public ICommand SaveCommand { get; }
- public ICommand CloseCommand { get; }
+ public ICommand AddRuleCommand { get; }
+ public ICommand RemoveRuleCommand { get; }
+ public ICommand ClearAllCommand { get; }
+ public ICommand SaveCommand { get; }
+ public ICommand CloseCommand { get; }
public LogFiltersViewModel(
List<LogFilterEntry> existingFilters,
@@ -106,30 +59,32 @@ public class LogFiltersViewModel : ViewModelBase
foreach (var f in existingFilters)
{
- FilterRows.Add(new LogFilterRowViewModel
+ Rules.Add(new LogFilterRuleViewModel
{
- Field = f.Field,
- Operator = f.Operator,
- Value = f.Value
+ Mode = f.Mode,
+ ProcessName = f.ProcessName,
+ Ip = f.Ip,
+ Port = f.Port,
+ Protocol = f.Protocol,
+ Action = f.Action
});
}
- AddFilterCommand = new RelayCommand(() =>
- FilterRows.Add(new LogFilterRowViewModel()));
-
- RemoveFilterRowCommand =
- new RelayCommandWithParameter<LogFilterRowViewModel>(row => FilterRows.Remove(row));
-
- ClearAllCommand = new RelayCommand(() => FilterRows.Clear());
+ AddRuleCommand = new RelayCommand(() => Rules.Add(new LogFilterRuleViewModel()));
+ RemoveRuleCommand = new RelayCommandWithParameter<LogFilterRuleViewModel>(rule => Rules.Remove(rule));
+ ClearAllCommand = new RelayCommand(() => Rules.Clear());
SaveCommand = new RelayCommand(() =>
{
- var filters = FilterRows
+ var filters = Rules
.Select(r => new LogFilterEntry
{
- Field = r.Field,
- Operator = r.Operator,
- Value = r.Value
+ Mode = r.Mode,
+ ProcessName = r.ProcessName,
+ Ip = r.Ip,
+ Port = r.Port,
+ Protocol = r.Protocol,
+ Action = r.Action
})
.ToList();
_onSave(filters);
diff --git a/Windows/gui/ViewModels/MainWindowViewModel.cs b/Windows/gui/ViewModels/MainWindowViewModel.cs
index 82e86d7..909f299 100644
--- a/Windows/gui/ViewModels/MainWindowViewModel.cs
+++ b/Windows/gui/ViewModels/MainWindowViewModel.cs
@@ -858,46 +858,40 @@ public class MainWindowViewModel : ViewModelBase
var filters = _activeFilters; // single volatile read → local snapshot
if (filters.Length == 0) return true;
- foreach (var f in filters)
- {
- // skip no-op rows: empty value, wildcard-all, or "All" dropdown selection
- if (string.IsNullOrEmpty(f.Value) || f.Value is "*" or "All") continue;
+ string protocol = proxyInfo.Contains("(UDP)", StringComparison.OrdinalIgnoreCase) ? "UDP" : "TCP";
+ string action = proxyInfo.StartsWith("Direct", StringComparison.OrdinalIgnoreCase) ? "Direct"
+ : proxyInfo.StartsWith("Proxy", StringComparison.OrdinalIgnoreCase) ? "Proxy"
+ : proxyInfo.StartsWith("Block", StringComparison.OrdinalIgnoreCase) ? "Blocked"
+ : "";
- string fieldValue = f.Field switch
- {
- "Process Name" => processName,
- "IP" => destIp,
- "Port" => destPort.ToString(),
- "Protocol" => proxyInfo.Contains("(UDP)", StringComparison.OrdinalIgnoreCase) ? "UDP" : "TCP",
- "Action" => proxyInfo.StartsWith("Direct", StringComparison.OrdinalIgnoreCase) ? "Direct"
- : proxyInfo.StartsWith("Proxy", StringComparison.OrdinalIgnoreCase) ? "Proxy"
- : proxyInfo.StartsWith("Block", StringComparison.OrdinalIgnoreCase) ? "Blocked"
- : proxyInfo,
- _ => ""
- };
+ bool TextMatch(string actual, string pattern)
+ {
+ if (string.IsNullOrEmpty(pattern) || pattern == "*") return true;
+ return pattern.Contains('*')
+ ? WildcardMatch(actual, pattern)
+ : actual.Contains(pattern, StringComparison.OrdinalIgnoreCase);
+ }
- if (!ApplyFilterOperator(fieldValue, f.Operator, f.Value))
- return false; // AND logic — one failure = entry rejected
+ bool RuleMatches(LogFilterEntry r)
+ {
+ if (!TextMatch(processName, r.ProcessName)) return false;
+ if (!TextMatch(destIp, r.Ip)) return false;
+ if (!TextMatch(destPort.ToString(), r.Port)) return false;
+ if (r.Protocol is not ("" or "All") && !r.Protocol.Equals(protocol, StringComparison.OrdinalIgnoreCase)) return false;
+ if (r.Action is not ("" or "All") && !r.Action.Equals(action, StringComparison.OrdinalIgnoreCase)) return false;
+ return true;
}
- return true;
- }
+ // Exclude rules run first — any match hides the entry
+ foreach (var f in filters)
+ if (f.Mode == "Exclude" && RuleMatches(f)) return false;
- private static bool ApplyFilterOperator(string fieldValue, string op, string filterValue)
- {
- bool hasWildcard = filterValue.Contains('*');
- return op switch
- {
- "Contains" => hasWildcard ? WildcardMatch(fieldValue, filterValue)
- : fieldValue.Contains(filterValue, StringComparison.OrdinalIgnoreCase),
- "Not Contains" => hasWildcard ? !WildcardMatch(fieldValue, filterValue)
- : !fieldValue.Contains(filterValue, StringComparison.OrdinalIgnoreCase),
- "Equals" => WildcardMatch(fieldValue, filterValue),
- "Not Equals" => !WildcardMatch(fieldValue, filterValue),
- "Starts With" => hasWildcard ? WildcardMatch(fieldValue, filterValue)
- : fieldValue.StartsWith(filterValue, StringComparison.OrdinalIgnoreCase),
- _ => true
- };
+ // Include rules — at least one must match if any exist
+ bool hasInclude = false;
+ foreach (var f in filters)
+ if (f.Mode == "Include") { hasInclude = true; if (RuleMatches(f)) return true; }
+
+ return !hasInclude;
}
private static bool WildcardMatch(string text, string pattern)
diff --git a/Windows/gui/Views/LogFiltersWindow.axaml b/Windows/gui/Views/LogFiltersWindow.axaml
index 843fe1f..c4729e6 100644
--- a/Windows/gui/Views/LogFiltersWindow.axaml
+++ b/Windows/gui/Views/LogFiltersWindow.axaml
@@ -61,73 +61,104 @@
<!-- Body -->
<DockPanel>
- <!-- Add Filter + column headers -->
+ <!-- Add Rule button -->
<Border DockPanel.Dock="Top" Background="#FF252526" Padding="16,10"
BorderBrush="#FF3F3F46" BorderThickness="0,0,0,1">
- <Button Content="+ Add Filter"
- Command="{Binding AddFilterCommand}"
+ <Button Content="+ Add Rule"
+ Command="{Binding AddRuleCommand}"
Classes="secondary"
Padding="12,6"
HorizontalAlignment="Left"/>
</Border>
- <Border DockPanel.Dock="Top" Background="#FF1E1E1E" Padding="16,8"
- BorderBrush="#FF3F3F46" BorderThickness="0,0,0,1">
- <Grid ColumnDefinitions="180,160,*,40">
- <TextBlock Grid.Column="0" Text="Field"
- FontSize="12" FontWeight="SemiBold" Foreground="#FFB0B0B0"/>
- <TextBlock Grid.Column="1" Text="Operator"
- FontSize="12" FontWeight="SemiBold" Foreground="#FFB0B0B0"/>
- <TextBlock Grid.Column="2" Text="Value"
- FontSize="12" FontWeight="SemiBold" Foreground="#FFB0B0B0"
- Margin="8,0,0,0"/>
- </Grid>
- </Border>
-
- <!-- Filter rows -->
+ <!-- Rule cards -->
<ScrollViewer VerticalScrollBarVisibility="Auto">
- <ItemsControl ItemsSource="{Binding FilterRows}">
+ <ItemsControl ItemsSource="{Binding Rules}" Margin="12,8">
<ItemsControl.ItemTemplate>
- <DataTemplate x:DataType="vm:LogFilterRowViewModel">
- <Border Background="#FF252526"
- BorderBrush="#FF3F3F46"
- BorderThickness="0,0,0,1"
- Padding="16,10">
- <Grid ColumnDefinitions="180,160,*,40">
- <ComboBox Grid.Column="0"
- ItemsSource="{Binding Fields}"
- SelectedItem="{Binding Field}"
- Margin="0,0,8,0"
- HorizontalAlignment="Stretch"
- Padding="8,6"/>
- <ComboBox Grid.Column="1"
- ItemsSource="{Binding AvailableOperators}"
- SelectedItem="{Binding Operator}"
- Margin="0,0,8,0"
- HorizontalAlignment="Stretch"
- Padding="8,6"/>
- <!-- Value: free-text for Process Name/IP/Port, dropdown for Action/Protocol -->
- <Panel Grid.Column="2" Margin="0,0,8,0">
- <TextBox Text="{Binding Value}"
- Watermark="{Binding ValuePlaceholder}"
- IsVisible="{Binding !IsValueDropdown}"
- Padding="8,6"/>
- <ComboBox ItemsSource="{Binding ValueOptions}"
- SelectedItem="{Binding Value}"
- IsVisible="{Binding IsValueDropdown}"
- HorizontalAlignment="Stretch"
- Padding="8,6"/>
- </Panel>
- <Button Grid.Column="3"
- Content="🗑"
- Command="{Binding $parent[ItemsControl].((vm:LogFiltersViewModel)DataContext).RemoveFilterRowCommand}"
- CommandParameter="{Binding}"
- Classes="secondary"
- Padding="4"
- Width="32"
- Height="32"
- VerticalAlignment="Center"/>
- </Grid>
+ <DataTemplate x:DataType="vm:LogFilterRuleViewModel">
+ <Border Background="#FF1E1E1E"
+ BorderBrush="#FF4A4A50"
+ BorderThickness="1"
+ CornerRadius="4"
+ Margin="0,0,0,8">
+ <StackPanel>
+
+ <!-- Rule header: Mode + remove button -->
+ <Border Background="#FF252526" Padding="12,8">
+ <Grid ColumnDefinitions="Auto,*,Auto">
+ <ComboBox Grid.Column="0"
+ ItemsSource="{Binding ModeList}"
+ SelectedItem="{Binding Mode}"
+ MinWidth="110"
+ Padding="8,5"/>
+ <Button Grid.Column="2"
+ Content="🗑"
+ Command="{Binding $parent[ItemsControl].((vm:LogFiltersViewModel)DataContext).RemoveRuleCommand}"
+ CommandParameter="{Binding}"
+ Classes="secondary"
+ Padding="4"
+ Width="32" Height="32"
+ VerticalAlignment="Center"/>
+ </Grid>
+ </Border>
+
+ <!-- Fields -->
+ <Border Padding="12,10">
+ <Grid ColumnDefinitions="80,*,20,80,*"
+ RowDefinitions="Auto,Auto,Auto">
+
+ <!-- Row 0: Process | Action -->
+ <TextBlock Grid.Row="0" Grid.Column="0"
+ Text="Process"
+ Foreground="#FF9E9E9E" FontSize="12"
+ VerticalAlignment="Center" Margin="0,0,8,6"/>
+ <TextBox Grid.Row="0" Grid.Column="1"
+ Text="{Binding ProcessName}"
+ Watermark="any (e.g. curl, chrome*, *.exe)"
+ Padding="8,5" Margin="0,0,0,6"/>
+ <TextBlock Grid.Row="0" Grid.Column="3"
+ Text="Action"
+ Foreground="#FF9E9E9E" FontSize="12"
+ VerticalAlignment="Center" Margin="0,0,8,6"/>
+ <ComboBox Grid.Row="0" Grid.Column="4"
+ ItemsSource="{Binding ActionList}"
+ SelectedItem="{Binding Action}"
+ HorizontalAlignment="Stretch"
+ Padding="8,5" Margin="0,0,0,6"/>
+
+ <!-- Row 1: IP | Protocol -->
+ <TextBlock Grid.Row="1" Grid.Column="0"
+ Text="IP"
+ Foreground="#FF9E9E9E" FontSize="12"
+ VerticalAlignment="Center" Margin="0,0,8,6"/>
+ <TextBox Grid.Row="1" Grid.Column="1"
+ Text="{Binding Ip}"
+ Watermark="any (e.g. 8.8.*, 192.168.*)"
+ Padding="8,5" Margin="0,0,0,6"/>
+ <TextBlock Grid.Row="1" Grid.Column="3"
+ Text="Protocol"
+ Foreground="#FF9E9E9E" FontSize="12"
+ VerticalAlignment="Center" Margin="0,0,8,6"/>
+ <ComboBox Grid.Row="1" Grid.Column="4"
+ ItemsSource="{Binding ProtocolList}"
+ SelectedItem="{Binding Protocol}"
+ HorizontalAlignment="Stretch"
+ Padding="8,5" Margin="0,0,0,6"/>
+
+ <!-- Row 2: Port -->
+ <TextBlock Grid.Row="2" Grid.Column="0"
+ Text="Port"
+ Foreground="#FF9E9E9E" FontSize="12"
+ VerticalAlignment="Center" Margin="0,0,8,0"/>
+ <TextBox Grid.Row="2" Grid.Column="1"
+ Text="{Binding Port}"
+ Watermark="any (e.g. 443, 8080)"
+ Padding="8,5"/>
+
+ </Grid>
+ </Border>
+
+ </StackPanel>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>