private void CheckUpdateCore(ECoreType type) { void _updateUI(bool success, string msg) { AppendText(false, msg); if (success) { CloseV2ray(); string fileName = Utils.GetPath(Utils.GetDownloadFileName(msg)); FileManager.ZipExtractToFile(fileName, config.ignoreGeoUpdateCore ? "geo" : ""); AppendText(false, ResUI.MsgUpdateV2rayCoreSuccessfullyMore); Global.reloadV2ray = true; _ = LoadV2ray(); AppendText(false, ResUI.MsgUpdateV2rayCoreSuccessfully); } }; Task.Run(() => { (new UpdateHandle()).CheckUpdateCore(type, config, _updateUI); }); }
public CoreInfo GetCoreInfo(ECoreType coreType) { if (coreInfos == null) { InitCoreInfo(); } return(coreInfos.Where(t => t.coreType == coreType).FirstOrDefault()); }
/// <summary> /// 获取V2RayCore版本 /// </summary> private string getCoreVersion(ECoreType type) { try { var coreInfo = LazyConfig.Instance.GetCoreInfo(type); string filePath = string.Empty; foreach (string name in coreInfo.coreExes) { string vName = $"{name}.exe"; vName = Utils.GetPath(vName); if (File.Exists(vName)) { filePath = vName; break; } } if (!File.Exists(filePath)) { string msg = string.Format(ResUI.NotFoundCore, @"", ""); //ShowMsg(true, msg); return(""); } Process p = new Process(); p.StartInfo.FileName = filePath; p.StartInfo.Arguments = coreInfo.versionArg; p.StartInfo.WorkingDirectory = Utils.StartupPath(); p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.CreateNoWindow = true; p.StartInfo.StandardOutputEncoding = Encoding.UTF8; p.Start(); p.WaitForExit(5000); string echo = p.StandardOutput.ReadToEnd(); string version = string.Empty; switch (type) { case ECoreType.v2fly: case ECoreType.SagerNet: case ECoreType.Xray: version = Regex.Match(echo, $"{coreInfo.match} ([0-9.]+) \\(").Groups[1].Value; break; case ECoreType.clash: case ECoreType.clash_meta: version = Regex.Match(echo, $"v[0-9.]+").Groups[0].Value; break; } return(version); } catch (Exception ex) { Utils.SaveLog(ex.Message, ex); _updateFunc(false, ex.Message); return(""); } }
public void CheckUpdateCore(ECoreType type, Config config, Action <bool, string> update, bool preRelease) { _config = config; _updateFunc = update; var url = string.Empty; DownloadHandle downloadHandle = null; if (downloadHandle == null) { downloadHandle = new DownloadHandle(); downloadHandle.UpdateCompleted += (sender2, args) => { if (args.Success) { _updateFunc(false, ResUI.MsgDownloadV2rayCoreSuccessfully); _updateFunc(false, ResUI.MsgUnpacking); try { _updateFunc(true, url); } catch (Exception ex) { _updateFunc(false, ex.Message); } } else { _updateFunc(false, args.Msg); } }; downloadHandle.Error += (sender2, args) => { _updateFunc(true, args.GetException().Message); }; } AbsoluteCompleted += (sender2, args) => { if (args.Success) { _updateFunc(false, string.Format(ResUI.MsgParsingSuccessfully, "Core")); url = args.Msg; askToDownload(downloadHandle, url, true); } else { _updateFunc(false, args.Msg); } }; _updateFunc(false, string.Format(ResUI.MsgStartUpdating, "Core")); CheckUpdateAsync(type, preRelease); }
private async void CheckUpdateAsync(ECoreType type) { try { Utils.SetSecurityProtocol(LazyConfig.Instance.GetConfig().enableSecurityProtocolTls13); WebRequestHandler webRequestHandler = new WebRequestHandler { AllowAutoRedirect = false }; if (httpProxyTest() > 0) { int httpPort = _config.GetLocalPort(Global.InboundHttp2); WebProxy webProxy = new WebProxy(Global.Loopback, httpPort); webRequestHandler.Proxy = webProxy; } HttpClient httpClient = new HttpClient(webRequestHandler); 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"); } HttpResponseMessage response = await httpClient.GetAsync(url); if (response.StatusCode.ToString() == "Redirect") { responseHandler(type, response.Headers.Location.ToString()); } else { Utils.SaveLog("StatusCode error: " + url); return; } } catch (Exception ex) { Utils.SaveLog(ex.Message, ex); _updateFunc(false, ex.Message); } }
private void responseHandler(ECoreType type, string redirectUrl) { try { string version = redirectUrl.Substring(redirectUrl.LastIndexOf("/", StringComparison.Ordinal) + 1); var coreInfo = LazyConfig.Instance.GetCoreInfo(type); string curVersion; string message; string url; if (type == ECoreType.v2fly) { curVersion = "v" + getCoreVersion(type); message = string.Format(ResUI.IsLatestCore, curVersion); string osBit = Environment.Is64BitProcess ? "64" : "32"; 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(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(coreInfo.coreDownloadUrl, version); } else { throw new ArgumentException("Type"); } if (curVersion == version) { AbsoluteCompleted?.Invoke(this, new ResultEventArgs(false, message)); return; } AbsoluteCompleted?.Invoke(this, new ResultEventArgs(true, url)); } catch (Exception ex) { Utils.SaveLog(ex.Message, ex); _updateFunc(false, ex.Message); } }
/// <summary> /// 获取V2RayCore版本 /// </summary> private string getCoreVersion(ECoreType type) { try { var core = string.Empty; var match = string.Empty; if (type == ECoreType.v2fly) { core = "v2ray.exe"; match = "V2Ray"; } else if (type == ECoreType.Xray) { core = "xray.exe"; match = "Xray"; } string filePath = Utils.GetPath(core); if (!File.Exists(filePath)) { string msg = string.Format(ResUI.NotFoundCore, @""); //ShowMsg(true, msg); return(""); } Process p = new Process(); p.StartInfo.FileName = filePath; p.StartInfo.Arguments = "-version"; p.StartInfo.WorkingDirectory = Utils.StartupPath(); p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.CreateNoWindow = true; p.StartInfo.StandardOutputEncoding = Encoding.UTF8; p.Start(); p.WaitForExit(5000); string echo = p.StandardOutput.ReadToEnd(); string version = Regex.Match(echo, $"{match} ([0-9.]+) \\(").Groups[1].Value; return(version); } catch (Exception ex) { Utils.SaveLog(ex.Message, ex); _updateFunc(false, ex.Message); return(""); } }
private async void CheckUpdateAsync(ECoreType type) { 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 result = await(new DownloadHandle()).UrlRedirectAsync(url, true); if (!Utils.IsNullOrEmpty(result)) { responseHandler(type, result); } else { Utils.SaveLog("StatusCode error: " + url); return; } } catch (Exception ex) { Utils.SaveLog(ex.Message, ex); _updateFunc(false, ex.Message); } }
private async void CheckUpdateAsync(ECoreType type, bool preRelease) { try { var coreInfo = LazyConfig.Instance.GetCoreInfo(type); string url = coreInfo.coreReleaseApiUrl; var result = await(new DownloadHandle()).DownloadStringAsync(url, true, ""); if (!Utils.IsNullOrEmpty(result)) { responseHandler(type, result, preRelease); } else { Utils.SaveLog("StatusCode error: " + url); return; } } catch (Exception ex) { Utils.SaveLog(ex.Message, ex); _updateFunc(false, ex.Message); } }
private void responseHandler(ECoreType type, string gitHubReleaseApi, bool preRelease) { try { var gitHubReleases = Utils.FromJson <List <GitHubRelease> >(gitHubReleaseApi); string version; if (preRelease) { version = gitHubReleases !.First().TagName; } else { version = gitHubReleases !.First(r => r.Prerelease == false).TagName; } var coreInfo = LazyConfig.Instance.GetCoreInfo(type); string curVersion; string message; string url; switch (type) { case ECoreType.v2fly: case ECoreType.SagerNet: case ECoreType.Xray: { curVersion = "v" + getCoreVersion(type); message = string.Format(ResUI.IsLatestCore, curVersion); string osBit = Environment.Is64BitProcess ? "64" : "32"; url = string.Format(coreInfo.coreDownloadUrl64, version, osBit); break; } case ECoreType.clash: case ECoreType.clash_meta: { curVersion = getCoreVersion(type); message = string.Format(ResUI.IsLatestCore, curVersion); if (Environment.Is64BitProcess) { url = string.Format(coreInfo.coreDownloadUrl64, version); } else { url = string.Format(coreInfo.coreDownloadUrl32, version); } break; } case ECoreType.v2rayN: { curVersion = FileVersionInfo.GetVersionInfo(Utils.GetExePath()).FileVersion.ToString(); message = string.Format(ResUI.IsLatestN, curVersion); url = string.Format(coreInfo.coreDownloadUrl64, version); break; } default: throw new ArgumentException("Type"); } if (curVersion == version) { AbsoluteCompleted?.Invoke(this, new ResultEventArgs(false, message)); return; } AbsoluteCompleted?.Invoke(this, new ResultEventArgs(true, url)); } catch (Exception ex) { Utils.SaveLog(ex.Message, ex); _updateFunc(false, ex.Message); } }