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 2.4 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
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Net;
  5. using System.IO;
  6. using Shadowsocks.Properties;
  7. using SimpleJson;
  8. using Shadowsocks.Util;
  9. using Shadowsocks.Model;
  10. namespace Shadowsocks.Controller
  11. {
  12. public class GFWListUpdater
  13. {
  14. private const string GFWLIST_URL = "https://autoproxy-gfwlist.googlecode.com/svn/trunk/gfwlist.txt";
  15. private static string PAC_FILE = PACServer.PAC_FILE;
  16. public event EventHandler UpdateCompleted;
  17. public event ErrorEventHandler Error;
  18. private void http_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
  19. {
  20. try
  21. {
  22. List<string> lines = ParseResult(e.Result);
  23. string abpContent = Utils.UnGzip(Resources.abp_js);
  24. abpContent = abpContent.Replace("__RULES__", SimpleJson.SimpleJson.SerializeObject(lines));
  25. File.WriteAllText(PAC_FILE, abpContent, Encoding.UTF8);
  26. if (UpdateCompleted != null)
  27. {
  28. UpdateCompleted(this, new EventArgs());
  29. }
  30. }
  31. catch (Exception ex)
  32. {
  33. if (Error != null)
  34. {
  35. Error(this, new ErrorEventArgs(ex));
  36. }
  37. }
  38. }
  39. public void UpdatePACFromGFWList(Configuration config)
  40. {
  41. WebClient http = new WebClient();
  42. http.Proxy = new WebProxy(IPAddress.Loopback.ToString(), config.localPort);
  43. http.DownloadStringCompleted += http_DownloadStringCompleted;
  44. http.DownloadStringAsync(new Uri(GFWLIST_URL));
  45. }
  46. public List<string> ParseResult(string response)
  47. {
  48. byte[] bytes = Convert.FromBase64String(response);
  49. string content = Encoding.ASCII.GetString(bytes);
  50. string[] lines = content.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
  51. List<string> valid_lines = new List<string>(lines.Length);
  52. foreach (string line in lines)
  53. {
  54. if (line.StartsWith("!") || line.StartsWith("["))
  55. continue;
  56. valid_lines.Add(line);
  57. }
  58. return valid_lines;
  59. }
  60. }
  61. }