Skip to content

Commit 0567096

Browse files
authored
Merge pull request #75 from BornToBeRoot/improve-dns-lookup
Improve dns lookup
2 parents 0786ec1 + 2ed0b05 commit 0567096

36 files changed

Lines changed: 382 additions & 304 deletions

Source/NETworkManager/Helpers/HistoryListHelper.cs renamed to Source/NETworkManager/Helpers/ListHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace NETworkManager.Helpers
44
{
5-
public static class HistoryListHelper
5+
public static class ListHelper
66
{
77
public static List<string> Modify(List<string> list, string entry, int length)
88
{

Source/NETworkManager/MainWindow.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -595,9 +595,9 @@
595595
<ColumnDefinition Width="Auto" />
596596
<ColumnDefinition Width="*" />
597597
</Grid.ColumnDefinitions>
598-
<Rectangle Grid.Column="0" Width="24" Height="24">
598+
<Rectangle Grid.Column="0" Width="20" Height="20">
599599
<Rectangle.OpacityMask>
600-
<VisualBrush Stretch="Uniform" Visual="{IconPacks:MaterialLight Kind=ArrowLeftCircle}" />
600+
<VisualBrush Stretch="Uniform" Visual="{IconPacks:MaterialLight Kind=ArrowLeft}" />
601601
</Rectangle.OpacityMask>
602602
<Rectangle.Style>
603603
<Style TargetType="{x:Type Rectangle}">

Source/NETworkManager/Models/Network/DNSLookup.cs

Lines changed: 92 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
using Heijden.DNS;
22
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
35
using System.Net;
6+
using System.Net.NetworkInformation;
47
using System.Threading.Tasks;
58

69
namespace NETworkManager.Models.Network
710
{
811
public class DNSLookup
912
{
1013
#region Variables
11-
Resolver DNSResolver = new Resolver();
14+
Resolver dnsResolver = new Resolver();
1215
#endregion
1316

1417
#region Events
@@ -26,103 +29,145 @@ protected virtual void OnLookupError(DNSLookupErrorArgs e)
2629
LookupError?.Invoke(this, e);
2730
}
2831

29-
public event EventHandler<DNSLookupCompleteArgs> LookupComplete;
32+
public event EventHandler LookupComplete;
3033

31-
protected virtual void OnLookupComplete(DNSLookupCompleteArgs e)
34+
protected virtual void OnLookupComplete()
3235
{
33-
LookupComplete?.Invoke(this, e);
36+
LookupComplete?.Invoke(this, EventArgs.Empty);
3437
}
3538
#endregion
3639

3740
#region Methods
38-
public void LookupAsync(string hostnameOrIPAddress, DNSLookupOptions DNSLookupOptions)
41+
public void LookupAsync(List<string> hosts, DNSLookupOptions dnsLookupOptions)
3942
{
4043
Task.Run(() =>
4144
{
42-
// This will convert the query to an Arpa request
43-
if (DNSLookupOptions.Type == QType.PTR)
45+
// DNS server list
46+
List<string> dnsServers = new List<string>();
47+
48+
if (dnsLookupOptions.UseCustomDNSServer)
49+
dnsLookupOptions.CustomDNSServers.ForEach(x => dnsServers.Add(x));
50+
else // Use windows default dns server, but filter/remove IPv6 site local addresses (fec0:0:0:0:ffff::)
51+
dnsResolver.DnsServers.Where(x => !x.Address.ToString().StartsWith(@"fec0")).Select(y => y.Address.ToString()).ToList().ForEach(x => dnsServers.Add(x));
52+
53+
// Foreach host
54+
foreach (string host in hosts)
4455
{
45-
if (IPAddress.TryParse(hostnameOrIPAddress, out IPAddress ip))
46-
hostnameOrIPAddress = Resolver.GetArpaFromIp(ip);
47-
}
56+
// Default
57+
string name = host;
4858

49-
if (DNSLookupOptions.Type == QType.NAPTR)
50-
hostnameOrIPAddress = Resolver.GetArpaFromEnum(hostnameOrIPAddress);
59+
string dnsSuffix = string.Empty;
5160

52-
if (DNSLookupOptions.UseCustomDNSServer)
53-
DNSResolver.DnsServer = DNSLookupOptions.CustomDNSServer;
61+
if (name.IndexOf(".", StringComparison.OrdinalIgnoreCase) == -1)
62+
{
63+
if (dnsLookupOptions.AddDNSSuffix)
64+
{
65+
if (dnsLookupOptions.UseCustomDNSSuffix)
66+
dnsSuffix = dnsLookupOptions.CustomDNSSuffix;
67+
else
68+
dnsSuffix = IPGlobalProperties.GetIPGlobalProperties().DomainName;
69+
}
70+
}
5471

55-
DNSResolver.Recursion = DNSLookupOptions.Recursion;
56-
DNSResolver.TransportType = DNSLookupOptions.TransportType;
57-
DNSResolver.Retries = DNSLookupOptions.Attempts;
58-
DNSResolver.TimeOut = DNSLookupOptions.Timeout;
72+
// Append dns suffix to hostname
73+
if (!string.IsNullOrEmpty(dnsSuffix))
74+
name += string.Format(".{0}", dnsSuffix);
5975

60-
Response dnsResponse = DNSResolver.Query(hostnameOrIPAddress, DNSLookupOptions.Type, DNSLookupOptions.Class);
76+
// PTR
77+
if (dnsLookupOptions.Type == QType.PTR)
78+
{
79+
if (IPAddress.TryParse(name, out IPAddress ip))
80+
name = Resolver.GetArpaFromIp(ip);
81+
}
6182

62-
// If there was an error... return
63-
if (!string.IsNullOrEmpty(dnsResponse.Error))
64-
{
65-
OnLookupError(new DNSLookupErrorArgs(dnsResponse.Error, DNSResolver.DnsServer));
66-
return;
67-
}
83+
// NAPTR
84+
if (dnsLookupOptions.Type == QType.NAPTR)
85+
name = Resolver.GetArpaFromEnum(name);
6886

69-
// Process the results...
70-
ProcessResponse(dnsResponse);
87+
int port = dnsLookupOptions.UseCustomDNSServer ? dnsLookupOptions.Port : Resolver.DefaultPort;
7188

72-
// If we get a CNAME back (from a result), do a second request and try to get the A, AAAA etc...
73-
if (DNSLookupOptions.ResolveCNAME && DNSLookupOptions.Type != QType.CNAME)
74-
{
75-
foreach (RecordCNAME r in dnsResponse.RecordsCNAME)
89+
Parallel.ForEach(dnsServers, dnsServer =>
7690
{
77-
Response DNSResponse2 = DNSResolver.Query(r.CNAME, DNSLookupOptions.Type, DNSLookupOptions.Class);
91+
// Create a new for each request
92+
Resolver resolver = new Resolver(dnsServer, port)
93+
{
94+
Recursion = dnsLookupOptions.Recursion,
95+
TransportType = dnsLookupOptions.TransportType,
96+
Retries = dnsLookupOptions.Attempts,
97+
TimeOut = dnsLookupOptions.Timeout
98+
};
7899

79-
if (!string.IsNullOrEmpty(DNSResponse2.Error))
100+
Response dnsResponse = resolver.Query(name, dnsLookupOptions.Type, dnsLookupOptions.Class);
101+
102+
// If there was an error... return
103+
if (!string.IsNullOrEmpty(dnsResponse.Error))
80104
{
81-
OnLookupError(new DNSLookupErrorArgs(DNSResponse2.Error, DNSResolver.DnsServer));
82-
continue;
105+
OnLookupError(new DNSLookupErrorArgs(dnsResponse.Error, resolver.DnsServer));
106+
return;
83107
}
84108

85-
ProcessResponse(DNSResponse2);
86-
}
109+
// Process the results...
110+
ProcessResponse(dnsResponse);
111+
112+
// If we get a CNAME back (from an ANY result), do a second request and try to get the A, AAAA etc...
113+
if (dnsLookupOptions.ResolveCNAME && dnsLookupOptions.Type == QType.ANY)
114+
{
115+
foreach (RecordCNAME r in dnsResponse.RecordsCNAME)
116+
{
117+
Response dnsResponse2 = resolver.Query(r.CNAME, dnsLookupOptions.Type, dnsLookupOptions.Class);
118+
119+
if (!string.IsNullOrEmpty(dnsResponse2.Error))
120+
{
121+
OnLookupError(new DNSLookupErrorArgs(dnsResponse2.Error, resolver.DnsServer));
122+
continue;
123+
}
124+
125+
ProcessResponse(dnsResponse2);
126+
}
127+
}
128+
});
87129
}
88-
89-
OnLookupComplete(new DNSLookupCompleteArgs(dnsResponse.Server.ToString(), dnsResponse.Questions.Count, dnsResponse.Answers.Count, dnsResponse.Authorities.Count, dnsResponse.Additionals.Count, dnsResponse.MessageSize));
130+
131+
OnLookupComplete();
90132
});
91133
}
92134

93135
private void ProcessResponse(Response dnsResponse)
94136
{
137+
string dnsServer = dnsResponse.Server.Address.ToString();
138+
int port = dnsResponse.Server.Port;
139+
95140
// A
96141
foreach (RecordA r in dnsResponse.RecordsA)
97-
OnRecordReceived(new DNSLookupRecordArgs(r.RR, r));
142+
OnRecordReceived(new DNSLookupRecordArgs(r.RR, r, dnsServer, port));
98143

99144
// AAAA
100145
foreach (RecordAAAA r in dnsResponse.RecordsAAAA)
101-
OnRecordReceived(new DNSLookupRecordArgs(r.RR, r));
146+
OnRecordReceived(new DNSLookupRecordArgs(r.RR, r, dnsServer, port));
102147

103148
// CNAME
104149
foreach (RecordCNAME r in dnsResponse.RecordsCNAME)
105-
OnRecordReceived(new DNSLookupRecordArgs(r.RR, r));
150+
OnRecordReceived(new DNSLookupRecordArgs(r.RR, r, dnsServer, port));
106151

107152
// MX
108153
foreach (RecordMX r in dnsResponse.RecordsMX)
109-
OnRecordReceived(new DNSLookupRecordArgs(r.RR, r));
154+
OnRecordReceived(new DNSLookupRecordArgs(r.RR, r, dnsServer, port));
110155

111156
// NS
112157
foreach (RecordNS r in dnsResponse.RecordsNS)
113-
OnRecordReceived(new DNSLookupRecordArgs(r.RR, r));
158+
OnRecordReceived(new DNSLookupRecordArgs(r.RR, r, dnsServer, port));
114159

115160
// PTR
116161
foreach (RecordPTR r in dnsResponse.RecordsPTR)
117-
OnRecordReceived(new DNSLookupRecordArgs(r.RR, r));
162+
OnRecordReceived(new DNSLookupRecordArgs(r.RR, r, dnsServer, port));
118163

119164
// SOA
120165
foreach (RecordSOA r in dnsResponse.RecordsSOA)
121-
OnRecordReceived(new DNSLookupRecordArgs(r.RR, r));
166+
OnRecordReceived(new DNSLookupRecordArgs(r.RR, r, dnsServer, port));
122167

123168
// TXT
124169
foreach (RecordTXT r in dnsResponse.RecordsTXT)
125-
OnRecordReceived(new DNSLookupRecordArgs(r.RR, r));
170+
OnRecordReceived(new DNSLookupRecordArgs(r.RR, r, dnsServer, port));
126171
}
127172
#endregion
128173
}

Source/NETworkManager/Models/Network/DNSLookupOptions.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
using Heijden.DNS;
2+
using System.Collections.Generic;
23

34
namespace NETworkManager.Models.Network
45
{
56
public class DNSLookupOptions
67
{
78
public bool UseCustomDNSServer { get; set; }
8-
public string CustomDNSServer { get; set; }
9+
public List<string> CustomDNSServers { get; set; }
10+
public int Port { get; set; }
11+
public bool AddDNSSuffix { get; set; }
12+
public bool UseCustomDNSSuffix { get; set; }
13+
public string CustomDNSSuffix { get; set; }
914
public QClass Class { get; set; }
1015
public QType Type { get; set; }
1116
public bool Recursion { get; set; }

Source/NETworkManager/Models/Network/DNSLookupRecordArgs.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,20 @@ public class DNSLookupRecordArgs : System.EventArgs
66
{
77
public RR ResourceRecord { get; set; }
88
public string Result { get; set; }
9+
public string DNSServer { get; set; }
10+
public int Port { get; set; }
911

1012
public DNSLookupRecordArgs()
1113
{
1214

1315
}
1416

15-
public DNSLookupRecordArgs(RR resourceRecord, object result)
17+
public DNSLookupRecordArgs(RR resourceRecord, object result, string dnsServer, int port)
1618
{
1719
ResourceRecord = resourceRecord;
1820
Result = result.ToString().TrimEnd();
21+
DNSServer = dnsServer;
22+
Port = port;
1923
}
2024
}
2125
}

Source/NETworkManager/Models/Network/DNSLookupRecordInfo.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,28 @@ public class DNSLookupRecordInfo
99
public string Class { get; set; }
1010
public string Type { get; set; }
1111
public string Result { get; set; }
12+
public string DNSServer { get; set; }
13+
public int Port { get; set; }
1214

1315
public DNSLookupRecordInfo()
1416
{
1517

1618
}
1719

18-
public DNSLookupRecordInfo(RR resourceRecord, string result)
20+
public DNSLookupRecordInfo(RR resourceRecord, string result, string dnsServer, int port)
1921
{
2022
Name = resourceRecord.NAME;
2123
TTL = resourceRecord.TTL;
2224
Class = resourceRecord.Class.ToString();
2325
Type = resourceRecord.Type.ToString();
2426
Result = result;
27+
DNSServer = dnsServer;
28+
Port = port;
2529
}
2630

2731
public static DNSLookupRecordInfo Parse(DNSLookupRecordArgs e)
2832
{
29-
return new DNSLookupRecordInfo(e.ResourceRecord, e.Result);
33+
return new DNSLookupRecordInfo(e.ResourceRecord, e.Result, e.DNSServer, e.Port);
3034
}
3135
}
3236
}

Source/NETworkManager/Models/Settings/SettingsInfo.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -903,8 +903,8 @@ public bool DNSLookup_UseCustomDNSServer
903903
}
904904
}
905905

906-
private string _dnsLookup_CustomDNSServer;
907-
public string DNSLookup_CustomDNSServer
906+
private List<string> _dnsLookup_CustomDNSServer;
907+
public List<string> DNSLookup_CustomDNSServer
908908
{
909909
get { return _dnsLookup_CustomDNSServer; }
910910
set
@@ -917,6 +917,20 @@ public string DNSLookup_CustomDNSServer
917917
}
918918
}
919919

920+
private int _dnsLookup_Port = 53;
921+
public int DNSLookup_Port
922+
{
923+
get { return _dnsLookup_Port; }
924+
set
925+
{
926+
if (value == _dnsLookup_Port)
927+
return;
928+
929+
_dnsLookup_Port = value;
930+
SettingsChanged = true;
931+
}
932+
}
933+
920934
private QClass _dnsLookup_Class = QClass.IN;
921935
public QClass DNSLookup_Class
922936
{
@@ -973,7 +987,7 @@ public bool DNSLookup_UseCustomDNSSuffix
973987
}
974988
}
975989

976-
private string _dnsLookup_CustomDNSSuffix;
990+
private string _dnsLookup_CustomDNSSuffix = string.Empty;
977991
public string DNSLookup_CustomDNSSuffix
978992
{
979993
get { return _dnsLookup_CustomDNSSuffix; }

Source/NETworkManager/NETworkManager.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@
214214
<Compile Include="Models\Update\UpdateAvailableArgs.cs" />
215215
<Compile Include="Models\Update\Updater.cs" />
216216
<Compile Include="Validators\Int32Validator.cs" />
217+
<Compile Include="Validators\MultipleIPAddressesValidator.cs" />
217218
<Compile Include="Validators\MultipleHostsValidator.cs" />
218219
<Compile Include="Validators\HttpAndHttpsUriValidator.cs" />
219220
<Compile Include="Validators\SubnetCalculatorSubnetValidator.cs" />
@@ -676,7 +677,7 @@
676677
<Compile Include="Converters\ValidateSettingsImportConverter.cs" />
677678
<Compile Include="Converters\ValidateSettingsResetConverter.cs" />
678679
<Compile Include="Converters\ValidateSettingsExportConverter.cs" />
679-
<Compile Include="Helpers\HistoryListHelper.cs" />
680+
<Compile Include="Helpers\ListHelper.cs" />
680681
<Compile Include="Helpers\HotKeysHelper.cs" />
681682
<Compile Include="Models\Network\ARPTable.cs" />
682683
<Compile Include="Helpers\PortRangeHelper.cs" />

0 commit comments

Comments
 (0)