| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- /*
- Copyright 2019-2020 Intel Corporation
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- var promise = require('promise');
- function qfe()
- {
- try {
- var tokens = require('win-wmi').query('ROOT\\CIMV2', 'SELECT * FROM Win32_QuickFixEngineering');
- if (tokens[0]){
- for (var index = 0; index < tokens.length; index++) {
- for (var key in tokens[index]) {
- if (key.startsWith('__')) delete tokens[index][key];
- }
- }
- return (tokens);
- } else {
- return ([]);
- }
- } catch (ex) {
- return ([]);
- }
- }
- function av()
- {
- var result = [];
- try {
- var tokens = require('win-wmi-fixed').query('ROOT\\SecurityCenter2', 'SELECT * FROM AntiVirusProduct');
- if (tokens.length == 0) { return ([]); }
- // Process each antivirus product
- for (var i = 0; i < tokens.length; ++i) {
- var product = tokens[i];
- var modifiedPath = product.pathToSignedProductExe || '';
- // Expand environment variables (e.g., %ProgramFiles%)
- var regex = /%([^%]+)%/g;
- var match;
- while ((match = regex.exec(product.pathToSignedProductExe)) !== null) {
- var envVar = match[1];
- var envValue = process.env[envVar] || '';
- if (envValue) {
- modifiedPath = modifiedPath.replace(match[0], envValue);
- }
- }
- // Check if the executable exists (unless it's Windows Defender pseudo-path)
- var flag = true;
- if (modifiedPath !== 'windowsdefender://') {
- try {
- if (!require('fs').existsSync(modifiedPath)) {
- flag = false;
- }
- } catch (ex) {
- flag = false;
- }
- }
- // Only include products with valid executables
- if (flag) {
- var status = {};
- status.product = product.displayName || '';
- status.updated = (parseInt(product.productState) & 0x10) == 0;
- status.enabled = (parseInt(product.productState) & 0x1000) == 0x1000;
- result.push(status);
- }
- }
- return (result);
- } catch (ex) {
- return ([]);
- }
- }
- function defrag(options)
- {
- var ret = new promise(function (res, rej) { this._res = res; this._rej = rej; });
- var path = '';
- switch(require('os').arch())
- {
- case 'x64':
- if (require('_GenericMarshal').PointerSize == 4)
- {
- // 32 Bit App on 64 Bit Windows
- ret._rej('Cannot defrag volume on 64 bit Windows from 32 bit application');
- return (ret);
- }
- else
- {
- // 64 Bit App
- path = process.env['windir'] + '\\System32\\defrag.exe';
- }
- break;
- case 'ia32':
- // 32 Bit App on 32 Bit Windows
- path = process.env['windir'] + '\\System32\\defrag.exe';
- break;
- default:
- ret._rej(require('os').arch() + ' not supported');
- return (ret);
- break;
- }
- ret.child = require('child_process').execFile(process.env['windir'] + '\\System32\\defrag.exe', ['defrag', options.volume + ' /A']);
- ret.child.promise = ret;
- ret.child.promise.options = options;
- ret.child.stdout.str = ''; ret.child.stdout.on('data', function (c) { this.str += c.toString(); });
- ret.child.stderr.str = ''; ret.child.stderr.on('data', function (c) { this.str += c.toString(); });
- ret.child.on('exit', function (code)
- {
- var lines = this.stdout.str.trim().split('\r\n');
- var obj = { volume: this.promise.options.volume };
- for (var i in lines)
- {
- var token = lines[i].split('=');
- if(token.length == 2)
- {
- switch(token[0].trim().toLowerCase())
- {
- case 'volume size':
- obj['size'] = token[1];
- break;
- case 'free space':
- obj['free'] = token[1];
- break;
- case 'total fragmented space':
- obj['fragmented'] = token[1];
- break;
- case 'largest free space size':
- obj['largestFragment'] = token[1];
- break;
- }
- }
- }
- this.promise._res(obj);
- });
- return (ret);
- }
- function regQuery(H, Path, Key)
- {
- try
- {
- return(require('win-registry').QueryKey(H, Path, Key));
- }
- catch(e)
- {
- return (null);
- }
- }
- function pendingReboot()
- {
- var tmp = null;
- var ret = null;
- var HKEY = require('win-registry').HKEY;
- if(regQuery(HKEY.LocalMachine, 'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Component Based Servicing', 'RebootPending') !=null)
- {
- ret = 'Component Based Servicing';
- }
- else if(regQuery(HKEY.LocalMachine, 'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\WindowsUpdate', 'RebootRequired'))
- {
- ret = 'Windows Update';
- }
- else if ((tmp=regQuery(HKEY.LocalMachine, 'SYSTEM\\CurrentControlSet\\Control\\Session Manager', 'PendingFileRenameOperations'))!=null && tmp != 0 && tmp != '')
- {
- ret = 'File Rename';
- }
- else if (regQuery(HKEY.LocalMachine, 'SYSTEM\\CurrentControlSet\\Control\\ComputerName\\ActiveComputerName', 'ComputerName') != regQuery(HKEY.LocalMachine, 'SYSTEM\\CurrentControlSet\\Control\\ComputerName\\ComputerName', 'ComputerName'))
- {
- ret = 'System Rename';
- }
- return (ret);
- }
- function installedApps()
- {
- var promise = require('promise');
- var ret = new promise(function (a, r) { this._resolve = a; this._reject = r; });
-
- var code = "\
- var reg = require('win-registry');\
- var result = [];\
- var val, tmp;\
- var items = reg.QueryKey(reg.HKEY.LocalMachine, 'SOFTWARE\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Uninstall');\
- for (var key in items.subkeys)\
- {\
- val = {};\
- try\
- {\
- val.name = reg.QueryKey(reg.HKEY.LocalMachine, 'SOFTWARE\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Uninstall\\\\' + items.subkeys[key], 'DisplayName');\
- }\
- catch(e)\
- {\
- continue;\
- }\
- try\
- {\
- val.version = reg.QueryKey(reg.HKEY.LocalMachine, 'SOFTWARE\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Uninstall\\\\' + items.subkeys[key], 'DisplayVersion');\
- if (val.version == '') { delete val.version; }\
- }\
- catch(e)\
- {\
- }\
- try\
- {\
- val.location = reg.QueryKey(reg.HKEY.LocalMachine, 'SOFTWARE\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Uninstall\\\\' + items.subkeys[key], 'InstallLocation');\
- if (val.location == '') { delete val.location; }\
- }\
- catch(e)\
- {\
- }\
- try\
- {\
- val.installdate = reg.QueryKey(reg.HKEY.LocalMachine, 'SOFTWARE\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Uninstall\\\\' + items.subkeys[key], 'InstallDate');\
- if (val.installdate == '') { delete val.installdate; }\
- }\
- catch(e)\
- {\
- }\
- result.push(val);\
- }\
- console.log(JSON.stringify(result,'', 1));process.exit();";
- ret.child = require('child_process').execFile(process.execPath, [process.execPath.split('\\').pop().split('.exe')[0], '-exec "' + code + '"']);
- ret.child.promise = ret;
- ret.child.stdout.str = ''; ret.child.stdout.on('data', function (c) { this.str += c.toString(); });
- ret.child.on('exit', function (c) { this.promise._resolve(JSON.parse(this.stdout.str.trim())); });
- return (ret);
- }
- function installedStoreApps(){
- try {
- var tokens = require('win-wmi-fixed').query('ROOT\\CIMV2', 'SELECT * FROM Win32_InstalledStoreProgram');
- if (tokens[0]){
- for (var index = 0; index < tokens.length; index++) {
- for (var key in tokens[index]) {
- if (key.startsWith('__')) delete tokens[index][key];
- }
- }
- return (tokens);
- } else {
- return ([]);
- };
- } catch (ex) {
- return ([]);
- }
- }
- function defender(){
- try {
- var tokens = require('win-wmi').query('ROOT\\Microsoft\\Windows\\Defender', 'SELECT * FROM MSFT_MpComputerStatus', ['RealTimeProtectionEnabled','IsTamperProtected','AntivirusSignatureVersion','AntivirusSignatureLastUpdated']);
- if (tokens[0]){
- var info = { RealTimeProtection: tokens[0].RealTimeProtectionEnabled, TamperProtected: tokens[0].IsTamperProtected };
- if (tokens[0].AntivirusSignatureVersion) { info.AntivirusSignatureVersion = tokens[0].AntivirusSignatureVersion; }
- if (tokens[0].AntivirusSignatureLastUpdated) { info.AntivirusSignatureLastUpdated = tokens[0].AntivirusSignatureLastUpdated; }
- return (info);
- } else {
- return ({});
- }
- } catch (ex) {
- return ({});
- }
- }
- if (process.platform == 'win32')
- {
- module.exports = { qfe: qfe, av: av, defrag: defrag, pendingReboot: pendingReboot, installedApps: installedApps, installedStoreApps: installedStoreApps, defender: defender };
- }
- else
- {
- var not_supported = function () { throw (process.platform + ' not supported'); };
- module.exports = { qfe: not_supported, av: not_supported, defrag: not_supported, pendingReboot: not_supported, installedApps: not_supported, installedStoreApps: not_supported, defender: not_supported };
- }
|