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.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Net;
  5. using System.IO;
  6. namespace Shadowsocks.Controller
  7. {
  8. public class GfwListUpdater
  9. {
  10. private const string GFWLIST_URL = "https://autoproxy-gfwlist.googlecode.com/svn/trunk/gfwlist.txt";
  11. public IWebProxy proxy = null;
  12. public class GfwListDownloadCompletedArgs : EventArgs
  13. {
  14. public string Content;
  15. }
  16. public event EventHandler<GfwListDownloadCompletedArgs> DownloadCompleted;
  17. public event ErrorEventHandler Error;
  18. public void Download()
  19. {
  20. WebClient http = new WebClient();
  21. http.Proxy = proxy;
  22. http.DownloadStringCompleted += http_DownloadStringCompleted;
  23. http.DownloadStringAsync(new Uri(GFWLIST_URL));
  24. }
  25. protected void ReportError(Exception e)
  26. {
  27. if (Error != null)
  28. {
  29. Error(this, new ErrorEventArgs(e));
  30. }
  31. }
  32. private void http_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
  33. {
  34. try
  35. {
  36. string response = e.Result;
  37. if (DownloadCompleted != null)
  38. {
  39. DownloadCompleted(this, new GfwListDownloadCompletedArgs
  40. {
  41. Content = response
  42. });
  43. }
  44. }
  45. catch (Exception ex)
  46. {
  47. ReportError(ex);
  48. }
  49. }
  50. public class Parser
  51. {
  52. private string _Content;
  53. public string Content
  54. {
  55. get { return _Content; }
  56. }
  57. public Parser(string response)
  58. {
  59. byte[] bytes = Convert.FromBase64String(response);
  60. this._Content = Encoding.ASCII.GetString(bytes);
  61. }
  62. public string[] GetValidLines()
  63. {
  64. string[] lines = Content.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
  65. List<string> valid_lines = new List<string>(lines.Length);
  66. foreach (string line in lines)
  67. {
  68. if (line.StartsWith("!") || line.StartsWith("["))
  69. continue;
  70. valid_lines.Add(line);
  71. }
  72. return valid_lines.ToArray();
  73. }
  74. }
  75. }
  76. }