From c1e9856e92230366baf566108af68b376ff2165e Mon Sep 17 00:00:00 2001 From: ChsBuffer <33744752+chsbuffer@users.noreply.github.com> Date: Fri, 23 Oct 2020 17:48:47 +0800 Subject: [PATCH] refactor: check and build dns string --- Netch/Controllers/TUNTAPController.cs | 2 +- Netch/Forms/SettingForm.cs | 50 ++++----------------------- Netch/Utils/DNS.cs | 21 +++++++++++ 3 files changed, 29 insertions(+), 44 deletions(-) diff --git a/Netch/Controllers/TUNTAPController.cs b/Netch/Controllers/TUNTAPController.cs index f67052e8..54cc66ba 100644 --- a/Netch/Controllers/TUNTAPController.cs +++ b/Netch/Controllers/TUNTAPController.cs @@ -73,7 +73,7 @@ namespace Netch.Controllers { if (Global.Settings.TUNTAP.DNS.Any()) { - dns = string.Join(",", Global.Settings.TUNTAP.DNS); + dns = DNS.Join(Global.Settings.TUNTAP.DNS); } else { diff --git a/Netch/Forms/SettingForm.cs b/Netch/Forms/SettingForm.cs index ef1ae666..0e231139 100644 --- a/Netch/Forms/SettingForm.cs +++ b/Netch/Forms/SettingForm.cs @@ -90,11 +90,7 @@ namespace Netch.Forms Global.Settings.ModifySystemDNS); BindTextBox(ModifiedDNSTextBox, - s => - { - var dns = s.Split(',').Select(ip => ip.Trim()).ToArray(); - return dns.Length <= 2 && dns.All(ip => IPAddress.TryParse(ip, out _)); - }, + s => DNS.TrySplit(s, out _, 2), s => Global.Settings.ModifiedDNS = s, Global.Settings.ModifiedDNS); @@ -119,6 +115,11 @@ namespace Netch.Forms Global.Settings.TUNTAP.UseCustomDNS); TUNTAPUseCustomDNSCheckBox_CheckedChanged(null, null); + BindTextBox(TUNTAPDNSTextBox, + s => !UseCustomDNSCheckBox.Checked || DNS.TrySplit(s, out _, 2), + s => Global.Settings.TUNTAP.DNS = DNS.Split(s).ToList(), + DNS.Join(Global.Settings.TUNTAP.DNS)); + BindCheckBox(ProxyDNSCheckBox, b => Global.Settings.TUNTAP.ProxyDNS = b, Global.Settings.TUNTAP.ProxyDNS); @@ -244,7 +245,7 @@ namespace Netch.Forms if (UseCustomDNSCheckBox.Checked) { TUNTAPDNSTextBox.Text = Global.Settings.TUNTAP.DNS.Any() - ? string.Join(",", Global.Settings.TUNTAP.DNS) + ? DNS.Join(Global.Settings.TUNTAP.DNS) : "1.1.1.1"; } else @@ -283,42 +284,6 @@ namespace Netch.Forms #region Check - #region TUNTAP - - var dns = new string[0]; - try - { - if (UseCustomDNSCheckBox.Checked) - { - dns = TUNTAPDNSTextBox.Text.Split(',').Where(s => !string.IsNullOrEmpty(s)).Select(s => s.Trim()) - .ToArray(); - if (dns.Any()) - { - foreach (var ip in dns) - IPAddress.Parse(ip); - } - else - { - MessageBoxX.Show("DNS can not be empty"); - return; - } - } - } - catch (Exception exception) - { - if (exception is FormatException) - MessageBoxX.Show(i18N.Translate("IP address format illegal. Try again.")); - - if (UseCustomDNSCheckBox.Checked) - { - TUNTAPDNSTextBox.Text = string.Join(",", Global.Settings.TUNTAP.DNS); - } - - return; - } - - #endregion - #region Behavior // STUN @@ -356,7 +321,6 @@ namespace Netch.Forms pair.Value.Invoke(pair.Key); } - Global.Settings.TUNTAP.DNS = dns.ToList(); Global.Settings.STUN_Server = stunServer; Global.Settings.STUN_Server_Port = stunServerPort; diff --git a/Netch/Utils/DNS.cs b/Netch/Utils/DNS.cs index 014637da..d4849369 100644 --- a/Netch/Utils/DNS.cs +++ b/Netch/Utils/DNS.cs @@ -1,5 +1,7 @@ using System; using System.Collections; +using System.Collections.Generic; +using System.Linq; using System.Net; using Microsoft.Win32; @@ -75,5 +77,24 @@ namespace Netch.Utils } set => AdapterRegistry(true).SetValue("NameServer", value, RegistryValueKind.String); } + + public static IEnumerable Split(string dns) + { + return dns.Split(',').Where(ip => !string.IsNullOrWhiteSpace(ip)).Select(ip => ip.Trim()); + } + + public static bool TrySplit(string value, out IEnumerable result, ushort maxCount = 0) + { + result = Split(value).ToArray(); + + return maxCount == 0 || result.Count() <= maxCount + && + result.All(ip => IPAddress.TryParse(ip, out _)); + } + + public static string Join(IEnumerable dns) + { + return string.Join(",", dns); + } } } \ No newline at end of file