From d5eaa56b607ff57d7829da8f3886c86c87dc12ea Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Mon, 6 Jun 2022 17:28:38 +0800 Subject: [PATCH] refactor update core url --- v2rayN/v2rayN/Forms/MainForm.cs | 10 ++------ v2rayN/v2rayN/Global.cs | 1 + v2rayN/v2rayN/Handler/LazyConfig.cs | 12 +++++++++ v2rayN/v2rayN/Handler/UpdateHandle.cs | 36 ++++++--------------------- v2rayN/v2rayN/Mode/CoreInfo.cs | 4 +++ 5 files changed, 26 insertions(+), 37 deletions(-) diff --git a/v2rayN/v2rayN/Forms/MainForm.cs b/v2rayN/v2rayN/Forms/MainForm.cs index bbc7b6a5..69740ec9 100644 --- a/v2rayN/v2rayN/Forms/MainForm.cs +++ b/v2rayN/v2rayN/Forms/MainForm.cs @@ -1314,10 +1314,7 @@ namespace v2rayN.Forms menuExit_Click(null, null); } }; - Task.Run(() => - { - (new UpdateHandle()).CheckUpdateGuiN(config, _updateUI); - }); + (new UpdateHandle()).CheckUpdateGuiN(config, _updateUI); } private void tsbCheckUpdateCore_Click(object sender, EventArgs e) @@ -1350,10 +1347,7 @@ namespace v2rayN.Forms AppendText(false, ResUI.MsgUpdateV2rayCoreSuccessfully); } }; - Task.Run(() => - { - (new UpdateHandle()).CheckUpdateCore(type, config, _updateUI); - }); + (new UpdateHandle()).CheckUpdateCore(type, config, _updateUI); } private void tsbCheckUpdateGeo_Click(object sender, EventArgs e) diff --git a/v2rayN/v2rayN/Global.cs b/v2rayN/v2rayN/Global.cs index f5fa4cf2..a6bc8353 100644 --- a/v2rayN/v2rayN/Global.cs +++ b/v2rayN/v2rayN/Global.cs @@ -18,6 +18,7 @@ namespace v2rayN public const string clashMetaCoreUrl = "https://github.com/MetaCubeX/Clash.Meta/releases"; public const string hysteriaCoreUrl = "https://github.com/HyNetwork/hysteria/releases"; public const string naiveproxyCoreUrl = "https://github.com/klzgrad/naiveproxy/releases"; + public const string geoUrl = "https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/{0}.dat"; diff --git a/v2rayN/v2rayN/Handler/LazyConfig.cs b/v2rayN/v2rayN/Handler/LazyConfig.cs index 26625455..42dbb990 100644 --- a/v2rayN/v2rayN/Handler/LazyConfig.cs +++ b/v2rayN/v2rayN/Handler/LazyConfig.cs @@ -66,12 +66,22 @@ namespace v2rayN.Handler { coreInfos = new List(); + coreInfos.Add(new CoreInfo + { + coreType = ECoreType.v2rayN, + coreUrl = Global.NUrl, + coreLatestUrl = Global.NUrl + "/latest", + coreDownloadUrl = Global.NUrl + "/download/{0}/v2rayN.zip", + }); + coreInfos.Add(new CoreInfo { coreType = ECoreType.v2fly, coreExes = new List { "wv2ray", "v2ray" }, arguments = "", coreUrl = Global.v2flyCoreUrl, + coreLatestUrl = Global.v2flyCoreUrl + "/latest", + coreDownloadUrl = Global.v2flyCoreUrl + "/download/{0}/v2ray-windows-{1}.zip", match = "V2Ray" }); @@ -81,6 +91,8 @@ namespace v2rayN.Handler coreExes = new List { "xray" }, arguments = "", coreUrl = Global.xrayCoreUrl, + coreLatestUrl = Global.xrayCoreUrl + "/latest", + coreDownloadUrl = Global.xrayCoreUrl + "/download/{0}/Xray-windows-{1}.zip", match = "Xray" }); diff --git a/v2rayN/v2rayN/Handler/UpdateHandle.cs b/v2rayN/v2rayN/Handler/UpdateHandle.cs index 630225d2..7ddf3f7f 100644 --- a/v2rayN/v2rayN/Handler/UpdateHandle.cs +++ b/v2rayN/v2rayN/Handler/UpdateHandle.cs @@ -31,14 +31,6 @@ namespace v2rayN.Handler } } - private readonly string nLatestUrl = Global.NUrl + "/latest"; - private const string nUrl = Global.NUrl + "/download/{0}/v2rayN.zip"; - private readonly string v2flyCoreLatestUrl = Global.v2flyCoreUrl + "/latest"; - private const string v2flyCoreUrl = Global.v2flyCoreUrl + "/download/{0}/v2ray-windows-{1}.zip"; - private readonly string xrayCoreLatestUrl = Global.xrayCoreUrl + "/latest"; - private const string xrayCoreUrl = Global.xrayCoreUrl + "/download/{0}/Xray-windows-{1}.zip"; - private const string geoUrl = "https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/{0}.dat"; - public void CheckUpdateGuiN(Config config, Action update) { _config = config; @@ -244,7 +236,7 @@ namespace v2rayN.Handler { _config = config; _updateFunc = update; - var url = string.Format(geoUrl, geoName); + var url = string.Format(Global.geoUrl, geoName); DownloadHandle downloadHandle = null; if (downloadHandle == null) @@ -306,23 +298,8 @@ namespace v2rayN.Handler { try { - string url; - if (type == ECoreType.v2fly) - { - url = v2flyCoreLatestUrl; - } - else if (type == ECoreType.Xray) - { - url = xrayCoreLatestUrl; - } - else if (type == ECoreType.v2rayN) - { - url = nLatestUrl; - } - else - { - throw new ArgumentException("Type"); - } + var coreInfo = LazyConfig.Instance.GetCoreInfo(type); + string url = coreInfo.coreLatestUrl; var result = await (new DownloadHandle()).UrlRedirectAsync(url, true); if (!Utils.IsNullOrEmpty(result)) @@ -396,6 +373,7 @@ namespace v2rayN.Handler try { string version = redirectUrl.Substring(redirectUrl.LastIndexOf("/", StringComparison.Ordinal) + 1); + var coreInfo = LazyConfig.Instance.GetCoreInfo(type); string curVersion; string message; @@ -405,20 +383,20 @@ namespace v2rayN.Handler curVersion = "v" + getCoreVersion(type); message = string.Format(ResUI.IsLatestCore, curVersion); string osBit = Environment.Is64BitProcess ? "64" : "32"; - url = string.Format(v2flyCoreUrl, version, osBit); + url = string.Format(coreInfo.coreDownloadUrl, version, osBit); } else if (type == ECoreType.Xray) { curVersion = "v" + getCoreVersion(type); message = string.Format(ResUI.IsLatestCore, curVersion); string osBit = Environment.Is64BitProcess ? "64" : "32"; - url = string.Format(xrayCoreUrl, version, osBit); + url = string.Format(coreInfo.coreDownloadUrl, version, osBit); } else if (type == ECoreType.v2rayN) { curVersion = FileVersionInfo.GetVersionInfo(Utils.GetExePath()).FileVersion.ToString(); message = string.Format(ResUI.IsLatestN, curVersion); - url = string.Format(nUrl, version); + url = string.Format(coreInfo.coreDownloadUrl, version); } else { diff --git a/v2rayN/v2rayN/Mode/CoreInfo.cs b/v2rayN/v2rayN/Mode/CoreInfo.cs index 8b2726b2..86391c7d 100644 --- a/v2rayN/v2rayN/Mode/CoreInfo.cs +++ b/v2rayN/v2rayN/Mode/CoreInfo.cs @@ -14,6 +14,10 @@ namespace v2rayN.Mode public string coreUrl { get; set; } + public string coreLatestUrl { get; set; } + + public string coreDownloadUrl { get; set; } + public string match { get; set; } } }