Skip to content

Commit

Permalink
refactor: check and build dns string
Browse files Browse the repository at this point in the history
  • Loading branch information
chsbuffer committed Oct 23, 2020
1 parent 41e74e0 commit c1e9856
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 44 deletions.
2 changes: 1 addition & 1 deletion Netch/Controllers/TUNTAPController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public bool Start(in Mode mode)
{
if (Global.Settings.TUNTAP.DNS.Any())
{
dns = string.Join(",", Global.Settings.TUNTAP.DNS);
dns = DNS.Join(Global.Settings.TUNTAP.DNS);
}
else
{
Expand Down
50 changes: 7 additions & 43 deletions Netch/Forms/SettingForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,7 @@ private void InitValue()
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);

Expand All @@ -119,6 +115,11 @@ private void InitValue()
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);
Expand Down Expand Up @@ -244,7 +245,7 @@ private void TUNTAPUseCustomDNSCheckBox_CheckedChanged(object sender, EventArgs
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
Expand Down Expand Up @@ -283,42 +284,6 @@ private void ControlButton_Click(object sender, EventArgs e)

#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
Expand Down Expand Up @@ -356,7 +321,6 @@ private void ControlButton_Click(object sender, EventArgs e)
pair.Value.Invoke(pair.Key);
}

Global.Settings.TUNTAP.DNS = dns.ToList();
Global.Settings.STUN_Server = stunServer;
Global.Settings.STUN_Server_Port = stunServerPort;

Expand Down
21 changes: 21 additions & 0 deletions Netch/Utils/DNS.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using Microsoft.Win32;

Expand Down Expand Up @@ -75,5 +77,24 @@ public static string OutboundDNS
}
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);
}
}
}

0 comments on commit c1e9856

Please sign in to comment.