From 28aa954f8cd38cf3daf461f00886fff4792c9c69 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Mon, 21 Oct 2024 20:31:19 +0800 Subject: [PATCH] Improved CheckUpdate --- v2rayN/ServiceLib/Services/DownloadService.cs | 1 + v2rayN/ServiceLib/Services/UpdateService.cs | 90 ++++++++++++------- 2 files changed, 57 insertions(+), 34 deletions(-) diff --git a/v2rayN/ServiceLib/Services/DownloadService.cs b/v2rayN/ServiceLib/Services/DownloadService.cs index 40e8718a..db28ea72 100644 --- a/v2rayN/ServiceLib/Services/DownloadService.cs +++ b/v2rayN/ServiceLib/Services/DownloadService.cs @@ -95,6 +95,7 @@ namespace ServiceLib.Services } else { + Error?.Invoke(this, new ErrorEventArgs(new Exception("StatusCode error: " + response.StatusCode))); Logging.SaveLog("StatusCode error: " + url); return null; } diff --git a/v2rayN/ServiceLib/Services/UpdateService.cs b/v2rayN/ServiceLib/Services/UpdateService.cs index 537487d3..179da11c 100644 --- a/v2rayN/ServiceLib/Services/UpdateService.cs +++ b/v2rayN/ServiceLib/Services/UpdateService.cs @@ -35,19 +35,19 @@ namespace ServiceLib.Services }; _updateFunc?.Invoke(false, string.Format(ResUI.MsgStartUpdating, ECoreType.v2rayN)); - var args = await CheckUpdateAsync(downloadHandle, ECoreType.v2rayN, preRelease); - if (args.Success) + var result = await CheckUpdateAsync(downloadHandle, ECoreType.v2rayN, preRelease); + if (result.Success) { _updateFunc?.Invoke(false, string.Format(ResUI.MsgParsingSuccessfully, ECoreType.v2rayN)); - _updateFunc?.Invoke(false, args.Msg); + _updateFunc?.Invoke(false, result.Msg); - url = args.Data?.ToString(); + url = result.Data?.ToString(); fileName = Utils.GetTempPath(Utils.GetGuid()); await downloadHandle.DownloadFileAsync(url, fileName, true, _timeout); } else { - _updateFunc?.Invoke(false, args.Msg); + _updateFunc?.Invoke(false, result.Msg); } } @@ -86,22 +86,22 @@ namespace ServiceLib.Services }; _updateFunc?.Invoke(false, string.Format(ResUI.MsgStartUpdating, type)); - var args = await CheckUpdateAsync(downloadHandle, type, preRelease); - if (args.Success) + var result = await CheckUpdateAsync(downloadHandle, type, preRelease); + if (result.Success) { _updateFunc?.Invoke(false, string.Format(ResUI.MsgParsingSuccessfully, type)); - _updateFunc?.Invoke(false, args.Msg); + _updateFunc?.Invoke(false, result.Msg); - url = args.Data?.ToString(); + url = result.Data?.ToString(); var ext = url.Contains(".tar.gz") ? ".tar.gz" : Path.GetExtension(url); fileName = Utils.GetTempPath(Utils.GetGuid() + ext); await downloadHandle.DownloadFileAsync(url, fileName, true, _timeout); } else { - if (!args.Msg.IsNullOrEmpty()) + if (!result.Msg.IsNullOrEmpty()) { - _updateFunc?.Invoke(false, args.Msg); + _updateFunc?.Invoke(false, result.Msg); } } } @@ -250,24 +250,18 @@ namespace ServiceLib.Services updateFunc?.Invoke(false, string.Format(ResUI.TestMeOutput, time)); } - #region private + #region CheckUpdate private private async Task CheckUpdateAsync(DownloadService downloadHandle, ECoreType type, bool preRelease) { try { - var coreInfo = CoreInfoHandler.Instance.GetCoreInfo(type); - var url = coreInfo?.ReleaseApiUrl; - - var result = await downloadHandle.TryDownloadString(url, true, Global.AppName); - if (Utils.IsNotEmpty(result)) + var result = await GetRemoteVersion(downloadHandle, type, preRelease); + if (!result.Success || result.Data is null) { - return await ParseDownloadUrl(type, result, preRelease); - } - else - { - return new RetResult(false, ""); + return result; } + return await ParseDownloadUrl(type, (SemanticVersion)result.Data); } catch (Exception ex) { @@ -277,9 +271,38 @@ namespace ServiceLib.Services } } - /// - /// 获取Core版本 - /// + private async Task GetRemoteVersion(DownloadService downloadHandle, ECoreType type, bool preRelease) + { + var coreInfo = CoreInfoHandler.Instance.GetCoreInfo(type); + var tagName = string.Empty; + if (preRelease) + { + var url = coreInfo?.ReleaseApiUrl; + var result = await downloadHandle.TryDownloadString(url, true, Global.AppName); + if (Utils.IsNullOrEmpty(result)) + { + return new RetResult(false, ""); + } + + var gitHubReleases = JsonUtils.Deserialize>(result); + var gitHubRelease = preRelease ? gitHubReleases?.First() : gitHubReleases?.First(r => r.Prerelease == false); + tagName = gitHubRelease?.TagName; + //var body = gitHubRelease?.Body; + } + else + { + var url = Path.Combine(coreInfo.Url, "latest"); + var lastUrl = await downloadHandle.UrlRedirectAsync(url, true); + if (lastUrl == null) + { + return new RetResult(false, ""); + } + + tagName = lastUrl?.Split("/tag/").LastOrDefault(); + } + return new RetResult(true, "", new SemanticVersion(tagName)); + } + private async Task GetCoreVersion(ECoreType type) { try @@ -333,15 +356,10 @@ namespace ServiceLib.Services } } - private async Task ParseDownloadUrl(ECoreType type, string gitHubReleaseApi, bool preRelease) + private async Task ParseDownloadUrl(ECoreType type, SemanticVersion version) { try { - var gitHubReleases = JsonUtils.Deserialize>(gitHubReleaseApi); - var gitHubRelease = preRelease ? gitHubReleases?.First() : gitHubReleases?.First(r => r.Prerelease == false); - var version = new SemanticVersion(gitHubRelease?.TagName); - var body = gitHubRelease?.Body; - var coreInfo = CoreInfoHandler.Instance.GetCoreInfo(type); SemanticVersion curVersion; string message; @@ -387,7 +405,7 @@ namespace ServiceLib.Services return new RetResult(false, message); } - return new RetResult(true, body, url); + return new RetResult(true, "", url); } catch (Exception ex) { @@ -405,7 +423,7 @@ namespace ServiceLib.Services if (coreInfo?.CoreType == ECoreType.v2rayN && File.Exists(Path.Combine(Utils.StartupPath(), "wpfgfx_cor3.dll")) && File.Exists(Path.Combine(Utils.StartupPath(), "D3DCompiler_47_cor3.dll")) - ) + ) { return coreInfo?.DownloadUrlWin64?.Replace(".zip", "-SelfContained.zip"); } @@ -430,6 +448,10 @@ namespace ServiceLib.Services return null; } + #endregion CheckUpdate private + + #region Geo private + private async Task UpdateGeoFile(string geoName, Config config, Action updateFunc) { _config = config; @@ -544,6 +566,6 @@ namespace ServiceLib.Services await downloadHandle.DownloadFileAsync(url, tmpFileName, true, _timeout); } - #endregion private + #endregion Geo private } } \ No newline at end of file