From 1b19ef54e48beed77c64ccbdf541c498db75f086 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Tue, 21 Feb 2023 20:42:55 +0800 Subject: [PATCH] Add Tun mode DNS settings --- v2rayN/v2rayN/Global.cs | 1 + v2rayN/v2rayN/Handler/TunHandler.cs | 16 +++++++ v2rayN/v2rayN/Mode/Config.cs | 1 + v2rayN/v2rayN/Mode/ConfigItems.cs | 2 + v2rayN/v2rayN/Resx/ResUI.Designer.cs | 9 ++++ v2rayN/v2rayN/Resx/ResUI.resx | 3 ++ v2rayN/v2rayN/Resx/ResUI.zh-Hans.resx | 5 ++- v2rayN/v2rayN/Sample/tun_singbox | 22 +--------- v2rayN/v2rayN/Sample/tun_singbox_dns | 21 +++++++++ v2rayN/v2rayN/Tool/Utils.cs | 4 ++ .../ViewModels/OptionSettingViewModel.cs | 6 +++ v2rayN/v2rayN/Views/OptionSettingWindow.xaml | 44 +++++++++++++++---- .../v2rayN/Views/OptionSettingWindow.xaml.cs | 2 + v2rayN/v2rayN/v2rayN.csproj | 7 ++- 14 files changed, 111 insertions(+), 32 deletions(-) create mode 100644 v2rayN/v2rayN/Sample/tun_singbox_dns diff --git a/v2rayN/v2rayN/Global.cs b/v2rayN/v2rayN/Global.cs index 9746e15d..0638ae16 100644 --- a/v2rayN/v2rayN/Global.cs +++ b/v2rayN/v2rayN/Global.cs @@ -33,6 +33,7 @@ public const string CustomRoutingFileName = "v2rayN.Sample.custom_routing_"; public const string v2raySampleInbound = "v2rayN.Sample.SampleInbound"; public const string TunSingboxFileName = "v2rayN.Sample.tun_singbox"; + public const string TunSingboxDNSFileName = "v2rayN.Sample.tun_singbox_dns"; public const string DefaultSecurity = "auto"; public const string DefaultNetwork = "tcp"; diff --git a/v2rayN/v2rayN/Handler/TunHandler.cs b/v2rayN/v2rayN/Handler/TunHandler.cs index 0ce373c6..304932b3 100644 --- a/v2rayN/v2rayN/Handler/TunHandler.cs +++ b/v2rayN/v2rayN/Handler/TunHandler.cs @@ -118,6 +118,22 @@ namespace v2rayN.Base //port configStr = configStr.Replace("$socksPort$", $"{_socksPort}"); + //dns + string dnsObject = String.Empty; + if (_config.tunModeItem.bypassMode) + { + dnsObject = _config.tunModeItem.directDNS; + } + else + { + dnsObject = _config.tunModeItem.proxyDNS; + } + if (dnsObject.IsNullOrEmpty() || Utils.ParseJson(dnsObject)?.ContainsKey("servers") == false) + { + dnsObject = Utils.GetEmbedText(Global.TunSingboxDNSFileName); + } + configStr = configStr.Replace("$dns_object$", dnsObject); + //exe List lstDnsExe = new(); List lstDirectExe = new(); diff --git a/v2rayN/v2rayN/Mode/Config.cs b/v2rayN/v2rayN/Mode/Config.cs index 25c7d52b..134c50b4 100644 --- a/v2rayN/v2rayN/Mode/Config.cs +++ b/v2rayN/v2rayN/Mode/Config.cs @@ -9,6 +9,7 @@ #region property public string indexId { get; set; } + public string subIndexId { get; set; } public string remoteDNS { get; set; } /// diff --git a/v2rayN/v2rayN/Mode/ConfigItems.cs b/v2rayN/v2rayN/Mode/ConfigItems.cs index 4deb94c8..86b5eaa9 100644 --- a/v2rayN/v2rayN/Mode/ConfigItems.cs +++ b/v2rayN/v2rayN/Mode/ConfigItems.cs @@ -168,8 +168,10 @@ namespace v2rayN.Mode public bool bypassMode { get; set; } = true; public List directIP { get; set; } public List directProcess { get; set; } + public string directDNS { get; set; } public List proxyIP { get; set; } public List proxyProcess { get; set; } + public string proxyDNS { get; set; } } diff --git a/v2rayN/v2rayN/Resx/ResUI.Designer.cs b/v2rayN/v2rayN/Resx/ResUI.Designer.cs index 9e089f77..7826aa2d 100644 --- a/v2rayN/v2rayN/Resx/ResUI.Designer.cs +++ b/v2rayN/v2rayN/Resx/ResUI.Designer.cs @@ -2833,6 +2833,15 @@ namespace v2rayN.Resx { } } + /// + /// 查找类似 DNS object, e.g. {"servers":[]} 的本地化字符串。 + /// + public static string TbSettingsTunModeDNS { + get { + return ResourceManager.GetString("TbSettingsTunModeDNS", resourceCulture); + } + } + /// /// 查找类似 Proxy IP CIDR, separated by commas (,) 的本地化字符串。 /// diff --git a/v2rayN/v2rayN/Resx/ResUI.resx b/v2rayN/v2rayN/Resx/ResUI.resx index 55f3ea6e..080a4842 100644 --- a/v2rayN/v2rayN/Resx/ResUI.resx +++ b/v2rayN/v2rayN/Resx/ResUI.resx @@ -1126,4 +1126,7 @@ SpeedTest Url + + DNS object, e.g. {"servers":[]} + \ 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 e2ec6f2b..377e6331 100644 --- a/v2rayN/v2rayN/Resx/ResUI.zh-Hans.resx +++ b/v2rayN/v2rayN/Resx/ResUI.zh-Hans.resx @@ -1126,4 +1126,7 @@ 测速文件地址 - + + DNS对象,例如 {"servers":[]} + + \ No newline at end of file diff --git a/v2rayN/v2rayN/Sample/tun_singbox b/v2rayN/v2rayN/Sample/tun_singbox index 128a058a..c9b2c70c 100644 --- a/v2rayN/v2rayN/Sample/tun_singbox +++ b/v2rayN/v2rayN/Sample/tun_singbox @@ -5,27 +5,7 @@ $log_output$ "timestamp": true }, - "dns": { - "servers": [ - { - "tag": "local", - "address": "223.5.5.5", - "detour": "direct" - }, - { - "tag": "block", - "address": "rcode://success" - } - ], - "rules": [ - { - "geosite": "category-ads-all", - "server": "block", - "disable_cache": true - } - ], - "strategy": "ipv4_only" - }, + "dns": $dns_object$ , "inbounds": [ { "type": "tun", diff --git a/v2rayN/v2rayN/Sample/tun_singbox_dns b/v2rayN/v2rayN/Sample/tun_singbox_dns new file mode 100644 index 00000000..c37b1248 --- /dev/null +++ b/v2rayN/v2rayN/Sample/tun_singbox_dns @@ -0,0 +1,21 @@ + { + "servers": [ + { + "tag": "local", + "address": "223.5.5.5", + "detour": "direct" + }, + { + "tag": "block", + "address": "rcode://success" + } + ], + "rules": [ + { + "geosite": "category-ads-all", + "server": "block", + "disable_cache": true + } + ], + "strategy": "ipv4_only" + } \ No newline at end of file diff --git a/v2rayN/v2rayN/Tool/Utils.cs b/v2rayN/v2rayN/Tool/Utils.cs index 2e883376..9c043931 100644 --- a/v2rayN/v2rayN/Tool/Utils.cs +++ b/v2rayN/v2rayN/Tool/Utils.cs @@ -111,6 +111,10 @@ namespace v2rayN string result = string.Empty; try { + if (obj == null) + { + return result; + } if (indented) { result = JsonConvert.SerializeObject(obj, diff --git a/v2rayN/v2rayN/ViewModels/OptionSettingViewModel.cs b/v2rayN/v2rayN/ViewModels/OptionSettingViewModel.cs index bb64314c..3d004d14 100644 --- a/v2rayN/v2rayN/ViewModels/OptionSettingViewModel.cs +++ b/v2rayN/v2rayN/ViewModels/OptionSettingViewModel.cs @@ -84,8 +84,10 @@ namespace v2rayN.ViewModels [Reactive] public bool TunBypassMode2 { get; set; } [Reactive] public string TunDirectIP { get; set; } [Reactive] public string TunDirectProcess { get; set; } + [Reactive] public string TunDirectDNS { get; set; } [Reactive] public string TunProxyIP { get; set; } [Reactive] public string TunProxyProcess { get; set; } + [Reactive] public string TunProxyDNS { get; set; } #endregion #region CoreType @@ -177,8 +179,10 @@ namespace v2rayN.ViewModels TunBypassMode = _config.tunModeItem.bypassMode; TunDirectIP = Utils.List2String(_config.tunModeItem.directIP, true); TunDirectProcess = Utils.List2String(_config.tunModeItem.directProcess, true); + TunDirectDNS = _config.tunModeItem.directDNS; TunProxyIP = Utils.List2String(_config.tunModeItem.proxyIP, true); TunProxyProcess = Utils.List2String(_config.tunModeItem.proxyProcess, true); + TunProxyDNS = _config.tunModeItem.proxyDNS; this.WhenAnyValue( x => x.TunBypassMode) .Subscribe(c => TunBypassMode2 = !TunBypassMode); @@ -350,8 +354,10 @@ namespace v2rayN.ViewModels _config.tunModeItem.bypassMode = TunBypassMode; _config.tunModeItem.directIP = Utils.String2List(TunDirectIP); _config.tunModeItem.directProcess = Utils.String2List(TunDirectProcess); + _config.tunModeItem.directDNS = Utils.ToJson(Utils.ParseJson(TunDirectDNS)); _config.tunModeItem.proxyIP = Utils.String2List(TunProxyIP); _config.tunModeItem.proxyProcess = Utils.String2List(TunProxyProcess); + _config.tunModeItem.proxyDNS = Utils.ToJson(Utils.ParseJson(TunProxyDNS)); //coreType SaveCoreType(); diff --git a/v2rayN/v2rayN/Views/OptionSettingWindow.xaml b/v2rayN/v2rayN/Views/OptionSettingWindow.xaml index a355244e..e5ab1a46 100644 --- a/v2rayN/v2rayN/Views/OptionSettingWindow.xaml +++ b/v2rayN/v2rayN/Views/OptionSettingWindow.xaml @@ -941,13 +941,15 @@ - - - + + + + + + + + + - - - + + + + + + + + + diff --git a/v2rayN/v2rayN/Views/OptionSettingWindow.xaml.cs b/v2rayN/v2rayN/Views/OptionSettingWindow.xaml.cs index 71141622..bd5b241c 100644 --- a/v2rayN/v2rayN/Views/OptionSettingWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/OptionSettingWindow.xaml.cs @@ -181,8 +181,10 @@ namespace v2rayN.Views 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.TunDirectDNS, v => v.txtDirectDNS.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.TunProxyDNS, v => v.txtProxyDNS.Text).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.CoreType1, v => v.cmbCoreType1.Text).DisposeWith(disposables); diff --git a/v2rayN/v2rayN/v2rayN.csproj b/v2rayN/v2rayN/v2rayN.csproj index 4c1f5885..4b61e1c8 100644 --- a/v2rayN/v2rayN/v2rayN.csproj +++ b/v2rayN/v2rayN/v2rayN.csproj @@ -13,7 +13,7 @@ 6.13 - + @@ -36,6 +36,9 @@ Never + + Never + Never @@ -65,7 +68,7 @@ Never - + Never