domotz-remote-pawn
Version:
Domotz Agent
262 lines (226 loc) • 7.84 kB
JavaScript
/**
* This file is part of Domotz Agent.
* Copyright (C) 2018 Domotz Ltd
*
* Domotz Agent is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Domotz Agent is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Domotz Agent. If not, see <http://www.gnu.org/licenses/>.
*
* Created by Alessio Sanfratello <alessio@domotz.com> on 02/10/18.
*/
const MODULE_NAME = 'PLATFORM';
const PUBLIC_SNAP = 'domotzpro-agent-publicstore';
const PRIVATE_SNAP = 'domotzpro-agent';
var nmapInformation = require('./nmapInformation');
var SSHInformation = require('./SSHInformation');
var childProcess = require('child_process');
const platformsMap = {
ubuntu_core_private: 'A',
ubuntu_core_public: 'B',
ubuntu_core_virtualbox: 'C',
win: 'D',
debian: 'E',
raspberry: 'F',
syno: 'G',
qnap: 'H',
luxul: 'I',
docker: 'J',
netgear_router: 'K',
};
const q = require('q');
function factory(fs) {
var nmapInfo;
var sshInfo;
function getNmapInfo() {
if (nmapInfo) {
return q.resolve(nmapInfo);
}
var res = nmapInformation.getNmapInformation();
res.then(function (data) {
nmapInfo = data;
}).catch(function (error) {
console.error('Unable to retrieve nmap info: %s', error);
deferred.reject(error);
});
return res;
}
function loadSSHInfo(logger) {
if (sshInfo) {
return q.resolve(sshInfo);
}
const shell = isWindows() ? getWindowsShell() : undefined;
return SSHInformation.getSSHInformation(logger, shell)
.then(function (data) {
logger.info('SSH Info %s', JSON.stringify(data));
sshInfo = data;
return data;
})
.catch(function (error) {
logger.error('Unable to retrieve SSH info: %s', error.message);
});
}
function getOpenSSHVersion() {
return sshInfo && sshInfo.openssh;
}
function getOpenSSLVersion() {
return sshInfo && sshInfo.openssl;
}
function getSSHInfo() {
return sshInfo;
}
function getNmapVersion() {
var deferred = q.defer();
getNmapInfo()
.then(function (data) {
deferred.resolve(data.nmap);
})
.catch(function (error) {
console.error('Unable to retrieve nmap version %s', error);
});
return deferred.promise;
}
function isVirtualBox() {
console.info('%s - Checking Virtual Box platform...', MODULE_NAME);
if (process.env.SNAP_COMMON && process.env.SNAP_COMMON.length > 0) {
var snapConfigFilePath = process.env.SNAP_COMMON + '/etc/virtual_spec.json';
if (fs.existsSync(snapConfigFilePath)) {
console.info('%s - Virtual Box platform recognized', MODULE_NAME);
return true;
}
}
console.info('%s - Virtual Box config not found', MODULE_NAME);
return false;
}
function getFullPlatform() {
var fullPlatform = process.env.DPLATFORM;
if (fullPlatform === 'ubuntu_core') {
var snapName = process.env.SNAP_NAME;
switch (snapName) {
case PUBLIC_SNAP:
fullPlatform = isVirtualBox() ? 'ubuntu_core_virtualbox' : 'ubuntu_core_public';
break;
case PRIVATE_SNAP:
fullPlatform = 'ubuntu_core_private';
break;
default:
console.warn('%s - SNAP name "%s" not recognized', MODULE_NAME, snapName);
break;
}
}
console.info('%s - full_platform is: %s', MODULE_NAME, fullPlatform);
return fullPlatform;
}
function isWindows() {
return process.env.DPLATFORM === 'win';
}
function getNodeVersion() {
return process.version;
}
function isNodeVersionZero() {
return getNodeVersion().charAt(1) === '0';
}
function isUbuntuCore() {
return process.env.DPLATFORM === 'ubuntu_core';
}
function isUbuntuCorePrivate() {
return getFullPlatform() === 'ubuntu_core_private';
}
function needsSudo() {
if (process.env.NO_SUDOERS) {
return false;
}
try {
return process.getuid() !== 0;
} catch (e) {
// Windows platform does not have getuid, and doesn't need sudo
return false;
}
}
function getWindowsShell() {
return process.env.DOMOTZ_ROOT_DIR + '\\lib\\portable-git\\bin\\domotz_bash.exe';
}
function _setNmapInfo(newNmapInfo) {
nmapInfo = newNmapInfo;
}
function supportsDomainAccountAuth() {
return !isNodeVersionZero();
}
function getFullPlatformID() {
return platformsMap[getFullPlatform()] || '?';
}
function getOsInfo(resourceLocator) {
try {
var versionNumber = resourceLocator.os.release();
var operatingSystemName = _validateOsName(resourceLocator.os.platform());
return { version: versionNumber, name: operatingSystemName };
} catch (e) {
console.warn('OS name and version not available: returning null.');
throw e;
}
}
function _validateOsName(osName) {
if (osName === 'win32') {
return 'windows';
}
return osName;
}
function _parsePowershellOutput(data) {
const regex = new RegExp('^PSVersion\\s+\\s*(.*)$', 'm');
const match = data.match(regex);
return match ? match[1].trim() : null;
}
function getPowershellVersion() {
if (!isWindows()) {
return q.reject();
}
var deferred = q.defer();
childProcess.exec('powershell.exe "$PSVersionTable"', function (err, stdout, stderr) {
if (err || stderr) {
deferred.reject(err || stderr);
} else {
deferred.resolve(_parsePowershellOutput(stdout));
}
});
return deferred.promise;
}
function isNetgearRouter() {
return process.env.DPLATFORM === 'netgear_router' && process.env.DARCHITECTURE === 'ipq95xx';
}
return {
isVirtualBox: isVirtualBox,
isWindows: isWindows,
isUbuntuCore: isUbuntuCore,
isUbuntuCorePrivate: isUbuntuCorePrivate,
getFullPlatform: getFullPlatform,
isNodeVersionZero: isNodeVersionZero,
getNodeVersion: getNodeVersion,
needsSudo: needsSudo,
getWindowsShell: getWindowsShell,
getNmapInfo: getNmapInfo,
loadSSHInfo: loadSSHInfo,
getOpenSSHVersion: getOpenSSHVersion,
getOpenSSLVersion: getOpenSSLVersion,
getSSHInfo: getSSHInfo,
getPowershellVersion: getPowershellVersion,
getNmapVersion: getNmapVersion,
getOsInfo: getOsInfo,
isNetgearRouter: isNetgearRouter,
//test only
_setNmapInfo: _setNmapInfo,
supportsDomainAccountAuth: supportsDomainAccountAuth,
getFullPlatformID: getFullPlatformID,
netgearRouterDefaultInterface: function () {
return 'br-lan';
},
};
}
module.exports.factory = factory;