Program.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 DnsServerCore;
  16. using System;
  17. using System.Diagnostics;
  18. using System.Reflection;
  19. using System.Security.Principal;
  20. using System.Threading;
  21. using TechnitiumLibrary.Net.Firewall;
  22. namespace DnsServerApp
  23. {
  24. class Program
  25. {
  26. static void Main(string[] args)
  27. {
  28. #region check windows firewall entry
  29. string appPath = Assembly.GetEntryAssembly().Location;
  30. if (!WindowsFirewallEntryExists(appPath))
  31. {
  32. bool isAdmin = new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator);
  33. if (isAdmin)
  34. {
  35. AddWindowsFirewallEntry(appPath);
  36. }
  37. else
  38. {
  39. ProcessStartInfo processInfo = new ProcessStartInfo(appPath, string.Join(" ", args));
  40. processInfo.UseShellExecute = true;
  41. processInfo.Verb = "runas";
  42. try
  43. {
  44. Process.Start(processInfo);
  45. return;
  46. }
  47. catch
  48. { }
  49. }
  50. }
  51. #endregion
  52. string configFolder = null;
  53. if (args.Length == 1)
  54. configFolder = args[0];
  55. EventWaitHandle waitHandle = new ManualResetEvent(false);
  56. EventWaitHandle exitHandle = new ManualResetEvent(false);
  57. WebService service = null;
  58. try
  59. {
  60. service = new WebService(configFolder, new Uri("https://go.technitium.com/?id=20"));
  61. service.Start();
  62. Console.CancelKeyPress += delegate (object sender, ConsoleCancelEventArgs e)
  63. {
  64. e.Cancel = true;
  65. waitHandle.Set();
  66. };
  67. AppDomain.CurrentDomain.ProcessExit += delegate (object sender, EventArgs e)
  68. {
  69. waitHandle.Set();
  70. exitHandle.WaitOne();
  71. };
  72. Console.WriteLine("Technitium DNS Server was started successfully.");
  73. Console.WriteLine("Using config folder: " + service.ConfigFolder);
  74. Console.WriteLine("");
  75. Console.WriteLine("Note: Open http://" + service.WebServiceHostname + ":" + service.WebServicePort + "/ in web browser to access web console.");
  76. Console.WriteLine("");
  77. Console.WriteLine("Press [CTRL + C] to stop...");
  78. waitHandle.WaitOne();
  79. }
  80. catch (Exception ex)
  81. {
  82. Console.WriteLine(ex.ToString());
  83. }
  84. finally
  85. {
  86. Console.WriteLine("");
  87. Console.WriteLine("Technitium DNS Server is stopping...");
  88. if (service != null)
  89. service.Dispose();
  90. Console.WriteLine("Technitium DNS Server was stopped successfully.");
  91. exitHandle.Set();
  92. }
  93. }
  94. #region private
  95. private static bool WindowsFirewallEntryExists(string appPath)
  96. {
  97. switch (Environment.OSVersion.Platform)
  98. {
  99. case PlatformID.Win32NT:
  100. if (Environment.OSVersion.Version.Major > 5)
  101. {
  102. //vista and above
  103. try
  104. {
  105. return WindowsFirewall.RuleExistsVista("", appPath) == RuleStatus.Allowed;
  106. }
  107. catch
  108. {
  109. return false;
  110. }
  111. }
  112. else
  113. {
  114. try
  115. {
  116. return WindowsFirewall.ApplicationExists(appPath) == RuleStatus.Allowed;
  117. }
  118. catch
  119. {
  120. return false;
  121. }
  122. }
  123. default:
  124. return false;
  125. }
  126. }
  127. private static bool AddWindowsFirewallEntry(string appPath)
  128. {
  129. switch (Environment.OSVersion.Platform)
  130. {
  131. case PlatformID.Win32NT:
  132. if (Environment.OSVersion.Version.Major > 5)
  133. {
  134. //vista and above
  135. try
  136. {
  137. RuleStatus status = WindowsFirewall.RuleExistsVista("", appPath);
  138. switch (status)
  139. {
  140. case RuleStatus.Blocked:
  141. case RuleStatus.Disabled:
  142. WindowsFirewall.RemoveRuleVista("", appPath);
  143. break;
  144. case RuleStatus.Allowed:
  145. return true;
  146. }
  147. 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);
  148. return true;
  149. }
  150. catch
  151. { }
  152. }
  153. else
  154. {
  155. try
  156. {
  157. RuleStatus status = WindowsFirewall.ApplicationExists(appPath);
  158. switch (status)
  159. {
  160. case RuleStatus.Disabled:
  161. WindowsFirewall.RemoveApplication(appPath);
  162. break;
  163. case RuleStatus.Allowed:
  164. return true;
  165. }
  166. WindowsFirewall.AddApplication("Technitium DNS Server", appPath);
  167. return true;
  168. }
  169. catch
  170. { }
  171. }
  172. break;
  173. }
  174. return false;
  175. }
  176. #endregion
  177. }
  178. }