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.

UpdateChecker.cs 4.6 kB

10 years ago
10 years ago
10 years ago
9 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using Shadowsocks.Model;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Net;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Text.RegularExpressions;
  9. using SimpleJson;
  10. namespace Shadowsocks.Controller
  11. {
  12. public class UpdateChecker
  13. {
  14. private const string UpdateURL = "https://api.github.com/repos/shadowsocks/shadowsocks-csharp/releases";
  15. public string LatestVersionNumber;
  16. public string LatestVersionURL;
  17. public event EventHandler NewVersionFound;
  18. public const string Version = "2.5.4";
  19. public void CheckUpdate(Configuration config)
  20. {
  21. // TODO test failures
  22. WebClient http = new WebClient();
  23. http.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.3319.102 Safari/537.36");
  24. http.Proxy = new WebProxy(IPAddress.Loopback.ToString(), config.localPort);
  25. http.DownloadStringCompleted += http_DownloadStringCompleted;
  26. http.DownloadStringAsync(new Uri(UpdateURL));
  27. }
  28. public static int CompareVersion(string l, string r)
  29. {
  30. var ls = l.Split('.');
  31. var rs = r.Split('.');
  32. for (int i = 0; i < Math.Max(ls.Length, rs.Length); i++)
  33. {
  34. int lp = (i < ls.Length) ? int.Parse(ls[i]) : 0;
  35. int rp = (i < rs.Length) ? int.Parse(rs[i]) : 0;
  36. if (lp != rp)
  37. {
  38. return lp - rp;
  39. }
  40. }
  41. return 0;
  42. }
  43. public class VersionComparer : IComparer<string>
  44. {
  45. // Calls CaseInsensitiveComparer.Compare with the parameters reversed.
  46. public int Compare(string x, string y)
  47. {
  48. return CompareVersion(ParseVersionFromURL(x), ParseVersionFromURL(y));
  49. }
  50. }
  51. private static string ParseVersionFromURL(string url)
  52. {
  53. Match match = Regex.Match(url, @".*Shadowsocks-win.*?-([\d\.]+)\.\w+", RegexOptions.IgnoreCase);
  54. if (match.Success)
  55. {
  56. if (match.Groups.Count == 2)
  57. {
  58. return match.Groups[1].Value;
  59. }
  60. }
  61. return null;
  62. }
  63. private void SortVersions(List<string> versions)
  64. {
  65. versions.Sort(new VersionComparer());
  66. }
  67. private bool IsNewVersion(string url)
  68. {
  69. if (url.IndexOf("prerelease") >= 0)
  70. {
  71. return false;
  72. }
  73. string version = ParseVersionFromURL(url);
  74. if (version == null)
  75. {
  76. return false;
  77. }
  78. string currentVersion = Version;
  79. return CompareVersion(version, currentVersion) > 0;
  80. }
  81. private void http_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
  82. {
  83. try
  84. {
  85. string response = e.Result;
  86. JsonArray result = (JsonArray)SimpleJson.SimpleJson.DeserializeObject(e.Result);
  87. List<string> versions = new List<string>();
  88. foreach (JsonObject release in result)
  89. {
  90. if ((bool)release["prerelease"])
  91. {
  92. continue;
  93. }
  94. foreach (JsonObject asset in (JsonArray)release["assets"])
  95. {
  96. string url = (string)asset["browser_download_url"];
  97. if (IsNewVersion(url))
  98. {
  99. versions.Add(url);
  100. }
  101. }
  102. }
  103. if (versions.Count == 0)
  104. {
  105. return;
  106. }
  107. // sort versions
  108. SortVersions(versions);
  109. LatestVersionURL = versions[versions.Count - 1];
  110. LatestVersionNumber = ParseVersionFromURL(LatestVersionURL);
  111. if (NewVersionFound != null)
  112. {
  113. NewVersionFound(this, new EventArgs());
  114. }
  115. }
  116. catch (Exception ex)
  117. {
  118. Logging.Debug(ex.ToString());
  119. return;
  120. }
  121. }
  122. }
  123. }