Jump to content

php shell execution


EffakT

Recommended Posts

So, I am working on my assessment for class, and I am working on a script that validates Node.js code, but it seems that it is running extremely slow on the first time the port is opened.

 

What it should do is create a js file containing the user's input,

Run the script in Node.JS,

Run a cURL on the script,

Echo the output of the cURL,

Close Node.JS (closing the open port)

Delete the user's script.

 

My php is as follows:

<?php
$a = $_GET['a'];
chdir('scripts');
$a = json_decode($a);
$time = round(microtime(true) * 1000);
$data = "";
$file = $time.'.js';
$handle = fopen($file, 'w') or die('Cannot open file:  '.$file);
foreach ($a as $b) {
$data .= $b."\r\n";
}
preg_match('/(listen\s*\((\d*)\))/', $data, $port);
$port = $port[count($port)-1];
fwrite($handle, $data);
fclose($handle);
$h = popen('"C:\Program Files\nodejs\node.exe" '.$file, 'r');
sleep(1);
exec('C:\curl '.$_SERVER['HTTP_HOST'].':'.$port, $output);
foreach($output as $out) {
echo $out."\r\n";
}
pclose($h);
unlink($file);
?>

Does anybody know of any other way to do this that would reduce this lag?
Also, it seems that the pclose($h); isn't stopping node properly.

 

Thanks

 

--EffakT

Link to comment
https://forums.phpfreaks.com/topic/287359-php-shell-execution/
Share on other sites

Okay, I managed to get it closing node.exe, now I just need it to not lag as much on loading Node.JS, but it seems to be logging the error:

PHP Fatal error:  Maximum execution time of 30 seconds exceeded in executeScript.php on line 20

New Code:

<?php
$a = $_GET['a'];
chdir('scripts');
$a = json_decode($a);
$time = round(microtime(true) * 1000);
$data = "";
$file = $time.'.js';
$handle = fopen($file, 'w') or die('Cannot open file:  '.$file);

foreach ($a as $b) {
	$data .= $b."\r\n";
}

preg_match('/(listen\s*\((\d*)\))/', $data, $port);
$port = $port[count($port)-1];

fwrite($handle, $data);
fclose($handle);
error_log("Launching Node");
shell_exec('"C:\Program Files\nodejs\node.exe" '.$file);
error_log("Running cURL");
exec('C:\curl '.$_SERVER['HTTP_HOST'].':'.$port, $output);
error_log("killing node");
exec('taskkill /F /IM node.exe');

foreach($output as $out) {
	echo $out."\r\n";
}

unlink($file);
?>
Link to comment
https://forums.phpfreaks.com/topic/287359-php-shell-execution/#findComment-1474364
Share on other sites

Sweet, thanks boompa, that wasn't but problem but actually helped a lot!

 

I run the node script in the background (this doesn't slow down my script), then run a cURL and then close node, if cURL returns an error, I run node normally since it will return an error and wont execute. This is so I can get the error that node returns.

Link to comment
https://forums.phpfreaks.com/topic/287359-php-shell-execution/#findComment-1474401
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.