Exemplo n.º 1
0
        private async Task <bool> DoUpdate()
        {
            ChangeProgress(999);
            ChangeText(I18N.GetString("Get latest version..."));
            var newVersion = await UpdateChecker.GetCoreVersion();

            string proxy = null;

            if (string.IsNullOrEmpty(newVersion))
            {
                proxy = Microsoft.VisualBasic.Interaction.InputBox(I18N.GetString("We need a proxy to download v2ray core"), "Yo", "http://127.0.0.1:1080");
                if (Uri.IsWellFormedUriString(proxy, UriKind.Absolute))
                {
                    newVersion = await UpdateChecker.GetCoreVersion(proxy);
                }
            }

            if (string.IsNullOrEmpty(newVersion))
            {
                Process.Start(UpdateChecker.V2RAY_URL);
                return(false);
            }

            ChangeText(I18N.GetString("Upgrade {0} to {1} ...", V2Ray.Version?.ToString() ?? "0.0.0", newVersion));
            WebClient webClient = new WebClient();

            webClient.Headers.Add(HttpRequestHeader.UserAgent, UA);
            if (!string.IsNullOrEmpty(proxy) && Uri.IsWellFormedUriString(proxy, UriKind.Absolute))
            {
                webClient.Proxy = new WebProxy(new Uri(proxy));
            }
            cts.Token.Register(() => webClient.CancelAsync());
            webClient.DownloadProgressChanged += (s, e) =>
            {
                ChangeProgress(e.ProgressPercentage);
                //ChangeText(newVersion + I18N.GetString("Downloading...") + $" {e.ProgressPercentage}%");
            };
            var plat        = Environment.Is64BitOperatingSystem ? "64" : "32";
            var downloadURL = $"https://github.wuyanzheshui.workers.dev/v2fly/v2ray-core/releases/download/v{newVersion}/v2ray-windows-{plat}.zip";

            ChangeTitle(I18N.GetString("Sit back and relax") + " " + I18N.GetString("Upgrade {0} to {1} ...", V2Ray.Version?.ToString() ?? "0.0.0", newVersion));
            ChangeText(I18N.GetString("Downloading file from {0}, You can download it manually and extract to same folder.", downloadURL));
            try
            {
                //var cl = await GetUrlLen(downloadURL);
                //if (cl > 0)
                //{
                //    if (IsIDMInstalled(out var idm))
                //        await DownloadIDM(idm, downloadURL, fileName);
                //    if (GetFileLen(filePath) != cl)
                //    {
                //        File.Delete(filePath);
                //        cts.CancelAfter(30000);
                //        await webClient.DownloadFileTaskAsync(downloadURL, filePath);
                //    }
                //}
                //else
                //{
                cts.CancelAfter(30000);
                await webClient.DownloadFileTaskAsync(downloadURL, filePath);

                //}
            }
            catch (Exception e)
            {
                Logging.LogUsefulException(e);
                if (File.Exists(filePath))
                {
                    File.Delete(filePath);
                }
            }
            if (!File.Exists(filePath))
            {
                downloadURL = $"https://github.com/v2fly/v2ray-core/releases/download/v{newVersion}/v2ray-windows-{plat}.zip";
                ChangeText(I18N.GetString("Downloading file from {0}, You can download it manually and extract to same folder.", downloadURL));
                try
                {
                    var cl = await GetUrlLen(downloadURL);

                    if (cl > 0)
                    {
                        if (IsIDMInstalled(out var idm))
                        {
                            await DownloadIDM(idm, downloadURL, fileName);
                        }

                        if (GetFileLen(filePath) != cl)
                        {
                            File.Delete(filePath);
                            await webClient.DownloadFileTaskAsync(downloadURL, filePath);
                        }
                    }
                    else
                    {
                        await webClient.DownloadFileTaskAsync(downloadURL, filePath);
                    }
                }
                catch (Exception e)
                {
                    Logging.LogUsefulException(e);
                    if (File.Exists(filePath))
                    {
                        File.Delete(filePath);
                    }
                }
            }
            if (!File.Exists(filePath))
            {
                if (MessageBox.Show(I18N.GetString("Download fail! Copy link?"), "Error", MessageBoxButtons.YesNo, MessageBoxIcon.Error) == DialogResult.Yes)
                {
                    Clipboard.SetText(downloadURL);
                }
                return(false);
            }
            ChangeProgress(100);
            ChangeText(newVersion + I18N.GetString("Extracting..."));
            try
            {
                using (ZipArchive archive = ZipFile.OpenRead(filePath))
                {
                    var additional = Path.Combine(Global.AppPath, "Additional");
                    if (!Directory.Exists(additional))
                    {
                        Directory.CreateDirectory(additional);
                    }
                    V2RayRunner.KillAll();
                    foreach (ZipArchiveEntry entry in archive.Entries)
                    {
                        if (entry.Length == 0)
                        {
                            continue;
                        }
                        if (entry.FullName.Contains('/'))
                        {
                            continue;//directory, useless
                        }
                        if (entry.FullName == "v2ray.exe" || entry.FullName == "v2ctl.exe")
                        {
                            entry.ExtractToFile(Path.Combine(Global.AppPath, entry.FullName), true);
                        }
                        else
                        {
                            entry.ExtractToFile(Path.Combine(additional, entry.FullName), true);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Logging.LogUsefulException(e);
                return(false);
            }
            finally
            {
                File.Delete(filePath);
            }
            return(true);
        }
Exemplo n.º 2
0
        private async Task DoUpdate()
        {
            ServicePointManager.Expect100Continue = true;
            ServicePointManager.SecurityProtocol  = SecurityProtocolType.Tls12;//3072
            ChangeProgress(999);
            ChangeText(I18N.GetString("Get latest version..."));
            var newVersion = await GetVersion();

            string proxy = null;

            if (string.IsNullOrEmpty(newVersion))
            {
                proxy = Microsoft.VisualBasic.Interaction.InputBox(I18N.GetString("We need a proxy to download v2ray core"), "Yo", "http://127.0.0.1:1080");
                if (Uri.IsWellFormedUriString(proxy, UriKind.Absolute))
                {
                    newVersion = await GetVersion(proxy);
                }
            }

            if (string.IsNullOrEmpty(newVersion))
            {
                System.Diagnostics.Process.Start(V2RAY_URL);
                GoodBye(DialogResult.Abort);
                return;
            }

            ChangeText(I18N.GetString("Upgrade {0} to {1} ...", V2Ray.Version?.ToString() ?? "0.0.0", newVersion));
            WebClient webClient = new WebClient();

            if (!string.IsNullOrEmpty(proxy) && Uri.IsWellFormedUriString(proxy, UriKind.Absolute))
            {
                webClient.Proxy = new WebProxy(new Uri(proxy));
            }
            webClient.DownloadProgressChanged += (s, e) =>
            {
                ChangeProgress(e.ProgressPercentage);
                //ChangeText(newVersion + I18N.GetString("Downloading...") + $" {e.ProgressPercentage}%");
            };
            var fileName    = Utils.GetTempPath(Guid.NewGuid().ToString("N"));
            var downloadURL = $"https://github.com/v2ray/v2ray-core/releases/download/v{newVersion}/v2ray-windows-{(Environment.Is64BitOperatingSystem ? "64" : "32")}.zip";

            ChangeText(I18N.GetString("Downloading file from {0}, You can download it manually and extract to same folder.", downloadURL));
            await webClient.DownloadFileTaskAsync(downloadURL, fileName);

            ChangeProgress(100);
            ChangeText(newVersion + I18N.GetString("Extracting..."));
            try
            {
                using (ZipArchive archive = ZipFile.OpenRead(fileName))
                {
                    var additional = Path.Combine(Global.AppPath, "Additional");
                    if (!Directory.Exists(additional))
                    {
                        Directory.CreateDirectory(additional);
                    }
                    V2RayRunner.KillAll();
                    foreach (ZipArchiveEntry entry in archive.Entries)
                    {
                        if (entry.Length == 0)
                        {
                            continue;
                        }
                        if (entry.FullName.Contains('/'))
                        {
                            continue;//directory, useless
                        }
                        if (entry.FullName == "v2ray.exe" || entry.FullName == "v2ctl.exe")
                        {
                            entry.ExtractToFile(Path.Combine(Global.AppPath, entry.FullName), true);
                        }
                        else
                        {
                            entry.ExtractToFile(Path.Combine(additional, entry.FullName), true);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Logging.LogUsefulException(e);
                GoodBye(DialogResult.Abort);
                return;
            }
            finally
            {
                File.Delete(fileName);
            }
            GoodBye(DialogResult.OK);
        }