You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

GfwListUpdater.cs 9.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Threading;
  5. using System.Net;
  6. using System.IO;
  7. using System.IO.Compression;
  8. using System.Security.Cryptography;
  9. using Shadowsocks.Model;
  10. using Shadowsocks.Properties;
  11. namespace Shadowsocks.Controller
  12. {
  13. public class GfwListUpdater
  14. {
  15. private const string GFWLIST_URL = "https://autoproxy-gfwlist.googlecode.com/svn/trunk/gfwlist.txt";
  16. private const int EXPIRE_HOURS = 6;
  17. public IWebProxy proxy = null;
  18. public bool useSystemProxy = true;
  19. public class GfwListChangedArgs : EventArgs
  20. {
  21. public string[] GfwList { get; set; }
  22. }
  23. public event EventHandler<GfwListChangedArgs> GfwListChanged;
  24. private bool running = false;
  25. private bool closed = false;
  26. private int jobId = 0;
  27. DateTime lastUpdateTimeUtc;
  28. string lastUpdateMd5;
  29. private object locker = new object();
  30. public GfwListUpdater()
  31. {
  32. }
  33. ~GfwListUpdater()
  34. {
  35. Stop();
  36. }
  37. public void Start()
  38. {
  39. lock (locker)
  40. {
  41. if (running)
  42. return;
  43. running = true;
  44. closed = false;
  45. jobId++;
  46. new Thread(new ParameterizedThreadStart(UpdateJob)).Start(jobId);
  47. }
  48. }
  49. public void Stop()
  50. {
  51. lock(locker)
  52. {
  53. closed = true;
  54. running = false;
  55. jobId++;
  56. }
  57. }
  58. public void ScheduleUpdateTime(int delaySeconds)
  59. {
  60. lock(locker)
  61. {
  62. lastUpdateTimeUtc = DateTime.UtcNow.AddHours(-1 * EXPIRE_HOURS).AddSeconds(delaySeconds);
  63. }
  64. }
  65. private string DownloadGfwListFile()
  66. {
  67. try
  68. {
  69. WebClient http = new WebClient();
  70. http.Proxy = useSystemProxy ? WebRequest.GetSystemWebProxy() : proxy;
  71. return http.DownloadString(new Uri(GFWLIST_URL));
  72. }
  73. catch (Exception ex)
  74. {
  75. Console.WriteLine(ex.ToString());
  76. }
  77. return null;
  78. }
  79. private bool IsExpire()
  80. {
  81. lock (locker)
  82. {
  83. TimeSpan ts = DateTime.UtcNow - lastUpdateTimeUtc;
  84. bool expire = ((int)ts.TotalHours) >= EXPIRE_HOURS;
  85. if (expire)
  86. lastUpdateTimeUtc = DateTime.UtcNow;
  87. return expire;
  88. }
  89. }
  90. private bool IsJobStop(int currentJobId)
  91. {
  92. lock (locker)
  93. {
  94. if (!running || closed || currentJobId != this.jobId)
  95. return true;
  96. }
  97. return false;
  98. }
  99. private bool IsGfwListChanged(string content)
  100. {
  101. byte[] inputBytes = Encoding.UTF8.GetBytes(content);
  102. byte[] md5Bytes = MD5.Create().ComputeHash(inputBytes);
  103. string md5 = "";
  104. for (int i = 0; i < md5Bytes.Length; i++)
  105. md5 += md5Bytes[i].ToString("x").PadLeft(2, '0');
  106. if (md5 == lastUpdateMd5)
  107. return false;
  108. lastUpdateMd5 = md5;
  109. return true;
  110. }
  111. private void ParseGfwList(string response)
  112. {
  113. if (!IsGfwListChanged(response))
  114. return;
  115. if (GfwListChanged != null)
  116. {
  117. try
  118. {
  119. Parser parser = new Parser(response);
  120. GfwListChangedArgs args = new GfwListChangedArgs
  121. {
  122. GfwList = parser.GetReducedDomains()
  123. };
  124. GfwListChanged(this, args);
  125. }
  126. catch(Exception ex)
  127. {
  128. Console.WriteLine(ex.ToString());
  129. }
  130. }
  131. }
  132. private void UpdateJob(object state)
  133. {
  134. int currentJobId = (int)state;
  135. int retryTimes = 3;
  136. while (!IsJobStop(currentJobId))
  137. {
  138. if (IsExpire())
  139. {
  140. string response = DownloadGfwListFile();
  141. if (response != null)
  142. {
  143. ParseGfwList(response);
  144. }
  145. else if (retryTimes > 0)
  146. {
  147. ScheduleUpdateTime(30); /*Delay 30 seconds to retry*/
  148. retryTimes--;
  149. }
  150. else
  151. {
  152. retryTimes = 3; /* reset retry times, and wait next update time. */
  153. }
  154. }
  155. Thread.Sleep(1000);
  156. }
  157. }
  158. class Parser
  159. {
  160. public string Content { get; private set; }
  161. public Parser(string response)
  162. {
  163. byte[] bytes = Convert.FromBase64String(response);
  164. this.Content = Encoding.ASCII.GetString(bytes);
  165. }
  166. public string[] GetLines()
  167. {
  168. return Content.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
  169. }
  170. /* refer https://github.com/clowwindy/gfwlist2pac/blob/master/gfwlist2pac/main.py */
  171. public string[] GetDomains()
  172. {
  173. string[] lines = GetLines();
  174. List<string> domains = new List<string>(lines.Length);
  175. for(int i =0;i < lines.Length;i++)
  176. {
  177. string line = lines[i];
  178. if (line.IndexOf(".*") >= 0)
  179. continue;
  180. else if (line.IndexOf("*") >= 0)
  181. line = line.Replace("*", "/");
  182. if (line.StartsWith("||"))
  183. line = line.Substring(2);
  184. else if (line.StartsWith("|"))
  185. line = line.Substring(1);
  186. else if (line.StartsWith("."))
  187. line = line.Substring(1);
  188. if (line.StartsWith("!"))
  189. continue;
  190. else if (line.StartsWith("["))
  191. continue;
  192. else if (line.StartsWith("@"))
  193. continue; /*ignore white list*/
  194. domains.Add(line);
  195. }
  196. return domains.ToArray();
  197. }
  198. /* refer https://github.com/clowwindy/gfwlist2pac/blob/master/gfwlist2pac/main.py */
  199. public string[] GetReducedDomains()
  200. {
  201. string[] domains = GetDomains();
  202. List<string> new_domains = new List<string>(domains.Length);
  203. IDictionary<string, string> tld_dic = GetTldDictionary();
  204. foreach(string domain in domains)
  205. {
  206. string last_root_domain = null;
  207. int pos;
  208. pos = domain.LastIndexOf('.');
  209. last_root_domain = domain.Substring(pos + 1);
  210. if (!tld_dic.ContainsKey(last_root_domain))
  211. continue;
  212. while(pos > 0)
  213. {
  214. pos = domain.LastIndexOf('.', pos - 1);
  215. last_root_domain = domain.Substring(pos + 1);
  216. if (tld_dic.ContainsKey(last_root_domain))
  217. continue;
  218. else
  219. break;
  220. }
  221. if (last_root_domain != null)
  222. new_domains.Add(last_root_domain);
  223. }
  224. return new_domains.ToArray();
  225. }
  226. private string[] GetTlds()
  227. {
  228. string[] tlds = null;
  229. byte[] pacGZ = Resources.tld_txt;
  230. byte[] buffer = new byte[1024];
  231. int n;
  232. using(MemoryStream sb = new MemoryStream())
  233. {
  234. using (GZipStream input = new GZipStream(new MemoryStream(pacGZ),
  235. CompressionMode.Decompress, false))
  236. {
  237. while ((n = input.Read(buffer, 0, buffer.Length)) > 0)
  238. {
  239. sb.Write(buffer, 0, n);
  240. }
  241. }
  242. tlds = System.Text.Encoding.UTF8.GetString(sb.ToArray())
  243. .Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
  244. }
  245. return tlds;
  246. }
  247. private IDictionary<string, string> GetTldDictionary()
  248. {
  249. string[] tlds = GetTlds();
  250. IDictionary<string, string> dic = new Dictionary<string, string>(tlds.Length);
  251. foreach (string tld in tlds)
  252. {
  253. if (!dic.ContainsKey(tld))
  254. dic.Add(tld, tld);
  255. }
  256. return dic;
  257. }
  258. }
  259. }
  260. }