2014-05-12 20:08:19 +02:00
|
|
|
'use strict';
|
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
const _ = require('lodash')
|
|
|
|
const async = require('async')
|
|
|
|
const crypto = require('crypto')
|
|
|
|
const fs = require('graceful-fs')
|
|
|
|
const glob = require('glob')
|
|
|
|
|
|
|
|
const config = require('../config').config
|
|
|
|
const ErrorTypes = require('../utils/errorTypes')
|
|
|
|
const Logger = require('../logger')
|
|
|
|
const MailService = require('../services/mailService')
|
|
|
|
const Strings = require('../utils/strings')
|
|
|
|
const UrlBuilder = require('../utils/urlBuilder')
|
|
|
|
|
|
|
|
const MAX_PARALLEL_NODES_PARSING = 10;
|
|
|
|
|
|
|
|
const linePrefixes = {
|
|
|
|
hostname: '# Knotenname: ',
|
|
|
|
nickname: '# Ansprechpartner: ',
|
|
|
|
email: '# Kontakt: ',
|
|
|
|
coords: '# Koordinaten: ',
|
|
|
|
mac: '# MAC: ',
|
|
|
|
token: '# Token: ',
|
|
|
|
monitoring: '# Monitoring: ',
|
|
|
|
monitoringToken: '# Monitoring-Token: '
|
|
|
|
};
|
|
|
|
|
|
|
|
const filenameParts = ['hostname', 'mac', 'key', 'token', 'monitoringToken'];
|
|
|
|
|
|
|
|
function generateToken() {
|
|
|
|
return crypto.randomBytes(8).toString('hex');
|
|
|
|
}
|
|
|
|
|
|
|
|
function toNodeFilesPattern(filter) {
|
|
|
|
const pattern = _.join(
|
|
|
|
_.map(filenameParts, function (field) {
|
|
|
|
return filter.hasOwnProperty(field) ? filter[field] : '*';
|
|
|
|
}),
|
|
|
|
'@'
|
|
|
|
);
|
|
|
|
|
|
|
|
return config.server.peersPath + '/' + pattern.toLowerCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
function findNodeFiles(filter, callback) {
|
|
|
|
glob(toNodeFilesPattern(filter), callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
function findNodeFilesSync(filter) {
|
|
|
|
return glob.sync(toNodeFilesPattern(filter));
|
|
|
|
}
|
|
|
|
|
|
|
|
function findFilesInPeersPath(callback) {
|
|
|
|
glob(config.server.peersPath + '/*', function (err, files) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
async.filter(files, function (file, fileCallback) {
|
|
|
|
if (file[0] === '.') {
|
|
|
|
return fileCallback(null, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.lstat(file, function (err, stats) {
|
|
|
|
if (err) {
|
|
|
|
return fileCallback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
fileCallback(null, stats.isFile());
|
|
|
|
});
|
|
|
|
}, callback);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseNodeFilename(filename) {
|
|
|
|
const parts = _.split(filename, '@', filenameParts.length);
|
|
|
|
const parsed = {};
|
|
|
|
_.each(_.zip(filenameParts, parts), function (part) {
|
|
|
|
parsed[part[0]] = part[1];
|
|
|
|
});
|
|
|
|
return parsed;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isDuplicate(filter, token) {
|
|
|
|
const files = findNodeFilesSync(filter);
|
|
|
|
if (files.length === 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (files.length > 1 || !token /* node is being created*/) {
|
|
|
|
return true;
|
2014-05-12 20:08:19 +02:00
|
|
|
}
|
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
return parseNodeFilename(files[0]).token !== token;
|
|
|
|
}
|
2016-05-23 22:36:27 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
function checkNoDuplicates(token, node, nodeSecrets) {
|
|
|
|
if (isDuplicate({ hostname: node.hostname }, token)) {
|
|
|
|
return {data: {msg: 'Already exists.', field: 'hostname'}, type: ErrorTypes.conflict};
|
2016-06-21 22:59:37 +02:00
|
|
|
}
|
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
if (node.key) {
|
|
|
|
if (isDuplicate({ key: node.key }, token)) {
|
|
|
|
return {data: {msg: 'Already exists.', field: 'key'}, type: ErrorTypes.conflict};
|
|
|
|
}
|
2016-06-21 22:59:37 +02:00
|
|
|
}
|
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
if (isDuplicate({ mac: node.mac }, token)) {
|
|
|
|
return {data: {msg: 'Already exists.', field: 'mac'}, type: ErrorTypes.conflict};
|
2014-05-12 20:08:19 +02:00
|
|
|
}
|
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
if (nodeSecrets.monitoringToken && isDuplicate({ monitoringToken: nodeSecrets.monitoringToken }, token)) {
|
|
|
|
return {data: {msg: 'Already exists.', field: 'monitoringToken'}, type: ErrorTypes.conflict};
|
|
|
|
}
|
2016-06-21 18:20:36 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function toNodeFilename(token, node, nodeSecrets) {
|
|
|
|
return config.server.peersPath + '/' +
|
|
|
|
(
|
|
|
|
(node.hostname || '') + '@' +
|
|
|
|
(node.mac || '') + '@' +
|
|
|
|
(node.key || '') + '@' +
|
|
|
|
(token || '') + '@' +
|
|
|
|
(nodeSecrets.monitoringToken || '')
|
|
|
|
).toLowerCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
function writeNodeFile(isUpdate, token, node, nodeSecrets, callback) {
|
|
|
|
const filename = toNodeFilename(token, node, nodeSecrets);
|
|
|
|
let data = '';
|
|
|
|
_.each(linePrefixes, function (prefix, key) {
|
|
|
|
let value;
|
|
|
|
switch (key) {
|
|
|
|
case 'monitoring':
|
|
|
|
if (node.monitoring && node.monitoringConfirmed) {
|
|
|
|
value = 'aktiv';
|
|
|
|
} else if (node.monitoring && !node.monitoringConfirmed) {
|
|
|
|
value = 'pending';
|
|
|
|
} else {
|
|
|
|
value = '';
|
2016-06-21 18:20:36 +02:00
|
|
|
}
|
2018-12-17 22:49:54 +01:00
|
|
|
break;
|
2016-06-21 18:20:36 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
case 'monitoringToken':
|
|
|
|
value = nodeSecrets.monitoringToken || '';
|
|
|
|
break;
|
2016-06-21 15:21:08 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
default:
|
|
|
|
value = key === 'token' ? token : node[key];
|
|
|
|
if (_.isUndefined(value)) {
|
|
|
|
value = _.isUndefined(nodeSecrets[key]) ? '' : nodeSecrets[key];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
data += prefix + value + '\n';
|
|
|
|
});
|
|
|
|
if (node.key) {
|
|
|
|
data += 'key "' + node.key + '";\n';
|
2016-06-21 15:21:08 +02:00
|
|
|
}
|
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
// since node.js is single threaded we don't need a lock
|
2016-05-23 22:36:27 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
let error;
|
|
|
|
|
|
|
|
if (isUpdate) {
|
|
|
|
const files = findNodeFilesSync({ token: token });
|
|
|
|
if (files.length !== 1) {
|
|
|
|
return callback({data: 'Node not found.', type: ErrorTypes.notFound});
|
2014-05-12 20:08:19 +02:00
|
|
|
}
|
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
error = checkNoDuplicates(token, node, nodeSecrets);
|
|
|
|
if (error) {
|
|
|
|
return callback(error);
|
2014-05-12 20:08:19 +02:00
|
|
|
}
|
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
const file = files[0];
|
|
|
|
try {
|
|
|
|
fs.unlinkSync(file);
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
Logger.tag('node', 'save').error('Could not delete old node file: ' + file, error);
|
|
|
|
return callback({data: 'Could not remove old node data.', type: ErrorTypes.internalError});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
error = checkNoDuplicates(null, node, nodeSecrets);
|
|
|
|
if (error) {
|
|
|
|
return callback(error);
|
|
|
|
}
|
2014-05-12 20:08:19 +02:00
|
|
|
}
|
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
try {
|
|
|
|
fs.writeFileSync(filename, data, 'utf8');
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
Logger.tag('node', 'save').error('Could not write node file: ' + filename, error);
|
|
|
|
return callback({data: 'Could not write node data.', type: ErrorTypes.internalError});
|
|
|
|
}
|
2014-05-12 20:08:19 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
return callback(null, token, node);
|
|
|
|
}
|
|
|
|
|
|
|
|
function deleteNodeFile(token, callback) {
|
|
|
|
findNodeFiles({ token: token }, function (err, files) {
|
|
|
|
if (err) {
|
|
|
|
Logger.tag('node', 'delete').error('Could not find node file: ' + files, err);
|
|
|
|
return callback({data: 'Could not delete node.', type: ErrorTypes.internalError});
|
2014-05-12 20:08:19 +02:00
|
|
|
}
|
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
if (files.length !== 1) {
|
|
|
|
return callback({data: 'Node not found.', type: ErrorTypes.notFound});
|
2014-05-12 20:08:19 +02:00
|
|
|
}
|
2016-05-23 22:36:27 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
try {
|
|
|
|
fs.unlinkSync(files[0]);
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
Logger.tag('node', 'delete').error('Could not delete node file: ' + files, error);
|
|
|
|
return callback({data: 'Could not delete node.', type: ErrorTypes.internalError});
|
2016-05-23 22:36:27 +02:00
|
|
|
}
|
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
return callback(null);
|
|
|
|
});
|
|
|
|
}
|
2014-05-12 20:08:19 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
function parseNodeFile(file, callback) {
|
|
|
|
fs.readFile(file, function (err, contents) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2016-06-21 15:21:08 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
const lines = contents.toString();
|
|
|
|
|
|
|
|
const node = {};
|
|
|
|
const nodeSecrets = {};
|
2016-05-18 19:32:19 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
_.each(lines.split('\n'), function (line) {
|
|
|
|
const entries = {};
|
2016-05-18 19:32:19 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
for (const key in linePrefixes) {
|
|
|
|
if (linePrefixes.hasOwnProperty(key)) {
|
|
|
|
const prefix = linePrefixes[key];
|
|
|
|
if (line.substring(0, prefix.length) === prefix) {
|
|
|
|
entries[key] = Strings.normalizeString(line.substr(prefix.length));
|
|
|
|
break;
|
2016-05-18 19:32:19 +02:00
|
|
|
}
|
2018-12-17 22:49:54 +01:00
|
|
|
}
|
2014-05-12 20:08:19 +02:00
|
|
|
}
|
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
if (_.isEmpty(entries) && line.substring(0, 5) === 'key "') {
|
|
|
|
entries.key = Strings.normalizeString(line.split('"')[1]);
|
2014-05-12 20:08:19 +02:00
|
|
|
}
|
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
_.each(entries, function (value, key) {
|
|
|
|
if (key === 'mac') {
|
|
|
|
node.mac = value;
|
|
|
|
node.mapId = _.toLower(value).replace(/:/g, '');
|
|
|
|
} else if (key === 'monitoring') {
|
|
|
|
const active = value === 'aktiv';
|
|
|
|
const pending = value === 'pending';
|
|
|
|
node.monitoring = active || pending;
|
|
|
|
node.monitoringConfirmed = active;
|
|
|
|
node.monitoringState = active ? 'active' : (pending ? 'pending' : 'disabled');
|
|
|
|
} else if (key === 'monitoringToken') {
|
|
|
|
nodeSecrets.monitoringToken = value;
|
|
|
|
} else {
|
|
|
|
node[key] = value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2014-05-12 20:08:19 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
callback(null, node, nodeSecrets);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function findNodeDataByFilePattern(filter, callback) {
|
|
|
|
findNodeFiles(filter, function (err, files) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
2014-05-12 20:08:19 +02:00
|
|
|
}
|
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
if (files.length !== 1) {
|
|
|
|
return callback(null);
|
2014-05-12 20:08:19 +02:00
|
|
|
}
|
2018-12-17 22:49:54 +01:00
|
|
|
|
|
|
|
const file = files[0];
|
|
|
|
return parseNodeFile(file, callback);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getNodeDataByFilePattern(filter, callback) {
|
|
|
|
findNodeDataByFilePattern(filter, function (err, node, nodeSecrets) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
2014-05-12 20:08:19 +02:00
|
|
|
}
|
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
if (!node) {
|
|
|
|
return callback({data: 'Node not found.', type: ErrorTypes.notFound});
|
|
|
|
}
|
2014-05-12 20:08:19 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
callback(null, node, nodeSecrets);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function sendMonitoringConfirmationMail(node, nodeSecrets, callback) {
|
|
|
|
const confirmUrl = UrlBuilder.monitoringConfirmUrl(nodeSecrets);
|
|
|
|
const disableUrl = UrlBuilder.monitoringDisableUrl(nodeSecrets);
|
|
|
|
|
|
|
|
MailService.enqueue(
|
|
|
|
config.server.email.from,
|
|
|
|
node.nickname + ' <' + node.email + '>',
|
|
|
|
'monitoring-confirmation',
|
|
|
|
{
|
|
|
|
node: node,
|
|
|
|
confirmUrl: confirmUrl,
|
|
|
|
disableUrl: disableUrl
|
|
|
|
},
|
|
|
|
function (err) {
|
2016-06-21 22:59:37 +02:00
|
|
|
if (err) {
|
2018-12-17 22:49:54 +01:00
|
|
|
Logger.tag('monitoring', 'confirmation').error('Could not enqueue confirmation mail.', err);
|
|
|
|
return callback({data: 'Internal error.', type: ErrorTypes.internalError});
|
2016-06-21 22:59:37 +02:00
|
|
|
}
|
2016-05-16 18:27:03 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
callback(null);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2016-05-16 18:27:03 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
module.exports = {
|
|
|
|
createNode: function (node, callback) {
|
|
|
|
const token = generateToken();
|
|
|
|
const nodeSecrets = {};
|
2016-06-21 22:59:37 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
node.monitoringConfirmed = false;
|
|
|
|
|
|
|
|
if (node.monitoring) {
|
|
|
|
nodeSecrets.monitoringToken = generateToken();
|
|
|
|
}
|
2016-05-16 18:27:03 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
writeNodeFile(false, token, node, nodeSecrets, function (err, token, node) {
|
2016-06-21 18:20:36 +02:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2014-05-12 20:08:19 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
if (node.monitoring && !node.monitoringConfirmed) {
|
|
|
|
return sendMonitoringConfirmationMail(node, nodeSecrets, function (err) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
2014-05-12 20:08:19 +02:00
|
|
|
}
|
2016-06-21 18:20:36 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
return callback(null, token, node);
|
2016-06-21 18:20:36 +02:00
|
|
|
});
|
2018-12-17 22:49:54 +01:00
|
|
|
}
|
2016-05-18 19:32:19 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
return callback(null, token, node);
|
2016-06-21 18:20:36 +02:00
|
|
|
});
|
2018-12-17 22:49:54 +01:00
|
|
|
},
|
2014-05-12 20:08:19 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
updateNode: function (token, node, callback) {
|
|
|
|
this.getNodeDataByToken(token, function (err, currentNode, nodeSecrets) {
|
2016-06-21 22:59:37 +02:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2016-05-18 22:50:06 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
let monitoringConfirmed = false;
|
|
|
|
let monitoringToken = '';
|
2016-05-18 22:50:06 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
if (node.monitoring) {
|
|
|
|
if (!currentNode.monitoring) {
|
|
|
|
// monitoring just has been enabled
|
|
|
|
monitoringConfirmed = false;
|
|
|
|
monitoringToken = generateToken();
|
2016-05-24 14:42:25 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
} else {
|
|
|
|
// monitoring is still enabled
|
2016-05-24 14:42:25 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
if (currentNode.email !== node.email) {
|
|
|
|
// new email so we need a new token and a reconfirmation
|
|
|
|
monitoringConfirmed = false;
|
|
|
|
monitoringToken = generateToken();
|
2016-05-24 14:42:25 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
} else {
|
|
|
|
// email unchanged, keep token (fix if not set) and confirmation state
|
|
|
|
monitoringConfirmed = currentNode.monitoringConfirmed;
|
|
|
|
monitoringToken = nodeSecrets.monitoringToken || generateToken();
|
|
|
|
}
|
2016-05-20 22:11:35 +02:00
|
|
|
}
|
|
|
|
}
|
2016-05-18 19:32:19 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
node.monitoringConfirmed = monitoringConfirmed;
|
|
|
|
nodeSecrets.monitoringToken = monitoringToken;
|
2016-05-18 19:32:19 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
writeNodeFile(true, token, node, nodeSecrets, function (err, token, node) {
|
2016-05-18 19:32:19 +02:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node.monitoring && !node.monitoringConfirmed) {
|
2016-05-20 22:11:35 +02:00
|
|
|
return sendMonitoringConfirmationMail(node, nodeSecrets, function (err) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return callback(null, token, node);
|
|
|
|
});
|
2016-05-18 19:32:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return callback(null, token, node);
|
|
|
|
});
|
2018-12-17 22:49:54 +01:00
|
|
|
});
|
|
|
|
},
|
2016-05-18 19:32:19 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
internalUpdateNode: function (token, node, nodeSecrets, callback) {
|
|
|
|
writeNodeFile(true, token, node, nodeSecrets, callback);
|
|
|
|
},
|
2016-05-18 19:32:19 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
deleteNode: function (token, callback) {
|
|
|
|
deleteNodeFile(token, callback);
|
|
|
|
},
|
2016-05-18 19:32:19 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
getAllNodes: function (callback) {
|
|
|
|
findNodeFiles({}, function (err, files) {
|
|
|
|
if (err) {
|
|
|
|
Logger.tag('nodes').error('Error getting all nodes:', err);
|
|
|
|
return callback({data: 'Internal error.', type: ErrorTypes.internalError});
|
|
|
|
}
|
2016-05-18 19:32:19 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
async.mapLimit(
|
|
|
|
files,
|
|
|
|
MAX_PARALLEL_NODES_PARSING,
|
|
|
|
parseNodeFile,
|
|
|
|
function (err, nodes) {
|
2016-05-18 19:32:19 +02:00
|
|
|
if (err) {
|
2018-12-17 22:49:54 +01:00
|
|
|
Logger.tag('nodes').error('Error getting all nodes:', err);
|
|
|
|
return callback({data: 'Internal error.', type: ErrorTypes.internalError});
|
2016-05-18 19:32:19 +02:00
|
|
|
}
|
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
return callback(null, nodes);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
},
|
2016-05-18 19:32:19 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
getNodeDataByMac: function (mac, callback) {
|
|
|
|
return findNodeDataByFilePattern({ mac: mac }, callback);
|
|
|
|
},
|
2014-05-12 20:08:19 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
getNodeDataByToken: function (token, callback) {
|
|
|
|
return getNodeDataByFilePattern({ token: token }, callback);
|
|
|
|
},
|
2016-05-18 22:50:06 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
getNodeDataByMonitoringToken: function (monitoringToken, callback) {
|
|
|
|
return getNodeDataByFilePattern({ monitoringToken: monitoringToken }, callback);
|
|
|
|
},
|
2016-05-16 18:27:03 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
fixNodeFilenames: function (callback) {
|
|
|
|
findFilesInPeersPath(function (err, files) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2016-06-07 00:21:26 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
async.mapLimit(
|
|
|
|
files,
|
|
|
|
MAX_PARALLEL_NODES_PARSING,
|
|
|
|
function (file, fileCallback) {
|
|
|
|
parseNodeFile(file, function (err, node, nodeSecrets) {
|
2016-06-21 22:59:37 +02:00
|
|
|
if (err) {
|
2018-12-17 22:49:54 +01:00
|
|
|
return fileCallback(err);
|
2016-06-21 22:59:37 +02:00
|
|
|
}
|
2016-06-07 00:21:26 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
const expectedFilename = toNodeFilename(node.token, node, nodeSecrets);
|
|
|
|
if (file !== expectedFilename) {
|
|
|
|
return fs.rename(file, expectedFilename, function (err) {
|
|
|
|
if (err) {
|
|
|
|
return fileCallback(new Error(
|
|
|
|
'Cannot rename file ' + file + ' to ' + expectedFilename + ' => ' + err
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
fileCallback(null);
|
|
|
|
});
|
|
|
|
}
|
2016-05-24 14:42:25 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
fileCallback(null);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
callback
|
|
|
|
);
|
|
|
|
});
|
|
|
|
},
|
2014-05-12 20:08:19 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
getNodeStatistics: function (callback) {
|
|
|
|
this.getAllNodes(function (err, nodes) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2016-06-21 15:21:08 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
const nodeStatistics = {
|
|
|
|
registered: _.size(nodes),
|
|
|
|
withVPN: 0,
|
|
|
|
withCoords: 0,
|
|
|
|
monitoring: {
|
|
|
|
active: 0,
|
|
|
|
pending: 0
|
2016-06-21 18:20:36 +02:00
|
|
|
}
|
2018-12-17 22:49:54 +01:00
|
|
|
};
|
2016-06-21 15:21:08 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
_.each(nodes, function (node) {
|
|
|
|
if (node.key) {
|
|
|
|
nodeStatistics.withVPN += 1;
|
2016-07-19 13:24:43 +02:00
|
|
|
}
|
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
if (node.coords) {
|
|
|
|
nodeStatistics.withCoords += 1;
|
|
|
|
}
|
2016-07-19 13:24:43 +02:00
|
|
|
|
2018-12-17 22:49:54 +01:00
|
|
|
switch (node.monitoringState) {
|
|
|
|
case 'active':
|
|
|
|
nodeStatistics.monitoring.active += 1;
|
|
|
|
break;
|
|
|
|
case 'pending':
|
|
|
|
nodeStatistics.monitoring.pending += 1;
|
|
|
|
break;
|
|
|
|
}
|
2017-05-06 17:31:34 +02:00
|
|
|
});
|
2018-12-17 22:49:54 +01:00
|
|
|
|
|
|
|
callback(null, nodeStatistics);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|