Skip to content

Commit 5282dc3

Browse files
committed
Feature: DNS server
1 parent 0f62d52 commit 5282dc3

5 files changed

Lines changed: 52 additions & 10 deletions

File tree

‎Source/NETworkManager.Models/Network/DNSLookup.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
using DnsClient;
22
using DnsClient.Protocol;
3+
using NETworkManager.Utilities;
34
using System;
45
using System.Collections.Generic;
56
using System.Linq;
67
using System.Net;
7-
using System.Net.NetworkInformation;
88
using System.Threading.Tasks;
99

1010
namespace NETworkManager.Models.Network;
@@ -24,7 +24,7 @@ public DNSLookup(DNSLookupSettings settings, IEnumerable<ServerConnectionInfo> d
2424
{
2525
_suffix = _settings.UseCustomDNSSuffix
2626
? _settings.CustomDNSSuffix
27-
: IPGlobalProperties.GetIPGlobalProperties().DomainName;
27+
: DNSClientHelper.DetectDNSSuffix();
2828

2929
_addSuffix = !string.IsNullOrEmpty(_suffix);
3030
}

‎Source/NETworkManager.Utilities/DNSClient.cs‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,9 @@ public class DNSClient : SingletonBase<DNSClient>
4343
/// <param name="settings"></param>
4444
public void Configure(DNSClientSettings settings)
4545
{
46-
_settings = settings;
47-
48-
_addSuffix = _settings.AddDNSSuffix && !string.IsNullOrEmpty(_settings.DNSSuffix);
49-
5046
Log.Debug("Configure - Configuring DNS client...");
47+
48+
_settings = settings;
5149

5250
if (_settings.UseCustomDNSServers)
5351
{
@@ -63,14 +61,19 @@ public void Configure(DNSClientSettings settings)
6361
}
6462

6563
Log.Debug("Configure - Creating LookupClient with custom DNS servers...");
66-
_client = new LookupClient(new LookupClientOptions(servers.ToArray()));
64+
_client = new LookupClient(new LookupClientOptions([.. servers]));
6765
}
6866
else
6967
{
7068
Log.Debug("Configure - Creating LookupClient with Windows default DNS servers...");
7169
_client = new LookupClient();
7270
}
7371

72+
_addSuffix = _settings.AddDNSSuffix && !string.IsNullOrEmpty(_settings.DNSSuffix);
73+
Log.Debug(_addSuffix
74+
? $"Configure - DNS suffix will be added to hostnames without a dot: {_settings.DNSSuffix}"
75+
: "Configure - DNS suffix will NOT be added to hostnames without a dot.");
76+
7477
Log.Debug("Configure - DNS client configured.");
7578
_isConfigured = true;
7679
}

‎Source/NETworkManager.Utilities/DNSClientHelper.cs‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,48 @@
1-
using System.Threading.Tasks;
1+
using System.Linq;
2+
using System.Net.NetworkInformation;
3+
using System.Threading.Tasks;
24

35
namespace NETworkManager.Utilities;
46

57
public static class DNSClientHelper
68
{
9+
/// <summary>
10+
/// Detect the DNS suffix to use for suffix-appending. Prefers the Windows "Primary DNS Suffix"
11+
/// (<see cref="IPGlobalProperties.DomainName" />), falling back to the connection-specific suffix
12+
/// of an active network adapter if no primary suffix is configured (e.g. machine is not domain-joined
13+
/// or the primary suffix was cleared manually), which mirrors what shows up under "DNS Suffix Search
14+
/// List" in <c>ipconfig /all</c> in that case.
15+
/// </summary>
16+
public static string DetectDNSSuffix()
17+
{
18+
var suffix = IPGlobalProperties.GetIPGlobalProperties().DomainName;
19+
20+
if (!string.IsNullOrWhiteSpace(suffix))
21+
return suffix;
22+
23+
// Rank candidates so the adapter most likely to be "the" active connection is tried first:
24+
// routed (has a gateway) > wired > wireless > other > faster link speed.
25+
var prioritizedAdapters = NetworkInterface.GetAllNetworkInterfaces()
26+
.Where(nic => nic.OperationalStatus == OperationalStatus.Up &&
27+
nic.NetworkInterfaceType != NetworkInterfaceType.Loopback &&
28+
nic.NetworkInterfaceType != NetworkInterfaceType.Tunnel)
29+
.OrderByDescending(nic => nic.GetIPProperties().GatewayAddresses.Count > 0)
30+
.ThenByDescending(nic => GetInterfaceTypePriority(nic.NetworkInterfaceType))
31+
.ThenByDescending(nic => nic.Speed);
32+
33+
return prioritizedAdapters
34+
.Select(nic => nic.GetIPProperties().DnsSuffix)
35+
.FirstOrDefault(s => !string.IsNullOrWhiteSpace(s))
36+
?? string.Empty;
37+
}
38+
39+
private static int GetInterfaceTypePriority(NetworkInterfaceType type) => type switch
40+
{
41+
NetworkInterfaceType.Ethernet => 2,
42+
NetworkInterfaceType.Wireless80211 => 1,
43+
_ => 0
44+
};
45+
746
public static async Task<DNSClientResultIPAddress> ResolveAorAaaaAsync(string query, bool preferIPv4)
847
{
948
DNSClientResultIPAddress firstResult = null;

‎Source/NETworkManager/MainWindow.xaml.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1922,7 +1922,7 @@ .. SettingsManager.Current.Network_CustomDNSServers
19221922
dnsSettings.AddDNSSuffix = true;
19231923
dnsSettings.DNSSuffix = SettingsManager.Current.Network_UseCustomDNSSuffix
19241924
? SettingsManager.Current.Network_CustomDNSSuffix?.TrimStart('.')
1925-
: IPGlobalProperties.GetIPGlobalProperties().DomainName;
1925+
: DNSClientHelper.DetectDNSSuffix();
19261926
}
19271927

19281928
DNSClient.GetInstance().Configure(dnsSettings);

‎Website/docs/application/dns-lookup.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ You can also use the Hotkeys `F2` (`edit`) or `Del` (`delete`) on a selected DNS
119119

120120
### Add DNS suffix (primary) to hostname
121121

122-
Add the primary DNS suffix to the hostname.
122+
Add the primary DNS suffix to the hostname. If no primary DNS suffix is configured (e.g. the computer is not domain-joined), the connection-specific DNS suffix of the active network adapter is used instead.
123123

124124
**Type:** `Boolean`
125125

0 commit comments

Comments
 (0)