diff --git a/v2rayN/v2rayN/Base/DownloaderHelper.cs b/v2rayN/v2rayN/Base/DownloaderHelper.cs index 15b27feb..4b5a5183 100644 --- a/v2rayN/v2rayN/Base/DownloaderHelper.cs +++ b/v2rayN/v2rayN/Base/DownloaderHelper.cs @@ -1,4 +1,5 @@ using Downloader; +using System.IO; using System.Net; namespace v2rayN.Base @@ -85,5 +86,72 @@ namespace v2rayN.Base downloader = null; downloadOpt = null; } + + public async Task DownloadFileAsync(IWebProxy webProxy, string url, string fileName, IProgress progress, int timeout) + { + if (string.IsNullOrEmpty(url)) + { + throw new ArgumentNullException("url"); + } + if (string.IsNullOrEmpty(fileName)) + { + throw new ArgumentNullException("fileName"); + } + if (File.Exists(fileName)) + { + File.Delete(fileName); + } + + var cancellationToken = new CancellationTokenSource(); + cancellationToken.CancelAfter(timeout * 1000); + + var downloadOpt = new DownloadConfiguration() + { + Timeout = timeout * 1000, + MaxTryAgainOnFailover = 2, + RequestConfiguration = + { + Timeout= timeout * 1000, + Proxy = webProxy + } + }; + + var progressPercentage = 0; + var hasValue = false; + var downloader = new DownloadService(downloadOpt); + downloader.DownloadStarted += (sender, value) => + { + if (progress != null) + { + progress.Report(0); + } + }; + downloader.DownloadProgressChanged += (sender, value) => + { + hasValue = true; + var percent = (int)value.ProgressPercentage;// Convert.ToInt32((totalRead * 1d) / (total * 1d) * 100); + if (progressPercentage != percent && percent % 10 == 0) + { + progressPercentage = percent; + progress.Report(percent); + } + }; + downloader.DownloadFileCompleted += (sender, value) => + { + if (progress != null) + { + if (hasValue && value.Error == null) + { + progress.Report(101); + } + } + }; + + await downloader.DownloadFileTaskAsync(url, fileName, cancellationToken: cancellationToken.Token); + + downloader.Dispose(); + downloader = null; + downloadOpt = null; + } } } \ No newline at end of file diff --git a/v2rayN/v2rayN/Handler/DownloadHandle.cs b/v2rayN/v2rayN/Handler/DownloadHandle.cs index bbba51e5..ab18e1bf 100644 --- a/v2rayN/v2rayN/Handler/DownloadHandle.cs +++ b/v2rayN/v2rayN/Handler/DownloadHandle.cs @@ -70,11 +70,6 @@ namespace v2rayN.Handler Utils.SetSecurityProtocol(LazyConfig.Instance.GetConfig().enableSecurityProtocolTls13); UpdateCompleted?.Invoke(this, new ResultEventArgs(false, ResUI.Downloading)); - var client = new HttpClient(new SocketsHttpHandler() - { - Proxy = GetWebProxy(blProxy) - }); - var progress = new Progress(); progress.ProgressChanged += (sender, value) => { @@ -85,12 +80,11 @@ namespace v2rayN.Handler } }; - var cancellationToken = new CancellationTokenSource(); - _ = HttpClientHelper.GetInstance().DownloadFileAsync(client, - url, - Utils.GetTempPath(Utils.GetDownloadFileName(url)), - progress, - cancellationToken.Token); + _ = DownloaderHelper.Instance.DownloadFileAsync(GetWebProxy(blProxy), + url, + Utils.GetTempPath(Utils.GetDownloadFileName(url)), + progress, + downloadTimeout); } catch (Exception ex) {