Program.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. Technitium DNS Server
  3. Copyright (C) 2021 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 Microsoft.Extensions.DependencyInjection;
  16. using Microsoft.Extensions.Hosting;
  17. using System;
  18. using System.Reflection;
  19. using TechnitiumLibrary.Net.Firewall;
  20. namespace DnsServerWindowsService
  21. {
  22. static class Program
  23. {
  24. public static void Main(string[] args)
  25. {
  26. #region check windows firewall entry
  27. string appPath = Assembly.GetEntryAssembly().Location;
  28. if (appPath.EndsWith(".dll", StringComparison.OrdinalIgnoreCase))
  29. appPath = appPath.Substring(0, appPath.Length - 4) + ".exe";
  30. if (!WindowsFirewallEntryExists(appPath))
  31. AddWindowsFirewallEntry(appPath);
  32. #endregion
  33. CreateHostBuilder(args).Build().Run();
  34. }
  35. public static IHostBuilder CreateHostBuilder(string[] args)
  36. {
  37. return Host.CreateDefaultBuilder(args)
  38. .ConfigureServices((hostContext, services) =>
  39. {
  40. services.AddHostedService<DnsServiceWorker>();
  41. })
  42. .UseWindowsService();
  43. }
  44. #region private
  45. private static bool WindowsFirewallEntryExists(string appPath)
  46. {
  47. switch (Environment.OSVersion.Platform)
  48. {
  49. case PlatformID.Win32NT:
  50. if (Environment.OSVersion.Version.Major > 5)
  51. {
  52. //vista and above
  53. try
  54. {
  55. return WindowsFirewall.RuleExistsVista("", appPath) == RuleStatus.Allowed;
  56. }
  57. catch
  58. {
  59. return false;
  60. }
  61. }
  62. else
  63. {
  64. try
  65. {
  66. return WindowsFirewall.ApplicationExists(appPath) == RuleStatus.Allowed;
  67. }
  68. catch
  69. {
  70. return false;
  71. }
  72. }
  73. default:
  74. return false;
  75. }
  76. }
  77. private static bool AddWindowsFirewallEntry(string appPath)
  78. {
  79. switch (Environment.OSVersion.Platform)
  80. {
  81. case PlatformID.Win32NT:
  82. if (Environment.OSVersion.Version.Major > 5)
  83. {
  84. //vista and above
  85. try
  86. {
  87. RuleStatus status = WindowsFirewall.RuleExistsVista("", appPath);
  88. switch (status)
  89. {
  90. case RuleStatus.Blocked:
  91. case RuleStatus.Disabled:
  92. WindowsFirewall.RemoveRuleVista("", appPath);
  93. break;
  94. case RuleStatus.Allowed:
  95. return true;
  96. }
  97. WindowsFirewall.AddRuleVista("Technitium DNS Server", "Allow incoming connection request to the DNS server.", FirewallAction.Allow, appPath, Protocol.ANY, null, null, null, null, InterfaceTypeFlags.All, true, Direction.Inbound, true);
  98. return true;
  99. }
  100. catch
  101. { }
  102. }
  103. else
  104. {
  105. try
  106. {
  107. RuleStatus status = WindowsFirewall.ApplicationExists(appPath);
  108. switch (status)
  109. {
  110. case RuleStatus.Disabled:
  111. WindowsFirewall.RemoveApplication(appPath);
  112. break;
  113. case RuleStatus.Allowed:
  114. return true;
  115. }
  116. WindowsFirewall.AddApplication("Technitium DNS Server", appPath);
  117. return true;
  118. }
  119. catch
  120. { }
  121. }
  122. break;
  123. }
  124. return false;
  125. }
  126. #endregion
  127. }
  128. }