HostsHelper.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.IO;
  5. using System.Text.RegularExpressions;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Net;
  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("DNS Cache is being generated, try again later!", "DNS Cache is running", 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("DNS Cache is being generated, try again later!", "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("DNS Cache is being generated, try again later!", "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("DNS Cache is being generated, try again later!", "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("DNS Cache is being generated, try again later!", "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("DNS Cache is being generated, try again later!", "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)
  175. {
  176. try
  177. {
  178. File.AppendAllText(HostsFile, NewLine + entry);
  179. }
  180. catch (Exception ex)
  181. {
  182. ErrorLogger.LogError("HostsHelper.AddEntry", ex.Message, ex.StackTrace);
  183. MessageBox.Show("DNS Cache is being generated, try again later!", "DNS Cache is running", MessageBoxButtons.OK, MessageBoxIcon.Information);
  184. }
  185. }
  186. internal static void RemoveEntry(string entry)
  187. {
  188. try
  189. {
  190. File.WriteAllLines(HostsFile, File.ReadLines(HostsFile).Where(x => x != entry).ToList());
  191. }
  192. catch (Exception ex)
  193. {
  194. ErrorLogger.LogError("HostsHelper.RemoveEntry", ex.Message, ex.StackTrace);
  195. MessageBox.Show("DNS Cache is being generated, try again later!", "DNS Cache is running", MessageBoxButtons.OK, MessageBoxIcon.Information);
  196. }
  197. }
  198. internal static void RemoveAllEntries(List<string> collection)
  199. {
  200. try
  201. {
  202. foreach (string text in collection)
  203. {
  204. File.WriteAllLines(HostsFile, File.ReadLines(HostsFile).Where(l => l != text).ToList());
  205. }
  206. }
  207. catch (Exception ex)
  208. {
  209. ErrorLogger.LogError("HostsHelper.RemoveAllEntries", ex.Message, ex.StackTrace);
  210. MessageBox.Show("DNS Cache is being generated, try again later!", "DNS Cache is running", MessageBoxButtons.OK, MessageBoxIcon.Information);
  211. }
  212. }
  213. internal static string SanitizeEntry(string entry)
  214. {
  215. // remove multiple white spaces and keep only one
  216. return Regex.Replace(entry, @"\s{2,}", " ");
  217. }
  218. internal static bool GetReadOnly()
  219. {
  220. try
  221. {
  222. return new FileInfo(HostsFile).IsReadOnly;
  223. }
  224. catch (Exception ex)
  225. {
  226. ErrorLogger.LogError("HostsHelper.ReadOnly", ex.Message, ex.StackTrace);
  227. return false;
  228. }
  229. }
  230. // edit read-only attibute
  231. internal static void ReadOnly(bool enable)
  232. {
  233. try
  234. {
  235. FileInfo fi = new FileInfo(HostsFile);
  236. fi.IsReadOnly = enable;
  237. }
  238. catch (Exception ex)
  239. {
  240. ErrorLogger.LogError("HostsHelper.ReadOnly", ex.Message, ex.StackTrace);
  241. }
  242. }
  243. }
  244. }