Skip to content

Commit

Permalink
Add endpoint to obtain logs
Browse files Browse the repository at this point in the history
It goes through a WebSocket; the secret is sent, and if valid, the logs
are sent back.
  • Loading branch information
espadrine committed Dec 3, 2017
1 parent e61373e commit 9cb9113
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 9 deletions.
20 changes: 18 additions & 2 deletions lib/log.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const listeners = [];

// Zero-pad a number in a string.
// eg. 4 becomes 04 but 17 stays 17.
function pad(string) {
Expand All @@ -19,9 +21,23 @@ function date() {
}

module.exports = function log(...msg) {
console.log(date(), ...msg);
const d = date();
listeners.forEach(f => f(d, ...msg))
console.log(d, ...msg);
};

module.exports.error = function error(...msg) {
console.error(date(), ...msg);
const d = date();
listeners.forEach(f => f(d, ...msg))
console.error(d, ...msg);
};

module.exports.addListener = function addListener(func) {
listeners.push(func);
};

module.exports.removeListener = function removeListener(func) {
const index = listeners.indexOf(func);
if (index < 0) { return; }
listeners.splice(index, 1);
};
44 changes: 37 additions & 7 deletions lib/sys/monitor.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,45 @@
'use strict';

const secretIsValid = require('./secret-is-valid');
const serverSecrets = require('../server-secrets');
const log = require('../log');

function secretInvalid(req, res) {
if (!secretIsValid(req.password)) {
// An unknown entity tries to connect. Let the connection linger for a minute.
setTimeout(function() {
res.json({errors: [{code: 'invalid_secrets'}]});
}, 10000);
return true;
}
return false;
}

function setRoutes(server) {
server.get('/sys/network', (req, res) => {
if (!secretIsValid(req.password)) {
// An unknown entity tries to connect. Let the connection linger for a minute.
return setTimeout(function() {
res.end(JSON.stringify({errors: [{code: 'invalid_secrets'}]}));
}, 10000);
server.handle(function(req, res, next) {
if (req.url.startsWith('/sys/')) {
if (secretInvalid(req, res)) { return; }
}
res.end(JSON.stringify({ips: serverSecrets.shieldsIps}));
next();
});

server.get('/sys/network', (req, res) => {
res.json({ips: serverSecrets.shieldsIps});
});

server.ws('/sys/logs', socket => {
const listener = (...msg) => socket.send(msg.join(' '));
socket.on('close', () => log.removeListener(listener));
socket.on('message', msg => {
let req;
try {
req = JSON.parse(msg);
} catch(e) { return; }
if (!secretIsValid(req.secret)) {
return socket.close();
}
log.addListener(listener);
});
});
}

Expand Down
2 changes: 2 additions & 0 deletions lib/sys/secret-is-valid.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

const serverSecrets = require('../server-secrets');

function secretIsValid(secret = '') {
Expand Down

0 comments on commit 9cb9113

Please sign in to comment.