HostsHelper.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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
  34. {
  35. MessageBox.Show("DNS Cache is being generated, try again later!", "DNS Cache is running", MessageBoxButtons.OK, MessageBoxIcon.Information);
  36. }
  37. }
  38. internal static void AdblockBasic()
  39. {
  40. try
  41. {
  42. if (File.Exists(HostsFile))
  43. {
  44. File.Delete(HostsFile);
  45. }
  46. File.WriteAllText(HostsFile, _client.DownloadString(AdBlockBasicLink));
  47. }
  48. catch
  49. {
  50. MessageBox.Show("DNS Cache is being generated, try again later!", "DNS Cache is running", MessageBoxButtons.OK, MessageBoxIcon.Information);
  51. }
  52. }
  53. internal static void AdBlockWithPorn()
  54. {
  55. try
  56. {
  57. if (File.Exists(HostsFile))
  58. {
  59. File.Delete(HostsFile);
  60. }
  61. File.WriteAllText(HostsFile, _client.DownloadString(AdBlockWithPornLink));
  62. }
  63. catch
  64. {
  65. MessageBox.Show("DNS Cache is being generated, try again later!", "DNS Cache is running", MessageBoxButtons.OK, MessageBoxIcon.Information);
  66. }
  67. }
  68. internal static void AdBlockWithSocial()
  69. {
  70. try
  71. {
  72. if (File.Exists(HostsFile))
  73. {
  74. File.Delete(HostsFile);
  75. }
  76. File.WriteAllText(HostsFile, _client.DownloadString(AdBlockWithSocialLink));
  77. }
  78. catch
  79. {
  80. MessageBox.Show("DNS Cache is being generated, try again later!", "DNS Cache is running", MessageBoxButtons.OK, MessageBoxIcon.Information);
  81. }
  82. }
  83. internal static void AdBlockUltimate()
  84. {
  85. try
  86. {
  87. if (File.Exists(HostsFile))
  88. {
  89. File.Delete(HostsFile);
  90. }
  91. File.WriteAllText(HostsFile, _client.DownloadString(AdBlockUltimateLink));
  92. }
  93. catch
  94. {
  95. MessageBox.Show("DNS Cache is being generated, try again later!", "DNS Cache is running", MessageBoxButtons.OK, MessageBoxIcon.Information);
  96. }
  97. }
  98. internal static string[] ReadHosts()
  99. {
  100. StringBuilder sb = new StringBuilder();
  101. using (StreamReader sr = File.OpenText(HostsFile))
  102. {
  103. sb.Append(sr.ReadToEnd());
  104. }
  105. return sb.ToString().Split(Environment.NewLine.ToCharArray());
  106. //return File.ReadAllLines(HostsFile);
  107. }
  108. internal static string ReadHostsFast()
  109. {
  110. StringBuilder sb = new StringBuilder();
  111. using (StreamReader sr = File.OpenText(HostsFile))
  112. {
  113. sb.Append(sr.ReadToEnd());
  114. }
  115. return sb.ToString();
  116. }
  117. internal static void LocateHosts()
  118. {
  119. Utilities.FindFile(HostsFile);
  120. }
  121. internal static void SaveHosts(string[] lines)
  122. {
  123. for (int i = 0; i < lines.Length; i++)
  124. {
  125. if (!lines[i].StartsWith("#") && (!string.IsNullOrEmpty(lines[i])))
  126. {
  127. lines[i] = SanitizeEntry(lines[i]);
  128. }
  129. }
  130. try
  131. {
  132. File.WriteAllText(HostsFile, string.Empty);
  133. File.WriteAllLines(HostsFile, lines);
  134. }
  135. catch
  136. {
  137. MessageBox.Show("DNS Cache is being generated, try again later!", "DNS Cache is running", MessageBoxButtons.OK, MessageBoxIcon.Information);
  138. }
  139. }
  140. internal static List<string> GetHostsEntries()
  141. {
  142. List<string> entries = new List<string>();
  143. //string[] lines = File.ReadAllLines(HostsFile);
  144. string[] lines = ReadHosts();
  145. foreach (string line in lines)
  146. {
  147. if (!line.StartsWith("#") && (!string.IsNullOrEmpty(line)))
  148. {
  149. entries.Add(line.Replace(" ", " : "));
  150. }
  151. }
  152. return entries;
  153. }
  154. internal static void AddEntry(string entry)
  155. {
  156. try
  157. {
  158. File.AppendAllText(HostsFile, NewLine + entry);
  159. }
  160. catch
  161. {
  162. MessageBox.Show("DNS Cache is being generated, try again later!", "DNS Cache is running", MessageBoxButtons.OK, MessageBoxIcon.Information);
  163. }
  164. }
  165. internal static void RemoveEntry(string entry)
  166. {
  167. try
  168. {
  169. File.WriteAllLines(HostsFile, File.ReadLines(HostsFile).Where(x => x != entry).ToList());
  170. }
  171. catch
  172. {
  173. MessageBox.Show("DNS Cache is being generated, try again later!", "DNS Cache is running", MessageBoxButtons.OK, MessageBoxIcon.Information);
  174. }
  175. }
  176. internal static void RemoveAllEntries(List<string> collection)
  177. {
  178. try
  179. {
  180. foreach (string text in collection)
  181. {
  182. File.WriteAllLines(HostsFile, File.ReadLines(HostsFile).Where(l => l != text).ToList());
  183. }
  184. }
  185. catch
  186. {
  187. MessageBox.Show("DNS Cache is being generated, try again later!", "DNS Cache is running", MessageBoxButtons.OK, MessageBoxIcon.Information);
  188. }
  189. }
  190. internal static string SanitizeEntry(string entry)
  191. {
  192. // remove multiple white spaces and keep only one
  193. return Regex.Replace(entry, @"\s{2,}", " ");
  194. }
  195. internal static bool GetReadOnly()
  196. {
  197. return new FileInfo(HostsFile).IsReadOnly;
  198. }
  199. internal static void ReadOnly(bool enable)
  200. {
  201. FileInfo fi = new FileInfo(HostsFile);
  202. fi.IsReadOnly = enable;
  203. }
  204. }
  205. }