2014-05-12 20:08:19 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
angular.module('ffffng')
|
2016-05-21 17:06:24 +02:00
|
|
|
.service('NodeService', function (
|
|
|
|
config,
|
|
|
|
_,
|
2016-06-07 00:21:26 +02:00
|
|
|
async,
|
2016-05-21 17:06:24 +02:00
|
|
|
crypto,
|
|
|
|
fs,
|
|
|
|
glob,
|
2016-05-24 21:08:34 +02:00
|
|
|
Logger,
|
2016-05-21 17:06:24 +02:00
|
|
|
MailService,
|
|
|
|
Strings,
|
|
|
|
ErrorTypes,
|
|
|
|
UrlBuilder
|
|
|
|
) {
|
2016-06-07 00:21:26 +02:00
|
|
|
var MAX_PARALLEL_NODES_PARSING = 10;
|
|
|
|
|
2014-05-12 20:08:19 +02:00
|
|
|
var linePrefixes = {
|
|
|
|
hostname: '# Knotenname: ',
|
|
|
|
nickname: '# Ansprechpartner: ',
|
|
|
|
email: '# Kontakt: ',
|
|
|
|
coords: '# Koordinaten: ',
|
|
|
|
mac: '# MAC: ',
|
2016-05-18 19:32:19 +02:00
|
|
|
token: '# Token: ',
|
|
|
|
monitoring: '# Monitoring: ',
|
|
|
|
monitoringToken: '# Monitoring-Token: '
|
2014-05-12 20:08:19 +02:00
|
|
|
};
|
|
|
|
|
2016-05-23 22:36:27 +02:00
|
|
|
var filenameParts = ['hostname', 'mac', 'key', 'token', 'monitoringToken'];
|
|
|
|
|
2014-05-12 20:08:19 +02:00
|
|
|
function generateToken() {
|
|
|
|
return crypto.randomBytes(8).toString('hex');
|
|
|
|
}
|
|
|
|
|
2016-05-23 22:36:27 +02:00
|
|
|
function findNodeFiles(filter) {
|
|
|
|
var pattern = _.join(
|
|
|
|
_.map(filenameParts, function (field) {
|
|
|
|
return filter.hasOwnProperty(field) ? filter[field] : '*';
|
|
|
|
}),
|
|
|
|
'@'
|
|
|
|
);
|
|
|
|
|
2014-06-06 21:22:57 +02:00
|
|
|
return glob.sync(config.server.peersPath + '/' + pattern.toLowerCase());
|
2014-05-12 20:08:19 +02:00
|
|
|
}
|
|
|
|
|
2016-05-23 22:36:27 +02:00
|
|
|
function parseNodeFilename(filename) {
|
|
|
|
var parts = _.split(filename, '@', filenameParts.length);
|
|
|
|
var parsed = {};
|
|
|
|
_.each(_.zip(filenameParts, parts), function (part) {
|
|
|
|
parsed[part[0]] = part[1];
|
|
|
|
});
|
|
|
|
return parsed;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isDuplicate(filter, token) {
|
|
|
|
var files = findNodeFiles(filter);
|
2014-05-12 20:08:19 +02:00
|
|
|
if (files.length === 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-05-23 22:36:27 +02:00
|
|
|
if (files.length > 1 || !token /* node is being created*/) {
|
2014-05-12 20:08:19 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-23 22:36:27 +02:00
|
|
|
return parseNodeFilename(files[0]).token !== token;
|
2014-05-12 20:08:19 +02:00
|
|
|
}
|
|
|
|
|
2016-05-23 22:36:27 +02:00
|
|
|
function checkNoDuplicates(token, node, nodeSecrets) {
|
|
|
|
if (isDuplicate({ hostname: node.hostname }, token)) {
|
2014-05-12 20:08:19 +02:00
|
|
|
return {data: {msg: 'Already exists.', field: 'hostname'}, type: ErrorTypes.conflict};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node.key) {
|
2016-05-23 22:36:27 +02:00
|
|
|
if (isDuplicate({ key: node.key }, token)) {
|
2014-05-12 20:08:19 +02:00
|
|
|
return {data: {msg: 'Already exists.', field: 'key'}, type: ErrorTypes.conflict};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-23 22:36:27 +02:00
|
|
|
if (isDuplicate({ mac: node.mac }, token)) {
|
2014-05-12 20:08:19 +02:00
|
|
|
return {data: {msg: 'Already exists.', field: 'mac'}, type: ErrorTypes.conflict};
|
|
|
|
}
|
2016-05-23 22:36:27 +02:00
|
|
|
|
|
|
|
if (nodeSecrets.monitoringToken && isDuplicate({ monitoringToken: nodeSecrets.monitoringToken }, token)) {
|
|
|
|
return {data: {msg: 'Already exists.', field: 'monitoringToken'}, type: ErrorTypes.conflict};
|
|
|
|
}
|
|
|
|
|
2014-05-12 20:08:19 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-05-18 19:32:19 +02:00
|
|
|
function writeNodeFile(isUpdate, token, node, nodeSecrets, callback) {
|
2014-05-12 20:08:19 +02:00
|
|
|
var filename =
|
2016-05-23 22:36:27 +02:00
|
|
|
config.server.peersPath + '/' +
|
|
|
|
(
|
|
|
|
node.hostname + '@' +
|
|
|
|
node.mac + '@' +
|
|
|
|
(node.key || '') + '@' +
|
|
|
|
token + '@' +
|
|
|
|
nodeSecrets.monitoringToken
|
|
|
|
).toLowerCase();
|
2014-05-12 20:08:19 +02:00
|
|
|
|
|
|
|
var data = '';
|
|
|
|
_.each(linePrefixes, function (prefix, key) {
|
2016-05-18 19:32:19 +02:00
|
|
|
var value;
|
|
|
|
switch (key) {
|
|
|
|
case 'monitoring':
|
|
|
|
if (node.monitoring && node.monitoringConfirmed) {
|
|
|
|
value = 'aktiv';
|
|
|
|
} else if (node.monitoring && !node.monitoringConfirmed) {
|
|
|
|
value = 'pending';
|
|
|
|
} else {
|
|
|
|
value = '';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'monitoringToken':
|
|
|
|
value = nodeSecrets.monitoringToken || '';
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
value = key === 'token' ? token : node[key];
|
|
|
|
if (_.isUndefined(value)) {
|
|
|
|
value = _.isUndefined(nodeSecrets[key]) ? '' : nodeSecrets[key];
|
|
|
|
}
|
|
|
|
break;
|
2014-05-12 20:08:19 +02:00
|
|
|
}
|
|
|
|
data += prefix + value + '\n';
|
|
|
|
});
|
|
|
|
if (node.key) {
|
|
|
|
data += 'key "' + node.key + '";\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
// since node.js is single threaded we don't need a lock
|
|
|
|
|
|
|
|
var error;
|
|
|
|
|
|
|
|
if (isUpdate) {
|
2016-05-23 22:36:27 +02:00
|
|
|
var files = findNodeFiles({ token: token });
|
2014-05-12 20:08:19 +02:00
|
|
|
if (files.length !== 1) {
|
|
|
|
return callback({data: 'Node not found.', type: ErrorTypes.notFound});
|
|
|
|
}
|
|
|
|
|
2016-05-23 22:36:27 +02:00
|
|
|
error = checkNoDuplicates(token, node, nodeSecrets);
|
2014-05-12 20:08:19 +02:00
|
|
|
if (error) {
|
|
|
|
return callback(error);
|
|
|
|
}
|
|
|
|
|
2016-05-24 21:08:34 +02:00
|
|
|
var file = files[0];
|
2016-05-18 19:32:19 +02:00
|
|
|
try {
|
|
|
|
fs.unlinkSync(file);
|
|
|
|
}
|
|
|
|
catch (error) {
|
2016-05-24 21:08:34 +02:00
|
|
|
Logger.tag('node', 'save').error('Could not delete old node file: ' + file, error);
|
2016-05-18 19:32:19 +02:00
|
|
|
return callback({data: 'Could not remove old node data.', type: ErrorTypes.internalError});
|
|
|
|
}
|
2014-05-12 20:08:19 +02:00
|
|
|
} else {
|
2016-05-23 22:36:27 +02:00
|
|
|
error = checkNoDuplicates(null, node, nodeSecrets);
|
2014-05-12 20:08:19 +02:00
|
|
|
if (error) {
|
|
|
|
return callback(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
fs.writeFileSync(filename, data, 'utf8');
|
|
|
|
}
|
|
|
|
catch (error) {
|
2016-05-24 21:08:34 +02:00
|
|
|
Logger.tag('node', 'save').error('Could not write node file: ' + filename, error);
|
2014-05-12 20:08:19 +02:00
|
|
|
return callback({data: 'Could not write node data.', type: ErrorTypes.internalError});
|
|
|
|
}
|
|
|
|
|
|
|
|
return callback(null, token, node);
|
|
|
|
}
|
|
|
|
|
2016-05-16 18:27:03 +02:00
|
|
|
function deleteNodeFile(token, callback) {
|
2016-05-23 22:36:27 +02:00
|
|
|
var files = findNodeFiles({ token: token });
|
2016-05-16 18:27:03 +02:00
|
|
|
if (files.length !== 1) {
|
|
|
|
return callback({data: 'Node not found.', type: ErrorTypes.notFound});
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
fs.unlinkSync(files[0]);
|
|
|
|
}
|
|
|
|
catch (error) {
|
2016-05-24 21:08:34 +02:00
|
|
|
Logger.tag('node', 'delete').error('Could not delete node file: ' + file, error);
|
2016-05-16 18:27:03 +02:00
|
|
|
return callback({data: 'Could not delete node.', type: ErrorTypes.internalError});
|
|
|
|
}
|
|
|
|
|
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
|
2014-05-12 20:08:19 +02:00
|
|
|
function parseNodeFile(file, callback) {
|
|
|
|
var lines = fs.readFileSync(file).toString();
|
|
|
|
|
|
|
|
var node = {};
|
2016-05-18 19:32:19 +02:00
|
|
|
var nodeSecrets = {};
|
2014-05-12 20:08:19 +02:00
|
|
|
|
|
|
|
_.each(lines.split('\n'), function (line) {
|
|
|
|
var entries = {};
|
|
|
|
|
|
|
|
for (var key in linePrefixes) {
|
|
|
|
if (linePrefixes.hasOwnProperty(key)) {
|
|
|
|
var prefix = linePrefixes[key];
|
|
|
|
if (line.substring(0, prefix.length) === prefix) {
|
|
|
|
entries[key] = Strings.normalizeString(line.substr(prefix.length));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_.isEmpty(entries) && line.substring(0, 5) === 'key "') {
|
|
|
|
entries.key = Strings.normalizeString(line.split('"')[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
_.each(entries, function (value, key) {
|
2016-05-18 19:32:19 +02:00
|
|
|
if (key === 'monitoring') {
|
|
|
|
var active = value === 'aktiv';
|
|
|
|
var pending = value === 'pending';
|
|
|
|
node.monitoring = active || pending;
|
|
|
|
node.monitoringConfirmed = active;
|
2016-06-07 11:58:29 +02:00
|
|
|
node.monitoringState = active ? 'active' : (pending ? 'pending' : '');
|
2016-05-18 19:32:19 +02:00
|
|
|
} else if (key === 'monitoringToken') {
|
|
|
|
nodeSecrets.monitoringToken = value;
|
|
|
|
} else {
|
|
|
|
node[key] = value;
|
|
|
|
}
|
2014-05-12 20:08:19 +02:00
|
|
|
});
|
|
|
|
});
|
2016-05-18 19:32:19 +02:00
|
|
|
|
|
|
|
callback(null, node, nodeSecrets);
|
2014-05-12 20:08:19 +02:00
|
|
|
}
|
|
|
|
|
2016-05-24 14:42:25 +02:00
|
|
|
function findNodeDataByFilePattern(filter, callback) {
|
2016-05-23 22:36:27 +02:00
|
|
|
var files = findNodeFiles(filter);
|
2016-05-18 22:50:06 +02:00
|
|
|
|
|
|
|
if (files.length !== 1) {
|
2016-05-24 14:42:25 +02:00
|
|
|
return callback(null);
|
2016-05-18 22:50:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var file = files[0];
|
|
|
|
return parseNodeFile(file, callback);
|
|
|
|
}
|
|
|
|
|
2016-05-24 14:42:25 +02:00
|
|
|
function getNodeDataByFilePattern(filter, callback) {
|
|
|
|
findNodeDataByFilePattern(filter, function (err, node, nodeSecrets) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!node) {
|
|
|
|
return callback({data: 'Node not found.', type: ErrorTypes.notFound});
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(null, node, nodeSecrets);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-05-20 22:11:35 +02:00
|
|
|
function sendMonitoringConfirmationMail(node, nodeSecrets, callback) {
|
2016-05-23 22:36:27 +02:00
|
|
|
var confirmUrl = UrlBuilder.monitoringConfirmUrl(nodeSecrets);
|
|
|
|
var disableUrl = UrlBuilder.monitoringDisableUrl(nodeSecrets);
|
2016-05-20 22:11:35 +02:00
|
|
|
|
|
|
|
MailService.enqueue(
|
|
|
|
config.server.email.from,
|
2016-05-21 17:06:24 +02:00
|
|
|
node.nickname + ' <' + node.email + '>',
|
2016-05-20 22:11:35 +02:00
|
|
|
'monitoring-confirmation',
|
|
|
|
{
|
|
|
|
node: node,
|
|
|
|
confirmUrl: confirmUrl,
|
|
|
|
disableUrl: disableUrl
|
|
|
|
},
|
|
|
|
function (err) {
|
2016-05-24 21:08:34 +02:00
|
|
|
if (err) {
|
|
|
|
Logger.tag('monitoring', 'confirmation').error('Could not enqueue confirmation mail.', error);
|
2016-05-20 22:11:35 +02:00
|
|
|
return callback({data: 'Internal error.', type: ErrorTypes.internalError});
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(null);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-05-12 20:08:19 +02:00
|
|
|
return {
|
|
|
|
createNode: function (node, callback) {
|
|
|
|
var token = generateToken();
|
2016-05-18 19:32:19 +02:00
|
|
|
var nodeSecrets = {};
|
|
|
|
|
|
|
|
node.monitoringConfirmed = false;
|
|
|
|
|
|
|
|
if (node.monitoring) {
|
|
|
|
nodeSecrets.monitoringToken = generateToken();
|
|
|
|
}
|
|
|
|
|
|
|
|
writeNodeFile(false, token, node, nodeSecrets, function (err, token, node) {
|
|
|
|
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);
|
|
|
|
});
|
2014-05-12 20:08:19 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
updateNode: function (token, node, callback) {
|
2016-05-18 22:50:06 +02:00
|
|
|
this.getNodeDataByToken(token, function (err, currentNode, nodeSecrets) {
|
2016-05-18 19:32:19 +02:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
var monitoringConfirmed = false;
|
|
|
|
var monitoringToken = '';
|
|
|
|
|
|
|
|
if (node.monitoring) {
|
|
|
|
if (!currentNode.monitoring) {
|
|
|
|
// monitoring just has been enabled
|
|
|
|
monitoringConfirmed = false;
|
|
|
|
monitoringToken = generateToken();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// monitoring is still enabled
|
|
|
|
|
|
|
|
if (currentNode.email != node.email) {
|
|
|
|
// new email so we need a new token and a reconfirmation
|
|
|
|
monitoringConfirmed = false;
|
|
|
|
monitoringToken = generateToken();
|
|
|
|
|
|
|
|
} else {
|
2016-05-18 22:50:06 +02:00
|
|
|
// email unchanged, keep token (fix if not set) and confirmation state
|
2016-05-18 19:32:19 +02:00
|
|
|
monitoringConfirmed = currentNode.monitoringConfirmed;
|
2016-05-18 22:50:06 +02:00
|
|
|
monitoringToken = nodeSecrets.monitoringToken || generateToken();
|
2016-05-18 19:32:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
node.monitoringConfirmed = monitoringConfirmed;
|
|
|
|
nodeSecrets.monitoringToken = monitoringToken;
|
|
|
|
|
|
|
|
writeNodeFile(true, token, node, nodeSecrets, function (err, token, node) {
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
2014-05-12 20:08:19 +02:00
|
|
|
},
|
|
|
|
|
2016-05-18 22:50:06 +02:00
|
|
|
internalUpdateNode: function (token, node, nodeSecrets, callback) {
|
|
|
|
writeNodeFile(true, token, node, nodeSecrets, callback);
|
|
|
|
},
|
|
|
|
|
2016-05-16 18:27:03 +02:00
|
|
|
deleteNode: function (token, callback) {
|
|
|
|
deleteNodeFile(token, callback);
|
|
|
|
},
|
|
|
|
|
2016-06-07 11:58:29 +02:00
|
|
|
getAllNodes: function (callback) {
|
|
|
|
var files = findNodeFiles({});
|
2016-06-07 00:21:26 +02:00
|
|
|
|
|
|
|
async.mapLimit(
|
2016-06-07 11:58:29 +02:00
|
|
|
files,
|
2016-06-07 00:21:26 +02:00
|
|
|
MAX_PARALLEL_NODES_PARSING,
|
|
|
|
parseNodeFile,
|
|
|
|
function (err, nodes) {
|
|
|
|
if (err) {
|
|
|
|
Logger.tag('nodes').error('Error getting all nodes:', error);
|
|
|
|
return callback({data: 'Internal error.', type: ErrorTypes.internalError});
|
|
|
|
}
|
|
|
|
|
2016-06-07 12:39:52 +02:00
|
|
|
return callback(null, nodes);
|
2016-06-07 00:21:26 +02:00
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2016-05-24 14:42:25 +02:00
|
|
|
findNodeDataByMac: function (mac, callback) {
|
|
|
|
return findNodeDataByFilePattern({ mac: mac }, callback);
|
|
|
|
},
|
|
|
|
|
2016-05-18 22:50:06 +02:00
|
|
|
getNodeDataByToken: function (token, callback) {
|
2016-05-23 22:36:27 +02:00
|
|
|
return getNodeDataByFilePattern({ token: token }, callback);
|
2016-05-18 22:50:06 +02:00
|
|
|
},
|
2014-05-12 20:08:19 +02:00
|
|
|
|
2016-05-23 22:36:27 +02:00
|
|
|
getNodeDataByMonitoringToken: function (monitoringToken, callback) {
|
|
|
|
return getNodeDataByFilePattern({ monitoringToken: monitoringToken }, callback);
|
2014-05-12 20:08:19 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|