From 6e366bf55a9b0c08e41c6cbd0f7531fa006ef288 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Mon, 6 Feb 2023 15:29:10 +0800 Subject: [PATCH] Improved enhanced tun mode --- v2rayN/v2rayN/Handler/CoreConfigHandler.cs | 5 +- v2rayN/v2rayN/Handler/TunHandler.cs | 74 +++++++++++++------ v2rayN/v2rayN/Mode/ConfigItems.cs | 3 + v2rayN/v2rayN/Resx/ResUI.Designer.cs | 36 +++++++++ v2rayN/v2rayN/Resx/ResUI.resx | 12 +++ v2rayN/v2rayN/Resx/ResUI.zh-Hans.resx | 12 +++ v2rayN/v2rayN/Sample/tun_singbox | 10 +++ .../ViewModels/OptionSettingViewModel.cs | 13 ++++ v2rayN/v2rayN/Views/OptionSettingWindow.xaml | 67 ++++++++++++++++- .../v2rayN/Views/OptionSettingWindow.xaml.cs | 5 ++ 10 files changed, 211 insertions(+), 26 deletions(-) diff --git a/v2rayN/v2rayN/Handler/CoreConfigHandler.cs b/v2rayN/v2rayN/Handler/CoreConfigHandler.cs index 86617d8c..bbce66ee 100644 --- a/v2rayN/v2rayN/Handler/CoreConfigHandler.cs +++ b/v2rayN/v2rayN/Handler/CoreConfigHandler.cs @@ -88,9 +88,10 @@ namespace v2rayN.Handler { if (config.logEnabled) { + var dtNow = DateTime.Now; v2rayConfig.log.loglevel = config.loglevel; - v2rayConfig.log.access = Utils.GetLogPath(v2rayConfig.log.access); - v2rayConfig.log.error = Utils.GetLogPath(v2rayConfig.log.error); + v2rayConfig.log.access = Utils.GetLogPath($"Vaccess_{dtNow.ToString("yyyy-MM-dd")}.txt"); + v2rayConfig.log.error = Utils.GetLogPath($"Verror_{dtNow.ToString("yyyy-MM-dd")}.txt"); } else { diff --git a/v2rayN/v2rayN/Handler/TunHandler.cs b/v2rayN/v2rayN/Handler/TunHandler.cs index 2f0746fa..591af86c 100644 --- a/v2rayN/v2rayN/Handler/TunHandler.cs +++ b/v2rayN/v2rayN/Handler/TunHandler.cs @@ -100,6 +100,17 @@ namespace v2rayN.Base configStr = configStr.Replace("$strict_route$", $"{_config.tunModeItem.strictRoute.ToString().ToLower()}"); configStr = configStr.Replace("$stack$", $"{_config.tunModeItem.stack}"); + //logs + if (_config.tunModeItem.showWindow) + { + configStr = configStr.Replace("$log_output$", $""); + } + else + { + var dtNow = DateTime.Now; + var log_output = $"\"output\": \"{Utils.GetLogPath($"singbox_{dtNow.ToString("yyyy-MM-dd")}.txt")}\", "; + configStr = configStr.Replace("$log_output$", $"{log_output.Replace(@"\", @"\\")}"); + } //port configStr = configStr.Replace("$socksPort$", $"{_socksPort}"); @@ -118,13 +129,13 @@ namespace v2rayN.Base { if (!lstDnsExe.Contains(it2) && it.coreType != ECoreType.sing_box) { - lstDnsExe.Add(it2); + //lstDnsExe.Add(it2); lstDnsExe.Add($"{it2}.exe"); } if (!lstDirectExe.Contains(it2)) { - lstDirectExe.Add(it2); + //lstDirectExe.Add(it2); lstDirectExe.Add($"{it2}.exe"); } } @@ -135,32 +146,50 @@ namespace v2rayN.Base string strDirect = string.Join("\",\"", lstDirectExe.ToArray()); configStr = configStr.Replace("$directProcessName$", $"\"{strDirect}\""); + if (_config.tunModeItem.bypassMode) + { + //direct ips + if (_config.tunModeItem.directIP != null && _config.tunModeItem.directIP.Count > 0) + { + var ips = new { outbound = "direct", ip_cidr = _config.tunModeItem.directIP }; + configStr = configStr.Replace("$ruleDirectIPs$", "," + Utils.ToJson(ips)); + } + //direct process + if (_config.tunModeItem.directProcess != null && _config.tunModeItem.directProcess.Count > 0) + { + var process = new { outbound = "direct", process_name = _config.tunModeItem.directProcess }; + configStr = configStr.Replace("$ruleDirectProcess$", "," + Utils.ToJson(process)); + } + } + else + { + //proxy ips + if (_config.tunModeItem.proxyIP != null && _config.tunModeItem.proxyIP.Count > 0) + { + var ips = new { outbound = "proxy", ip_cidr = _config.tunModeItem.proxyIP }; + configStr = configStr.Replace("$ruleProxyIPs$", "," + Utils.ToJson(ips)); + } + //proxy process + if (_config.tunModeItem.proxyProcess != null && _config.tunModeItem.proxyProcess.Count > 0) + { + var process = new { outbound = "proxy", process_name = _config.tunModeItem.proxyProcess }; + configStr = configStr.Replace("$ruleProxyProcess$", "," + Utils.ToJson(process)); + } - //ips - if (_config.tunModeItem.directIP != null && _config.tunModeItem.directIP.Count > 0) - { - var ips = new { outbound = "direct", ip_cidr = _config.tunModeItem.directIP }; - configStr = configStr.Replace("$ruleDirectIPs$", "," + Utils.ToJson(ips)); - } - else - { - configStr = configStr.Replace("$ruleDirectIPs$", ""); - } - //process - if (_config.tunModeItem.directProcess != null && _config.tunModeItem.directProcess.Count > 0) - { - var process = new { outbound = "direct", process_name = _config.tunModeItem.directProcess }; - configStr = configStr.Replace("$ruleDirectProcess$", "," + Utils.ToJson(process)); - } - else - { - configStr = configStr.Replace("$ruleDirectProcess$", ""); + var final = new { outbound = "direct", inbound = "tun-in" }; + configStr = configStr.Replace("$ruleFinally$", "," + Utils.ToJson(final)); } + configStr = configStr.Replace("$ruleDirectIPs$", ""); + configStr = configStr.Replace("$ruleDirectProcess$", ""); + configStr = configStr.Replace("$ruleProxyIPs$", ""); + configStr = configStr.Replace("$ruleProxyProcess$", ""); + configStr = configStr.Replace("$ruleFinally$", ""); + File.WriteAllText(Utils.GetConfigPath(_tunConfigName), configStr); return true; - } + } private void CoreStop() { @@ -172,6 +201,7 @@ namespace v2rayN.Base KillProcess(_process); _process.Dispose(); _process = null; + _needRestart = true; } } catch (Exception ex) diff --git a/v2rayN/v2rayN/Mode/ConfigItems.cs b/v2rayN/v2rayN/Mode/ConfigItems.cs index f38aef19..0bce7632 100644 --- a/v2rayN/v2rayN/Mode/ConfigItems.cs +++ b/v2rayN/v2rayN/Mode/ConfigItems.cs @@ -110,8 +110,11 @@ namespace v2rayN.Mode public string stack { get; set; } public int mtu { get; set; } public string customTemplate { get; set; } + public bool bypassMode { get; set; } = true; public List directIP { get; set; } public List directProcess { get; set; } + public List proxyIP { get; set; } + public List proxyProcess { get; set; } } } diff --git a/v2rayN/v2rayN/Resx/ResUI.Designer.cs b/v2rayN/v2rayN/Resx/ResUI.Designer.cs index 0bceb619..ffddd60f 100644 --- a/v2rayN/v2rayN/Resx/ResUI.Designer.cs +++ b/v2rayN/v2rayN/Resx/ResUI.Designer.cs @@ -2770,6 +2770,24 @@ namespace v2rayN.Resx { } } + /// + /// 查找类似 Bypass Mode 的本地化字符串。 + /// + public static string TbSettingsTunModeBypassMode { + get { + return ResourceManager.GetString("TbSettingsTunModeBypassMode", resourceCulture); + } + } + + /// + /// 查找类似 Enable: If no route matches, the final proxy 的本地化字符串。 + /// + public static string TbSettingsTunModeBypassModeTip { + get { + return ResourceManager.GetString("TbSettingsTunModeBypassModeTip", resourceCulture); + } + } + /// /// 查找类似 Custom Template 的本地化字符串。 /// @@ -2797,6 +2815,24 @@ namespace v2rayN.Resx { } } + /// + /// 查找类似 Proxy IP CIDR, separated by commas (,) 的本地化字符串。 + /// + public static string TbSettingsTunModeProxyIP { + get { + return ResourceManager.GetString("TbSettingsTunModeProxyIP", resourceCulture); + } + } + + /// + /// 查找类似 Proxy Process name, separated by commas (,) 的本地化字符串。 + /// + public static string TbSettingsTunModeProxyProcess { + get { + return ResourceManager.GetString("TbSettingsTunModeProxyProcess", resourceCulture); + } + } + /// /// 查找类似 Show console 的本地化字符串。 /// diff --git a/v2rayN/v2rayN/Resx/ResUI.resx b/v2rayN/v2rayN/Resx/ResUI.resx index f1a8d7d7..91a5abbd 100644 --- a/v2rayN/v2rayN/Resx/ResUI.resx +++ b/v2rayN/v2rayN/Resx/ResUI.resx @@ -1108,4 +1108,16 @@ FontSize + + Proxy IP CIDR, separated by commas (,) + + + Proxy Process name, separated by commas (,) + + + Bypass Mode + + + Enable: If no route matches, the final proxy + \ No newline at end of file diff --git a/v2rayN/v2rayN/Resx/ResUI.zh-Hans.resx b/v2rayN/v2rayN/Resx/ResUI.zh-Hans.resx index 43468197..a02aaddf 100644 --- a/v2rayN/v2rayN/Resx/ResUI.zh-Hans.resx +++ b/v2rayN/v2rayN/Resx/ResUI.zh-Hans.resx @@ -1108,4 +1108,16 @@ 字体大小 + + 代理的IP CIDR,用逗号(,)分隔 + + + 代理的进程名,用逗号(,)分隔 + + + 绕行模式 + + + 启用:路由无匹配则最终代理 + \ No newline at end of file diff --git a/v2rayN/v2rayN/Sample/tun_singbox b/v2rayN/v2rayN/Sample/tun_singbox index 51904d0e..319fddca 100644 --- a/v2rayN/v2rayN/Sample/tun_singbox +++ b/v2rayN/v2rayN/Sample/tun_singbox @@ -1,4 +1,10 @@ { + "log": { + "disabled": false, + "level": "info", + $log_output$ + "timestamp": true + }, "dns": { "servers": [ { @@ -23,6 +29,7 @@ "inbounds": [ { "type": "tun", + "tag": "tun-in", "interface_name": "singbox_tun", "inet4_address": "172.19.0.1/30", @@ -102,6 +109,9 @@ } $ruleDirectIPs$ $ruleDirectProcess$ + $ruleProxyIPs$ + $ruleProxyProcess$ + $ruleFinally$ ] } } \ No newline at end of file diff --git a/v2rayN/v2rayN/ViewModels/OptionSettingViewModel.cs b/v2rayN/v2rayN/ViewModels/OptionSettingViewModel.cs index c4489919..756eacbe 100644 --- a/v2rayN/v2rayN/ViewModels/OptionSettingViewModel.cs +++ b/v2rayN/v2rayN/ViewModels/OptionSettingViewModel.cs @@ -77,8 +77,12 @@ namespace v2rayN.ViewModels [Reactive] public string TunStack { get; set; } [Reactive] public int TunMtu { get; set; } [Reactive] public string TunCustomTemplate { get; set; } + [Reactive] public bool TunBypassMode { get; set; } + [Reactive] public bool TunBypassMode2 { get; set; } [Reactive] public string TunDirectIP { get; set; } [Reactive] public string TunDirectProcess { get; set; } + [Reactive] public string TunProxyIP { get; set; } + [Reactive] public string TunProxyProcess { get; set; } #endregion #region CoreType @@ -164,8 +168,14 @@ namespace v2rayN.ViewModels TunStack = _config.tunModeItem.stack; TunMtu = _config.tunModeItem.mtu; TunCustomTemplate = _config.tunModeItem.customTemplate; + TunBypassMode = _config.tunModeItem.bypassMode; TunDirectIP = Utils.List2String(_config.tunModeItem.directIP, true); TunDirectProcess = Utils.List2String(_config.tunModeItem.directProcess, true); + TunProxyIP = Utils.List2String(_config.tunModeItem.proxyIP, true); + TunProxyProcess = Utils.List2String(_config.tunModeItem.proxyProcess, true); + this.WhenAnyValue( + x => x.TunBypassMode) + .Subscribe(c => TunBypassMode2 = !TunBypassMode); #endregion @@ -328,8 +338,11 @@ namespace v2rayN.ViewModels _config.tunModeItem.stack = TunStack; _config.tunModeItem.mtu = TunMtu; _config.tunModeItem.customTemplate = TunCustomTemplate; + _config.tunModeItem.bypassMode = TunBypassMode; _config.tunModeItem.directIP = Utils.String2List(TunDirectIP); _config.tunModeItem.directProcess = Utils.String2List(TunDirectProcess); + _config.tunModeItem.proxyIP = Utils.String2List(TunProxyIP); + _config.tunModeItem.proxyProcess = Utils.String2List(TunProxyProcess); //coreType SaveCoreType(); diff --git a/v2rayN/v2rayN/Views/OptionSettingWindow.xaml b/v2rayN/v2rayN/Views/OptionSettingWindow.xaml index d78b6fe4..14e76d05 100644 --- a/v2rayN/v2rayN/Views/OptionSettingWindow.xaml +++ b/v2rayN/v2rayN/Views/OptionSettingWindow.xaml @@ -771,6 +771,7 @@ + @@ -849,7 +850,7 @@ x:Name="txtCustomTemplate" Grid.Row="5" Grid.Column="1" - Width="600" + Width="400" Margin="{StaticResource SettingItemMargin}" VerticalAlignment="Top" AcceptsReturn="True" @@ -864,9 +865,35 @@ Click="btnBrowse_Click" Content="{x:Static resx:ResUI.TbBrowse}" Style="{StaticResource DefButton}" /> + + + + - + + @@ -896,6 +923,42 @@ VerticalScrollBarVisibility="Auto" /> + + + + + + + + + + + + + + + + diff --git a/v2rayN/v2rayN/Views/OptionSettingWindow.xaml.cs b/v2rayN/v2rayN/Views/OptionSettingWindow.xaml.cs index 2d53ec19..62ed97bc 100644 --- a/v2rayN/v2rayN/Views/OptionSettingWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/OptionSettingWindow.xaml.cs @@ -164,8 +164,13 @@ namespace v2rayN.Views this.Bind(ViewModel, vm => vm.TunStack, v => v.cmbStack.Text).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.TunMtu, v => v.cmbMtu.Text).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.TunCustomTemplate, v => v.txtCustomTemplate.Text).DisposeWith(disposables); + this.Bind(ViewModel, vm => vm.TunBypassMode, v => v.togBypassMode.IsChecked).DisposeWith(disposables); + this.OneWayBind(ViewModel, vm => vm.TunBypassMode, v => v.gridTunModeDirect.Visibility, vmToViewConverterOverride: new BooleanToVisibilityTypeConverter()).DisposeWith(disposables); + this.OneWayBind(ViewModel, vm => vm.TunBypassMode2, v => v.gridTunModeProxy.Visibility, vmToViewConverterOverride: new BooleanToVisibilityTypeConverter()).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.TunDirectIP, v => v.txtDirectIP.Text).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.TunDirectProcess, v => v.txtDirectProcess.Text).DisposeWith(disposables); + this.Bind(ViewModel, vm => vm.TunProxyIP, v => v.txtProxyIP.Text).DisposeWith(disposables); + this.Bind(ViewModel, vm => vm.TunProxyProcess, v => v.txtProxyProcess.Text).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.CoreType1, v => v.cmbCoreType1.Text).DisposeWith(disposables);