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.1 kB

10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 void CheckUpdate()
  18. {
  19. // TODO test failures
  20. WebClient http = new WebClient();
  21. http.DownloadStringCompleted += http_DownloadStringCompleted;
  22. http.DownloadStringAsync(new Uri(UpdateURL));
  23. }
  24. public static int CompareVersion(string l, string r)
  25. {
  26. var ls = l.Split('.');
  27. var rs = r.Split('.');
  28. for (int i = 0; i < Math.Max(ls.Length, rs.Length); i++)
  29. {
  30. int lp = (i < ls.Length) ? int.Parse(ls[i]) : 0;
  31. int rp = (i < rs.Length) ? int.Parse(rs[i]) : 0;
  32. if (lp != rp)
  33. {
  34. return lp - rp;
  35. }
  36. }
  37. return 0;
  38. }
  39. public class VersionComparer : IComparer<string>
  40. {
  41. // Calls CaseInsensitiveComparer.Compare with the parameters reversed.
  42. public int Compare(string x, string y)
  43. {
  44. return CompareVersion(ParseVersionFromURL(x), ParseVersionFromURL(y));
  45. }
  46. }
  47. private static string ParseVersionFromURL(string url)
  48. {
  49. Match match = Regex.Match(url, @".*Shadowsocks-win.*?-([\d\.]+)\.\w+", RegexOptions.IgnoreCase);
  50. if (match.Success)
  51. {
  52. if (match.Groups.Count == 2)
  53. {
  54. return match.Groups[1].Value;
  55. }
  56. }
  57. return null;
  58. }
  59. private void SortVersions(List<string> versions)
  60. {
  61. versions.Sort(new VersionComparer());
  62. }
  63. private bool IsNewVersion(string url)
  64. {
  65. // check dotnet 4.0
  66. AssemblyName[] references = Assembly.GetExecutingAssembly().GetReferencedAssemblies();
  67. Version dotNetVersion = Environment.Version;
  68. foreach (AssemblyName reference in references)
  69. {
  70. if (reference.Name == "mscorlib")
  71. {
  72. dotNetVersion = reference.Version;
  73. }
  74. }
  75. if (dotNetVersion.Major >= 4)
  76. {
  77. if (url.IndexOf("dotnet4.0") < 0)
  78. {
  79. return false;
  80. }
  81. }
  82. else
  83. {
  84. if (url.IndexOf("dotnet4.0") >= 0)
  85. {
  86. return false;
  87. }
  88. }
  89. string version = ParseVersionFromURL(url);
  90. if (version == null)
  91. {
  92. return false;
  93. }
  94. string currentVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();
  95. return CompareVersion(version, currentVersion) > 0;
  96. }
  97. private void http_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
  98. {
  99. try
  100. {
  101. string response = e.Result;
  102. XmlDocument xmlDoc = new XmlDocument();
  103. xmlDoc.LoadXml(response);
  104. XmlNodeList elements = xmlDoc.GetElementsByTagName("media:content");
  105. List<string> versions = new List<string>();
  106. foreach (XmlNode el in elements)
  107. {
  108. foreach (XmlAttribute attr in el.Attributes)
  109. {
  110. if (attr.Name == "url")
  111. {
  112. Console.WriteLine(attr.Value);
  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. }