Skip to content

Commit 4e2cf24

Browse files
committed
1 parent ef6e044 commit 4e2cf24

19 files changed

Lines changed: 259 additions & 350 deletions

Source/NETworkManager/GlobalStaticConfiguration.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using NETworkManager.Utilities;
1111
using NETworkManager.Enum;
1212
using NETworkManager.Models.RemoteDesktop;
13+
using DnsClient;
1314

1415
// ReSharper disable InconsistentNaming
1516

@@ -56,7 +57,7 @@ public class GlobalStaticConfiguration
5657
public static int IPScanner_ICMPAttempts => 2;
5758
public static int IPScanner_ICMPBuffer => 32;
5859
public static int IPScanner_DNSPort => 53;
59-
public static bool IPScanner_DNSTCPOnly => false;
60+
public static bool IPScanner_DNSUseTCPOnly => false;
6061
public static int IPScanner_DNSTimeout => 2000;
6162
public static int IPScanner_DNSRetries => 3;
6263
public static int IPScanner_ICMPTimeout => 4000;
@@ -83,10 +84,10 @@ public class GlobalStaticConfiguration
8384
public static ExportManager.ExportFileType Traceroute_ExportFileType => ExportManager.ExportFileType.CSV;
8485

8586
// Application: DNS Lookup
86-
public static QClass DNSLookup_Class => QClass.IN;
87-
public static QType DNSLookup_Type => QType.ANY;
88-
public static TransportType DNSLookup_TransportType => TransportType.Udp;
89-
public static int DNSLookup_Attempts => 3;
87+
public static QueryClass DNSLookup_QueryClass => QueryClass.IN;
88+
public static QueryType DNSLookup_QueryType => QueryType.ANY;
89+
public static bool DNSLookup_UseTCPOnly => false;
90+
public static int DNSLookup_Retries => 3;
9091
public static int DNSLookup_Timeout => 2000;
9192
public static ExportManager.ExportFileType DNSLookup_ExportFileType => ExportManager.ExportFileType.CSV;
9293

Source/NETworkManager/Models/Export/ExportManager.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -295,10 +295,10 @@ private static void CreateCSV(IEnumerable<DNSLookupRecordInfo> collection, strin
295295
{
296296
var stringBuilder = new StringBuilder();
297297

298-
stringBuilder.AppendLine($"{nameof(DNSLookupRecordInfo.Name)},{nameof(DNSLookupRecordInfo.TTL)},{nameof(DNSLookupRecordInfo.Class)},{nameof(DNSLookupRecordInfo.Type)},{nameof(DNSLookupRecordInfo.Result)},{nameof(DNSLookupRecordInfo.DNSServer)},{nameof(DNSLookupRecordInfo.Port)}");
298+
stringBuilder.AppendLine($"{nameof(DNSLookupRecordInfo.DomainName)},{nameof(DNSLookupRecordInfo.TTL)},{nameof(DNSLookupRecordInfo.Class)},{nameof(DNSLookupRecordInfo.Type)},{nameof(DNSLookupRecordInfo.Result)},{nameof(DNSLookupRecordInfo.DNSServer)},{nameof(DNSLookupRecordInfo.Port)}");
299299

300300
foreach (var info in collection)
301-
stringBuilder.AppendLine($"{info.Name},{info.TTL},{info.Class},{info.Type},{info.Result},{info.DNSServer},{info.Port}");
301+
stringBuilder.AppendLine($"{info.DomainName},{info.TTL},{info.Class},{info.Type},{info.Result},{info.DNSServer},{info.Port}");
302302

303303
System.IO.File.WriteAllText(filePath, stringBuilder.ToString());
304304
}
@@ -486,7 +486,7 @@ public static void CreateXML(IEnumerable<DNSLookupRecordInfo> collection, string
486486
from info in collection
487487
select
488488
new XElement(nameof(DNSLookupRecordInfo),
489-
new XElement(nameof(DNSLookupRecordInfo.Name), info.Name),
489+
new XElement(nameof(DNSLookupRecordInfo.DomainName), info.DomainName),
490490
new XElement(nameof(DNSLookupRecordInfo.TTL), info.TTL),
491491
new XElement(nameof(DNSLookupRecordInfo.Class), info.Class),
492492
new XElement(nameof(DNSLookupRecordInfo.Type), info.Type),
@@ -721,7 +721,7 @@ public static void CreateJSON(ObservableCollection<DNSLookupRecordInfo> collecti
721721
{
722722
jsonData[i] = new
723723
{
724-
collection[i].Name,
724+
collection[i].DomainName,
725725
collection[i].TTL,
726726
collection[i].Class,
727727
collection[i].Type,

Source/NETworkManager/Models/Network/DNSLookup.cs

Lines changed: 52 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Heijden.DNS;
1+
using DnsClient;
22
using System;
33
using System.Collections.Generic;
44
using System.Linq;
@@ -13,21 +13,18 @@ public class DNSLookup
1313
{
1414
#region Variables
1515
public bool UseCustomDNSServer = false;
16-
public List<string> CustomDNSServers = new List<string> { "1.1.1.1", "1.0.0.1" };
17-
public int Port = 53;
16+
public DNSServerInfo CustomDNSServer;
1817
public bool AddDNSSuffix = true;
1918
public bool UseCustomDNSSuffix = false;
2019
public string CustomDNSSuffix;
21-
public QClass Class = QClass.IN;
22-
public QType Type = QType.ANY;
20+
public QueryClass QueryClass = QueryClass.IN;
21+
public QueryType QueryType = QueryType.ANY;
22+
23+
public bool UseCache = false;
2324
public bool Recursion = true;
24-
public bool UseResolverCache = false;
25-
public TransportType TransportType = TransportType.Udp;
26-
public int Attempts = 3;
27-
public int Timeout = 2000;
28-
public bool ResolveCNAME = true;
29-
30-
private readonly Resolver _dnsResolver = new Resolver();
25+
public bool UseTCPOnly = false;
26+
public int Retries = 3;
27+
public TimeSpan Timeout = new TimeSpan(2000);
3128
#endregion
3229

3330
#region Events
@@ -54,71 +51,28 @@ protected virtual void OnLookupComplete()
5451
#endregion
5552

5653
#region Methods
57-
public Task<Tuple<IPAddress, List<string>>> ResolvePTRAsync(IPAddress host)
54+
private List<IPEndPoint> GetDnsServer()
5855
{
59-
return Task.Run(() => ResolvePTR(host));
60-
}
61-
62-
public Tuple<IPAddress, List<string>> ResolvePTR(IPAddress host)
63-
{
64-
// DNS server list
65-
var dnsServers = new List<string>();
56+
List<IPEndPoint> dnsServers = new List<IPEndPoint>();
6657

6758
if (UseCustomDNSServer)
68-
CustomDNSServers.ForEach(x => dnsServers.Add(x));
69-
else // Use windows default dns server, but filter/remove IPv6 site local addresses (fec0:0:0:0:ffff::)
70-
_dnsResolver.DnsServers.Where(x => !x.Address.ToString().StartsWith(@"fec0")).Select(y => y.Address.ToString()).ToList().ForEach(x => dnsServers.Add(x));
71-
72-
// PTR
73-
var name = Resolver.GetArpaFromIp(host);
74-
75-
var port = UseCustomDNSServer ? Port : Resolver.DefaultPort;
76-
77-
var dnsServerIPAddress = string.Empty;
78-
var ptrResults = new List<string>();
79-
80-
foreach (var dnsServer in dnsServers)
8159
{
82-
dnsServerIPAddress = dnsServer;
83-
84-
// Create a new for each request
85-
var resolver = new Resolver(dnsServer, port)
86-
{
87-
Recursion = Recursion,
88-
TransportType = TransportType,
89-
UseCache = UseResolverCache,
90-
Retries = Attempts,
91-
TimeOut = Timeout
92-
};
93-
94-
var dnsResponse = resolver.Query(name, QType.PTR);
95-
96-
if (!string.IsNullOrEmpty(dnsResponse.Error)) // On error... try next dns server...
97-
continue;
98-
99-
// PTR
100-
ptrResults.AddRange(dnsResponse.RecordsPTR.Select(r => r.PTRDNAME));
101-
102-
// If we got results, break... else --> check next dns server
103-
if (ptrResults.Count > 0)
104-
break;
60+
foreach (var dnsServer in CustomDNSServer.Servers)
61+
dnsServers.Add(new IPEndPoint(IPAddress.Parse(dnsServer), CustomDNSServer.Port));
62+
}
63+
else
64+
{
65+
foreach (var dnsServer in NameServer.ResolveNameServers(true, false))
66+
dnsServers.Add(dnsServer);
10567
}
10668

107-
return new Tuple<IPAddress, List<string>>(IPAddress.Parse(dnsServerIPAddress), ptrResults);
69+
return dnsServers;
10870
}
10971

11072
public void ResolveAsync(List<string> hosts)
11173
{
11274
Task.Run(() =>
11375
{
114-
// DNS server list
115-
var dnsServers = new List<string>();
116-
117-
if (UseCustomDNSServer)
118-
CustomDNSServers.ForEach(x => dnsServers.Add(x));
119-
else // Use windows default dns server, but filter/remove IPv6 site local addresses (fec0:0:0:0:ffff::)
120-
_dnsResolver.DnsServers.Where(x => !x.Address.ToString().StartsWith(@"fec0")).Select(y => y.Address.ToString()).ToList().ForEach(x => dnsServers.Add(x));
121-
12276
// Foreach host
12377
foreach (var host in hosts)
12478
{
@@ -130,113 +84,79 @@ public void ResolveAsync(List<string> hosts)
13084
if (name.IndexOf(".", StringComparison.OrdinalIgnoreCase) == -1)
13185
{
13286
if (AddDNSSuffix)
133-
{
13487
dnsSuffix = UseCustomDNSSuffix ? CustomDNSSuffix : IPGlobalProperties.GetIPGlobalProperties().DomainName;
135-
}
13688
}
13789

13890
// Append dns suffix to hostname
13991
if (!string.IsNullOrEmpty(dnsSuffix))
14092
name += $".{dnsSuffix}";
14193

142-
switch (Type)
94+
Parallel.ForEach(GetDnsServer(), dnsServer =>
14395
{
144-
// PTR
145-
case QType.PTR:
146-
if (IPAddress.TryParse(name, out IPAddress ip))
147-
name = Resolver.GetArpaFromIp(ip);
148-
break;
149-
// NAPTR
150-
case QType.NAPTR:
151-
name = Resolver.GetArpaFromEnum(name);
152-
break;
153-
}
96+
LookupClient dnsLookupClient = new LookupClient(dnsServer);
97+
dnsLookupClient.UseTcpOnly = UseTCPOnly;
98+
dnsLookupClient.UseCache = UseCache;
99+
dnsLookupClient.Recursion = Recursion;
100+
dnsLookupClient.Timeout = Timeout;
101+
dnsLookupClient.Retries = Retries;
154102

155-
var port = UseCustomDNSServer ? Port : Resolver.DefaultPort;
156-
157-
Parallel.ForEach(dnsServers, dnsServer =>
158-
{
159-
// Create a new for each request
160-
var resolver = new Resolver(dnsServer, port)
161-
{
162-
Recursion = Recursion,
163-
TransportType = TransportType,
164-
UseCache = UseResolverCache,
165-
Retries = Attempts,
166-
TimeOut = Timeout
167-
};
168-
169-
var dnsResponse = resolver.Query(name, Type, Class);
103+
// PTR vs A, AAAA, CNAME etc.
104+
var dnsResponse = QueryType == QueryType.PTR ? dnsLookupClient.QueryReverse(IPAddress.Parse(host)) : dnsLookupClient.Query(host, QueryType, QueryClass);
170105

171106
// If there was an error... return
172-
if (!string.IsNullOrEmpty(dnsResponse.Error))
107+
if (dnsResponse.HasError)
173108
{
174-
OnLookupError(new DNSLookupErrorArgs(dnsResponse.Error, resolver.DnsServer));
109+
OnLookupError(new DNSLookupErrorArgs(dnsResponse.ErrorMessage, dnsResponse.NameServer.Endpoint));
175110
return;
176111
}
177112

178113
// Process the results...
179-
ProcessResponse(dnsResponse);
180-
181-
// If we get a CNAME back (from an ANY result), do a second request and try to get the A, AAAA etc...
182-
if (!ResolveCNAME || Type != QType.ANY)
183-
return;
184-
185-
foreach (var record in dnsResponse.RecordsCNAME)
186-
{
187-
var dnsResponse2 = resolver.Query(record.CNAME, Type, Class);
188-
189-
if (!string.IsNullOrEmpty(dnsResponse2.Error))
190-
{
191-
OnLookupError(new DNSLookupErrorArgs(dnsResponse2.Error, resolver.DnsServer));
192-
continue;
193-
}
194-
195-
ProcessResponse(dnsResponse2);
196-
}
114+
ProcessDnsQueryResponse(dnsResponse);
197115
});
198116
}
199117

200118
OnLookupComplete();
201119
});
202120
}
203121

204-
private void ProcessResponse(Response dnsResponse)
122+
private void ProcessDnsQueryResponse(IDnsQueryResponse dnsQueryResponse)
205123
{
206-
var dnsServer = dnsResponse.Server.Address.ToString();
207-
var port = dnsResponse.Server.Port;
124+
var dnsServer = dnsQueryResponse.NameServer.Endpoint;
208125

209126
// A
210-
foreach (var r in dnsResponse.RecordsA)
211-
OnRecordReceived(new DNSLookupRecordArgs(r.RR, r, dnsServer, port));
127+
foreach (var record in dnsQueryResponse.Answers.ARecords())
128+
OnRecordReceived(new DNSLookupRecordArgs(record.DomainName, record.TimeToLive, record.RecordClass, record.RecordType, record.Address.ToString(), dnsServer));
212129

213130
// AAAA
214-
foreach (var r in dnsResponse.RecordsAAAA)
215-
OnRecordReceived(new DNSLookupRecordArgs(r.RR, r, dnsServer, port));
131+
foreach (var record in dnsQueryResponse.Answers.AaaaRecords())
132+
OnRecordReceived(new DNSLookupRecordArgs(record.DomainName, record.TimeToLive, record.RecordClass, record.RecordType, record.Address.ToString(), dnsServer));
216133

217134
// CNAME
218-
foreach (var r in dnsResponse.RecordsCNAME)
219-
OnRecordReceived(new DNSLookupRecordArgs(r.RR, r, dnsServer, port));
135+
foreach (var record in dnsQueryResponse.Answers.CnameRecords())
136+
OnRecordReceived(new DNSLookupRecordArgs(record.DomainName, record.TimeToLive, record.RecordClass, record.RecordType, record.CanonicalName, dnsServer));
220137

221138
// MX
222-
foreach (var r in dnsResponse.RecordsMX)
223-
OnRecordReceived(new DNSLookupRecordArgs(r.RR, r, dnsServer, port));
139+
foreach (var record in dnsQueryResponse.Answers.MxRecords())
140+
OnRecordReceived(new DNSLookupRecordArgs(record.DomainName, record.TimeToLive, record.RecordClass, record.RecordType, record.Exchange, dnsServer));
224141

225142
// NS
226-
foreach (var r in dnsResponse.RecordsNS)
227-
OnRecordReceived(new DNSLookupRecordArgs(r.RR, r, dnsServer, port));
143+
foreach (var record in dnsQueryResponse.Answers.NsRecords())
144+
OnRecordReceived(new DNSLookupRecordArgs(record.DomainName, record.TimeToLive, record.RecordClass, record.RecordType, record.NSDName, dnsServer));
228145

229146
// PTR
230-
foreach (var r in dnsResponse.RecordsPTR)
231-
OnRecordReceived(new DNSLookupRecordArgs(r.RR, r, dnsServer, port));
147+
foreach (var record in dnsQueryResponse.Answers.PtrRecords())
148+
OnRecordReceived(new DNSLookupRecordArgs(record.DomainName, record.TimeToLive, record.RecordClass, record.RecordType, record.PtrDomainName, dnsServer));
232149

233150
// SOA
234-
foreach (var r in dnsResponse.RecordsSOA)
235-
OnRecordReceived(new DNSLookupRecordArgs(r.RR, r, dnsServer, port));
151+
foreach (var record in dnsQueryResponse.Answers.SoaRecords())
152+
OnRecordReceived(new DNSLookupRecordArgs(record.DomainName, record.TimeToLive, record.RecordClass, record.RecordType, record.MName + " " + record.RName, dnsServer));
236153

237154
// TXT
238-
foreach (var r in dnsResponse.RecordsTXT)
239-
OnRecordReceived(new DNSLookupRecordArgs(r.RR, r, dnsServer, port));
155+
foreach (var record in dnsQueryResponse.Answers.TxtRecords())
156+
OnRecordReceived(new DNSLookupRecordArgs(record.DomainName, record.TimeToLive, record.RecordClass, record.RecordType, record.Text.ToString(), dnsServer));
157+
158+
// ToDo: implement more
159+
240160
}
241161
#endregion
242162
}

Source/NETworkManager/Models/Network/DNSLookupErrorArgs.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
namespace NETworkManager.Models.Network
1+
using System.Net;
2+
3+
namespace NETworkManager.Models.Network
24
{
35
public class DNSLookupErrorArgs : System.EventArgs
46
{
57
public string ErrorCode { get; set; }
6-
public string DNSServer { get; set; }
8+
public IPEndPoint DNSServer { get; set; }
79

810
public DNSLookupErrorArgs()
911
{
1012

1113
}
1214

13-
public DNSLookupErrorArgs(string errorCode, string dnsServer)
15+
public DNSLookupErrorArgs(string errorCode, IPEndPoint dnsServer)
1416
{
1517
ErrorCode = errorCode;
1618
DNSServer = dnsServer;
Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
1-
using Heijden.DNS;
1+
using DnsClient;
2+
using DnsClient.Protocol;
3+
using System.Net;
24

35
namespace NETworkManager.Models.Network
46
{
57
public class DNSLookupRecordArgs : System.EventArgs
68
{
7-
public RR ResourceRecord { get; set; }
9+
public string DomainName { get; set; }
10+
public int TTL { get; set; }
11+
public QueryClass Class { get; set; }
12+
public ResourceRecordType Type { get; set; }
813
public string Result { get; set; }
9-
public string DNSServer { get; set; }
10-
public int Port { get; set; }
14+
public IPEndPoint DNSServer { get; set; }
1115

1216
public DNSLookupRecordArgs()
1317
{
1418

1519
}
1620

17-
public DNSLookupRecordArgs(RR resourceRecord, object result, string dnsServer, int port)
21+
public DNSLookupRecordArgs(string domainName, int ttl, QueryClass queryClass, ResourceRecordType queryType, string result, IPEndPoint dnsServer)
1822
{
19-
ResourceRecord = resourceRecord;
20-
Result = result.ToString().TrimEnd();
23+
DomainName = domainName;
24+
TTL = ttl;
25+
Class = queryClass;
26+
Type = queryType;
27+
Result = result;
2128
DNSServer = dnsServer;
22-
Port = port;
2329
}
2430
}
2531
}

0 commit comments

Comments
 (0)