From b77cc3c33b5570347acb7a49194ae908ded87286 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Sat, 22 Jun 2024 16:44:15 +0800 Subject: [PATCH] Add network attribute to route rule https://github.com/2dust/v2rayN/discussions/5256 --- v2rayN/v2rayN/App.xaml.cs | 2 +- v2rayN/v2rayN/Common/Utils.cs | 2 +- v2rayN/v2rayN/Global.cs | 1 + .../Handler/CoreConfig/CoreConfigSingbox.cs | 6 ++- .../Handler/CoreConfig/CoreConfigV2ray.cs | 44 ++++++++++--------- v2rayN/v2rayN/Models/RulesItem.cs | 17 +++---- v2rayN/v2rayN/Models/SingboxConfig.cs | 2 +- v2rayN/v2rayN/Models/V2rayConfig.cs | 1 + .../ViewModels/RoutingRuleSettingViewModel.cs | 1 + .../Views/RoutingRuleDetailsWindow.xaml | 20 ++++++++- .../Views/RoutingRuleDetailsWindow.xaml.cs | 5 +++ .../Views/RoutingRuleSettingWindow.xaml | 12 +++-- 12 files changed, 75 insertions(+), 38 deletions(-) diff --git a/v2rayN/v2rayN/App.xaml.cs b/v2rayN/v2rayN/App.xaml.cs index 7348d354..d5b510b3 100644 --- a/v2rayN/v2rayN/App.xaml.cs +++ b/v2rayN/v2rayN/App.xaml.cs @@ -62,7 +62,7 @@ namespace v2rayN //Under Win10 if (Environment.OSVersion.Version.Major < 10) { - Environment.SetEnvironmentVariable("DOTNET_EnableWriteXorExecute", "0", EnvironmentVariableTarget.Process); + Environment.SetEnvironmentVariable("DOTNET_EnableWriteXorExecute", "0", EnvironmentVariableTarget.User); } } diff --git a/v2rayN/v2rayN/Common/Utils.cs b/v2rayN/v2rayN/Common/Utils.cs index e8c56d18..39f2f9a8 100644 --- a/v2rayN/v2rayN/Common/Utils.cs +++ b/v2rayN/v2rayN/Common/Utils.cs @@ -79,7 +79,7 @@ namespace v2rayN /// /// /// - public static string List2String(List lst, bool wrap = false) + public static string List2String(List? lst, bool wrap = false) { try { diff --git a/v2rayN/v2rayN/Global.cs b/v2rayN/v2rayN/Global.cs index b325da28..27ad33e0 100644 --- a/v2rayN/v2rayN/Global.cs +++ b/v2rayN/v2rayN/Global.cs @@ -174,6 +174,7 @@ namespace v2rayN public static readonly List LogLevels = new() { "debug", "info", "warning", "error", "none" }; public static readonly List InboundTags = new() { "socks", "http", "socks2", "http2" }; public static readonly List RuleProtocols = new() { "http", "tls", "bittorrent" }; + public static readonly List RuleNetworks = new() { "", "tcp", "udp", "tcp,udp" }; public static readonly List destOverrideProtocols = ["http", "tls", "quic", "fakedns", "fakedns+others"]; public static readonly List TunMtus = new() { "1280", "1408", "1500", "9000" }; public static readonly List TunStacks = new() { "gvisor", "system" }; diff --git a/v2rayN/v2rayN/Handler/CoreConfig/CoreConfigSingbox.cs b/v2rayN/v2rayN/Handler/CoreConfig/CoreConfigSingbox.cs index 6ceb3154..89f468c4 100644 --- a/v2rayN/v2rayN/Handler/CoreConfig/CoreConfigSingbox.cs +++ b/v2rayN/v2rayN/Handler/CoreConfig/CoreConfigSingbox.cs @@ -552,7 +552,7 @@ namespace v2rayN.Handler.CoreConfig singboxConfig.route.rules.Add(new() { port = [53], - network = "udp", + network = ["udp"], outbound = dnsOutbound }); } @@ -668,6 +668,10 @@ namespace v2rayN.Handler.CoreConfig rule.port = new List { Utils.ToInt(item.port) }; } } + if (!Utils.IsNullOrEmpty(item.network)) + { + rule.network = Utils.String2List(item.network); + } if (item.protocol?.Count > 0) { rule.protocol = item.protocol; diff --git a/v2rayN/v2rayN/Handler/CoreConfig/CoreConfigV2ray.cs b/v2rayN/v2rayN/Handler/CoreConfig/CoreConfigV2ray.cs index 9a912c9a..0fac445e 100644 --- a/v2rayN/v2rayN/Handler/CoreConfig/CoreConfigV2ray.cs +++ b/v2rayN/v2rayN/Handler/CoreConfig/CoreConfigV2ray.cs @@ -220,39 +220,43 @@ namespace v2rayN.Handler.CoreConfig return 0; } - private int GenRoutingUserRule(RulesItem4Ray? rules, V2rayConfig v2rayConfig) + private int GenRoutingUserRule(RulesItem4Ray? rule, V2rayConfig v2rayConfig) { try { - if (rules == null) + if (rule == null) { return 0; } - if (Utils.IsNullOrEmpty(rules.port)) + if (Utils.IsNullOrEmpty(rule.port)) { - rules.port = null; + rule.port = null; } - if (rules.domain?.Count == 0) + if (Utils.IsNullOrEmpty(rule.network)) { - rules.domain = null; + rule.network = null; } - if (rules.ip?.Count == 0) + if (rule.domain?.Count == 0) { - rules.ip = null; + rule.domain = null; } - if (rules.protocol?.Count == 0) + if (rule.ip?.Count == 0) { - rules.protocol = null; + rule.ip = null; } - if (rules.inboundTag?.Count == 0) + if (rule.protocol?.Count == 0) { - rules.inboundTag = null; + rule.protocol = null; + } + if (rule.inboundTag?.Count == 0) + { + rule.inboundTag = null; } var hasDomainIp = false; - if (rules.domain?.Count > 0) + if (rule.domain?.Count > 0) { - var it = JsonUtils.DeepCopy(rules); + var it = JsonUtils.DeepCopy(rule); it.ip = null; it.type = "field"; for (int k = it.domain.Count - 1; k >= 0; k--) @@ -266,9 +270,9 @@ namespace v2rayN.Handler.CoreConfig v2rayConfig.routing.rules.Add(it); hasDomainIp = true; } - if (rules.ip?.Count > 0) + if (rule.ip?.Count > 0) { - var it = JsonUtils.DeepCopy(rules); + var it = JsonUtils.DeepCopy(rule); it.domain = null; it.type = "field"; v2rayConfig.routing.rules.Add(it); @@ -276,12 +280,12 @@ namespace v2rayN.Handler.CoreConfig } if (!hasDomainIp) { - if (!Utils.IsNullOrEmpty(rules.port) - || rules.protocol?.Count > 0 - || rules.inboundTag?.Count > 0 + if (!Utils.IsNullOrEmpty(rule.port) + || rule.protocol?.Count > 0 + || rule.inboundTag?.Count > 0 ) { - var it = JsonUtils.DeepCopy(rules); + var it = JsonUtils.DeepCopy(rule); it.type = "field"; v2rayConfig.routing.rules.Add(it); } diff --git a/v2rayN/v2rayN/Models/RulesItem.cs b/v2rayN/v2rayN/Models/RulesItem.cs index cf6c367e..78c4e846 100644 --- a/v2rayN/v2rayN/Models/RulesItem.cs +++ b/v2rayN/v2rayN/Models/RulesItem.cs @@ -4,21 +4,22 @@ public class RulesItem { public string id { get; set; } - public string type { get; set; } + public string? type { get; set; } - public string port { get; set; } + public string? port { get; set; } + public string? network { get; set; } - public List inboundTag { get; set; } + public List? inboundTag { get; set; } - public string outboundTag { get; set; } + public string? outboundTag { get; set; } - public List ip { get; set; } + public List? ip { get; set; } - public List domain { get; set; } + public List? domain { get; set; } - public List protocol { get; set; } + public List? protocol { get; set; } - public List process { get; set; } + public List? process { get; set; } public bool enabled { get; set; } = true; } diff --git a/v2rayN/v2rayN/Models/SingboxConfig.cs b/v2rayN/v2rayN/Models/SingboxConfig.cs index da4675b9..330401a3 100644 --- a/v2rayN/v2rayN/Models/SingboxConfig.cs +++ b/v2rayN/v2rayN/Models/SingboxConfig.cs @@ -48,7 +48,7 @@ public List? protocol { get; set; } public string? type { get; set; } public string? mode { get; set; } - public string? network { get; set; } + public List? network { get; set; } public bool? ip_is_private { get; set; } public List? port { get; set; } public List? port_range { get; set; } diff --git a/v2rayN/v2rayN/Models/V2rayConfig.cs b/v2rayN/v2rayN/Models/V2rayConfig.cs index e693f0cc..109730b4 100644 --- a/v2rayN/v2rayN/Models/V2rayConfig.cs +++ b/v2rayN/v2rayN/Models/V2rayConfig.cs @@ -394,6 +394,7 @@ namespace v2rayN.Models public string? type { get; set; } public string? port { get; set; } + public string? network { get; set; } public List? inboundTag { get; set; } diff --git a/v2rayN/v2rayN/ViewModels/RoutingRuleSettingViewModel.cs b/v2rayN/v2rayN/ViewModels/RoutingRuleSettingViewModel.cs index 2d5b532f..569de0f4 100644 --- a/v2rayN/v2rayN/ViewModels/RoutingRuleSettingViewModel.cs +++ b/v2rayN/v2rayN/ViewModels/RoutingRuleSettingViewModel.cs @@ -130,6 +130,7 @@ namespace v2rayN.ViewModels id = item.id, outboundTag = item.outboundTag, port = item.port, + network = item.network, protocols = Utils.List2String(item.protocol), inboundTags = Utils.List2String(item.inboundTag), domains = Utils.List2String(item.domain), diff --git a/v2rayN/v2rayN/Views/RoutingRuleDetailsWindow.xaml b/v2rayN/v2rayN/Views/RoutingRuleDetailsWindow.xaml index b05bd131..6168b948 100644 --- a/v2rayN/v2rayN/Views/RoutingRuleDetailsWindow.xaml +++ b/v2rayN/v2rayN/Views/RoutingRuleDetailsWindow.xaml @@ -125,16 +125,32 @@ Margin="4" VerticalAlignment="Center" Style="{StaticResource ToolbarTextBlock}" + Text="network" /> + + + + { + cmbNetwork.Items.Add(it); + }); if (!rulesItem.id.IsNullOrEmpty()) { @@ -56,6 +60,7 @@ namespace v2rayN.Views { this.Bind(ViewModel, vm => vm.SelectedSource.outboundTag, v => v.cmbOutboundTag.Text).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.SelectedSource.port, v => v.txtPort.Text).DisposeWith(disposables); + this.Bind(ViewModel, vm => vm.SelectedSource.network, v => v.cmbNetwork.Text).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.SelectedSource.enabled, v => v.togEnabled.IsChecked).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.Domain, v => v.txtDomain.Text).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.IP, v => v.txtIP.Text).DisposeWith(disposables); diff --git a/v2rayN/v2rayN/Views/RoutingRuleSettingWindow.xaml b/v2rayN/v2rayN/Views/RoutingRuleSettingWindow.xaml index 6bafd42d..b8b238e2 100644 --- a/v2rayN/v2rayN/Views/RoutingRuleSettingWindow.xaml +++ b/v2rayN/v2rayN/Views/RoutingRuleSettingWindow.xaml @@ -302,7 +302,7 @@ +