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

10 years ago
10 years ago
10 years ago
10 years ago
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
10 years ago
10 years ago
10 years ago
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Net;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using System.IO;
  9. using SimpleJson;
  10. using Shadowsocks.Model;
  11. using Shadowsocks.Util;
  12. namespace Shadowsocks.Controller
  13. {
  14. public class UpdateChecker
  15. {
  16. private const string UpdateURL = "https://api.github.com/repos/shadowsocks/shadowsocks-windows/releases";
  17. private const string UserAgent = "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.3319.102 Safari/537.36";
  18. private Configuration config;
  19. public bool NewVersionFound;
  20. public string LatestVersionNumber;
  21. public string LatestVersionName;
  22. public string LatestVersionURL;
  23. public string LatestVersionLocalName;
  24. public event EventHandler CheckUpdateCompleted;
  25. public const string Version = "2.5.8";
  26. public void CheckUpdate(Configuration config)
  27. {
  28. this.config = config;
  29. try
  30. {
  31. WebClient http = CreateWebClient();
  32. http.DownloadStringCompleted += http_DownloadStringCompleted;
  33. http.DownloadStringAsync(new Uri(UpdateURL));
  34. }
  35. catch (Exception ex)
  36. {
  37. Logging.LogUsefulException(ex);
  38. }
  39. }
  40. private void http_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
  41. {
  42. try
  43. {
  44. string response = e.Result;
  45. JsonArray result = (JsonArray)SimpleJson.SimpleJson.DeserializeObject(e.Result);
  46. List<Asset> asserts = new List<Asset>();
  47. foreach (JsonObject release in result)
  48. {
  49. if ((bool)release["prerelease"])
  50. {
  51. continue;
  52. }
  53. foreach (JsonObject asset in (JsonArray)release["assets"])
  54. {
  55. Asset ass = new Asset();
  56. ass.Parse(asset);
  57. if (ass.IsNewVersion(Version))
  58. {
  59. asserts.Add(ass);
  60. }
  61. }
  62. }
  63. if (asserts.Count != 0)
  64. {
  65. SortByVersions(asserts);
  66. Asset asset = asserts[asserts.Count - 1];
  67. NewVersionFound = true;
  68. LatestVersionURL = asset.browser_download_url;
  69. LatestVersionNumber = asset.version;
  70. LatestVersionName = asset.name;
  71. startDownload();
  72. }
  73. else if (CheckUpdateCompleted != null)
  74. {
  75. CheckUpdateCompleted(this, new EventArgs());
  76. }
  77. }
  78. catch (Exception ex)
  79. {
  80. Logging.LogUsefulException(ex);
  81. }
  82. }
  83. private void startDownload()
  84. {
  85. try
  86. {
  87. string temppath = Utils.GetTempPath();
  88. LatestVersionLocalName = Path.Combine(temppath, LatestVersionName);
  89. WebClient http = CreateWebClient();
  90. http.DownloadFileCompleted += Http_DownloadFileCompleted;
  91. http.DownloadFileAsync(new Uri(LatestVersionURL), LatestVersionLocalName);
  92. }
  93. catch (Exception ex)
  94. {
  95. Logging.LogUsefulException(ex);
  96. }
  97. }
  98. private void Http_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
  99. {
  100. try
  101. {
  102. if(e.Error != null)
  103. {
  104. Logging.LogUsefulException(e.Error);
  105. return;
  106. }
  107. if (CheckUpdateCompleted != null)
  108. {
  109. CheckUpdateCompleted(this, new EventArgs());
  110. }
  111. }
  112. catch (Exception ex)
  113. {
  114. Logging.LogUsefulException(ex);
  115. }
  116. }
  117. private WebClient CreateWebClient()
  118. {
  119. WebClient http = new WebClient();
  120. http.Headers.Add("User-Agent", UserAgent);
  121. http.Proxy = new WebProxy(IPAddress.Loopback.ToString(), config.localPort);
  122. return http;
  123. }
  124. private void SortByVersions(List<Asset> asserts)
  125. {
  126. asserts.Sort(new VersionComparer());
  127. }
  128. public class Asset
  129. {
  130. public bool prerelease;
  131. public string name;
  132. public string version;
  133. public string browser_download_url;
  134. public bool IsNewVersion(string currentVersion)
  135. {
  136. if (prerelease)
  137. {
  138. return false;
  139. }
  140. if (version == null)
  141. {
  142. return false;
  143. }
  144. return CompareVersion(version, currentVersion) > 0;
  145. }
  146. public void Parse(JsonObject asset)
  147. {
  148. name = (string)asset["name"];
  149. browser_download_url = (string)asset["browser_download_url"];
  150. version = ParseVersionFromURL(browser_download_url);
  151. prerelease = browser_download_url.IndexOf("prerelease") >= 0;
  152. }
  153. private static string ParseVersionFromURL(string url)
  154. {
  155. Match match = Regex.Match(url, @".*Shadowsocks-win.*?-([\d\.]+)\.\w+", RegexOptions.IgnoreCase);
  156. if (match.Success)
  157. {
  158. if (match.Groups.Count == 2)
  159. {
  160. return match.Groups[1].Value;
  161. }
  162. }
  163. return null;
  164. }
  165. public static int CompareVersion(string l, string r)
  166. {
  167. var ls = l.Split('.');
  168. var rs = r.Split('.');
  169. for (int i = 0; i < Math.Max(ls.Length, rs.Length); i++)
  170. {
  171. int lp = (i < ls.Length) ? int.Parse(ls[i]) : 0;
  172. int rp = (i < rs.Length) ? int.Parse(rs[i]) : 0;
  173. if (lp != rp)
  174. {
  175. return lp - rp;
  176. }
  177. }
  178. return 0;
  179. }
  180. }
  181. class VersionComparer : IComparer<Asset>
  182. {
  183. // Calls CaseInsensitiveComparer.Compare with the parameters reversed.
  184. public int Compare(Asset x, Asset y)
  185. {
  186. return Asset.CompareVersion(x.version, y.version);
  187. }
  188. }
  189. }
  190. }