mirror of
https://github.com/netchx/netch.git
synced 2026-05-07 22:44:03 +08:00
refactor: check and build dns string
This commit is contained in:
@@ -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
|
||||
{
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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<string> Split(string dns)
|
||||
{
|
||||
return dns.Split(',').Where(ip => !string.IsNullOrWhiteSpace(ip)).Select(ip => ip.Trim());
|
||||
}
|
||||
|
||||
public static bool TrySplit(string value, out IEnumerable<string> 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<string> dns)
|
||||
{
|
||||
return string.Join(",", dns);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user