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