DnsServiceWorker.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. Technitium DNS Server
  3. Copyright (C) 2023 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 System;
  18. using System.Reflection;
  19. using System.Threading;
  20. using System.Threading.Tasks;
  21. using TechnitiumLibrary.Net.Firewall;
  22. namespace DnsServerWindowsService
  23. {
  24. public class DnsServiceWorker : BackgroundService
  25. {
  26. readonly DnsWebService _service;
  27. public DnsServiceWorker()
  28. {
  29. string configFolder = null;
  30. string[] args = Environment.GetCommandLineArgs();
  31. if (args.Length == 2)
  32. configFolder = args[1];
  33. _service = new DnsWebService(configFolder, new Uri("https://go.technitium.com/?id=43"), new Uri("https://go.technitium.com/?id=44"));
  34. }
  35. public override async Task StartAsync(CancellationToken cancellationToken)
  36. {
  37. CheckFirewallEntries();
  38. await _service.StartAsync();
  39. }
  40. public override async Task StopAsync(CancellationToken cancellationToken)
  41. {
  42. await _service.StopAsync();
  43. }
  44. public override void Dispose()
  45. {
  46. if (_service != null)
  47. _service.Dispose();
  48. }
  49. protected override Task ExecuteAsync(CancellationToken stoppingToken)
  50. {
  51. return Task.CompletedTask;
  52. }
  53. private void CheckFirewallEntries()
  54. {
  55. string appPath = Assembly.GetEntryAssembly().Location;
  56. if (appPath.EndsWith(".dll", StringComparison.OrdinalIgnoreCase))
  57. appPath = appPath.Substring(0, appPath.Length - 4) + ".exe";
  58. if (!WindowsFirewallEntryExists(appPath))
  59. AddWindowsFirewallEntry(appPath);
  60. }
  61. private bool WindowsFirewallEntryExists(string appPath)
  62. {
  63. try
  64. {
  65. return WindowsFirewall.RuleExistsVista("", appPath) == RuleStatus.Allowed;
  66. }
  67. catch
  68. {
  69. return false;
  70. }
  71. }
  72. private bool AddWindowsFirewallEntry(string appPath)
  73. {
  74. try
  75. {
  76. RuleStatus status = WindowsFirewall.RuleExistsVista("", appPath);
  77. switch (status)
  78. {
  79. case RuleStatus.Blocked:
  80. case RuleStatus.Disabled:
  81. WindowsFirewall.RemoveRuleVista("", appPath);
  82. break;
  83. case RuleStatus.Allowed:
  84. return true;
  85. }
  86. 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);
  87. return true;
  88. }
  89. catch
  90. {
  91. return false;
  92. }
  93. }
  94. }
  95. }