Skip to content

Commit 123440d

Browse files
committed
Enter multiple hosts and dns server
1 parent 0786ec1 commit 123440d

25 files changed

Lines changed: 206 additions & 129 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/Models/Network/DNSLookup.cs

Lines changed: 92 additions & 48 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,144 @@ 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)
44-
{
45-
if (IPAddress.TryParse(hostnameOrIPAddress, out IPAddress ip))
46-
hostnameOrIPAddress = Resolver.GetArpaFromIp(ip);
47-
}
45+
// DNS server list
46+
List<string> dnsServers = new List<string>();
4847

49-
if (DNSLookupOptions.Type == QType.NAPTR)
50-
hostnameOrIPAddress = Resolver.GetArpaFromEnum(hostnameOrIPAddress);
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));
5152

52-
if (DNSLookupOptions.UseCustomDNSServer)
53-
DNSResolver.DnsServer = DNSLookupOptions.CustomDNSServer;
53+
// Foreach host
54+
foreach (string host in hosts)
55+
{
56+
// Default
57+
string name = host.Trim();
5458

55-
DNSResolver.Recursion = DNSLookupOptions.Recursion;
56-
DNSResolver.TransportType = DNSLookupOptions.TransportType;
57-
DNSResolver.Retries = DNSLookupOptions.Attempts;
58-
DNSResolver.TimeOut = DNSLookupOptions.Timeout;
59+
string dnsSuffix = string.Empty;
5960

60-
Response dnsResponse = DNSResolver.Query(hostnameOrIPAddress, DNSLookupOptions.Type, DNSLookupOptions.Class);
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+
}
6171

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-
}
72+
// Append dns suffix to hostname
73+
if (!string.IsNullOrEmpty(dnsSuffix))
74+
name += string.Format(".{0}", dnsSuffix);
6875

69-
// Process the results...
70-
ProcessResponse(dnsResponse);
76+
// PTR
77+
if (dnsLookupOptions.Type == QType.PTR)
78+
{
79+
if (IPAddress.TryParse(name, out IPAddress ip))
80+
name = Resolver.GetArpaFromIp(ip);
81+
}
7182

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)
83+
// NAPTR
84+
if (dnsLookupOptions.Type == QType.NAPTR)
85+
name = Resolver.GetArpaFromEnum(name);
86+
87+
Parallel.ForEach(dnsServers, dnsServer =>
7688
{
77-
Response DNSResponse2 = DNSResolver.Query(r.CNAME, DNSLookupOptions.Type, DNSLookupOptions.Class);
89+
// Create a new for each request
90+
Resolver resolver = new Resolver(dnsServer)
91+
{
92+
Recursion = dnsLookupOptions.Recursion,
93+
TransportType = dnsLookupOptions.TransportType,
94+
Retries = dnsLookupOptions.Attempts,
95+
TimeOut = dnsLookupOptions.Timeout
96+
};
7897

79-
if (!string.IsNullOrEmpty(DNSResponse2.Error))
98+
Response dnsResponse = resolver.Query(name, dnsLookupOptions.Type, dnsLookupOptions.Class);
99+
100+
// If there was an error... return
101+
if (!string.IsNullOrEmpty(dnsResponse.Error))
80102
{
81-
OnLookupError(new DNSLookupErrorArgs(DNSResponse2.Error, DNSResolver.DnsServer));
82-
continue;
103+
// OnLookupError(new DNSLookupErrorArgs(dnsResponse.Error, resolver.DnsServer));
104+
// return;
83105
}
84106

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

93134
private void ProcessResponse(Response dnsResponse)
94135
{
136+
string dnsServer = dnsResponse.Server.Address.ToString();
137+
int port = dnsResponse.Server.Port;
138+
95139
// A
96140
foreach (RecordA r in dnsResponse.RecordsA)
97-
OnRecordReceived(new DNSLookupRecordArgs(r.RR, r));
141+
OnRecordReceived(new DNSLookupRecordArgs(r.RR, r, dnsServer, port));
98142

99143
// AAAA
100144
foreach (RecordAAAA r in dnsResponse.RecordsAAAA)
101-
OnRecordReceived(new DNSLookupRecordArgs(r.RR, r));
145+
OnRecordReceived(new DNSLookupRecordArgs(r.RR, r, dnsServer, port));
102146

103147
// CNAME
104148
foreach (RecordCNAME r in dnsResponse.RecordsCNAME)
105-
OnRecordReceived(new DNSLookupRecordArgs(r.RR, r));
149+
OnRecordReceived(new DNSLookupRecordArgs(r.RR, r, dnsServer, port));
106150

107151
// MX
108152
foreach (RecordMX r in dnsResponse.RecordsMX)
109-
OnRecordReceived(new DNSLookupRecordArgs(r.RR, r));
153+
OnRecordReceived(new DNSLookupRecordArgs(r.RR, r, dnsServer, port));
110154

111155
// NS
112156
foreach (RecordNS r in dnsResponse.RecordsNS)
113-
OnRecordReceived(new DNSLookupRecordArgs(r.RR, r));
157+
OnRecordReceived(new DNSLookupRecordArgs(r.RR, r, dnsServer, port));
114158

115159
// PTR
116160
foreach (RecordPTR r in dnsResponse.RecordsPTR)
117-
OnRecordReceived(new DNSLookupRecordArgs(r.RR, r));
161+
OnRecordReceived(new DNSLookupRecordArgs(r.RR, r, dnsServer, port));
118162

119163
// SOA
120164
foreach (RecordSOA r in dnsResponse.RecordsSOA)
121-
OnRecordReceived(new DNSLookupRecordArgs(r.RR, r));
165+
OnRecordReceived(new DNSLookupRecordArgs(r.RR, r, dnsServer, port));
122166

123167
// TXT
124168
foreach (RecordTXT r in dnsResponse.RecordsTXT)
125-
OnRecordReceived(new DNSLookupRecordArgs(r.RR, r));
169+
OnRecordReceived(new DNSLookupRecordArgs(r.RR, r, dnsServer, port));
126170
}
127171
#endregion
128172
}

Source/NETworkManager/Models/Network/DNSLookupOptions.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
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 bool AddDNSSuffix { get; set; }
11+
public bool UseCustomDNSSuffix { get; set; }
12+
public string CustomDNSSuffix { get; set; }
913
public QClass Class { get; set; }
1014
public QType Type { get; set; }
1115
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: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -903,16 +903,16 @@ public bool DNSLookup_UseCustomDNSServer
903903
}
904904
}
905905

906-
private string _dnsLookup_CustomDNSServer;
907-
public string DNSLookup_CustomDNSServer
906+
private List<string> _dnsLookup_CustomDNSServers;
907+
public List<string> DNSLookup_CustomDNSServers
908908
{
909-
get { return _dnsLookup_CustomDNSServer; }
909+
get { return _dnsLookup_CustomDNSServers; }
910910
set
911911
{
912-
if (value == _dnsLookup_CustomDNSServer)
912+
if (value == _dnsLookup_CustomDNSServers)
913913
return;
914914

915-
_dnsLookup_CustomDNSServer = value;
915+
_dnsLookup_CustomDNSServers = value;
916916
SettingsChanged = true;
917917
}
918918
}
@@ -973,7 +973,7 @@ public bool DNSLookup_UseCustomDNSSuffix
973973
}
974974
}
975975

976-
private string _dnsLookup_CustomDNSSuffix;
976+
private string _dnsLookup_CustomDNSSuffix = string.Empty;
977977
public string DNSLookup_CustomDNSSuffix
978978
{
979979
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" />

Source/NETworkManager/Resources/Localization/Resources.de-DE.xaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@
214214
<system:String x:Key="String_Service">Dienst</system:String>
215215
<system:String x:Key="String_ShowClosedPorts">Geschlossene Ports anzeigen</system:String>
216216
<system:String x:Key="String_HostnameOrIPAddress">Hostname oder IP-Adresse</system:String>
217-
<system:String x:Key="String_Ports">Ports</system:String>
217+
<system:String x:Key="String_Ports">Port(s)</system:String>
218218
<system:String x:Key="String_NumberOfStoredEntries">Anzahl gespeicherter Einträge</system:String>
219219
<system:String x:Key="String_NetworkLocationCannotBeReached">Die Netzwerkadresse kann nicht erreicht werden. Überprüfen Sie, ob Ihr Computer mit dem Netzwerk verbunden ist. Informationen zur Netzwerk-Fehlersuche finden Sie in der Windows-Hilfe.</system:String>
220220
<system:String x:Key="String_NumberOfErrorsAfterWhichIsCanceled">Anzahl Fehler nach denen abgebrochen wird:</system:String>
@@ -496,7 +496,8 @@
496496
<system:String x:Key="String_ValidateError_EnterValidWebsiteUri">Geben Sie eine gültige Webseite ein (z.B. https://example.com/index.html)</system:String>
497497
<system:String x:Key="String_ValidateError_EnterValidHosts">Geben Sie gültige Hosts ein (mehrere Hosts dürfen nicht mit ";" enden)!</system:String>
498498
<system:String x:Key="String_ValidateError_EnterValidNumber">Geben Sie eine gültige Zahl ein!</system:String>
499-
499+
<system:String x:Key="String_ValidateError_EnterOneOrMoreValidIPAddresses">Geben Sie eine oder mehrere gültige IP-Adressen ein!</system:String>
500+
500501
<!-- ApplicationInfo.Name -->
501502
<system:String x:Key="String_ApplicationName_IPScanner">IP-Scanner</system:String>
502503
<system:String x:Key="String_ApplicationName_NetworkInterface">Netzwerkinterface</system:String>

Source/NETworkManager/Resources/Localization/Resources.en-US.xaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@
214214
<system:String x:Key="String_Service">Service</system:String>
215215
<system:String x:Key="String_ShowClosedPorts">Show closed ports</system:String>
216216
<system:String x:Key="String_HostnameOrIPAddress">Hostname or IP address</system:String>
217-
<system:String x:Key="String_Ports">Ports</system:String>
217+
<system:String x:Key="String_Ports">Port(s)</system:String>
218218
<system:String x:Key="String_NumberOfStoredEntries">Number of stored entries</system:String>
219219
<system:String x:Key="String_NetworkLocationCannotBeReached">The network address cannot be reached. Check if your computer is connected to the network. For information about network troubleshooting, see Windows Help.</system:String>
220220
<system:String x:Key="String_NumberOfErrorsAfterWhichIsCanceled">Number of errors after which is canceled:</system:String>
@@ -496,6 +496,7 @@
496496
<system:String x:Key="String_ValidateError_EnterValidWebsiteUri">Enter a valid website (like https://example.com/index.html)</system:String>
497497
<system:String x:Key="String_ValidateError_EnterValidHosts">Enter valid hosts (multiple hosts can not end with ";")!</system:String>
498498
<system:String x:Key="String_ValidateError_EnterValidNumber">Enter a valid number!</system:String>
499+
<system:String x:Key="String_ValidateError_EnterOneOrMoreValidIPAddresses">Enter one or more valid IP addresses!</system:String>
499500

500501
<!-- ApplicationView.Name -->
501502
<system:String x:Key="String_ApplicationName_IPScanner">IP Scanner</system:String>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using NETworkManager.Helpers;
2+
using System.Globalization;
3+
using System.Text.RegularExpressions;
4+
using System.Windows;
5+
using System.Windows.Controls;
6+
7+
namespace NETworkManager.Validators
8+
{
9+
public class MultipleIPAddressesValidator : ValidationRule
10+
{
11+
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
12+
{
13+
foreach (string ipAddress in (value as string).Split(';'))
14+
{
15+
if (!Regex.IsMatch(ipAddress.Trim(), RegexHelper.IPv4AddressRegex) && !Regex.IsMatch(ipAddress.Trim(), RegexHelper.IPv6AddressRegex))
16+
return new ValidationResult(false, Application.Current.Resources["String_ValidateError_EnterOneOrMoreValidIPAddresses"] as string);
17+
}
18+
return ValidationResult.ValidResult;
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)