DnsServerWindowsService: implemented support for auto firewall entry option.

This commit is contained in:
Shreyas Zare
2024-05-19 15:18:19 +05:30
parent 3c82530ddf
commit e73823cb82

View File

@@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
using DnsServerCore;
using Microsoft.Extensions.Hosting;
using Microsoft.Win32;
using System;
using System.Reflection;
using System.Threading;
@@ -66,6 +67,25 @@ namespace DnsServerWindowsService
}
private static void CheckFirewallEntries()
{
bool autoFirewallEntry = true;
try
{
#pragma warning disable CA1416 // Validate platform compatibility
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Technitium\DNS Server", false))
{
if (key is not null)
autoFirewallEntry = Convert.ToInt32(key.GetValue("AutoFirewallEntry", 1)) == 1;
}
#pragma warning restore CA1416 // Validate platform compatibility
}
catch
{ }
if (autoFirewallEntry)
{
string appPath = Assembly.GetEntryAssembly().Location;
@@ -75,6 +95,7 @@ namespace DnsServerWindowsService
if (!WindowsFirewallEntryExists(appPath))
AddWindowsFirewallEntry(appPath);
}
}
private static bool WindowsFirewallEntryExists(string appPath)
{