mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-24 03:05:41 +00:00
71 lines
No EOL
2.5 KiB
JavaScript
71 lines
No EOL
2.5 KiB
JavaScript
/* module make possible connectoin thrue socket.io on a server */
|
|
|
|
var io = require('socket.io'),
|
|
exec = require('child_process').exec,
|
|
Socket = {},
|
|
Win32_b = false;
|
|
|
|
if(process.platform === 'win32')
|
|
Win32_b = true;
|
|
|
|
/**
|
|
* Function listen on servers port
|
|
* @pServer {Object} started server object
|
|
*/
|
|
exports.listen = function(pServer){
|
|
io = io.listen(pServer);
|
|
|
|
io.sockets.on('connection', function (socket) {
|
|
Socket = socket;
|
|
socket.send('hello from server!');
|
|
|
|
console.log('server connected');
|
|
|
|
socket.on('message', function(pCommand) {
|
|
console.log(pCommand);
|
|
|
|
exec(pCommand, getExec);
|
|
});
|
|
|
|
});
|
|
};
|
|
|
|
|
|
/**
|
|
* function send result of command to client
|
|
* @param error
|
|
* @param stdout
|
|
* @param stderr
|
|
*/
|
|
function getExec(error, stdout, stderr) {
|
|
if(stdout){
|
|
if(Win32_b)
|
|
stdout = win2unicode(stdout);
|
|
|
|
console.log(stdout);
|
|
Socket.send(stdout);
|
|
}
|
|
if(stderr){
|
|
if(Win32_b)
|
|
stderr = win2unicode(stderr);
|
|
|
|
console.log('stderr: ' + stderr);
|
|
Socket.send(stderr);
|
|
}
|
|
|
|
if (error !== null) {
|
|
console.log('exec error: ' + error);
|
|
}
|
|
}
|
|
|
|
|
|
// Windows code page 1251 Cyrillic
|
|
var encodings = '\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f\u0402\u0403\u201a\u0453\u201e\u2026\u2020\u2021\u20ac\u2030\u0409\u2039\u040a\u040c\u040b\u040f\u0452\u2018\u2019\u201c\u201d\u2022\u2013\u2014\ufffd\u2122\u0459\u203a\u045a\u045c\u045b\u045f\xa0\u040e\u045e\u0408\xa4\u0490\xa6\xa7\u0401\xa9\u0404\xab\xac\xad\xae\u0407\xb0\xb1\u0406\u0456\u0491\xb5\xb6\xb7\u0451\u2116\u0454\xbb\u0458\u0405\u0455\u0457\u0410\u0411\u0412\u0413\u0414\u0415\u0416\u0417\u0418\u0419\u041a\u041b\u041c\u041d\u041e\u041f\u0420\u0421\u0422\u0423\u0424\u0425\u0426\u0427\u0428\u0429\u042a\u042b\u042c\u042d\u042e\u042f\u0430\u0431\u0432\u0433\u0434\u0435\u0436\u0437\u0438\u0439\u043a\u043b\u043c\u043d\u043e\u043f\u0440\u0441\u0442\u0443\u0444\u0445\u0446\u0447\u0448\u0449\u044a\u044b\u044c\u044d\u044e\u044f';
|
|
|
|
function win2unicode(bytes) {
|
|
var n= bytes.length;
|
|
var chars= new Array(n);
|
|
for (var i= 0; i<n; i++)
|
|
chars[i]= encodings.charAt(bytes.charCodeAt(i));
|
|
return chars.join('');
|
|
} |