public static DownloadHandle GetHandle(string filename, long filesize, string downloadUrl, MegaNode node, string tempPath = null) { var handle = new DownloadHandle(filename, filesize, tempPath); handle.DownloadUrl = downloadUrl; handle.Node = node; return handle; }
private void FinishFile(DownloadHandle handle) { if (handle.Status == TransferHandleStatus.Cancelled) { return; } handle.Stream.Close(); handle.Mac = new byte[16]; foreach (var chunk in handle.Chunks) { handle.Mac = handle.Mac.Xor(chunk.Mac); handle.Mac = Crypto.Encrypt(handle.AesAlg, handle.Mac); } var check = new byte[8]; handle.Mac.XorWith(0, handle.Mac, 4, 4); handle.Mac.XorWith(8, handle.Mac, 12, 4); Array.Copy(handle.Mac, 0, check, 0, 4); Array.Copy(handle.Mac, 8, check, 4, 4); var ok = check.SequenceEqual(handle.MacCheck); if (ok) { if (handle.TargetPath != null) { if (File.Exists(handle.TargetPath)) { File.Delete(handle.TargetPath); } File.Move(handle.TempFile, handle.TargetPath); } handle.EndTransfer(null); } else { handle.EndTransfer(MegaApiError.EKEY); } }
private void tsbCheckUpdateCore_Click(object sender, EventArgs e) { if (downloadHandle == null) { downloadHandle = new DownloadHandle(); downloadHandle.AbsoluteCompleted += (sender2, args) => { if (args.Success) { AppendText(false, UIRes.I18N("MsgParsingV2rayCoreSuccessfully")); string url = args.Msg; this.Invoke((MethodInvoker)(delegate { if (UI.ShowYesNo(string.Format(UIRes.I18N("DownloadYesNo"), url)) == DialogResult.No) { return; } else { downloadHandle.DownloadFileAsync(config, url, null, -1); } })); } else { AppendText(false, args.Msg); } }; downloadHandle.UpdateCompleted += (sender2, args) => { if (args.Success) { AppendText(false, UIRes.I18N("MsgDownloadV2rayCoreSuccessfully")); AppendText(false, UIRes.I18N("MsgUnpacking")); try { CloseV2ray(); string fileName = downloadHandle.DownloadFileName; fileName = Utils.GetPath(fileName); using (ZipArchive archive = ZipFile.OpenRead(fileName)) { foreach (ZipArchiveEntry entry in archive.Entries) { if (entry.Length == 0) { continue; } entry.ExtractToFile(Utils.GetPath(entry.Name), true); } } AppendText(false, UIRes.I18N("MsgUpdateV2rayCoreSuccessfullyMore")); Global.reloadV2ray = true; LoadV2ray(); AppendText(false, UIRes.I18N("MsgUpdateV2rayCoreSuccessfully")); } catch (Exception ex) { AppendText(false, ex.Message); } } else { AppendText(false, args.Msg); } }; downloadHandle.Error += (sender2, args) => { AppendText(true, args.GetException().Message); }; } AppendText(false, UIRes.I18N("MsgStartUpdatingV2rayCore")); downloadHandle.AbsoluteV2rayCore(config); }
private void tsbSubUpdate_Click(object sender, EventArgs e) { AppendText(false, UIRes.I18N("MsgUpdateSubscriptionStart")); if (config.subItem == null || config.subItem.Count <= 0) { AppendText(false, UIRes.I18N("MsgNoValidSubscription")); return; } for (int k = 1; k <= config.subItem.Count; k++) { string id = config.subItem[k - 1].id.TrimEx(); string url = config.subItem[k - 1].url.TrimEx(); string hashCode = $"{k}->"; if (config.subItem[k - 1].enabled == false) { continue; } if (Utils.IsNullOrEmpty(id) || Utils.IsNullOrEmpty(url)) { AppendText(false, $"{hashCode}{UIRes.I18N("MsgNoValidSubscription")}"); continue; } DownloadHandle downloadHandle3 = new DownloadHandle(); downloadHandle3.UpdateCompleted += (sender2, args) => { if (args.Success) { AppendText(false, $"{hashCode}{UIRes.I18N("MsgGetSubscriptionSuccessfully")}"); var result = Utils.Base64Decode(args.Msg); if (Utils.IsNullOrEmpty(result)) { AppendText(false, $"{hashCode}{UIRes.I18N("MsgSubscriptionDecodingFailed")}"); return; } ConfigHandler.RemoveServerViaSubid(ref config, id); AppendText(false, $"{hashCode}{UIRes.I18N("MsgClearSubscription")}"); RefreshServers(); if (AddBatchServers(result, id) == 0) { } else { AppendText(false, $"{hashCode}{UIRes.I18N("MsgFailedImportSubscription")}"); } AppendText(false, $"{hashCode}{UIRes.I18N("MsgUpdateSubscriptionEnd")}"); } else { AppendText(false, args.Msg); } }; downloadHandle3.Error += (sender2, args) => { AppendText(true, args.GetException().Message); }; downloadHandle3.WebDownloadString(url); AppendText(false, $"{hashCode}{UIRes.I18N("MsgStartGettingSubscriptions")}"); } }
private void tsbCheckUpdateCore_Click(object sender, EventArgs e) { DownloadHandle downloadHandle = null; if (downloadHandle == null) { downloadHandle = new DownloadHandle(); downloadHandle.AbsoluteCompleted += (sender2, args) => { if (args.Success) { AppendText(false, UIRes.I18N("MsgParsingV2rayCoreSuccessfully")); string url = args.Msg; this.Invoke((MethodInvoker)(delegate { askToDownload(downloadHandle, url); })); } else { AppendText(false, args.Msg); } }; downloadHandle.UpdateCompleted += (sender2, args) => { if (args.Success) { AppendText(false, UIRes.I18N("MsgDownloadV2rayCoreSuccessfully")); AppendText(false, UIRes.I18N("MsgUnpacking")); try { CloseV2ray(); string fileName = downloadHandle.DownloadFileName; fileName = Utils.GetPath(fileName); FileManager.ZipExtractToFile(fileName); AppendText(false, UIRes.I18N("MsgUpdateV2rayCoreSuccessfullyMore")); Global.reloadV2ray = true; LoadV2ray(); AppendText(false, UIRes.I18N("MsgUpdateV2rayCoreSuccessfully")); } catch (Exception ex) { AppendText(false, ex.Message); } } else { AppendText(false, args.Msg); } }; downloadHandle.Error += (sender2, args) => { AppendText(true, args.GetException().Message); }; } AppendText(false, string.Format(UIRes.I18N("MsgStartUpdating"), "v2rayCore")); downloadHandle.CheckUpdateAsync("Core"); }
private void tsbCheckUpdateN_Click(object sender, EventArgs e) { //System.Diagnostics.Process.Start(Global.UpdateUrl); DownloadHandle downloadHandle = null; if (downloadHandle == null) { downloadHandle = new DownloadHandle(); downloadHandle.AbsoluteCompleted += (sender2, args) => { if (args.Success) { AppendText(false, string.Format(UIRes.I18N("MsgParsingSuccessfully"), "v2rayN")); string url = args.Msg; this.Invoke((MethodInvoker)(delegate { askToDownload(downloadHandle, url); })); } else { AppendText(false, args.Msg); } }; downloadHandle.UpdateCompleted += (sender2, args) => { if (args.Success) { AppendText(false, UIRes.I18N("MsgDownloadV2rayCoreSuccessfully")); try { string fileName = Utils.GetPath(downloadHandle.DownloadFileName); Process process = new Process { StartInfo = new ProcessStartInfo { FileName = "v2rayUpgrade.exe", Arguments = "\"" + fileName + "\"", WorkingDirectory = Utils.StartupPath() } }; process.Start(); if (process.Id > 0) { menuExit_Click(null, null); } } catch (Exception ex) { AppendText(false, ex.Message); } } else { AppendText(false, args.Msg); } }; downloadHandle.Error += (sender2, args) => { AppendText(true, args.GetException().Message); }; } AppendText(false, string.Format(UIRes.I18N("MsgStartUpdating"), "v2rayN")); downloadHandle.CheckUpdateAsync("v2rayN"); }