HostsHelper.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. using (StreamReader sr = File.OpenText(HostsFile))
  107. {
  108. sb.Append(sr.ReadToEnd());
  109. }
  110. return sb.ToString().Split(Environment.NewLine.ToCharArray());
  111. //return File.ReadAllLines(HostsFile);
  112. }
  113. internal static string ReadHostsFast()
  114. {
  115. StringBuilder sb = new StringBuilder();
  116. using (StreamReader sr = File.OpenText(HostsFile))
  117. {
  118. sb.Append(sr.ReadToEnd());
  119. }
  120. return sb.ToString();
  121. }
  122. internal static void LocateHosts()
  123. {
  124. Utilities.FindFile(HostsFile);
  125. }
  126. internal static void SaveHosts(string[] lines)
  127. {
  128. for (int i = 0; i < lines.Length; i++)
  129. {
  130. if (!lines[i].StartsWith("#") && (!string.IsNullOrEmpty(lines[i])))
  131. {
  132. lines[i] = SanitizeEntry(lines[i]);
  133. }
  134. }
  135. try
  136. {
  137. File.WriteAllText(HostsFile, string.Empty);
  138. File.WriteAllLines(HostsFile, lines);
  139. }
  140. catch (Exception ex)
  141. {
  142. ErrorLogger.LogError("HostsHelper.SaveHosts", ex.Message, ex.StackTrace);
  143. MessageBox.Show("DNS Cache is being generated, try again later!", "DNS Cache is running", MessageBoxButtons.OK, MessageBoxIcon.Information);
  144. }
  145. }
  146. internal static List<string> GetHostsEntries()
  147. {
  148. List<string> entries = new List<string>();
  149. //string[] lines = File.ReadAllLines(HostsFile);
  150. string[] lines = ReadHosts();
  151. foreach (string line in lines)
  152. {
  153. if (!line.StartsWith("#") && (!string.IsNullOrEmpty(line)))
  154. {
  155. entries.Add(line.Replace(" ", " : "));
  156. }
  157. }
  158. return entries;
  159. }
  160. internal static void AddEntry(string entry)
  161. {
  162. try
  163. {
  164. File.AppendAllText(HostsFile, NewLine + entry);
  165. }
  166. catch (Exception ex)
  167. {
  168. ErrorLogger.LogError("HostsHelper.AddEntry", ex.Message, ex.StackTrace);
  169. MessageBox.Show("DNS Cache is being generated, try again later!", "DNS Cache is running", MessageBoxButtons.OK, MessageBoxIcon.Information);
  170. }
  171. }
  172. internal static void RemoveEntry(string entry)
  173. {
  174. try
  175. {
  176. File.WriteAllLines(HostsFile, File.ReadLines(HostsFile).Where(x => x != entry).ToList());
  177. }
  178. catch (Exception ex)
  179. {
  180. ErrorLogger.LogError("HostsHelper.RemoveEntry", ex.Message, ex.StackTrace);
  181. MessageBox.Show("DNS Cache is being generated, try again later!", "DNS Cache is running", MessageBoxButtons.OK, MessageBoxIcon.Information);
  182. }
  183. }
  184. internal static void RemoveAllEntries(List<string> collection)
  185. {
  186. try
  187. {
  188. foreach (string text in collection)
  189. {
  190. File.WriteAllLines(HostsFile, File.ReadLines(HostsFile).Where(l => l != text).ToList());
  191. }
  192. }
  193. catch (Exception ex)
  194. {
  195. ErrorLogger.LogError("HostsHelper.RemoveAllEntries", ex.Message, ex.StackTrace);
  196. MessageBox.Show("DNS Cache is being generated, try again later!", "DNS Cache is running", MessageBoxButtons.OK, MessageBoxIcon.Information);
  197. }
  198. }
  199. internal static string SanitizeEntry(string entry)
  200. {
  201. // remove multiple white spaces and keep only one
  202. return Regex.Replace(entry, @"\s{2,}", " ");
  203. }
  204. internal static bool GetReadOnly()
  205. {
  206. return new FileInfo(HostsFile).IsReadOnly;
  207. }
  208. internal static void ReadOnly(bool enable)
  209. {
  210. FileInfo fi = new FileInfo(HostsFile);
  211. fi.IsReadOnly = enable;
  212. }
  213. }
  214. }