global.cp = require("child_process"); global.net = require("net"); global.util = require("util"); global.vm = require("vm"); global.opt = { time:[0,0], listen:false, port:9999, proc:[ /* proc, proc, proc,... proc : { id:#id, type:"type", <= exec, execFile, fork, spawn log:[ [ "type", <=stdout_ "data", #time ] ], run:bool(run), clients:[], time:[], intv: } */ ], buff2str : true }; opt.itv = setInterval(function(){opt.time[1] = (new Date()).getTime();}, 1); global.n = net.createServer(); /* vv exec cmd vv execFile cmd v? fork cmd vv spawn cmd vv list all vv list run vv list stop vv info #id vv info #id --full vv kill #id vv log #id full vv log #id listen --start vv log #id listen --stop vv msg #id text_support_metacharacter... vv write #id text_support_metacharacter... vv cmd command return JSON; */ n.listen(opt.port || 9999, function(){ console.log(`listening on ${opt.port || 9999}`) }); n.log = []; n.on("connection", function(s){ s.on("data", function(dt){ n.log.push(["sock_data", {sock:s, data:dt}, opt.time[1]]); dt = dt.toString(); if(dt.slice(-1) == "\n" || dt.slice(-1) == "\r") dt = dt.slice(0, -1); if(dt.slice(-1) == "\n" || dt.slice(-1) == "\r") dt = dt.slice(0, -1); dt = txt2meta(dt); try{ var cmd = dt.slice(0, (dt.indexOf(" ") > -1 ? dt.indexOf(" ") : dt.length)); switch(cmd){ case "exec": case "execFile": case "fork": case "spawn": if(dt.indexOf(" ") > -1){ var i = opt.proc.length; if(cmd == "exec"){ opt.proc[i] = cp.exec(dt.slice(dt.indexOf(" ") + 1)); }else{ var arg = splitArgs(dt.slice(dt.indexOf(" ") + 1)); var arg0 = arg[0], arg1 = JSON.parse(JSON.stringify(arg)); arg1.shift(); opt.proc[i] = cp[cmd](arg[0], arg1); } opt.proc[i].id = i opt.proc[i].clients = []; opt.proc[i].time = [opt.time[1], 0]; opt.proc[i].intv = setInterval(function(){ opt.proc[i].time[1] = opt.time[1]; }, 1); opt.proc[i].type = cmd; opt.proc[i].log = []; opt.proc[i].run = true; log(opt.proc[i], function(type, dt){ if(type == "exit"){ opt.proc[i].run = false; } if(opt.buff2str) dt = dt.toString(); opt.proc[i].log.push([type, dt, opt.proc[i].time[1]]); opt.proc[i].clients.forEach(function(sn){ if(!sn.destroyed) sn.write(JSON.stringify([type, opt.proc[i].id, dt])); if(sn.destroyed) opt.proc[i].clients = kickMid(opt.proc[i].clients, opt.proc[i].clients.indexOf(sn)); }); }); }else{ n.log.push(["sock_data_resp", {sock:s, data:dt, res:"error, cmd should : exec fileorapps.ext"}, opt.time[1]]); s.write(`error, cmd should : ${cmd} appsorfile.ext cmd`); } break; case "list": var arg1 = dt.slice(dt.indexOf(" ") + 1); var p = [], i=undefined; switch(arg1){ case "all": for(i=0;i-1){ full[u] += ` ${full[i]}`; x.push(i); } if(u == -1 && (full[i].slice(0,1) == "\"" || full[i].slice(0,1) == "\'")){ u=i; l=full[i].slice(0,1); full[i] = full[i].slice(1); } if(u > -1 && full[i].slice(-1) == l){ full[u] = full[u].slice(0, -1); u=-1; } } var i = undefined; for(i=x.length - 1;i>-1;i--){ full = kickMid(full, x[i]); } return full; } function txt2meta(txt){ //hello \" \' \r\n \123 \xff \u2f3a //\\" \\' \\` \\r\\n \\123 \\xff \\u2f3a txt = txt.replace(/\\"/g, "\"").replace(/\\'/g, "\'").replace(/\\`/g, "\`").replace(/\\r/g, "\r").replace(/\\n/g, "\n").replace(/\\\\/g, "\\"); var rg = [[/\\([abcdef]|[ABCDEF]|[0-9]){3}/g, 8], [/\\x([abcdef]|[ABCDEF]|[0-9]){2}/g, 16], [/\\u([abcdef]|[ABCDEF]|[0-9]){4}/g, 16]]; var i; for(i=0;i