using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics.CodeAnalysis; using System.Drawing; using System.Globalization; using System.IO; using System.Linq; using System.Net; using System.Net.Http; using System.Runtime.InteropServices; using System.Security.Cryptography; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using Aliyun.OSS; using Aliyun.OSS.Common; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System.Collections.Concurrent; using System.Diagnostics; using System.Configuration; namespace CheckDownload { public partial class Update : Form { // MD5文件名称 private const string Md5File = "md5.json"; // 阿里云OSS访问地址 private const string OssEndpoint = "oss-cn-hongkong.aliyuncs.com"; // 阿里云OSS存储空间名称 private const string OssBucketName = "suwin-oss"; // 阿里云OSS访问密钥ID private const string OssAccessKeyId = "LTAI5tCwRcL5LUgyHB2j7w82"; // 阿里云OSS访问密钥Secret private const string OssAccessKeySecret = "7ClQns3wz6psmIp9T2OfuEn3tpzrCK"; // 123盘鉴权密钥 private const string OneDriveAuthKey = "6SwdpWdSJuJRSh"; // 123盘UID private const string OneDriveUid = "1826795402"; // 123盘路径(不包含域名)- 修改此处即可同时生效于主备域名 private const string OneDrivePath = "/1826795402/KeyAuth"; // 123盘主域名 private const string OneDriveMainDomain = "vip.123pan.cn"; // 123盘备用域名 private const string OneDriveBackupDomain = "vip.123yx.com"; // 123盘鉴权有效时间(秒) private const int OneDriveAuthTimeout = 600; // 下载流超时时间(毫秒) private const int DownloadStreamTimeoutMs = 60000; // 网络优化: 静态HttpClient实例,避免套接字耗尽 private static readonly HttpClient _httpClient = new HttpClient { Timeout = TimeSpan.FromSeconds(20) }; // OSS客户端:仅初始化一次,避免频繁创建导致内存占用过高 private static readonly ClientConfiguration _ossClientConfig = new ClientConfiguration { ConnectionTimeout = 20000, // 20 seconds MaxErrorRetry = 0 }; private static readonly OssClient _ossClient = new OssClient(OssEndpoint, OssAccessKeyId, OssAccessKeySecret, _ossClientConfig); // 网络优化: 备用DNS服务列表,提高解析成功率 private static readonly List _dnsServers = new List { "223.5.5.5", "119.29.29.29" }; // 最大并发下载数量 private static readonly int MaxConcurrentDownloads = int.TryParse(ConfigurationManager.AppSettings["MaxConcurrentDownloads"], out var mcd) ? mcd : 4; // 最大下载重试次数 private static readonly int MaxDownloadRetries = int.TryParse(ConfigurationManager.AppSettings["MaxDownloadRetries"], out var mdr) ? mdr : 2; // 用于存储下载的文件数据 private Dictionary _downloadedFiles = new Dictionary(); // 已完成的下载数量 private int _completedCount = 0; // 总下载数量 private int _totalCount = 0; // 已下载总字节数 private long _totalDownloadedBytes = 0; // 下载总计时器 private Stopwatch _overallStopwatch; // 已下载速度字节数 private long _speedDownloadedBytes = 0; // 更新计数显示(线程安全) private void UpdateCountBox(int completed, int total) { string text = total > 0 ? $"{completed}/{total}" : ""; if (this.InvokeRequired) { this.Invoke((MethodInvoker)delegate { Count_Box.Text = text; }); } else { Count_Box.Text = text; } } // 更新文件大小和速度显示(线程安全) private void UpdateSizeBox(string text) { if (this.InvokeRequired) { this.Invoke((MethodInvoker)delegate { Size_Box.Text = text; }); } else { Size_Box.Text = text; } } // 累计已下载字节并更新Size_Box private void AddDownloadedBytes(long bytes, bool countForSpeed) { long total = Interlocked.Add(ref _totalDownloadedBytes, bytes); if (countForSpeed) { Interlocked.Add(ref _speedDownloadedBytes, bytes); } if (_overallStopwatch == null) return; double mbTotal = total / 1024.0 / 1024.0; double mbSpeed = (_speedDownloadedBytes / 1024.0 / 1024.0) / Math.Max(0.1, _overallStopwatch.Elapsed.TotalSeconds); UpdateSizeBox($"{mbTotal:F1}MB/{mbSpeed:F1}MB/s"); } // 临时文件夹路径 private string _tempDirectory; // 基准目录路径(用于文件更新的目标目录) private string _baseDirectory; /// /// 初始化窗体 /// public Update() { InitializeComponent(); ConfigureProgressBar(); InitializeTempDirectory(); } /// /// 配置进度条的基本属性 /// private void ConfigureProgressBar() { Update_Pro.Minimum = 0; Update_Pro.Maximum = 100; Update_Pro.Value = 0; Update_Pro.Step = 1; } /// /// 更新进度条的值(线程安全) /// /// 进度百分比 private void UpdateProgressValue(int percentage) { if (percentage < 0) percentage = 0; if (percentage > 100) percentage = 100; if (this.InvokeRequired) { this.Invoke((MethodInvoker)delegate { Update_Pro.Value = Math.Min(Math.Max(percentage, Update_Pro.Minimum), Update_Pro.Maximum); }); } else { Update_Pro.Value = Math.Min(Math.Max(percentage, Update_Pro.Minimum), Update_Pro.Maximum); } } /// /// 更新状态显示文本(线程安全) /// /// 状态消息 private void UpdateStatus(string message) { if (this.InvokeRequired) { this.Invoke((MethodInvoker)delegate { Status_Box.Text = message; }); } else { Status_Box.Text = message; } } /// /// 窗体加载事件处理,启动更新流程 /// public async void Update_Load(object sender, EventArgs e) { PositionFormToBottomRight(); try { await UpdateFile(); } finally { } } /// /// 将窗体定位到屏幕右下角 /// private void PositionFormToBottomRight() { Rectangle workingArea = Screen.GetWorkingArea(this); this.Location = new Point(workingArea.Right - this.Width, workingArea.Bottom - this.Height); } /// /// 主要的文件更新流程 /// private async Task UpdateFile() { try { CleanupNewFiles(); UpdateStatus("下载MD5..."); string tempFilePath = Path.Combine(_tempDirectory, Md5File); // 使用新的带123盘的下载方法 if (!await DownloadMd5FileWithFallback(Md5File, tempFilePath)) { UpdateStatus("MD5下载失败"); await Task.Delay(3000); this.Close(); return; } var onlineData = ReadOnlineMd5File(tempFilePath); if (!ValidateOnlineData(onlineData.Version, onlineData.Md5, onlineData.Data)) { UpdateStatus("MD5文件无效"); await Task.Delay(3000); this.Close(); return; } // 基于md5.json内容智能检测基准目录 InitializeBaseDirectoryFromMd5Data(onlineData.Data); UpdateStatus("比较文件..."); var compareResult = CompareMd5Data(onlineData.Data); if (compareResult.Count == 0) { UpdateProgressValue(100); UpdateCountBox(0, 0); UpdateSizeBox(""); // 无需更新时清理临时文件夹 CleanupTempDirectory(); UpdateStatus("无需更新"); await Task.Delay(2000); this.Close(); return; } UpdateStatus("下载文件..."); // 根据路径长度排序,优先下载小文件/浅层文件,可加其它排序规则 var orderedFileList = compareResult.OrderBy(k => k.Key.Length) .ToDictionary(k => k.Key, v => v.Value); _totalCount = orderedFileList.Count; _completedCount = 0; _totalDownloadedBytes = 0; _speedDownloadedBytes = 0; _overallStopwatch = Stopwatch.StartNew(); UpdateSizeBox("0MB/0MB/s"); UpdateCountBox(_completedCount, _totalCount); _downloadedFiles.Clear(); var failedFiles = new ConcurrentDictionary(); await PerformDownloads(orderedFileList, failedFiles); if (!failedFiles.IsEmpty) { UpdateStatus($"重试 {failedFiles.Count} 个失败文件..."); var stillFailing = await RetryFailedFilesAsync(new Dictionary(failedFiles)); if (stillFailing.Any()) { UpdateStatus($"{stillFailing.Count} 个文件重试失败"); } } if (_completedCount == 0 && orderedFileList.Count > 0) { throw new Exception("全部文件下载失败"); } await VerifyAndSaveAllFiles(); // 校验和保存成功后清理临时目录 CleanupTempDirectory(); // 显示完成状态并退出 UpdateStatus("更新完成"); UpdateCountBox(0, 0); UpdateSizeBox(""); await Task.Delay(3000); this.Close(); return; } catch (Exception ex) { UpdateStatus($"更新失败: {ex.Message.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).FirstOrDefault()}"); await Task.Delay(3000); this.Close(); } } /// /// 清理旧的更新文件,删除所有.new后缀的临时文件 /// private void CleanupNewFiles() { try { var newFiles = Directory.GetFiles(_baseDirectory, "*.new", SearchOption.AllDirectories); if (newFiles.Length > 0) { UpdateStatus("清理旧文件..."); foreach (var file in newFiles) { try { File.Delete(file); } catch { } } } } catch { } } /// /// 读取在线MD5文件并解析JSON内容,提取版本信息和文件清单数据 /// /// MD5文件的本地路径 /// 包含版本号、MD5值和文件数据的元组 private (string Version, string Md5, JObject Data) ReadOnlineMd5File(string filePath) { try { using (var reader = new StreamReader(filePath)) { string json = reader.ReadToEnd(); var parsed = JObject.Parse(json); string version = parsed["version"]?.ToString(); var data = (JObject)parsed["data"]; string jsonMd5 = CalculateMD5(json); return (version, jsonMd5, data); } } catch (OssException) { UpdateStatus("读取MD5失败"); return (null, null, null); } catch (JsonException) { UpdateStatus("读取MD5失败"); return (null, null, null); } } /// /// 验证在线MD5数据的完整性和有效性 /// /// 版本号字符串 /// 文件的MD5哈希值 /// 包含文件信息的JSON对象 /// 如果所有必需字段都有效则返回true,否则返回false private bool ValidateOnlineData(string version, string md5, JObject data) { if (string.IsNullOrEmpty(version) || string.IsNullOrEmpty(md5) || data == null) { UpdateStatus("MD5文件无效"); return false; } return true; } /// /// 递归比较本地文件和在线MD5数据,识别需要更新的文件 /// /// 在线文件清单的JSON数据 /// 当前递归路径,用于构建完整的文件路径 /// 包含需要更新的文件路径和对应MD5值的字典 private Dictionary CompareMd5Data(JObject onlineData, string currentPath = "") { var differences = new Dictionary(); foreach (var onlineProperty in onlineData.Properties()) { string key = onlineProperty.Name; JToken onlineValue = onlineProperty.Value; string relativePath = string.IsNullOrEmpty(currentPath) ? key : Path.Combine(currentPath, key); string localFullPath = Path.Combine(_baseDirectory, relativePath); if (onlineValue.Type == JTokenType.String) { string expectedMd5 = onlineValue.ToString(); if (!File.Exists(localFullPath)) { if (!differences.ContainsKey(relativePath)) { differences[relativePath] = expectedMd5; } } else { string localMd5 = CalculateMD5FromFile(localFullPath); if (!localMd5.Equals(expectedMd5, StringComparison.OrdinalIgnoreCase)) { if (!differences.ContainsKey(relativePath)) { differences[relativePath] = expectedMd5; } } } } else if (onlineValue.Type == JTokenType.Object) { var subDifferences = CompareMd5Data((JObject)onlineValue, relativePath); foreach (var diff in subDifferences) { if (!differences.ContainsKey(diff.Key)) { differences[diff.Key] = diff.Value; } } } } return differences; } /// /// 批量下载文件列表并进行MD5验证,支持重试机制 /// /// 包含文件路径和期望MD5值的字典 private async Task DownloadAndVerifyFiles(Dictionary fileList) { _totalCount = fileList.Count; _completedCount = 0; _totalDownloadedBytes = 0; _speedDownloadedBytes = 0; _overallStopwatch = Stopwatch.StartNew(); UpdateSizeBox("0MB/0MB/s"); UpdateCountBox(_completedCount, _totalCount); _downloadedFiles.Clear(); var failedFiles = new ConcurrentDictionary(); await PerformDownloads(fileList, failedFiles); if (!failedFiles.IsEmpty) { UpdateStatus($"重试 {failedFiles.Count} 个失败文件..."); var stillFailing = await RetryFailedFilesAsync(new Dictionary(failedFiles)); if (stillFailing.Any()) { UpdateStatus($"{stillFailing.Count} 个文件重试失败"); } } if (_completedCount == 0 && fileList.Count > 0) { throw new Exception("所有文件下载失败。"); } await VerifyAndSaveAllFiles(); } /// /// 使用信号量控制并发数量,执行多个文件的并发下载任务 /// /// 包含文件路径和MD5值的下载任务字典 /// 用于收集下载失败文件的线程安全集合 private async Task PerformDownloads(IDictionary filesToDownload, ConcurrentDictionary failedDownloads) { var semaphore = new SemaphoreSlim(MaxConcurrentDownloads); var downloadTasks = filesToDownload.Select(async (file) => { await semaphore.WaitAsync(); try { bool success = await AttemptDownloadAsync(file.Key, file.Value); if (success) { lock (_downloadedFiles) { _downloadedFiles[file.Key] = file.Value; } Interlocked.Increment(ref _completedCount); int progress = (int)((double)_completedCount / _totalCount * 95); UpdateProgressValue(progress); UpdateStatus($"下载: {Path.GetFileName(file.Key)}"); UpdateCountBox(_completedCount, _totalCount); } else { failedDownloads.TryAdd(file.Key, file.Value); } } finally { semaphore.Release(); } }); await Task.WhenAll(downloadTasks); } /// /// 尝试从多个数据源下载单个文件,优先使用123盘,OSS作为备用 /// /// 文件的相对路径 /// 文件的期望MD5值,用于验证完整性 /// 下载成功返回true,否则返回false private async Task AttemptDownloadAsync(string filePath, string expectedMd5) { string tempFilePath = Path.Combine(_tempDirectory, filePath); string fileName = Path.GetFileName(filePath); long downloadedBytes = 0; try { if (await CheckExistingTempFile(tempFilePath, expectedMd5, fileName)) { // 已存在临时文件 downloadedBytes = new FileInfo(tempFilePath).Length; AddDownloadedBytes(downloadedBytes, false); return true; } UpdateStatus($"下载: {fileName}"); UpdateCountBox(_completedCount + 1, _totalCount); if (await DownloadFileFromOneDrive(filePath, expectedMd5, tempFilePath)) { downloadedBytes = new FileInfo(tempFilePath).Length; AddDownloadedBytes(downloadedBytes, true); return true; } UpdateStatus($"下载: {fileName}"); UpdateCountBox(_completedCount + 1, _totalCount); string ossKey = $"File/{expectedMd5}"; var obj = _ossClient.GetObject(OssBucketName, ossKey); string tempDir = Path.GetDirectoryName(tempFilePath); if (!Directory.Exists(tempDir)) { Directory.CreateDirectory(tempDir); } using (var fileStream = File.Create(tempFilePath)) { using (var cts = new CancellationTokenSource(DownloadStreamTimeoutMs)) { await obj.Content.CopyToAsync(fileStream, 81920, cts.Token); } } downloadedBytes = new FileInfo(tempFilePath).Length; AddDownloadedBytes(downloadedBytes, true); return true; } catch (Exception ex) when (ex is OssException || ex is WebException || ex is OperationCanceledException) { UpdateStatus($"下载: {fileName}"); UpdateCountBox(_completedCount + 1, _totalCount); string ossKey = $"File/{expectedMd5}"; bool ok = await DownloadFileWithFallback(ossKey, tempFilePath); if (ok) { try { downloadedBytes = new FileInfo(tempFilePath).Length; AddDownloadedBytes(downloadedBytes, true); } catch { // File might not be accessible for a moment after download } } return ok; } catch { UpdateStatus($"下载异常: {fileName}"); return false; } } /// /// 检查临时目录中是否已存在完整的文件,通过MD5验证文件完整性 /// /// 临时文件的完整路径 /// 文件的期望MD5哈希值 /// 文件名,用于状态显示 /// 如果文件存在且MD5匹配返回true,否则返回false private async Task CheckExistingTempFile(string tempFilePath, string expectedMd5, string fileName) { try { if (!File.Exists(tempFilePath)) { return false; } UpdateStatus($"检查: {fileName}"); string actualMd5 = await Task.Run(() => CalculateMD5FromFile(tempFilePath)); if (actualMd5.Equals(expectedMd5, StringComparison.OrdinalIgnoreCase)) { UpdateStatus($"跳过: {fileName}"); return true; } else { UpdateStatus($"重下载: {fileName}"); File.Delete(tempFilePath); return false; } } catch (Exception ex) { UpdateStatus($"检查出错: {fileName}"); try { if (File.Exists(tempFilePath)) { File.Delete(tempFilePath); } } catch { } return false; } } /// /// 从123盘下载文件,先尝试主域名,失败后自动切换到备用域名 /// /// 123盘的基础URL地址 /// 要下载的文件名 /// 文件保存的本地路径 /// 下载成功返回true,两个域名都失败则返回false private async Task DownloadFromOneDrive(string baseUrl, string fileName, string localPath) { try { UpdateStatus($"123盘下载: {fileName}"); string authUrl = GenerateAuthUrl($"http://{OneDriveMainDomain}{OneDrivePath}", fileName); UpdateStatus("123盘主域..."); var request = new HttpRequestMessage(HttpMethod.Get, authUrl) { Version = HttpVersion.Version11 }; request.Headers.Add("User-Agent", "CheckDownload/1.0"); using (var response = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead)) { response.EnsureSuccessStatusCode(); string localDir = Path.GetDirectoryName(localPath); if (!Directory.Exists(localDir)) { Directory.CreateDirectory(localDir); } using (var remote = await response.Content.ReadAsStreamAsync()) using (var localFile = File.Create(localPath)) { using (var cts = new CancellationTokenSource(DownloadStreamTimeoutMs)) { await remote.CopyToAsync(localFile, 81920, cts.Token); } } UpdateStatus($"主域成功: {fileName}"); return true; } } catch { UpdateStatus("主域失败, 转备用..."); } try { UpdateStatus($"123盘备用下载: {fileName}"); string authUrl = GenerateAuthUrl($"http://{OneDriveBackupDomain}{OneDrivePath}", fileName); UpdateStatus("123盘备用域..."); var request = new HttpRequestMessage(HttpMethod.Get, authUrl) { Version = HttpVersion.Version11 }; request.Headers.Add("User-Agent", "CheckDownload/1.0"); using (var response = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead)) { response.EnsureSuccessStatusCode(); string localDir = Path.GetDirectoryName(localPath); if (!Directory.Exists(localDir)) { Directory.CreateDirectory(localDir); } using (var remote = await response.Content.ReadAsStreamAsync()) using (var localFile = File.Create(localPath)) { using (var cts = new CancellationTokenSource(DownloadStreamTimeoutMs)) { await remote.CopyToAsync(localFile, 81920, cts.Token); } } UpdateStatus($"备用域成功: {fileName}"); return true; } } catch { UpdateStatus($"备用域失败: {fileName}"); return false; } } /// /// 根据文件MD5从123盘下载文件,使用主备域名双重保障 /// /// 文件的相对路径(仅用于日志显示) /// 文件的MD5值,用于构建123盘存储路径 /// 文件保存的本地完整路径 /// 下载成功返回true,两个域名都失败则返回false private async Task DownloadFileFromOneDrive(string filePath, string expectedMd5, string localPath) { string fileName = $"File/{expectedMd5}"; try { string authUrl = GenerateAuthUrl($"http://{OneDriveMainDomain}{OneDrivePath}", fileName); var request = new HttpRequestMessage(HttpMethod.Get, authUrl) { Version = HttpVersion.Version11 }; request.Headers.Add("User-Agent", "CheckDownload/1.0"); using (var response = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead)) { response.EnsureSuccessStatusCode(); string localDir = Path.GetDirectoryName(localPath); if (!Directory.Exists(localDir)) { Directory.CreateDirectory(localDir); } using (var remote = await response.Content.ReadAsStreamAsync()) using (var localFile = File.Create(localPath)) { using (var cts = new CancellationTokenSource(DownloadStreamTimeoutMs)) { await remote.CopyToAsync(localFile, 81920, cts.Token); } } return true; } } catch (Exception ex) { } try { string authUrl = GenerateAuthUrl($"http://{OneDriveBackupDomain}{OneDrivePath}", fileName); var request = new HttpRequestMessage(HttpMethod.Get, authUrl) { Version = HttpVersion.Version11 }; request.Headers.Add("User-Agent", "CheckDownload/1.0"); using (var response = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead)) { response.EnsureSuccessStatusCode(); string localDir = Path.GetDirectoryName(localPath); if (!Directory.Exists(localDir)) { Directory.CreateDirectory(localDir); } using (var remote = await response.Content.ReadAsStreamAsync()) using (var localFile = File.Create(localPath)) { using (var cts = new CancellationTokenSource(DownloadStreamTimeoutMs)) { await remote.CopyToAsync(localFile, 81920, cts.Token); } } return true; } } catch { return false; } } /// /// 对下载失败的文件进行重试,最多重试指定次数 /// /// 包含失败文件路径和MD5值的字典 /// 重试后仍然失败的文件字典 private async Task> RetryFailedFilesAsync(Dictionary failedFiles) { var filesToRetry = new Dictionary(failedFiles); for (int i = 0; i < MaxDownloadRetries && filesToRetry.Any(); i++) { UpdateStatus($"重试 {i + 1}/{MaxDownloadRetries}, 剩{filesToRetry.Count}个"); var failedThisRound = new ConcurrentDictionary(); await PerformDownloads(filesToRetry, failedThisRound); filesToRetry = new Dictionary(failedThisRound); if (filesToRetry.Any() && i < MaxDownloadRetries - 1) { UpdateStatus("等待3秒后重试"); await Task.Delay(3000); } } return filesToRetry; } /// /// 使用多个DNS服务器解析域名获取IP地址列表,提高解析成功率 /// /// 要解析的目标域名 /// 解析成功的IP地址列表,解析失败返回空列表 private async Task> GetIpAddressesForDomain(string domain) { foreach (var dnsServer in _dnsServers) { try { var requestUri = $"http://{dnsServer}/resolve?name={domain}&type=1&short=1"; var request = new HttpRequestMessage(HttpMethod.Get, requestUri) { Version = HttpVersion.Version11 }; request.Headers.Add("User-Agent", "CheckDownload/1.0"); request.Headers.Host = dnsServer; var response = await _httpClient.SendAsync(request); response.EnsureSuccessStatusCode(); string responseBody = await response.Content.ReadAsStringAsync(); var ips = JsonConvert.DeserializeObject>(responseBody); if (ips != null && ips.Any()) { UpdateStatus($"DNS解析成功: {dnsServer}"); return ips; } } catch { UpdateStatus($"DNS解析失败: {dnsServer}"); await Task.Delay(500); } } UpdateStatus("所有DNS解析失败"); return new List(); } /// /// 验证所有下载文件的MD5完整性并保存到目标位置,处理文件占用情况 /// private async Task VerifyAndSaveAllFiles() { UpdateStatus("校验文件..."); var failedFiles = new ConcurrentBag(); var filesForScripting = new ConcurrentBag<(string original, string newFile)>(); int totalFiles = _downloadedFiles.Count; int completed = 0; var semaphore = new SemaphoreSlim(Environment.ProcessorCount); var tasks = _downloadedFiles.Select(async item => { await semaphore.WaitAsync(); try { string relativePath = item.Key; string expectedMd5 = item.Value; string tempFilePath = Path.Combine(_tempDirectory, relativePath); string actualMd5 = CalculateMD5FromFile(tempFilePath); if (actualMd5 != expectedMd5.ToLower()) { failedFiles.Add(relativePath); return; } string localPath = Path.Combine(_baseDirectory, relativePath); string localDir = Path.GetDirectoryName(localPath); if (!Directory.Exists(localDir)) { Directory.CreateDirectory(localDir); } if (!await TryMoveFileAsync(tempFilePath, localPath)) { string backupPath = localPath + ".new"; File.Move(tempFilePath, backupPath); filesForScripting.Add((localPath, backupPath)); } } finally { int done = Interlocked.Increment(ref completed); int percentage = 95 + (int)(done * 5.0 / totalFiles); UpdateProgressValue(Math.Min(100, percentage)); semaphore.Release(); } }); await Task.WhenAll(tasks); if (filesForScripting.Any()) { CreateReplaceScriptForAll(filesForScripting.ToList()); } foreach (var failed in failedFiles) { _downloadedFiles.Remove(failed); } if (failedFiles.Count > 0) { throw new Exception($"{failedFiles.Count}个文件校验失败"); } else { UpdateStatus("校验保存成功"); UpdateProgressValue(100); } } /// /// 尝试将文件从临时位置移动到目标位置(异步),若文件被占用则等待一秒后重试一次 /// /// 源文件的完整路径 /// 目标文件的完整路径 /// 移动成功返回true,失败返回false private async Task TryMoveFileAsync(string sourcePath, string targetPath) { try { File.Move(sourcePath, targetPath); return true; } catch (IOException) { UpdateStatus("文件占用,解锁中..."); try { await Task.Delay(1000); File.Move(sourcePath, targetPath); UpdateStatus("解锁并更新成功"); return true; } catch { UpdateStatus("文件仍被占用"); return false; } } catch (Exception ex) { UpdateStatus("移动文件出错"); return false; } } /// /// 为被占用文件创建批处理脚本,在程序退出后自动完成文件替换 /// /// 包含原始文件路径和新文件路径的元组列表 private void CreateReplaceScriptForAll(List<(string original, string newFile)> files) { if (!files.Any()) return; try { string batchFilePath = Path.Combine(_baseDirectory, "update_files.bat"); string processId = Process.GetCurrentProcess().Id.ToString(); var batchContent = new StringBuilder(); batchContent.AppendLine("@echo off"); batchContent.AppendLine("chcp 65001 > nul"); batchContent.AppendLine(":check_process"); batchContent.AppendLine($"tasklist /FI \"PID eq {processId}\" 2>NUL | find /I \"{processId}\" >NUL"); batchContent.AppendLine("if %ERRORLEVEL% == 0 ("); batchContent.AppendLine(" timeout /t 1 /nobreak > NUL"); batchContent.AppendLine(" goto check_process"); batchContent.AppendLine(")"); batchContent.AppendLine("timeout /t 2 /nobreak > NUL"); foreach (var file in files) { batchContent.AppendLine($"del \"{file.original}\" /f /q"); batchContent.AppendLine($"move \"{file.newFile}\" \"{file.original}\""); } batchContent.AppendLine("del \"%~f0\" /f /q"); batchContent.AppendLine("exit"); File.WriteAllText(batchFilePath, batchContent.ToString(), new UTF8Encoding(false)); var startInfo = new ProcessStartInfo { FileName = "cmd.exe", Arguments = $"/c \"{batchFilePath}\"", CreateNoWindow = true, UseShellExecute = false, WindowStyle = ProcessWindowStyle.Hidden }; Process.Start(startInfo); } catch { UpdateStatus("创建脚本出错"); } } /// /// 初始化程序使用的临时目录,用于存储下载过程中的临时文件 /// private void InitializeTempDirectory() { try { _tempDirectory = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Temp", "CheckDownload" ); if (!Directory.Exists(_tempDirectory)) { Directory.CreateDirectory(_tempDirectory); } } catch { UpdateStatus("初始化Temp失败"); } } /// /// 计算字符串的MD5哈希值,返回32位小写十六进制字符串 /// /// 要计算哈希值的字符串 /// 32位小写十六进制MD5哈希值 private string CalculateMD5(string input) { using (var md5 = MD5.Create()) { byte[] inputBytes = Encoding.UTF8.GetBytes(input); byte[] hashBytes = md5.ComputeHash(inputBytes); return BitConverter.ToString(hashBytes).Replace("-", "").ToLowerInvariant(); } } /// /// 计算字节数组的MD5哈希值,返回32位小写十六进制字符串 /// /// 要计算哈希值的字节数组 /// 32位小写十六进制MD5哈希值 private string CalculateMD5(byte[] data) { using (var md5 = MD5.Create()) { byte[] hashBytes = md5.ComputeHash(data); return BitConverter.ToString(hashBytes).Replace("-", "").ToLowerInvariant(); } } /// /// 从阿里云OSS下载文件,包含SDK直连和IP直连两种备用方案 /// /// OSS对象的存储键值 /// 文件保存的本地路径 /// 下载成功返回true,所有方案都失败返回false private async Task DownloadFileWithFallback(string ossKey, string localPath) { try { var obj = _ossClient.GetObject(OssBucketName, ossKey); string localDir = Path.GetDirectoryName(localPath); if (!Directory.Exists(localDir)) { Directory.CreateDirectory(localDir); } using (var fileStream = File.Create(localPath)) { using (var cts = new CancellationTokenSource(DownloadStreamTimeoutMs)) { await obj.Content.CopyToAsync(fileStream, 81920, cts.Token); } } return true; } catch (OssException) { UpdateStatus("主下载失败,转备用..."); } catch (WebException) { UpdateStatus("主下载失败,转备用..."); } catch (IOException) { UpdateStatus("主下载失败,转备用..."); } catch (OperationCanceledException) { UpdateStatus("主下载失败,转备用..."); } var domain = new Uri("https://" + OssEndpoint).Host; List ips = await GetIpAddressesForDomain(domain); if (ips == null || ips.Count == 0) { UpdateStatus($"无法获取IP地址"); return false; } var req = new GeneratePresignedUriRequest(OssBucketName, ossKey, SignHttpMethod.Get) { Expiration = DateTime.Now.AddMinutes(10) }; var signedUrl = _ossClient.GeneratePresignedUri(req).ToString(); var signedUri = new Uri(signedUrl); foreach (var ip in ips) { try { var ipUrl = signedUrl.Replace(signedUri.Host, ip); var request = new HttpRequestMessage(HttpMethod.Get, ipUrl) { Version = HttpVersion.Version11 }; request.Headers.Host = signedUri.Host; using (var response = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead)) { response.EnsureSuccessStatusCode(); string localDir = Path.GetDirectoryName(localPath); if (!Directory.Exists(localDir)) { Directory.CreateDirectory(localDir); } using (var remote = await response.Content.ReadAsStreamAsync()) using (var localFile = File.Create(localPath)) { using (var cts = new CancellationTokenSource(DownloadStreamTimeoutMs)) { await remote.CopyToAsync(localFile, 81920, cts.Token); } } } return true; } catch (HttpRequestException) { UpdateStatus($"IP下载失败: {ip}"); } catch (OperationCanceledException) { UpdateStatus($"IP下载失败: {ip}"); } } return false; } /// /// 生成基于当前UTC时间的Unix时间戳,并添加指定的秒数偏移 /// /// 要在当前时间戳基础上增加的秒数 /// Unix时间戳(秒) private long GenerateTimestamp(int number) { return (long)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds + number; } /// /// 使用指定数字作为随机种子生成确定性的32位UUID字符串 /// /// 用作随机种子的数字 /// 不含连字符的32位UUID字符串 private string GenerateUUID(int number) { var random = new Random(number); var guid = new byte[16]; random.NextBytes(guid); guid[7] = (byte)((guid[7] & 0x0F) | 0x40); guid[8] = (byte)((guid[8] & 0x3F) | 0x80); var resultGuid = new Guid(guid); return resultGuid.ToString("N").Replace("-", ""); } /// /// 计算字符串的MD5哈希值,用于123盘鉴权签名生成 /// /// 要计算哈希值的输入字符串 /// 32位小写十六进制MD5哈希值 private string GenerateMD5(string input) { using (var md5 = MD5.Create()) { byte[] inputBytes = Encoding.UTF8.GetBytes(input); byte[] hashBytes = md5.ComputeHash(inputBytes); return BitConverter.ToString(hashBytes).Replace("-", "").ToLowerInvariant(); } } /// /// 为123盘文件访问生成带有时间戳和签名验证的鉴权URL /// /// 123盘的基础访问URL /// 要访问的文件名或路径 /// 包含鉴权参数的完整访问URL private string GenerateAuthUrl(string url, string file) { try { long timestamp = GenerateTimestamp(OneDriveAuthTimeout); string rand = GenerateUUID(16); string pathPart = ExtractPathFromUrl(url); string fullUrl = $"{url}/{file}"; string signString = $"/{pathPart}/{file}-{timestamp}-{rand}-{OneDriveUid}-{OneDriveAuthKey}"; string signature = GenerateMD5(signString); string authUrl = $"{fullUrl}?auth_key={timestamp}-{rand}-{OneDriveUid}-{signature}"; return authUrl; } catch { UpdateStatus("生成AuthUrl失败"); return $"{url}/{file}"; } } /// /// 从完整URL中解析并提取域名后的路径部分,用于生成123盘签名 /// /// 要解析的完整URL地址 /// 去除域名和协议后的路径字符串 private string ExtractPathFromUrl(string url) { string pathPart = ""; try { var uri = new Uri(url); pathPart = uri.AbsolutePath.TrimStart('/'); if (string.IsNullOrEmpty(pathPart)) { string basePattern = $"{uri.Scheme}://{uri.Host}"; if (url.StartsWith(basePattern)) { pathPart = url.Substring(basePattern.Length).TrimStart('/'); } } } catch { int protocolIndex = url.IndexOf("://"); if (protocolIndex > 0) { int domainEndIndex = url.IndexOf('/', protocolIndex + 3); if (domainEndIndex > 0) { pathPart = url.Substring(domainEndIndex + 1); } } } return pathPart; } /// /// 下载MD5配置文件,优先使用123盘,失败后切换到阿里云OSS备用方案 /// /// MD5配置文件名 /// 文件保存的本地路径 /// 下载成功返回true,所有方案都失败返回false private async Task DownloadMd5FileWithFallback(string fileName, string localPath) { try { UpdateStatus("123盘下载MD5..."); if (await DownloadFromOneDrive($"http://{OneDriveMainDomain}{OneDrivePath}", fileName, localPath)) { return true; } UpdateStatus("123盘失败,转OSS..."); return await DownloadFileWithFallback(fileName, localPath); } catch (Exception ex) { UpdateStatus("所有下载方式均失败"); return false; } } /// /// 清理程序使用的临时目录,删除所有下载过程中产生的临时文件 /// private void CleanupTempDirectory() { try { string tempPath = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Temp", "CheckDownload" ); if (Directory.Exists(tempPath)) { UpdateStatus("清理Temp..."); Directory.Delete(tempPath, true); } } catch (Exception ex) { } } /// /// 通过分析md5.json文件内容智能确定项目基准目录,用于正确定位文件更新路径 /// /// md5.json文件中的数据对象 private void InitializeBaseDirectoryFromMd5Data(JObject data) { try { string currentProgramName = Path.GetFileName(Application.ExecutablePath); string programPathInMd5 = FindFileInMd5Data(data, currentProgramName); if (!string.IsNullOrEmpty(programPathInMd5)) { string programDirInMd5 = Path.GetDirectoryName(programPathInMd5); if (string.IsNullOrEmpty(programDirInMd5)) { programDirInMd5 = ""; } string currentProgramDir = Application.StartupPath; if (string.IsNullOrEmpty(programDirInMd5)) { _baseDirectory = currentProgramDir; } else { _baseDirectory = FindProjectBaseDirectory(currentProgramDir, programDirInMd5); } } else { _baseDirectory = Application.StartupPath; } } catch (Exception ex) { _baseDirectory = Application.StartupPath; } } /// /// 在md5.json的数据结构中递归搜索指定的文件名 /// /// md5.json解析后的JSON对象 /// 要搜索的目标文件名 /// 当前递归的路径,用于构建完整路径 /// 找到的文件在md5.json中的完整相对路径,未找到返回null private string FindFileInMd5Data(JObject data, string fileName, string currentPath = "") { foreach (var property in data.Properties()) { string key = property.Name; JToken value = property.Value; string fullPath = string.IsNullOrEmpty(currentPath) ? key : Path.Combine(currentPath, key); if (value.Type == JTokenType.String) { if (string.Equals(key, fileName, StringComparison.OrdinalIgnoreCase)) { return fullPath; } } else if (value.Type == JTokenType.Object) { string result = FindFileInMd5Data((JObject)value, fileName, fullPath); if (!string.IsNullOrEmpty(result)) { return result; } } } return null; } /// /// 通过分析程序在md5.json中的相对路径,向上递归查找项目根目录 /// /// 当前程序运行的目录 /// 程序在md5.json中的相对路径 /// 项目的基准目录路径 private string FindProjectBaseDirectory(string currentDir, string expectedRelativePath) { try { string[] expectedDirs = expectedRelativePath.Split(new char[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries); string checkDir = currentDir; for (int i = 0; i < expectedDirs.Length; i++) { string parentDir = Directory.GetParent(checkDir)?.FullName; if (string.IsNullOrEmpty(parentDir)) { break; } string currentDirName = Path.GetFileName(checkDir); string expectedDirName = expectedDirs[expectedDirs.Length - 1 - i]; if (string.Equals(currentDirName, expectedDirName, StringComparison.OrdinalIgnoreCase)) { if (i == expectedDirs.Length - 1) { return parentDir; } checkDir = parentDir; } else { break; } } return currentDir; } catch { return currentDir; } } /// /// 使用文件流计算指定文件的 MD5 哈希值(32 位小写十六进制)。 /// /// 要计算 MD5 的文件完整路径 /// 32 位小写十六进制 MD5 字符串 private string CalculateMD5FromFile(string filePath) { using (var fs = File.OpenRead(filePath)) using (var md5 = MD5.Create()) { var hashBytes = md5.ComputeHash(fs); return BitConverter.ToString(hashBytes).Replace("-", string.Empty).ToLowerInvariant(); } } } }