Skip to content

Commit

Permalink
Merge pull request #74 from axians/dev
Browse files Browse the repository at this point in the history
2020-08-18
  • Loading branch information
wmmihaa authored Aug 18, 2020
2 parents cfbd4e1 + ad3b800 commit 02d45e0
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 11 deletions.
99 changes: 90 additions & 9 deletions lib/MicroServiceBusHost.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ function MicroServiceBusHost(settingsHelper) {
var _lastConnectedTime;
var _vulnerabilitiesScanJob;
var _dockerHelper;
var _ptyProcess, _ptyProcessUser;
// Add npm path to home folder as services are otherwise not able to require them...
// This should be added to start.js in mSB-node
if (!settingsHelper.isRunningAsSnap) {
Expand Down Expand Up @@ -611,8 +612,10 @@ function MicroServiceBusHost(settingsHelper) {
var https = require('https');
log("Downloading image from ".yellow + metamodel.uri.grey);


var request = https.get(metamodel.uri, function (response) {
let options = {
timeout: 1000 * 60 * 10, //10 min timeout
};
var request = https.get(metamodel.uri, options, function (response) {
response.pipe(file);
file.on('finish', function () {
file.close(function () {
Expand Down Expand Up @@ -706,10 +709,10 @@ function MicroServiceBusHost(settingsHelper) {
_client.invoke('logMessage', settingsHelper.settings.nodeName, `Polycies for ${settingsHelper.settings.nodeName} has been updated.`, settingsHelper.settings.organizationId);
setTimeout(restart, 2000);
}
function OnExecuteScript(patchScript, connid) {

function OnExecuteScript(patchScript, parametes, connid) {
let localPatchUri = settingsHelper.settings.hubUri + '/api/Scripts/' + patchScript;
localPatchUri = localPatchUri.replace('wss://', 'https://');

let requestOptions = {
retries: 2,
uri: localPatchUri
Expand All @@ -728,7 +731,7 @@ function MicroServiceBusHost(settingsHelper) {

fs.writeFileSync(localPatchScript, scriptContent);
log("Executing script");
util.executeScript(localPatchScript, function (err) {
util.executeScript(`${localPatchScript} ${parametes}`, function (err) {
log(`Done executing script. Errors: ${err}`);
if (connid)
_client.invoke('notify', connid, `${fileName} execute successfully on ${settingsHelper.settings.nodeName}.`, "INFO");
Expand Down Expand Up @@ -895,16 +898,75 @@ function MicroServiceBusHost(settingsHelper) {
});
};

// Pseudo terminal
function OnStartTerminal(connid, user) {
log(`Starting terminal for ${user}`);
var pty, os;
try{
pty = require('node-pty');
}
catch(ex){
_client.invoke('terminalError', `Terminal dependancies are not yet installed. Please try again in a couple of minutes.`,connid);
util.addNpmPackage('node-pty',(err)=>{
if(err){
log(`Unable to install terminal dependancies. Error ${err}`);
}
else{
log(`Terminal dependancies installed successfully.`);
_client.invoke('terminalReady',connid);
}
});
return;
}
_ptyProcessUser = user;
os = require('os');
pty = require('node-pty');

var shell = os.platform() === 'win32' ? 'powershell.exe' : 'bash';

_ptyProcess = pty.spawn(shell, [], {
name: 'xterm-color',
cols: 100,
rows: 100,
cwd: process.env.HOME,
env: process.env
});

_ptyProcess.on('data', function (data) {
_client.invoke('terminalData', data, connid);
});
_client.invoke('terminalReady',connid);
log(`Terminal started`);
}
function OnStopTerminal() {
if(_ptyProcess){
_ptyProcess.kill();
_ptyProcess = null;
log(`terminal closed for ${_ptyProcessUser}`);
_ptyProcessUser = null;

}
}
function OnTerminalCommand(command) {
_ptyProcess.write(command);
//console.log(`terminal command: ${command}`);
}

function initDockerHelper() {
if (!_dockerHelper) {
let DockerHelper = require('./DockerHelper');
_dockerHelper = new DockerHelper();
_dockerHelper.on("log", function (m) {
log(`docker: ${m}`);
});
_dockerHelper.isInstalled()
.then(installed => {
if (installed) {
_dockerHelper.init();
}
});
}
}

// Sends heartbeat to server every disconnectPolicy.heartbeatTimeout interval
/* istanbul ignore next */
function startHeartBeat() {
Expand Down Expand Up @@ -957,7 +1019,7 @@ function MicroServiceBusHost(settingsHelper) {
}
/* istanbul ignore next */
function startVulnerabilitiesScan() {
if (_vulnerabilitiesScanJob) {
if (_vulnerabilitiesScanJob || !settingsHelper.settings.isManaged) {
return;
}

Expand Down Expand Up @@ -1378,7 +1440,13 @@ function MicroServiceBusHost(settingsHelper) {
OnSetBootPartition(partition, connid);
});
_client.on('executeScript', function (patchScript, connid) {
OnExecuteScript(patchScript, connid);
let parameters = "";
if(path.basename(patchScript).indexOf(" ") > 0) { // Check for parameters
parameters= patchScript.split(" ").splice(1).join(" ");
patchScript = patchScript.split(" ")[0];
}

OnExecuteScript(patchScript, parameters, connid);
});
_client.on('updateVulnerabilities', function (connid) {
OnUpdateVulnerabilities(connid);
Expand All @@ -1401,6 +1469,15 @@ function MicroServiceBusHost(settingsHelper) {
_client.on('dockerStopContainer', function (request, connid) {
OnDockerStopContainer(request, connid);
});
_client.on('startTerminal', function (connid, user) {
OnStartTerminal(connid, user);
});
_client.on('stopTerminal', function () {
OnStopTerminal();
});
_client.on('terminalCommand', function (command) {
OnTerminalCommand(command);
});
}
// this function is called when you want the server to die gracefully
// i.e. wait for existing connections
Expand Down Expand Up @@ -1462,7 +1539,7 @@ function MicroServiceBusHost(settingsHelper) {
}, 500);
}, 500);
}
this.isConected = function () {
this.isConnected = function () {
return _client.isConnected();
}
this.version = function () {
Expand Down Expand Up @@ -1795,6 +1872,9 @@ function MicroServiceBusHost(settingsHelper) {

setupClient();
_client.start()
.then(() => {
initDockerHelper();
})
.catch((e) => {
setTimeout(() => {
restart();
Expand All @@ -1804,6 +1884,7 @@ function MicroServiceBusHost(settingsHelper) {
// Start interval if we're not online after 2 min
setTimeout(() => {
startHeartBeat();

}, 120000);

if (os.platform() !== 'win32' && !process.env.UNITTEST) {
Expand Down
1 change: 1 addition & 0 deletions lib/MicroServiceBusNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,7 @@ function MicroServiceBusNode(settingsHelper) {
settingsHelper.settings.enableTracking = response.enableTracking;
settingsHelper.settings.timezone = response.timezone;
settingsHelper.settings.policies = response.policies;
settingsHelper.settings.isManaged = response.isManaged;
settingsHelper.retentionPeriod = response.retentionPeriod;
settingsHelper.mode = response.mode;

Expand Down
11 changes: 11 additions & 0 deletions lib/SignalRClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,17 @@ function SignalRClient(uri) {
connection.on('dockerStopContainer', (request, connid) => {
self.emit('dockerStopContainer', request, connid);
});

// PseudoTerminal
connection.on('startTerminal', (connid, user) => {
self.emit('startTerminal', connid, user);
});
connection.on('stopTerminal', () => {
self.emit('stopTerminal');
});
connection.on('terminalCommand', (command) => {
self.emit('terminalCommand', command);
});
}
function getArgValues(params) {
var res = [];
Expand Down
7 changes: 6 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,12 @@ exports.refreshSnap = function (snap, mode, callback) {
}
exports.executeScript = function (path, callback) {
console.log(`Executing ${path}`);
exec(`sudo bash ${path}`, function (error, stdout, stderr) {
if (process.platform === 'win32') {}
else{

}
let command = process.platform === 'win32' ? "powershell.exe" : "sudo bash";
exec(`${command} ${path}`, function (error, stdout, stderr) {
console.log("refreshSnap - complete");
console.log("error: " + error);
console.log("stdout: " + stdout);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,5 @@
"lint": "jshint Utils.js",
"test": "nyc --reporter=html --reporter=text mocha"
},
"version": "3.3.1"
"version": "3.4.1"
}

0 comments on commit 02d45e0

Please sign in to comment.