HostsHelper.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using System.Windows.Forms;
  9. namespace Optimizer
  10. {
  11. internal static class HostsHelper
  12. {
  13. internal static string NewLine = Environment.NewLine;
  14. internal static readonly string HostsFile = CleanHelper.System32Folder + "\\drivers\\etc\\hosts";
  15. //static string AdBlockBasicLink = "https://raw.githubusercontent.com/StevenBlack/hosts/master/alternates/fakenews-gambling/hosts";
  16. //static string AdBlockWithPornLink = "https://raw.githubusercontent.com/StevenBlack/hosts/master/alternates/fakenews-gambling-porn/hosts";
  17. //static string AdBlockWithSocialLink = "https://raw.githubusercontent.com/StevenBlack/hosts/master/alternates/fakenews-gambling-social/hosts";
  18. //static string AdBlockUltimateLink = "https://raw.githubusercontent.com/StevenBlack/hosts/master/alternates/fakenews-gambling-porn-social/hosts";
  19. static WebClient _client = new WebClient()
  20. {
  21. Encoding = Encoding.UTF8
  22. };
  23. internal static void RestoreDefaultHosts()
  24. {
  25. try
  26. {
  27. if (File.Exists(HostsFile))
  28. {
  29. File.Delete(HostsFile);
  30. }
  31. File.WriteAllBytes(HostsFile, Properties.Resources.hosts);
  32. }
  33. catch (Exception ex)
  34. {
  35. ErrorLogger.LogError("HostsHelper.RestoreDefaultHosts", ex.Message, ex.StackTrace);
  36. MessageBox.Show(Options.TranslationList("dnsCacheM").ToString(), "Optimizer", MessageBoxButtons.OK, MessageBoxIcon.Information);
  37. }
  38. }
  39. //internal static void AdblockBasic()
  40. //{
  41. // try
  42. // {
  43. // if (File.Exists(HostsFile))
  44. // {
  45. // File.Delete(HostsFile);
  46. // }
  47. // File.WriteAllText(HostsFile, _client.DownloadString(AdBlockBasicLink));
  48. // }
  49. // catch (Exception ex)
  50. // {
  51. // ErrorLogger.LogError("HostsHelper.AdblockBasic", ex.Message, ex.StackTrace);
  52. // MessageBox.Show(Options.TranslationList("dnsCacheM").ToString(), "DNS Cache is running", MessageBoxButtons.OK, MessageBoxIcon.Information);
  53. // }
  54. //}
  55. //internal static void AdBlockWithPorn()
  56. //{
  57. // try
  58. // {
  59. // if (File.Exists(HostsFile))
  60. // {
  61. // File.Delete(HostsFile);
  62. // }
  63. // File.WriteAllText(HostsFile, _client.DownloadString(AdBlockWithPornLink));
  64. // }
  65. // catch (Exception ex)
  66. // {
  67. // ErrorLogger.LogError("HostsHelper.AdBlockWithPorn", ex.Message, ex.StackTrace);
  68. // MessageBox.Show(Options.TranslationList("dnsCacheM").ToString(), "DNS Cache is running", MessageBoxButtons.OK, MessageBoxIcon.Information);
  69. // }
  70. //}
  71. //internal static void AdBlockWithSocial()
  72. //{
  73. // try
  74. // {
  75. // if (File.Exists(HostsFile))
  76. // {
  77. // File.Delete(HostsFile);
  78. // }
  79. // File.WriteAllText(HostsFile, _client.DownloadString(AdBlockWithSocialLink));
  80. // }
  81. // catch (Exception ex)
  82. // {
  83. // ErrorLogger.LogError("HostsHelper.AdBlockWithSocial", ex.Message, ex.StackTrace);
  84. // MessageBox.Show(Options.TranslationList("dnsCacheM").ToString(), "DNS Cache is running", MessageBoxButtons.OK, MessageBoxIcon.Information);
  85. // }
  86. //}
  87. //internal static void AdBlockUltimate()
  88. //{
  89. // try
  90. // {
  91. // if (File.Exists(HostsFile))
  92. // {
  93. // File.Delete(HostsFile);
  94. // }
  95. // File.WriteAllText(HostsFile, _client.DownloadString(AdBlockUltimateLink));
  96. // }
  97. // catch (Exception ex)
  98. // {
  99. // ErrorLogger.LogError("HostsHelper.AdBlockUltimate", ex.Message, ex.StackTrace);
  100. // MessageBox.Show(Options.TranslationList("dnsCacheM").ToString(), "DNS Cache is running", MessageBoxButtons.OK, MessageBoxIcon.Information);
  101. // }
  102. //}
  103. internal static string[] ReadHosts()
  104. {
  105. StringBuilder sb = new StringBuilder();
  106. try
  107. {
  108. using (StreamReader sr = File.OpenText(HostsFile))
  109. {
  110. sb.Append(sr.ReadToEnd());
  111. }
  112. }
  113. catch (Exception ex)
  114. {
  115. ErrorLogger.LogError("HostsHelper.ReadHosts", ex.Message, ex.StackTrace);
  116. }
  117. return sb.ToString().Split(Environment.NewLine.ToCharArray());
  118. //return File.ReadAllLines(HostsFile);
  119. }
  120. internal static string ReadHostsFast()
  121. {
  122. StringBuilder sb = new StringBuilder();
  123. try
  124. {
  125. using (StreamReader sr = File.OpenText(HostsFile))
  126. {
  127. sb.Append(sr.ReadToEnd());
  128. }
  129. }
  130. catch (Exception ex)
  131. {
  132. ErrorLogger.LogError("HostsHelper.ReadHostsFast", ex.Message, ex.StackTrace);
  133. }
  134. return sb.ToString();
  135. }
  136. internal static void LocateHosts()
  137. {
  138. Utilities.FindFile(HostsFile);
  139. }
  140. internal static void SaveHosts(string[] lines)
  141. {
  142. for (int i = 0; i < lines.Length; i++)
  143. {
  144. if (!lines[i].StartsWith("#") && (!string.IsNullOrEmpty(lines[i])))
  145. {
  146. lines[i] = SanitizeEntry(lines[i]);
  147. }
  148. }
  149. try
  150. {
  151. File.WriteAllText(HostsFile, string.Empty);
  152. File.WriteAllLines(HostsFile, lines);
  153. }
  154. catch (Exception ex)
  155. {
  156. ErrorLogger.LogError("HostsHelper.SaveHosts", ex.Message, ex.StackTrace);
  157. MessageBox.Show(Options.TranslationList("dnsCacheM").ToString(), "DNS Cache is running", MessageBoxButtons.OK, MessageBoxIcon.Information);
  158. }
  159. }
  160. internal static List<string> GetHostsEntries()
  161. {
  162. List<string> entries = new List<string>();
  163. //string[] lines = File.ReadAllLines(HostsFile);
  164. string[] lines = ReadHosts();
  165. foreach (string line in lines)
  166. {
  167. if (!line.StartsWith("#") && (!string.IsNullOrEmpty(line)))
  168. {
  169. entries.Add(line.Replace(" ", " : "));
  170. }
  171. }
  172. return entries;
  173. }
  174. internal static void AddEntry(string entry, string comment = null)
  175. {
  176. try
  177. {
  178. if (string.IsNullOrEmpty(comment))
  179. {
  180. File.AppendAllText(HostsFile, NewLine + $"{entry}");
  181. return;
  182. }
  183. File.AppendAllText(HostsFile, NewLine + $"{entry} #{comment}");
  184. }
  185. catch (Exception ex)
  186. {
  187. ErrorLogger.LogError("HostsHelper.AddEntry", ex.Message, ex.StackTrace);
  188. MessageBox.Show(Options.TranslationList("dnsCacheM").ToString(), "DNS Cache is running", MessageBoxButtons.OK, MessageBoxIcon.Information);
  189. }
  190. }
  191. internal static void RemoveEntry(string entry)
  192. {
  193. try
  194. {
  195. File.WriteAllLines(HostsFile, File.ReadLines(HostsFile).Where(x => x != entry).ToList());
  196. }
  197. catch (Exception ex)
  198. {
  199. ErrorLogger.LogError("HostsHelper.RemoveEntry", ex.Message, ex.StackTrace);
  200. MessageBox.Show(Options.TranslationList("dnsCacheM").ToString(), "DNS Cache is running", MessageBoxButtons.OK, MessageBoxIcon.Information);
  201. }
  202. }
  203. internal static void RemoveAllEntries(List<string> collection)
  204. {
  205. try
  206. {
  207. foreach (string text in collection)
  208. {
  209. File.WriteAllLines(HostsFile, File.ReadLines(HostsFile).Where(l => l != text).ToList());
  210. }
  211. }
  212. catch (Exception ex)
  213. {
  214. ErrorLogger.LogError("HostsHelper.RemoveAllEntries", ex.Message, ex.StackTrace);
  215. MessageBox.Show(Options.TranslationList("dnsCacheM").ToString(), "DNS Cache is running", MessageBoxButtons.OK, MessageBoxIcon.Information);
  216. }
  217. }
  218. internal static string SanitizeEntry(string entry)
  219. {
  220. // remove multiple white spaces and keep only one
  221. return Regex.Replace(entry, @"\s{2,}", " ");
  222. }
  223. internal static bool GetReadOnly()
  224. {
  225. try
  226. {
  227. return new FileInfo(HostsFile).IsReadOnly;
  228. }
  229. catch (Exception ex)
  230. {
  231. ErrorLogger.LogError("HostsHelper.ReadOnly", ex.Message, ex.StackTrace);
  232. return false;
  233. }
  234. }
  235. // edit read-only attibute
  236. internal static void ReadOnly(bool enable)
  237. {
  238. try
  239. {
  240. FileInfo fi = new FileInfo(HostsFile);
  241. fi.IsReadOnly = enable;
  242. }
  243. catch (Exception ex)
  244. {
  245. ErrorLogger.LogError("HostsHelper.ReadOnly", ex.Message, ex.StackTrace);
  246. }
  247. }
  248. }
  249. }