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