| 1 | using Newtonsoft.Json; |
| 2 | using System; |
| 3 | using System.Collections.Generic; |
| 4 | using System.IO; |
| 5 | using System.Linq; |
| 6 | using System.Net; |
| 7 | using System.Text; |
| 8 | using System.Threading; |
| 9 | using System.Threading.Tasks; |
| 10 | |
| 11 | namespace Updater |
| 12 | { |
| 13 | |
| 14 | public class GithubRelease |
| 15 | { |
| 16 | public class VersionInfo |
| 17 | { |
| 18 | ///<summary> |
| 19 | /// |
| 20 | ///</summary> |
| 21 | public string Title { get; set; } |
| 22 | ///<summary> |
| 23 | /// |
| 24 | ///</summary> |
| 25 | public string Version { get; set; } |
| 26 | ///<summary> |
| 27 | /// |
| 28 | ///</summary> |
| 29 | public bool IsPre { get; set; } |
| 30 | ///<summary> |
| 31 | /// |
| 32 | ///</summary> |
| 33 | public string DownloadUrl { get; set; } |
| 34 | ///<summary> |
| 35 | /// |
| 36 | ///</summary> |
| 37 | public string HtmlUrl { get; set; } |
| 38 | } |
| 39 | public class GithubModel |
| 40 | { |
| 41 | public string tag_name { get; set; } |
| 42 | public string html_url { get; set; } |
| 43 | public string name { get; set; } |
| 44 | public bool prerelease { get; set; } |
| 45 | |
| 46 | public List<GithubAssetsModel> assets { get; set; } |
| 47 | } |
| 48 | public class GithubAssetsModel |
| 49 | { |
| 50 | public string browser_download_url { get; set; } |
| 51 | } |
| 52 | private string githubUrl; |
| 53 | private string nowVersion; |
| 54 | public VersionInfo Info { get; set; } |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | public GithubRelease(string githubUrl, string nowVersion) |
| 60 | { |
| 61 | this.githubUrl = githubUrl; |
| 62 | this.nowVersion = nowVersion; |
| 63 | Info = new VersionInfo(); |
| 64 | } |
| 65 | public bool IsCanUpdate() |
| 66 | { |
| 67 | return !(nowVersion == Info.Version); |
| 68 | } |
| 69 | public async Task<VersionInfo> GetRequest() |
| 70 | { |
| 71 | var result = await Task.Run(() => |
| 72 | { |
| 73 | HttpWebResponse httpWebRespones = null; |
| 74 | try |
| 75 | { |
| 76 | System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; |
| 77 | HttpWebRequest httpWebRequest = WebRequest.Create(githubUrl) as HttpWebRequest; |
| 78 | httpWebRequest.Timeout = 60 * 1000; |
| 79 | httpWebRequest.ReadWriteTimeout = 60000; |
| 80 | httpWebRequest.AllowAutoRedirect = true; |
| 81 | httpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"; |
| 82 | httpWebRespones = (HttpWebResponse)httpWebRequest.GetResponse(); |
| 83 | |
| 84 | |
| 85 | using (Stream stream = httpWebRespones.GetResponseStream()) |
| 86 | { |
| 87 | List<byte> lst = new List<byte>(); |
| 88 | int nRead = 0; |
| 89 | while ((nRead = stream.ReadByte()) != -1) lst.Add((byte)nRead); |
| 90 | byte[] bodyBytes = lst.ToArray(); |
| 91 | |
| 92 | string body = Encoding.UTF8.GetString(bodyBytes, 0, bodyBytes.Length); |
| 93 | |
| 94 | var data = JsonConvert.DeserializeObject<GithubModel>(body); |
| 95 | Info.IsPre = data.prerelease; |
| 96 | Info.Title = data.name; |
| 97 | Info.Version = data.tag_name; |
| 98 | Info.DownloadUrl = data.assets[0].browser_download_url; |
| 99 | Info.HtmlUrl = data.html_url; |
| 100 | return Info; |
| 101 | } |
| 102 | |
| 103 | } |
| 104 | catch (Exception ec) |
| 105 | { |
| 106 | return null; |
| 107 | } |
| 108 | |
| 109 | }); |
| 110 | return result; |
| 111 | } |
| 112 | } |
| 113 | } |