Last active
August 29, 2015 14:25
-
-
Save tak1827/77cadcb1d4e4a7da356e to your computer and use it in GitHub Desktop.
This script is for node.js. Execute shell command form your web browser's search box.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var PORT = 8081; | |
var net = require('net'); | |
var child_process = require('child_process'); | |
net.createServer(function(socket) { | |
socket.setEncoding('utf8'); | |
socket.setTimeout(10*60*1000);// 10 minitus | |
socket.on('data', function(data) { | |
var cmd = srchCmd(data);// Get shell command | |
if (cmd === 'favicon.ico') return;// Ignore a favicon | |
var output = ''; | |
// Execute shell command | |
child_process.exec(cmd, | |
{ encoding: 'utf8' }, | |
function(err, stdout, stderr) { | |
output = (err !== null) ? err : (stdout !== null) ? stdout : stderr; | |
if (typeof output === 'string') { | |
output = output.replace(/\n/g, "<br>");// Reflect return | |
output = output.replace(/\t/g, " "); // Replace tab into 2 half-width space | |
output = output.replace(/ /g, " "); // Replace ' ' into half-width space | |
// Create http response header | |
var header = "HTTP/1.1 200 OK\r\n" + | |
"Content-Type: text/html\r\n" + | |
"\r\n"; | |
// Create http response body | |
var body = "<html><body>" + output + "</body</html>"; | |
// Send output | |
socket.write(header + body, 'utf8'); | |
} | |
socket.end(); | |
} | |
); | |
}); | |
}).listen(PORT); | |
// Search for shell command in http request | |
function srchCmd(data) { | |
var cmd = data.substring( | |
data.indexOf('/')+1, | |
data.indexOf(' HTTP')) || ''; | |
return decodeURI(cmd); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment