diff --git a/v2rayN/ServiceLib/Global.cs b/v2rayN/ServiceLib/Global.cs index 6c4a4362..e80408fa 100644 --- a/v2rayN/ServiceLib/Global.cs +++ b/v2rayN/ServiceLib/Global.cs @@ -87,6 +87,8 @@ public class Global public const string SingboxFinalResolverTag = "final_resolver"; public const string SingboxHostsDNSTag = "hosts_dns"; public const string SingboxFakeDNSTag = "fake_dns"; + public const string RoutingRuleType = "Routing"; + public const string DNSRuleType = "DNS"; public static readonly List IEProxyProtocols = [ @@ -479,6 +481,12 @@ public class Global "tcp,udp" ]; + public static readonly List RuleTypes = + [ + RoutingRuleType, + DNSRuleType + ]; + public static readonly List destOverrideProtocols = [ "http", diff --git a/v2rayN/ServiceLib/Models/RulesItem.cs b/v2rayN/ServiceLib/Models/RulesItem.cs index 2aedae08..69b1b095 100644 --- a/v2rayN/ServiceLib/Models/RulesItem.cs +++ b/v2rayN/ServiceLib/Models/RulesItem.cs @@ -15,4 +15,5 @@ public class RulesItem public List? Process { get; set; } public bool Enabled { get; set; } = true; public string? Remarks { get; set; } + public List? RuleTypes { get; set; } } diff --git a/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxDnsService.cs b/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxDnsService.cs index 9bd2502d..b8d6b6f7 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxDnsService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxDnsService.cs @@ -253,6 +253,11 @@ public partial class CoreConfigSingboxService continue; } + if ((item.RuleTypes?.Count ?? 0) > 0 && !item.RuleTypes.Contains(Global.DNSRuleType)) + { + continue; + } + var rule = new Rule4Sbox(); var validDomains = item.Domain.Count(it => ParseV2Domain(it, rule)); if (validDomains <= 0) diff --git a/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxRoutingService.cs b/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxRoutingService.cs index 21dfb101..b7c02255 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxRoutingService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxRoutingService.cs @@ -136,13 +136,21 @@ public partial class CoreConfigSingboxService var rules = JsonUtils.Deserialize>(routing.RuleSet); foreach (var item1 in rules ?? []) { - if (item1.Enabled) + if (!item1.Enabled) { - await GenRoutingUserRule(item1, singboxConfig); - if (item1.Ip != null && item1.Ip.Count > 0) - { - ipRules.Add(item1); - } + continue; + } + + if ((item1.RuleTypes?.Count ?? 0) > 0 && !item1.RuleTypes.Contains(Global.RoutingRuleType)) + { + continue; + } + + await GenRoutingUserRule(item1, singboxConfig); + + if (item1.Ip?.Count > 0) + { + ipRules.Add(item1); } } } diff --git a/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayDnsService.cs b/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayDnsService.cs index 744286d4..6268b740 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayDnsService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayDnsService.cs @@ -142,6 +142,11 @@ public partial class CoreConfigV2rayService continue; } + if ((item.RuleTypes?.Count ?? 0) > 0 && !item.RuleTypes.Contains(Global.DNSRuleType)) + { + continue; + } + foreach (var domain in item.Domain) { if (domain.StartsWith('#')) diff --git a/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayRoutingService.cs b/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayRoutingService.cs index d39ea833..1f204f36 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayRoutingService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayRoutingService.cs @@ -20,11 +20,18 @@ public partial class CoreConfigV2rayService var rules = JsonUtils.Deserialize>(routing.RuleSet); foreach (var item in rules) { - if (item.Enabled) + if (!item.Enabled) { - var item2 = JsonUtils.Deserialize(JsonUtils.Serialize(item)); - await GenRoutingUserRule(item2, v2rayConfig); + continue; } + + if ((item.RuleTypes?.Count ?? 0) > 0 && !item.RuleTypes.Contains(Global.RoutingRuleType)) + { + continue; + } + + var item2 = JsonUtils.Deserialize(JsonUtils.Serialize(item)); + await GenRoutingUserRule(item2, v2rayConfig); } } } diff --git a/v2rayN/ServiceLib/ViewModels/RoutingRuleDetailsViewModel.cs b/v2rayN/ServiceLib/ViewModels/RoutingRuleDetailsViewModel.cs index 758aa8fe..2e227172 100644 --- a/v2rayN/ServiceLib/ViewModels/RoutingRuleDetailsViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/RoutingRuleDetailsViewModel.cs @@ -21,6 +21,8 @@ public class RoutingRuleDetailsViewModel : MyReactiveObject [Reactive] public string Process { get; set; } + public IList Types { get; set; } + [Reactive] public bool AutoSort { get; set; } @@ -51,6 +53,11 @@ public class RoutingRuleDetailsViewModel : MyReactiveObject Domain = Utils.List2String(SelectedSource.Domain, true); IP = Utils.List2String(SelectedSource.Ip, true); Process = Utils.List2String(SelectedSource.Process, true); + Types = SelectedSource.RuleTypes?.ToList(); + if (Types == null || Types.Count == 0) + { + Types = Global.RuleTypes; + } } private async Task SaveRulesAsync() @@ -73,6 +80,7 @@ public class RoutingRuleDetailsViewModel : MyReactiveObject } SelectedSource.Protocol = ProtocolItems?.ToList(); SelectedSource.InboundTag = InboundTagItems?.ToList(); + SelectedSource.RuleTypes = Types?.ToList(); var hasRule = SelectedSource.Domain?.Count > 0 || SelectedSource.Ip?.Count > 0 diff --git a/v2rayN/v2rayN.Desktop/Views/RoutingRuleDetailsWindow.axaml b/v2rayN/v2rayN.Desktop/Views/RoutingRuleDetailsWindow.axaml index dd0ac331..a32d7c15 100644 --- a/v2rayN/v2rayN.Desktop/Views/RoutingRuleDetailsWindow.axaml +++ b/v2rayN/v2rayN.Desktop/Views/RoutingRuleDetailsWindow.axaml @@ -32,13 +32,25 @@ Width="300" Margin="{StaticResource Margin4}" HorizontalAlignment="Left" /> - + VerticalAlignment="Center" + Orientation="Horizontal"> + + + @@ -109,4 +119,12 @@ public partial class RoutingRuleDetailsWindow : WindowBase().ToList(); + } + } } diff --git a/v2rayN/v2rayN/Views/RoutingRuleDetailsWindow.xaml b/v2rayN/v2rayN/Views/RoutingRuleDetailsWindow.xaml index f24a38d7..8b438ae0 100644 --- a/v2rayN/v2rayN/Views/RoutingRuleDetailsWindow.xaml +++ b/v2rayN/v2rayN/Views/RoutingRuleDetailsWindow.xaml @@ -48,13 +48,25 @@ Margin="{StaticResource Margin4}" HorizontalAlignment="Left" Style="{StaticResource DefTextBox}" /> - + VerticalAlignment="Center" + Orientation="Horizontal"> + + + @@ -101,4 +111,12 @@ public partial class RoutingRuleDetailsWindow } } } + + private void ClbRuleTypes_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) + { + if (ViewModel != null) + { + ViewModel.Types = clbRuleTypes.SelectedItems.Cast().ToList(); + } + } }