Update lib/system.js and related files

This commit is contained in:
Namhyeon Go 2023-12-11 16:54:21 +09:00
parent c4dfca7c62
commit 79bc3fe593
3 changed files with 129 additions and 79 deletions

View File

@ -1,7 +1,17 @@
var SYS = require("lib/system");
var HTTP = require("lib/http");
function main(args) { function main(args) {
console.log("Hello world"); console.log("Hello world");
if (typeof WScript !== "undefined") { if (typeof WScript !== "undefined") {
console.log("Runtime version:", WScript.Version); console.log("Process version:", SYS.getProcessVersion());
}
try {
var web = HTTP.create();
console.log(web.userAgent);
} catch (e) {
console.error("Something wrong");
} }
} }

View File

@ -1,6 +1,5 @@
//////////////////////////////////////////////////////////////////////// // http.js
// HTTP API with PIPE-IPC // https://github.com/gnh1201/welsonjs
////////////////////////////////////////////////////////////////////////
var SYS = require("lib/system"); var SYS = require("lib/system");
var FILE = require("lib/file"); var FILE = require("lib/file");
var SHELL = require("lib/shell"); var SHELL = require("lib/shell");
@ -11,6 +10,7 @@ var PipeIPC = require("lib/pipe-ipc");
var OS_NAME = SYS.getOS(); var OS_NAME = SYS.getOS();
var OS_ARCH = SYS.getArch(); var OS_ARCH = SYS.getArch();
var DEVICE_UUID = SYS.getUUID(); var DEVICE_UUID = SYS.getUUID();
var PROCESS_VERSION = SYS.getProcessVersion();
var HTTPObject = function(engine) { var HTTPObject = function(engine) {
this.interface = null; this.interface = null;
@ -21,7 +21,7 @@ var HTTPObject = function(engine) {
this.headers = {}; this.headers = {};
this.parameters = {}; this.parameters = {};
this.dataType = null; this.dataType = null;
this.userAgent = "WelsonJS/0.2.7 (" + OS_NAME + "; " + OS_ARCH + "; " + DEVICE_UUID + "; https://catswords.social/@catswords_oss; abuse@catswords.net)"; this.userAgent = "WelsonJS/0.2.7 (" + OS_NAME + "; " + OS_ARCH + "; " + PROCESS_VERSION + "; " + DEVICE_UUID + "; abuse@catswords.net)";
this.isAsynchronous = false; this.isAsynchronous = false;
this.proxy = { this.proxy = {
"enabled": false, "enabled": false,

View File

@ -1,11 +1,9 @@
//////////////////////////////////////////////////////////////////////// // lib/system.js
// System API // https://github.com/gnh1201/welsonjs
////////////////////////////////////////////////////////////////////////
var SHELL = require("lib/shell"); var SHELL = require("lib/shell");
var WSH = CreateObject("WScript.Shell");
var WMI = require("lib/wmi"); var WMI = require("lib/wmi");
exports.createProcess = function(cmd) { function createProcess(cmd) {
var SW_HIDE = 0; var SW_HIDE = 0;
//wmi.Get("Win32_Process").Create(cmd, null, si, pid); //wmi.Get("Win32_Process").Create(cmd, null, si, pid);
return WMI.setClass("Win32_Process").setMethod("Create").setParameters({ return WMI.setClass("Win32_Process").setMethod("Create").setParameters({
@ -20,9 +18,10 @@ exports.createProcess = function(cmd) {
.setAttribute("ySize", 1) .setAttribute("ySize", 1)
.getInstance() .getInstance()
}).execute().get("ProcessID"); }).execute().get("ProcessID");
}; }
exports.getEnvString = function(envName) { function getEnvString(envName) {
var WSH = CreateObject("WScript.Shell");
return (function(s) { return (function(s) {
switch(s) { switch(s) {
case "PROGRAMFILES": case "PROGRAMFILES":
@ -33,10 +32,10 @@ exports.getEnvString = function(envName) {
return WSH.ExpandEnvironmentStrings('%' + s + '%'); return WSH.ExpandEnvironmentStrings('%' + s + '%');
} }
})(envName.toUpperCase()); })(envName.toUpperCase());
}; }
exports.get32BitFolder = function() { function get32BitFolder() {
var base = exports.getEnvString("WINDIR"); var base = getEnvString("WINDIR");
var syswow64 = base + "\\SysWOW64\\"; var syswow64 = base + "\\SysWOW64\\";
if (CreateObject("Scripting.FileSystemObject").FolderExists(syswow64)) if (CreateObject("Scripting.FileSystemObject").FolderExists(syswow64))
@ -45,107 +44,103 @@ exports.get32BitFolder = function() {
return base + "\\System32\\"; return base + "\\System32\\";
} }
exports.isElevated = function() { function isElevated() {
var WSH = CreateObject("WScript.Shell");
try { try {
WSH.RegRead("HKEY_USERS\\s-1-5-19\\"); WSH.RegRead("HKEY_USERS\\s-1-5-19\\");
return true; return true;
} catch (e) { } catch (e) {}
return false;
}
};
exports.getOS = function() { return false;
}
function getOS() {
return WMI.execQuery("SELECT Caption FROM Win32_OperatingSystem").fetch().get("Caption").trim(); return WMI.execQuery("SELECT Caption FROM Win32_OperatingSystem").fetch().get("Caption").trim();
}; }
exports.getDCName = function() { function getDCName() {
var WSH = CreateObject("WScript.Shell");
try { try {
var DC = WSH.RegRead("HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Group Policy\\History\\DCName"); var DC = WSH.RegRead("HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Group Policy\\History\\DCName");
if (DC.length > 0) if (DC.length > 0)
return DC; return DC;
} catch (e) {} } catch (e) {}
}; }
exports.getArch = function() { function getArch() {
return WMI.execQuery("SELECT OSArchitecture FROM Win32_OperatingSystem").fetch().get("OSArchitecture"); return WMI.execQuery("SELECT OSArchitecture FROM Win32_OperatingSystem").fetch().get("OSArchitecture");
}; }
exports.getUUID = function() { function getUUID() {
return WMI.execQuery("SELECT UUID FROM Win32_ComputerSystemProduct").fetch().get("UUID").toLowerCase(); return WMI.execQuery("SELECT UUID FROM Win32_ComputerSystemProduct").fetch().get("UUID").toLowerCase();
}; }
exports.getCurrentWorkingDirectory = function() { function getCurrentWorkingDirectory() {
try { try {
cwd = SHELL.exec("cd", "cwd.txt").trim(); return SHELL.exec("cd").trim();
return cwd;
} catch (e) {} } catch (e) {}
}; }
exports.getDirName = function(path) { function getDirName(path) {
var delimiter = "\\"; var pos = Math.max.apply(null, [path.lastIndexOf("\\"), path.lastIndexOf("/")]);
var pos = path.lastIndexOf(delimiter);
return (pos > -1 ? path.substring(0, pos) : ""); return (pos > -1 ? path.substring(0, pos) : "");
}; }
exports.getFileName = function(path) { function getFileName(path) {
var delimiter = "\\"; var pos = Math.max.apply(null, [path.lastIndexOf("\\"), path.lastIndexOf("/")]);
var pos = path.lastIndexOf(delimiter); return (pos > -1 ? path.substring(pos + 1) : "");
return (pos > -1 ? path.substring(pos + delimiter.length) : ""); }
};
exports.getCurrentScriptDirectory = function() { function getCurrentScriptDirectory() {
if (typeof(WScript) !== "undefined") { if (typeof(WScript) !== "undefined") {
return exports.getDirName(WScript.ScriptFullName); return getDirName(WScript.ScriptFullName);
} else if (typeof(document) !== "undefined") { } else if (typeof(document) !== "undefined") {
return exports.getDirName(document.location.pathname); return getDirName(document.location.pathname);
} else { } else {
return "."; return ".";
} }
}; }
exports.getCurrentScriptName = function() { function getCurrentScriptName() {
if (typeof(WScript) !== "undefined") { if (typeof(WScript) !== "undefined") {
return WScript.ScriptName; return WScript.ScriptName;
} else if (typeof(document) !== "undefined") { } else if (typeof(document) !== "undefined") {
return exports.getFileName(document.location.pathname); return getFileName(document.location.pathname);
} else { } else {
return ""; return "";
} }
}; }
exports.getNetworkInterfaces = function() { function getNetworkInterfaces() {
return WMI.execQuery("SELECT * FROM Win32_NetworkAdapterConfiguration").fetchAll(); return WMI.execQuery("SELECT * FROM Win32_NetworkAdapterConfiguration").fetchAll();
}; }
exports.getProcessList = function() { function getProcessList() {
return WMI.execQuery("Select * From Win32_Process").fetchAll(); return WMI.execQuery("SELECT * FROM Win32_Process").fetchAll();
}; }
exports.getPIDList = function() { function getPIDList() {
var result = []; return processes.map(function(x) {
var processes = exports.getProcessList(); return x.ProcessID;
for (var i = 0; i < processes.length; i++) { });
result.push(processes[i].ProcessID); }
}
return result;
};
exports.isAlivePID = function(pid) { function isAlivePID(pid) {
if (!pid) { if (!pid) {
return false; return false;
} else { } else {
return (exports.getPIDList().indexOf(pid) > -1); return (getPIDList().indexOf(pid) > -1);
} }
}; }
exports.getProcessListByName = function(name) { function getProcessListByName(name) {
return exports.getProcessList().filter(function(s) { return getProcessList().filter(function(x) {
return (s.Caption === name); return (x.Caption === name);
}); });
}; }
exports.killProcess = function(pid) { function killProcess(pid) {
var processes = exports.getProcessList(); var processes = getProcessList();
for (var i = 0; i < processes.length; i++) { for (var i = 0; i < processes.length; i++) {
try { try {
@ -154,15 +149,16 @@ exports.killProcess = function(pid) {
return true; return true;
} }
} catch (e) { } catch (e) {
console.error("Lib -> System -> killProcess() -> ", e.message); console.error("Failed to kill process: ", e.message);
} }
} }
return false; return false;
}; }
exports.createShortcut = function(shoutcutName, fileName) { function createShortcut(shoutcutName, fileName) {
var workingDirectory = exports.getCurrentWorkingDirectory(); var WSH = CreateObject("WScript.Shell");
var workingDirectory = getCurrentWorkingDirectory();
var desktopPath = WSH.SpecialFolders("Desktop"); var desktopPath = WSH.SpecialFolders("Desktop");
var link = WSH.CreateShortcut(desktopPath + "\\" + shoutcutName + ".lnk"); var link = WSH.CreateShortcut(desktopPath + "\\" + shoutcutName + ".lnk");
link.IconLocation = fileName + ",1"; link.IconLocation = fileName + ",1";
@ -170,12 +166,56 @@ exports.createShortcut = function(shoutcutName, fileName) {
link.WindowStyle = 1; link.WindowStyle = 1;
link.WorkingDirectory = workingDirectory; link.WorkingDirectory = workingDirectory;
link.Save(); link.Save();
}; }
exports.ping = function(address) { function ping(address) {
return WMI.execQuery("Select ResponseTime From Win32_PingStatus where address='" + address + "'").fetch().get("ResponseTime"); return WMI.execQuery("SELECT ResponseTime FROM Win32_PingStatus WHERE address='" + address + "'").fetch().get("ResponseTime");
}; }
exports.VERSIONINFO = "System Module (system.js) version 0.1.2"; function getProcessVersion() {
var getIEVersion = function() {
var rv = -1;
if (navigator.appName == 'Microsoft Internet Explorer') {
var ua = navigator.userAgent;
var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
if (re.exec(ua) != null)
rv = parseFloat( RegExp.$1 );
}
return rv;
}
if (typeof WScript !== "undefined") {
return "Microsoft JScript" + (' ' + WScript.Version);
} else if (typeof navigator !== "undefined") {
return (function(rv) {
return "MSIE" + (rv < 0 ? '' : (' ' + rv));
})(getIEVersion());
}
}
exports.createProcess = createProcess;
exports.getEnvString = getEnvString;
exports.get32BitFolder = get32BitFolder;
exports.isElevated = isElevated;
exports.getOS = getOS;
exports.getDCName = getDCName;
exports.getArch = getArch;
exports.getUUID = getUUID;
exports.getCurrentWorkingDirectory = getCurrentWorkingDirectory;
exports.getDirName = getDirName;
exports.getFileName = getFileName;
exports.getCurrentScriptDirectory = getCurrentScriptDirectory;
exports.getCurrentScriptName = getCurrentScriptName;
exports.getNetworkInterfaces = getNetworkInterfaces;
exports.getProcessList = getProcessList;
exports.getPIDList = getPIDList;
exports.isAlivePID = isAlivePID;
exports.getProcessListByName = getProcessListByName;
exports.killProcess = killProcess;
exports.createShortcut = createShortcut;
exports.ping = ping;
exports.getProcessVersion = getProcessVersion;
exports.VERSIONINFO = "System Module (system.js) version 0.1.5";
exports.global = global; exports.global = global;
exports.require = global.require; exports.require = global.require;