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 4.1 kB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net;
  5. using System.Text;
  6. using Shadowsocks.Model;
  7. using Shadowsocks.Properties;
  8. using Shadowsocks.Util;
  9. namespace Shadowsocks.Controller
  10. {
  11. public class GFWListUpdater
  12. {
  13. private const string GFWLIST_URL = "https://raw.githubusercontent.com/gfwlist/gfwlist/master/gfwlist.txt";
  14. private static string PAC_FILE = PACServer.PAC_FILE;
  15. private static string USER_RULE_FILE = PACServer.USER_RULE_FILE;
  16. private static string USER_ABP_FILE = PACServer.USER_ABP_FILE;
  17. public event EventHandler<ResultEventArgs> UpdateCompleted;
  18. public event ErrorEventHandler Error;
  19. public class ResultEventArgs : EventArgs
  20. {
  21. public bool Success;
  22. public ResultEventArgs(bool success)
  23. {
  24. this.Success = success;
  25. }
  26. }
  27. private void http_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
  28. {
  29. try
  30. {
  31. File.WriteAllText(Utils.GetTempPath("gfwlist.txt"), e.Result, Encoding.UTF8);
  32. List<string> lines = ParseResult(e.Result);
  33. if (File.Exists(USER_RULE_FILE))
  34. {
  35. string local = File.ReadAllText(USER_RULE_FILE, Encoding.UTF8);
  36. string[] rules = local.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
  37. foreach (string rule in rules)
  38. {
  39. if (rule.StartsWith("!") || rule.StartsWith("["))
  40. continue;
  41. lines.Add(rule);
  42. }
  43. }
  44. string abpContent;
  45. if (File.Exists(USER_ABP_FILE))
  46. {
  47. abpContent = File.ReadAllText(USER_ABP_FILE, Encoding.UTF8);
  48. }
  49. else
  50. {
  51. abpContent = Utils.UnGzip(Resources.abp_js);
  52. }
  53. abpContent = abpContent.Replace("__RULES__", SimpleJson.SimpleJson.SerializeObject(lines));
  54. if (File.Exists(PAC_FILE))
  55. {
  56. string original = File.ReadAllText(PAC_FILE, Encoding.UTF8);
  57. if (original == abpContent)
  58. {
  59. UpdateCompleted(this, new ResultEventArgs(false));
  60. return;
  61. }
  62. }
  63. File.WriteAllText(PAC_FILE, abpContent, Encoding.UTF8);
  64. if (UpdateCompleted != null)
  65. {
  66. UpdateCompleted(this, new ResultEventArgs(true));
  67. }
  68. }
  69. catch (Exception ex)
  70. {
  71. if (Error != null)
  72. {
  73. Error(this, new ErrorEventArgs(ex));
  74. }
  75. }
  76. }
  77. public void UpdatePACFromGFWList(Configuration config)
  78. {
  79. WebClient http = new WebClient();
  80. http.Proxy = new WebProxy(IPAddress.Loopback.ToString(), config.localPort);
  81. http.DownloadStringCompleted += http_DownloadStringCompleted;
  82. http.DownloadStringAsync(new Uri(GFWLIST_URL));
  83. }
  84. public static List<string> ParseResult(string response)
  85. {
  86. byte[] bytes = Convert.FromBase64String(response);
  87. string content = Encoding.ASCII.GetString(bytes);
  88. string[] lines = content.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
  89. List<string> valid_lines = new List<string>(lines.Length);
  90. foreach (string line in lines)
  91. {
  92. if (line.StartsWith("!") || line.StartsWith("["))
  93. continue;
  94. valid_lines.Add(line);
  95. }
  96. return valid_lines;
  97. }
  98. }
  99. }