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

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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.Xml;
  9. namespace Shadowsocks.Controller
  10. {
  11. public class UpdateChecker
  12. {
  13. private const string UpdateURL = "https://sourceforge.net/api/file/index/project-id/1817190/path/dist/mtime/desc/limit/10/rss";
  14. public string LatestVersionNumber;
  15. public string LatestVersionURL;
  16. public event EventHandler NewVersionFound;
  17. public const string Version = "2.1.6";
  18. public void CheckUpdate()
  19. {
  20. // TODO test failures
  21. WebClient http = new WebClient();
  22. http.DownloadStringCompleted += http_DownloadStringCompleted;
  23. http.DownloadStringAsync(new Uri(UpdateURL));
  24. }
  25. public static int CompareVersion(string l, string r)
  26. {
  27. var ls = l.Split('.');
  28. var rs = r.Split('.');
  29. for (int i = 0; i < Math.Max(ls.Length, rs.Length); i++)
  30. {
  31. int lp = (i < ls.Length) ? int.Parse(ls[i]) : 0;
  32. int rp = (i < rs.Length) ? int.Parse(rs[i]) : 0;
  33. if (lp != rp)
  34. {
  35. return lp - rp;
  36. }
  37. }
  38. return 0;
  39. }
  40. public class VersionComparer : IComparer<string>
  41. {
  42. // Calls CaseInsensitiveComparer.Compare with the parameters reversed.
  43. public int Compare(string x, string y)
  44. {
  45. return CompareVersion(ParseVersionFromURL(x), ParseVersionFromURL(y));
  46. }
  47. }
  48. private static string ParseVersionFromURL(string url)
  49. {
  50. Match match = Regex.Match(url, @".*Shadowsocks-win.*?-([\d\.]+)\.\w+", RegexOptions.IgnoreCase);
  51. if (match.Success)
  52. {
  53. if (match.Groups.Count == 2)
  54. {
  55. return match.Groups[1].Value;
  56. }
  57. }
  58. return null;
  59. }
  60. private void SortVersions(List<string> versions)
  61. {
  62. versions.Sort(new VersionComparer());
  63. }
  64. private bool IsNewVersion(string url)
  65. {
  66. // check dotnet 4.0
  67. AssemblyName[] references = Assembly.GetExecutingAssembly().GetReferencedAssemblies();
  68. Version dotNetVersion = Environment.Version;
  69. foreach (AssemblyName reference in references)
  70. {
  71. if (reference.Name == "mscorlib")
  72. {
  73. dotNetVersion = reference.Version;
  74. }
  75. }
  76. if (dotNetVersion.Major >= 4)
  77. {
  78. if (url.IndexOf("dotnet4.0") < 0)
  79. {
  80. return false;
  81. }
  82. }
  83. else
  84. {
  85. if (url.IndexOf("dotnet4.0") >= 0)
  86. {
  87. return false;
  88. }
  89. }
  90. string version = ParseVersionFromURL(url);
  91. if (version == null)
  92. {
  93. return false;
  94. }
  95. string currentVersion = Version;
  96. return CompareVersion(version, currentVersion) > 0;
  97. }
  98. private void http_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
  99. {
  100. try
  101. {
  102. string response = e.Result;
  103. XmlDocument xmlDoc = new XmlDocument();
  104. xmlDoc.LoadXml(response);
  105. XmlNodeList elements = xmlDoc.GetElementsByTagName("media:content");
  106. List<string> versions = new List<string>();
  107. foreach (XmlNode el in elements)
  108. {
  109. foreach (XmlAttribute attr in el.Attributes)
  110. {
  111. if (attr.Name == "url")
  112. {
  113. if (IsNewVersion(attr.Value))
  114. {
  115. versions.Add(attr.Value);
  116. }
  117. }
  118. }
  119. }
  120. if (versions.Count == 0)
  121. {
  122. return;
  123. }
  124. // sort versions
  125. SortVersions(versions);
  126. LatestVersionURL = versions[versions.Count - 1];
  127. LatestVersionNumber = ParseVersionFromURL(LatestVersionURL);
  128. if (NewVersionFound != null)
  129. {
  130. NewVersionFound(this, new EventArgs());
  131. }
  132. }
  133. catch (Exception ex)
  134. {
  135. Console.Write(ex.ToString());
  136. return;
  137. }
  138. }
  139. }
  140. }