From 20dfb6aa3cc37bc657a45c88437c721dd052deb6 Mon Sep 17 00:00:00 2001 From: Shreyas Zare Date: Sat, 14 Aug 2021 12:04:53 +0530 Subject: [PATCH] MainApplicationContext: added `--first-run` command line option to allow setting network dns to work with the local dns server after setup completes. --- .../MainApplicationContext.cs | 59 +++++++++++-------- 1 file changed, 34 insertions(+), 25 deletions(-) diff --git a/DnsServerSystemTrayApp/MainApplicationContext.cs b/DnsServerSystemTrayApp/MainApplicationContext.cs index 7092f136..2bb8c43b 100644 --- a/DnsServerSystemTrayApp/MainApplicationContext.cs +++ b/DnsServerSystemTrayApp/MainApplicationContext.cs @@ -103,6 +103,10 @@ namespace DnsServerSystemTrayApp case "--service-stop": StopServiceMenuItem_Click(this, EventArgs.Empty); break; + + case "--first-run": + SetNetworkDns(new DnsProvider("Technitium", new IPAddress[] { IPAddress.Loopback, IPAddress.IPv6Loopback })); + break; } } } @@ -290,6 +294,35 @@ namespace DnsServerSystemTrayApp } } + private void SetNetworkDns(DnsProvider dnsProvider) + { + if (!Program.IsAdmin) + { + Program.RunAsAdmin("--network-dns-item " + dnsProvider.Name); + return; + } + + try + { + foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces()) + { + if (nic.OperationalStatus != OperationalStatus.Up) + continue; + + IPInterfaceProperties properties = nic.GetIPProperties(); + + if ((properties.DnsAddresses.Count > 0) && !properties.DnsAddresses[0].IsIPv6SiteLocal) + SetNameServer(nic, dnsProvider.Addresses); + } + + MessageBox.Show("The network DNS servers were set to " + dnsProvider.Name + " successfully.", dnsProvider.Name + " Configured - " + Resources.ServiceName, MessageBoxButtons.OK, MessageBoxIcon.Information); + } + catch (Exception ex) + { + MessageBox.Show("Error occured while setting " + dnsProvider.Name + " as network DNS server. " + ex.Message, "Error - " + Resources.ServiceName, MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + private static void SetNameServer(NetworkInterface nic, ICollection dnsAddresses) { SetNameServerIPv4(nic, dnsAddresses); @@ -627,31 +660,7 @@ namespace DnsServerSystemTrayApp ToolStripMenuItem item = sender as ToolStripMenuItem; DnsProvider dnsProvider = item.Tag as DnsProvider; - if (!Program.IsAdmin) - { - Program.RunAsAdmin("--network-dns-item " + dnsProvider.Name); - return; - } - - try - { - foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces()) - { - if (nic.OperationalStatus != OperationalStatus.Up) - continue; - - IPInterfaceProperties properties = nic.GetIPProperties(); - - if ((properties.DnsAddresses.Count > 0) && !properties.DnsAddresses[0].IsIPv6SiteLocal) - SetNameServer(nic, dnsProvider.Addresses); - } - - MessageBox.Show("The network DNS servers were set to " + dnsProvider.Name + " successfully.", dnsProvider.Name + " Configured - " + Resources.ServiceName, MessageBoxButtons.OK, MessageBoxIcon.Information); - } - catch (Exception ex) - { - MessageBox.Show("Error occured while setting " + dnsProvider.Name + " as network DNS server. " + ex.Message, "Error - " + Resources.ServiceName, MessageBoxButtons.OK, MessageBoxIcon.Error); - } + SetNetworkDns(dnsProvider); } private void StartServiceMenuItem_Click(object sender, EventArgs e)