DnsServiceWorker.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. Technitium DNS Server
  3. Copyright (C) 2024 Shreyas Zare (shreyas@technitium.com)
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. using DnsServerCore;
  16. using Microsoft.Extensions.Hosting;
  17. using Microsoft.Win32;
  18. using System;
  19. using System.Reflection;
  20. using System.Threading;
  21. using System.Threading.Tasks;
  22. using TechnitiumLibrary.Net.Firewall;
  23. namespace DnsServerWindowsService
  24. {
  25. public sealed class DnsServiceWorker : BackgroundService
  26. {
  27. readonly DnsWebService _service;
  28. public DnsServiceWorker()
  29. {
  30. string configFolder = null;
  31. string[] args = Environment.GetCommandLineArgs();
  32. if (args.Length == 2)
  33. configFolder = args[1];
  34. _service = new DnsWebService(configFolder, new Uri("https://go.technitium.com/?id=43"), new Uri("https://go.technitium.com/?id=44"));
  35. }
  36. public override async Task StartAsync(CancellationToken cancellationToken)
  37. {
  38. CheckFirewallEntries();
  39. await _service.StartAsync();
  40. }
  41. public override async Task StopAsync(CancellationToken cancellationToken)
  42. {
  43. await _service.StopAsync();
  44. }
  45. public override void Dispose()
  46. {
  47. if (_service != null)
  48. _service.Dispose();
  49. }
  50. protected override Task ExecuteAsync(CancellationToken stoppingToken)
  51. {
  52. return Task.CompletedTask;
  53. }
  54. private static void CheckFirewallEntries()
  55. {
  56. bool autoFirewallEntry = true;
  57. try
  58. {
  59. #pragma warning disable CA1416 // Validate platform compatibility
  60. using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Technitium\DNS Server", false))
  61. {
  62. if (key is not null)
  63. autoFirewallEntry = Convert.ToInt32(key.GetValue("AutoFirewallEntry", 1)) == 1;
  64. }
  65. #pragma warning restore CA1416 // Validate platform compatibility
  66. }
  67. catch
  68. { }
  69. if (autoFirewallEntry)
  70. {
  71. string appPath = Assembly.GetEntryAssembly().Location;
  72. if (appPath.EndsWith(".dll", StringComparison.OrdinalIgnoreCase))
  73. appPath = appPath.Substring(0, appPath.Length - 4) + ".exe";
  74. if (!WindowsFirewallEntryExists(appPath))
  75. AddWindowsFirewallEntry(appPath);
  76. }
  77. }
  78. private static bool WindowsFirewallEntryExists(string appPath)
  79. {
  80. try
  81. {
  82. return WindowsFirewall.RuleExistsVista("", appPath) == RuleStatus.Allowed;
  83. }
  84. catch
  85. {
  86. return false;
  87. }
  88. }
  89. private static bool AddWindowsFirewallEntry(string appPath)
  90. {
  91. try
  92. {
  93. RuleStatus status = WindowsFirewall.RuleExistsVista("", appPath);
  94. switch (status)
  95. {
  96. case RuleStatus.Blocked:
  97. case RuleStatus.Disabled:
  98. WindowsFirewall.RemoveRuleVista("", appPath);
  99. break;
  100. case RuleStatus.Allowed:
  101. return true;
  102. }
  103. WindowsFirewall.AddRuleVista("Technitium DNS Server", "Allows incoming connection request to the DNS server.", FirewallAction.Allow, appPath, Protocol.ANY, null, null, null, null, InterfaceTypeFlags.All, true, Direction.Inbound, true);
  104. return true;
  105. }
  106. catch
  107. {
  108. return false;
  109. }
  110. }
  111. }
  112. }