diff --git a/DnsServerSystemTrayApp/MainApplicationContext.cs b/DnsServerSystemTrayApp/MainApplicationContext.cs index 2bb8c43b..8fb6be98 100644 --- a/DnsServerSystemTrayApp/MainApplicationContext.cs +++ b/DnsServerSystemTrayApp/MainApplicationContext.cs @@ -1,6 +1,6 @@ /* Technitium DNS Server -Copyright (C) 2021 Shreyas Zare (shreyas@technitium.com) +Copyright (C) 2022 Shreyas Zare (shreyas@technitium.com) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -105,7 +105,34 @@ namespace DnsServerSystemTrayApp break; case "--first-run": - SetNetworkDns(new DnsProvider("Technitium", new IPAddress[] { IPAddress.Loopback, IPAddress.IPv6Loopback })); + bool usingLoopbackAsDns = false; + + try + { + foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces()) + { + if (nic.OperationalStatus != OperationalStatus.Up) + continue; + + foreach (IPAddress dnsAddress in nic.GetIPProperties().DnsAddresses) + { + if (IPAddress.IsLoopback(dnsAddress)) + { + usingLoopbackAsDns = true; + break; + } + } + + if (usingLoopbackAsDns) + break; + } + } + catch + { } + + if (!usingLoopbackAsDns && MessageBox.Show("Do you want to update this computer's network connections to use the locally running Technitium DNS Server?\r\n\r\nNote! It is recommended that you use the locally running Technitium DNS Server unless you explicitly want to keep using your existing network DNS configuration.", "Switch Network DNS?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + SetNetworkDns(new DnsProvider("Technitium", new IPAddress[] { IPAddress.Loopback, IPAddress.IPv6Loopback })); + break; } } @@ -294,7 +321,7 @@ namespace DnsServerSystemTrayApp } } - private void SetNetworkDns(DnsProvider dnsProvider) + private static void SetNetworkDns(DnsProvider dnsProvider) { if (!Program.IsAdmin) { @@ -446,7 +473,6 @@ namespace DnsServerSystemTrayApp catch { } - NetworkDnsMenuItem.DropDownItems.Clear(); NetworkDnsMenuItem.DropDownItems.Add(DefaultNetworkDnsMenuItem); NetworkDnsMenuItem.DropDownItems.Add(new ToolStripSeparator());