Estou desenvolvendo um terminal on web
aplicativo para uso pessoal e uso Ratchet
para o servidor de socket. E está funcionando muito com comandos simples uma vez que ls
etc. Mas quando executo comandos uma vez que ping
ele simplesmente não envia o teor de stdout
e stderr
para o cliente de socket No entanto, mesmo nesses comandos, consigo ver que os comandos são exibidos no terminal. Cá está minha implementação
open_processes = [];
$this->clients = new SplObjectStorage;
}
public function onOpen(ConnectionInterface $conn) {
echo "New connectionn";
$this->clients->attach($conn);
}
static function get_output_json( string $message , array $opts = []){
return json_encode( [ 'data' => $message , 'opts' => $opts ] );
}
private function command_exec(ConnectionInterface &$from , string $command){
$descriptorspec = [
0 => ["pipe", "r"], // stdin
1 => ["pipe", "w"], // stdout
2 => ["pipe", "w"], // stderr
];
$process = proc_open($command, $descriptorspec, $pipes);
$this->open_processes[] = $process;
if (is_resource($process)) {
// Close the input pipe, since we're not sending any input
fclose($pipes[0]);
// Set the pipes to non-blocking mode
stream_set_blocking($pipes[1], false);
stream_set_blocking($pipes[2], false);
while (true) {
$output = stream_get_contents($pipes[1]);
$error = stream_get_contents($pipes[2]);
if ($output) {
$string_to_send = $this->get_output_json($output);
echo "Out: $string_to_sendn";
$from->send($string_to_send);
}
if ($error) {
$string_to_send = $this->get_output_json($error);
echo "Err: $string_to_sendn";
$from->send($string_to_send);
}
flush();
// Check if the process is still running
$status = proc_get_status($process);
if (!$status['running']) {
break;
}
// Small delay to prevent busy-waiting
usleep(100000); // 100ms
}
// Close pipes and process
fclose($pipes[1]);
fclose($pipes[2]);
$return_value = $this->command_end($process);
$from->send($this->get_output_json("Command ended with code $return_valuen", ['exit' => true]));
} else {
$from->send($this->get_output_json("Command failed to execute", ['exit' => true]));
}
}
/**end or terminate a command and return the exit code */
public static function command_end( $proc ) :int|false {
if( !is_resource($proc) ){
return false;
}
$status = proc_get_status( $proc );
if( $status['running'] === true ) {
proc_terminate($proc , SIGINT);
return SIGINT;
} else {
return proc_close($proc);
}
Sua visita nos ajuda a continuar oferecendo o melhor para você! }
public function onMessage(ConnectionInterface $from, $msg) {
$command = trim($msg);
Sua visita nos ajuda a continuar oferecendo o melhor para você!
// $allowedCommands = ['ls -l', 'pwd', 'whoami'];
if (true || in_array($command, $allowedCommands)) {
$this->command_exec($from , $command);
} else {
Sua visita nos ajuda a continuar oferecendo o melhor para você! $from->send($this->get_output_json("Command not allowed.rn"));
}
}
public function onClose(ConnectionInterface $conn) {
echo "Connection closedn";
foreach ($this->open_processes as $p) {
var_dump($p);
proc_close($p);
}
$this->clients->detach($conn);
}
public function onError(ConnectionInterface $conn, Exception $e) {
echo "Connection closed because errorn";
$conn->close();
}
}
$app = new RatchetApp('localhost', 2222);
$app->route('/terminal', new TerminalServer, ['*']);
echo "No errors so farn";
$app->run();
Tentei fazer o comando trespassar posteriormente ler 10 linhas alterando o command_exec
função e envia a saída de uma só vez depois de trespassar
private function command_exec(ConnectionInterface &$from , string $command){
$descriptorspec = [
0 => ["pipe", "r"], // stdin
1 => ["pipe", "w"], // stdout
2 => ["pipe", "w"], // stderr
];
$process = proc_open($command, $descriptorspec, $pipes);
$this->open_processes[] = $process;
if (is_resource($process)) {
// Close the input pipe, since we're not sending any input
fclose($pipes[0]);
// Set the pipes to non-blocking mode
stream_set_blocking($pipes[1], false);
stream_set_blocking($pipes[2], false);
$i = 0;//count line read
while (true) {
$output = stream_get_contents($pipes[1]);
$error = stream_get_contents($pipes[2]);
if ($output) {
$string_to_send = $this->get_output_json($output);
echo "Out: $string_to_sendn";
$from->send($string_to_send);
$i++;
if( $i > 10 ){
break;
}
}
if ($error) {
$string_to_send = $this->get_output_json($error);
echo "Err: $string_to_sendn";
$from->send($string_to_send);
}
flush();
// Check if the process is still running
$status = proc_get_status($process);
if (!$status['running']) {
break;
}
// Small delay to prevent busy-waiting
usleep(100000); // 100ms
}
// Close pipes and process
fclose($pipes[1]);
fclose($pipes[2]);
$return_value = $this->command_end($process);
$from->send($this->get_output_json("Command ended with code $return_valuen", ['exit' => true]));
} else {
$from->send($this->get_output_json("Command failed to execute", ['exit' => true]));
}
}