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