From 08788751a38c4dde3ca25ff8618d673052ce4f89 Mon Sep 17 00:00:00 2001 From: John Edwerd Date: Thu, 11 Jan 2024 23:32:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=AE=80=E5=8D=95=E7=9A=84?= =?UTF-8?q?=E5=88=86=E6=B5=81=E5=8A=9F=E8=83=BD=EF=BC=9B=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=9D=9E=E7=AE=A1=E7=90=86=E5=91=98=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F=E4=B8=8Btun=E6=A8=A1=E5=BC=8F=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- v2rayN/v2rayN/Handler/CoreConfigSingbox.cs | 53 ++++++++++++++++++- v2rayN/v2rayN/Handler/CoreConfigV2ray.cs | 49 +++++++++++++++++ v2rayN/v2rayN/Mode/RulesItem.cs | 3 ++ v2rayN/v2rayN/Sample/tun_singbox_rules | 11 ---- .../v2rayN/ViewModels/MainWindowViewModel.cs | 8 ++- v2rayN/v2rayN/Views/MainWindow.xaml.cs | 3 +- .../Views/RoutingRuleDetailsWindow.xaml | 17 +++++- .../Views/RoutingRuleDetailsWindow.xaml.cs | 38 +++++++++++++ 8 files changed, 166 insertions(+), 16 deletions(-) diff --git a/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs b/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs index 7b8c4438..f2197a1c 100644 --- a/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs +++ b/v2rayN/v2rayN/Handler/CoreConfigSingbox.cs @@ -41,6 +41,7 @@ namespace v2rayN.Handler msg = ResUI.FailedGenDefaultConfiguration; return -1; } + Outbound4Sbox originOutBound = JsonUtils.DeepCopy(singboxConfig.outbounds[0]); GenLog(singboxConfig); @@ -48,6 +49,8 @@ namespace v2rayN.Handler GenOutbound(node, singboxConfig.outbounds[0]); + GenOutbounds(singboxConfig, originOutBound); + GenMoreOutbounds(node, singboxConfig); GenRouting(singboxConfig); @@ -205,6 +208,32 @@ namespace v2rayN.Handler return inbound; } + /// + /// 生成分流的出站规则 + /// + private int GenOutbounds(SingboxConfig singboxConfig, Outbound4Sbox outbound) + { + var routing = ConfigHandler.GetDefaultRouting(_config); + var ruleSet = JsonUtils.Deserialize>(routing.ruleSet); + if (ruleSet == null) return -1; + + int index = 1; + foreach (var item in ruleSet) + { + string outBoundIndexId = item.bingServerIndexId; + if (outBoundIndexId == null) continue; + var serverItem = LazyConfig.Instance.GetProfileItem(outBoundIndexId); + if (serverItem == null) continue; + + var newOutbounds = JsonUtils.DeepCopy(outbound); + newOutbounds.tag = $"{(serverItem.indexId.Equals(_config.indexId) ? "[proxy]" : $"[{index++}]")}{serverItem.remarks} ## {serverItem.indexId}"; + singboxConfig.outbounds.Add(newOutbounds); + GenOutbound(serverItem, newOutbounds); + } + + return 0; + } + private int GenOutbound(ProfileItem node, Outbound4Sbox outbound) { try @@ -520,8 +549,18 @@ namespace v2rayN.Handler if (routing != null) { var rules = JsonUtils.Deserialize>(routing.ruleSet); - foreach (var item in rules!) + int index = 1; + foreach (var item in rules) { + // 分流修改 + string outBoundIndexId = item.bingServerIndexId; + + if (outBoundIndexId == null) continue; + + var serverItem = LazyConfig.Instance.GetProfileItem(outBoundIndexId); + if (serverItem == null) continue; + item.outboundTag = $"{(serverItem.indexId.Equals(_config.indexId) ? "[proxy]" : $"[{index++}]")}{serverItem.remarks} ## {serverItem.indexId}"; + // end if (item.enabled) { GenRoutingUserRule(item, singboxConfig.route.rules); @@ -535,8 +574,18 @@ namespace v2rayN.Handler if (lockedItem != null) { var rules = JsonUtils.Deserialize>(lockedItem.ruleSet); - foreach (var item in rules!) + int index = 1; + foreach (var item in rules) { + // 分流修改 + string outBoundIndexId = item.bingServerIndexId; + + if (outBoundIndexId == null) continue; + + var serverItem = LazyConfig.Instance.GetProfileItem(outBoundIndexId); + if (serverItem == null) continue; + item.outboundTag = $"{(serverItem.indexId.Equals(_config.indexId) ? "[proxy]" : $"[{index++}]")}{serverItem.remarks} ## {serverItem.indexId}"; + // end GenRoutingUserRule(item, singboxConfig.route.rules); } } diff --git a/v2rayN/v2rayN/Handler/CoreConfigV2ray.cs b/v2rayN/v2rayN/Handler/CoreConfigV2ray.cs index a77e94bc..93d6aa8d 100644 --- a/v2rayN/v2rayN/Handler/CoreConfigV2ray.cs +++ b/v2rayN/v2rayN/Handler/CoreConfigV2ray.cs @@ -43,6 +43,7 @@ namespace v2rayN.Handler msg = ResUI.FailedGenDefaultConfiguration; return -1; } + Outbounds4Ray originOutBound = JsonUtils.DeepCopy(v2rayConfig.outbounds[0]); GenLog(v2rayConfig); @@ -52,6 +53,8 @@ namespace v2rayN.Handler GenOutbound(node, v2rayConfig.outbounds[0]); + GenOutbounds(v2rayConfig, originOutBound); + GenMoreOutbounds(node, v2rayConfig); GenDns(v2rayConfig); @@ -187,8 +190,18 @@ namespace v2rayN.Handler v2rayConfig.routing.domainStrategy = routing.domainStrategy; } var rules = JsonUtils.Deserialize>(routing.ruleSet); + int index = 1; foreach (var item in rules) { + // 分流 + string outBoundIndexId = item.bingServerIndexId; + + if (outBoundIndexId == null) continue; + + var serverItem = LazyConfig.Instance.GetProfileItem(outBoundIndexId); + if (serverItem == null) continue; + item.outboundTag = $"{(serverItem.indexId.Equals(_config.indexId) ? "[proxy]" : $"[{index++}]")}{serverItem.remarks} ## {serverItem.indexId}"; + // end if (item.enabled) { var item2 = JsonUtils.Deserialize(JsonUtils.Serialize(item)); @@ -203,8 +216,18 @@ namespace v2rayN.Handler if (lockedItem != null) { var rules = JsonUtils.Deserialize>(lockedItem.ruleSet); + int index = 1; foreach (var item in rules) { + // 分流修改 + string outBoundIndexId = item.bingServerIndexId; + + if (outBoundIndexId == null) continue; + + var serverItem = LazyConfig.Instance.GetProfileItem(outBoundIndexId); + if (serverItem == null) continue; + item.outboundTag = $"{(serverItem.indexId.Equals(_config.indexId) ? "[proxy]" : $"[{index++}]")}{serverItem.remarks} ## {serverItem.indexId}"; + // end var item2 = JsonUtils.Deserialize(JsonUtils.Serialize(item)); GenRoutingUserRule(item2, v2rayConfig); } @@ -293,6 +316,32 @@ namespace v2rayN.Handler return 0; } + /// + /// 生成分流的出站规则 + /// + private int GenOutbounds(V2rayConfig v2rayConfig, Outbounds4Ray outbound) + { + var routing = ConfigHandler.GetDefaultRouting(_config); + var ruleSet = JsonUtils.Deserialize>(routing.ruleSet); + if (ruleSet == null) return -1; + + int index = 1; + foreach (var item in ruleSet) + { + string outBoundIndexId = item.bingServerIndexId; + if (outBoundIndexId == null) continue; + var serverItem = LazyConfig.Instance.GetProfileItem(outBoundIndexId); + if (serverItem == null) continue; + + var newOutbounds = JsonUtils.DeepCopy(outbound); + newOutbounds.tag = $"{(serverItem.indexId.Equals(_config.indexId) ? "[proxy]" : $"[{index++}]")}{serverItem.remarks} ## {serverItem.indexId}"; + v2rayConfig.outbounds.Add(newOutbounds); + GenOutbound(serverItem, newOutbounds); + } + + return 0; + } + private int GenOutbound(ProfileItem node, Outbounds4Ray outbound) { try diff --git a/v2rayN/v2rayN/Mode/RulesItem.cs b/v2rayN/v2rayN/Mode/RulesItem.cs index 1985c258..c7505493 100644 --- a/v2rayN/v2rayN/Mode/RulesItem.cs +++ b/v2rayN/v2rayN/Mode/RulesItem.cs @@ -12,6 +12,9 @@ public string outboundTag { get; set; } + // 分流修改 + public string bingServerIndexId { get; set; } + public List ip { get; set; } public List domain { get; set; } diff --git a/v2rayN/v2rayN/Sample/tun_singbox_rules b/v2rayN/v2rayN/Sample/tun_singbox_rules index 528adb76..df1dc4ec 100644 --- a/v2rayN/v2rayN/Sample/tun_singbox_rules +++ b/v2rayN/v2rayN/Sample/tun_singbox_rules @@ -1,8 +1,4 @@ [ - { - "protocol": [ "dns" ], - "outbound": "dns_out" - }, { "network": "udp", "port": [ @@ -20,12 +16,5 @@ "ff00::/8" ], "outbound": "block" - }, - { - "source_ip_cidr": [ - "224.0.0.0/3", - "ff00::/8" - ], - "outbound": "block" } ] \ No newline at end of file diff --git a/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs b/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs index eb622188..a17006f7 100644 --- a/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs +++ b/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs @@ -1621,6 +1621,12 @@ namespace v2rayN.ViewModels if (_config.tunModeItem.enableTun != EnableTun) { _config.tunModeItem.enableTun = EnableTun; + // ǹԱʱtunģʽ߼޸ + if (!Utils.IsAdministrator()) + { + RebootAsAdmin(); + return; + } Reload(); } } @@ -1839,7 +1845,7 @@ namespace v2rayN.ViewModels if (_config.uiItem.autoHideStartup) { Observable.Range(1, 1) - .Delay(TimeSpan.FromSeconds(2)) + .Delay(TimeSpan.FromSeconds(0.5)) // ʱ޸ .Subscribe(x => { Application.Current.Dispatcher.Invoke(() => diff --git a/v2rayN/v2rayN/Views/MainWindow.xaml.cs b/v2rayN/v2rayN/Views/MainWindow.xaml.cs index e4f30d56..ae5117ba 100644 --- a/v2rayN/v2rayN/Views/MainWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/MainWindow.xaml.cs @@ -212,7 +212,8 @@ namespace v2rayN.Views var IsAdministrator = Utils.IsAdministrator(); this.Title = $"{Utils.GetVersion()} - {(IsAdministrator ? ResUI.RunAsAdmin : ResUI.NotRunAsAdmin)}"; - spEnableTun.Visibility = IsAdministrator ? Visibility.Visible : Visibility.Collapsed; + // 非管理员运行时tun模式开启逻辑修改 + // spEnableTun.Visibility = IsAdministrator ? Visibility.Visible : Visibility.Collapsed; //if (_config.uiItem.autoHideStartup) //{ diff --git a/v2rayN/v2rayN/Views/RoutingRuleDetailsWindow.xaml b/v2rayN/v2rayN/Views/RoutingRuleDetailsWindow.xaml index bbce43c4..2d482c59 100644 --- a/v2rayN/v2rayN/Views/RoutingRuleDetailsWindow.xaml +++ b/v2rayN/v2rayN/Views/RoutingRuleDetailsWindow.xaml @@ -52,7 +52,22 @@ Margin="4" MaxDropDownHeight="1000" Style="{StaticResource DefComboBox}" /> - + + + + { clbProtocol.Items.Add(it); @@ -54,6 +79,19 @@ namespace v2rayN.Views this.WhenActivated(disposables => { + // 分流 + cmbOutboundIndexId.SelectedItem = cmbOutboundIndexId.Items.Cast().FirstOrDefault(item => item.ToString() == ViewModel.SelectedSource.bingServerIndexId); + cmbOutboundTag.SelectedIndex = cmbOutboundIndexId.SelectedIndex; + this.WhenAnyValue(v => v.cmbOutboundTag.SelectedItem) + .Where(selectedItem => selectedItem != null) + .Subscribe(selectedItem => + { + cmbOutboundIndexId.SelectedIndex = cmbOutboundTag.SelectedIndex; + ViewModel.SelectedSource.bingServerIndexId = cmbOutboundIndexId.SelectedItem.ToString(); + ViewModel.SelectedSource.outboundTag = cmbOutboundTag.Text; + }) + .DisposeWith(disposables); + // end 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.enabled, v => v.togEnabled.IsChecked).DisposeWith(disposables);