Program.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. Technitium DNS Server
  3. Copyright (C) 2020 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 System;
  16. using System.Reflection;
  17. using System.ServiceProcess;
  18. using TechnitiumLibrary.Net.Firewall;
  19. namespace DnsService
  20. {
  21. static class Program
  22. {
  23. /// <summary>
  24. /// The main entry point for the application.
  25. /// </summary>
  26. static void Main()
  27. {
  28. #region check windows firewall entry
  29. string appPath = Assembly.GetEntryAssembly().Location;
  30. if (!WindowsFirewallEntryExists(appPath))
  31. AddWindowsFirewallEntry(appPath);
  32. #endregion
  33. ServiceBase[] ServicesToRun;
  34. ServicesToRun = new ServiceBase[]
  35. {
  36. new DnsService()
  37. };
  38. ServiceBase.Run(ServicesToRun);
  39. }
  40. #region private
  41. private static bool WindowsFirewallEntryExists(string appPath)
  42. {
  43. switch (Environment.OSVersion.Platform)
  44. {
  45. case PlatformID.Win32NT:
  46. if (Environment.OSVersion.Version.Major > 5)
  47. {
  48. //vista and above
  49. try
  50. {
  51. return WindowsFirewall.RuleExistsVista("", appPath) == RuleStatus.Allowed;
  52. }
  53. catch
  54. {
  55. return false;
  56. }
  57. }
  58. else
  59. {
  60. try
  61. {
  62. return WindowsFirewall.ApplicationExists(appPath) == RuleStatus.Allowed;
  63. }
  64. catch
  65. {
  66. return false;
  67. }
  68. }
  69. default:
  70. return false;
  71. }
  72. }
  73. private static bool AddWindowsFirewallEntry(string appPath)
  74. {
  75. switch (Environment.OSVersion.Platform)
  76. {
  77. case PlatformID.Win32NT:
  78. if (Environment.OSVersion.Version.Major > 5)
  79. {
  80. //vista and above
  81. try
  82. {
  83. RuleStatus status = WindowsFirewall.RuleExistsVista("", appPath);
  84. switch (status)
  85. {
  86. case RuleStatus.Blocked:
  87. case RuleStatus.Disabled:
  88. WindowsFirewall.RemoveRuleVista("", appPath);
  89. break;
  90. case RuleStatus.Allowed:
  91. return true;
  92. }
  93. 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);
  94. return true;
  95. }
  96. catch
  97. { }
  98. }
  99. else
  100. {
  101. try
  102. {
  103. RuleStatus status = WindowsFirewall.ApplicationExists(appPath);
  104. switch (status)
  105. {
  106. case RuleStatus.Disabled:
  107. WindowsFirewall.RemoveApplication(appPath);
  108. break;
  109. case RuleStatus.Allowed:
  110. return true;
  111. }
  112. WindowsFirewall.AddApplication("Technitium DNS Server", appPath);
  113. return true;
  114. }
  115. catch
  116. { }
  117. }
  118. break;
  119. }
  120. return false;
  121. }
  122. #endregion
  123. }
  124. }