From a2c475a89ada5d40570a2cab5f67fcabd38187aa Mon Sep 17 00:00:00 2001 From: "Namhyeon, Go" Date: Sun, 26 Jul 2020 20:40:25 +0900 Subject: [PATCH] Update shell.js and relative files --- bootstrap.js | 11 +++++-- lib/autohotkey.js | 16 ++++------ lib/autoit3.js | 16 ++++------ lib/cloudflare.js | 11 +++---- lib/powershell.js | 61 +++++++++++++---------------------- lib/service.js | 80 ++++++++++++++++++++++------------------------ lib/shadowsocks.js | 37 +++++++++++++-------- lib/shell.js | 33 +++++++++++++++---- lib/vbscript.js | 16 ++++------ 9 files changed, 145 insertions(+), 136 deletions(-) diff --git a/bootstrap.js b/bootstrap.js index 74f7beb..b3ed831 100644 --- a/bootstrap.js +++ b/bootstrap.js @@ -10,7 +10,7 @@ var SYS = require("lib/system"); var SHELL = require("lib/shell"); return { - main: function() { + main: function(args) { // unlock file console.log("Starting unlock files..."); PS.execCommand("dir | Unblock-File"); @@ -29,8 +29,13 @@ return { // open HTA file console.log("Trying open GUI..."); - SHELL.run("app.hta"); + if (typeof(args) !== "undefined") { + SHELL.run(["app.hta"].concat(args)); + } else { + SHELL.run("app.hta"); + } - console.log("done"); + // echo welcome + console.log("welcome"); } }; diff --git a/lib/autohotkey.js b/lib/autohotkey.js index 1e8815c..a5b6ac9 100644 --- a/lib/autohotkey.js +++ b/lib/autohotkey.js @@ -9,16 +9,14 @@ exports.global = global; exports.require = global.require; exports.execScript = function(scriptName, args) { - var commandOptions = []; + var cmd = [ + "%PROGRAMFILES%\\AutoHotkey\\AutoHotkey.exe", + scriptName + ".ahk" + ]; - commandOptions.push("\"%PROGRAMFILES%\\AutoHotkey\\AutoHotkey.exe\""); - commandOptions.push(scriptName + ".ahk"); - - if(typeof(args) !== "undefined") { - for(var i in args) { - commandOptions.push(args[i]); - } + if (typeof(args) !== "undefined") { + cmd = cmd.concat(args); } - return SHELL.exec(commandOptions.join(' ')); + return SHELL.exec(cmd); }; diff --git a/lib/autoit3.js b/lib/autoit3.js index 4d004f5..6b0b681 100644 --- a/lib/autoit3.js +++ b/lib/autoit3.js @@ -9,16 +9,14 @@ exports.global = global; exports.require = global.require; exports.execScript = function(scriptName, args) { - var commandOptions = []; + var cmd = [ + "%PROGRAMFILES(X86)%\\AutoIt3\\AutoIt3.exe", + scriptName + ".au3" + ]; - commandOptions.push("\"%PROGRAMFILES(X86)%\\AutoIt3\\AutoIt3.exe\""); - commandOptions.push(scriptName + ".au3"); - - if(typeof(args) !== "undefined") { - for(var i in args) { - commandOptions.push(args[i]); - } + if (typeof(args) !== "undefined") { + cmd = cmd.concat(args); } - return SHELL.exec(commandOptions.join(' ')); + return SHELL.exec(cmd); }; diff --git a/lib/cloudflare.js b/lib/cloudflare.js index 1072f81..478b0d9 100644 --- a/lib/cloudflare.js +++ b/lib/cloudflare.js @@ -19,10 +19,9 @@ if(arch.indexOf("64") > -1) { // TODO: https://developers.cloudflare.com/access/rdp/rdp-guide/ exports.installService = function() { - var commandOptions = []; - commandOptions.push(exports.binPath); - commandOptions.push("service"); - commandOptions.push("install"); - - return SHELL.exec(commandOptions.join(' ')); + return SHELL.exec([ + exports.binPath, + "service", + "install" + ]); }; diff --git a/lib/powershell.js b/lib/powershell.js index a3bfb50..f1a0c6e 100644 --- a/lib/powershell.js +++ b/lib/powershell.js @@ -8,49 +8,32 @@ exports.VERSIONINFO = "Powershell (powershell.js) version 0.1"; exports.global = global; exports.require = global.require; -exports.addslashes = function(string) { - return string.replace(/\\/g, '\\\\'). - replace(/\u0008/g, '\\b'). - replace(/\t/g, '\\t'). - replace(/\n/g, '\\n'). - replace(/\f/g, '\\f'). - replace(/\r/g, '\\r'). - replace(/'/g, '\\\''). - replace(/"/g, '\\"'); -}; - exports.execScript = function(scriptName, args) { - var commandOptions = []; + var cmd = [ + "powershell.exe", + "-NoProfile", + "-ExecutionPolicy", + "ByPass", + "-nologo", + "-file", + scriptName + ".ps1" + ]; - commandOptions.push("powershell.exe"); - commandOptions.push("-NoProfile"); - commandOptions.push("-ExecutionPolicy"); - commandOptions.push("ByPass"); - commandOptions.push("-nologo") - commandOptions.push("-file"); - commandOptions.push(scriptName + ".ps1"); - - if(typeof(args) !== "undefined") { - for(var i in args) { - commandOptions.push(args[i]); - } + if (typeof(cmd) !== "undefined") { + cmd = cmd.concat(args); } - return SHELL.exec(commandOptions.join(' ')); + return SHELL.exec(cmd); }; -exports.execCommand = function(command) { - var commandOptions = []; - - commandOptions.push("powershell.exe"); - commandOptions.push("-NoProfile"); - commandOptions.push("-ExecutionPolicy"); - commandOptions.push("ByPass"); - commandOptions.push("-nologo") - commandOptions.push("-Command"); - commandOptions.push("\"& {"); - commandOptions.push(exports.addslashes(command)); - commandOptions.push("}\""); - - return SHELL.exec(commandOptions.join(' ')); +exports.execCommand = function(cmd) { + return SHELL.exec([ + "powershell.exe", + "-NoProfile", + "-ExecutionPolicy", + "ByPass", + "-nologo", + "-Command", + "& {" + cmd + "}" + ]); }; diff --git a/lib/service.js b/lib/service.js index 2cd27d6..dc61af8 100644 --- a/lib/service.js +++ b/lib/service.js @@ -6,7 +6,11 @@ exports.require = global.require; // https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2012-r2-and-2012/dd228922(v=ws.11) exports.queryService = function(name, options) { - var commandOptons = [], + var cmd = [ + "sc", + "query", + name + ], _options = { type: [ "service", // type= {driver | service | all} @@ -26,22 +30,26 @@ exports.queryService = function(name, options) { if(_options[k] !== false) { if(_options[k].constructor == Array) { for(var i in options[k]) { - commandOptions.push(k + "="); - commandOptions.push("\"" + _options[k][i] + "\""); + cmd.push(k + '='); + cmd.push(_options[k][i]); } } else { - commandOptions.push(k + '='); - commandOptions.push("\"" + _options[k] + "\""); + cmd.push(k + '='); + cmd.push(_options[k]); } } } - return SHELL.exec(commandOptions.join(' ')); + return SHELL.exec(cmd); }; // https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/sc-create exports.createService = function(name, options) { - var commandOptions = [], + var cmd = [ + "sc", + "create", + name + ], _options = { type: "share", // type= {own | share | kernel | filesys | rec | interact type= {own | share}} start: "demand", // start= {boot | system | auto | demand | disabled | delayed-auto } @@ -55,10 +63,6 @@ exports.createService = function(name, options) { password: false // password= }; - commandOptions.push("sc"); - commandOptions.push("create"); - commandOptions.push(name); - for(var k in _options) { if(k in options) { _options[k] = options[k]; @@ -67,51 +71,45 @@ exports.createService = function(name, options) { if(_options[k] !== false) { if(_options[k].constructor == Array) { for(var i in options[k]) { - commandOptions.push(k + "="); - commandOptions.push("\"" + _options[k][i] + "\""); + cmd.push(k + '='); + cmd.push(_options[k][i]); } } else { - commandOptions.push(k + '='); - commandOptions.push("\"" + _options[k] + "\""); + cmd.push(k + '='); + cmd.push(_options[k]); } } } - return SHELL.exec(commandOptions.join(' ')); + return SHELL.exec(cmd); }; exports.startService = function(name, args) { - var commandOptions = []; - - commandOptions.push("sc"); - commandOptions.push("start"); - commandOptions.push(name); + var cmd = [ + "sc", + "start", + name + ]; if(typeof(args) !== "undefined") { - for(var i in args) { - commandOptions.push(args[i]); - } + cmd = cmd.concat(args); } - - return SHELL.exec(commandOptions.join(' ')); + + return SHELL.exec(cmd); }; -exports.stopService = function(name, args) { - var commandOptions = []; - - commandOptions.push("sc"); - commandOptions.push("stop"); - commandOptions.push(name); - - return SHELL.exec(commandOptions.join(' ')); +exports.stopService = function(name) { + return SHELL.exec([ + "sc", + "stop", + name + ]); }; exports.deleteService = function(name) { - var commandOptions = []; - - commandOptions.push("sc"); - commandOptions.push("delete"); - commandOptions.push(name); - - return SHELL.exec(commandOptions.join(' ')); + return SHELL.exec([ + "sc", + "delete", + name + ]); }; diff --git a/lib/shadowsocks.js b/lib/shadowsocks.js index e18d44e..807b197 100644 --- a/lib/shadowsocks.js +++ b/lib/shadowsocks.js @@ -16,18 +16,27 @@ if(arch.indexOf("64") > -1) { exports.binPath = "bin/shadowsocks-lib-mingw-x86/ss-local"; } -exports.connect = function(host, remotePort, localPort, password, algorithm) { - var commandOptions = []; - commandOptions.push(exports.binPath); - commandOptions.push("-s"); - commandOptions.push(host); - commandOptions.push("-p"); - commandOptions.push(remotePort); - commandOptions.push("-l"); - commandOptions.push(localPorts); - commandOptions.push("-k"); - commandOptions.push(password); - commandOptions.push("-m"); - commandOptions.push(algoritm); - SHELL.run(commandOptions.join(' '), true); +exports.getRandomInt = function(min, max) { + var x = Math.random(); + return min + Math.floor((max - min) * x); +}; + +exports.connect = function() { + var port = exports.getRandomInt(49152, 65535); + + SHELL.run([ + exports.binPath, + "-s", + __config.shadowsocks.host, + "-p", + __config.shadowsocks.port, + "-l", + port, + "-k", + __config.shadowsocks.password, + "-m", + __config.shadowsocks.cipher + ], true); + + return port; }; diff --git a/lib/shell.js b/lib/shell.js index ab57afa..5592ab1 100644 --- a/lib/shell.js +++ b/lib/shell.js @@ -8,14 +8,35 @@ exports.VERSIONINFO = "Shell Module (shell.js) version 0.1"; exports.global = global; exports.require = global.require; -exports.exec = function(cmd, stdOutPath) { - var WSH = CreateObject("WScript.Shell"), - data; +exports.addslashes = function(string) { + return string.replace(/\\/g, '\\\\'). + replace(/\u0008/g, '\\b'). + replace(/\t/g, '\\t'). + replace(/\n/g, '\\n'). + replace(/\f/g, '\\f'). + replace(/\r/g, '\\r'). + replace(/'/g, '\\\''). + replace(/"/g, '\\"'); +}; - if (typeof(stdOutPath) == "undefined") { +exports.makeCmdLine = function(cmd) { + if (typeof(cmd) === "string") { + return cmd; + } else if (typeof(cmd) === "object") { + return cmd.map(function(s) { + return "\"" + exports.addslashes(s) + "\""; + }).join(' '); + } else { + return ""; + } +}; + +exports.exec = function(cmd, stdOutPath) { + var WSH = CreateObject("WScript.Shell"), data; + if (typeof(stdOutPath) === "undefined") { stdOutPath = "stdout.txt"; } - var c = "%comspec% /c (" + cmd + ") 1> " + stdOutPath; + var c = "%comspec% /c (" + exports.makeCmdLine(cmd) + ") 1> " + stdOutPath; c += " 2>&1"; WSH.Run(c, 0, true); data = FILE.readFile(stdOutPath, "utf-8"); @@ -30,6 +51,6 @@ exports.exec = function(cmd, stdOutPath) { exports.run = function(cmd, fork) { var WSH = CreateObject("WScript.Shell"); var fork = (typeof(fork) !== "undefined") ? fork : true; - var c = "%comspec% /q /c " + cmd; + var c = "%comspec% /q /c " + exports.makeCmdLine(cmd); WSH.Run(c, 0, !fork); }; diff --git a/lib/vbscript.js b/lib/vbscript.js index b92c1cd..8d4a6e0 100644 --- a/lib/vbscript.js +++ b/lib/vbscript.js @@ -9,18 +9,16 @@ exports.global = global; exports.require = global.require; exports.execScript = function(scriptName, args) { - var commandOptions = []; + var cmd = [ + "cscript", + scriptName + ".vbs" + ]; - commandOptions.push("cscript"); - commandOptions.push(scriptName + ".vbs"); - - if(typeof(args) !== "undefined") { - for(var i in args) { - commandOptions.push(args[i]); - } + if (typeof(args) !== "undefined") { + cmd = cmd.concat(args); } - return SHELL.exec(commandOptions.join(' ')); + return SHELL.exec(cmd); }; exports.execCommand = function(command) {