diff --git a/Gruntfile.js b/Gruntfile.js index 16ee631..095812e 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -374,12 +374,6 @@ module.exports = function (grunt) { 'README.md' ] }, - { - expand: true, - cwd: 'bin', - dest: '<%= yeoman.dist %>/bin', - src: ['*'] - }, { expand: true, cwd: 'admin', diff --git a/bin/fix_key_file_names.py b/bin/fix_key_file_names.py deleted file mode 100755 index c15bada..0000000 --- a/bin/fix_key_file_names.py +++ /dev/null @@ -1,74 +0,0 @@ -#!/usr/bin/env python2 - -import os -import sys - -if len(sys.argv) != 2: - print('usage: ' + sys.argv[0] + ' /path/to/peers') - sys.exit(1) - -peersDir = sys.argv[1] - -def normalizeMac(mac): - mac = mac.lower() - normalized = '' - - n = 0 - - for c in mac: - if c != ':': - if n > 0 and n % 2 == 0: - normalized = normalized + ':' - normalized = normalized + c - n += 1 - - return normalized - -def toFilename(peer): - filename = '' - for field in ['name', 'mac', 'vpn', 'token', 'monitoring-token']: - if peer.has_key(field): - filename = filename + peer[field] - filename = filename + '@' - - return filename[:-1] - -for filename in os.listdir(peersDir): - if len(filename) == 0 or filename[0] == '.': - continue - - absFilename = peersDir + '/' + filename - if os.path.isfile(absFilename): - peerFile = open(absFilename, 'r') - try: - peerLines = peerFile.readlines() - peer = {} - mac = None - for line in peerLines: - parts = line.split() - - if len(parts) > 0: - for i in range(0, 3 - len(parts)): - parts.append('') - - if parts[1] == 'Knotenname:': - peer['name'] = parts[2].lower() - elif parts[1] == 'MAC:': - peer['mac'] = normalizeMac(parts[2]) - elif parts[1] == 'Token:': - peer['token'] = parts[2].lower() - elif parts[1] == 'Monitoring-Token:': - peer['monitoring-token'] = parts[2].lower() - elif parts[0] == 'key': - peer['vpn'] = parts[1].split('"')[1].lower() - - newFilename = toFilename(peer) - - if filename != newFilename: - os.rename(absFilename, peersDir + '/' + newFilename) - - except Exception as e: - print('Error in %s, ignoring peer: %s' % (absFilename, e)); - finally: - peerFile.close() - diff --git a/package.json b/package.json index 6511422..05b7bfc 100644 --- a/package.json +++ b/package.json @@ -20,8 +20,7 @@ }, "bin": { - "ffffng": "server/main.js", - "ffffng-fix-key-file-names": "bin/fix_key_file_names.py" + "ffffng": "server/main.js" }, "dependencies": { diff --git a/server/jobs/fixNodeFilenamesJob.js b/server/jobs/fixNodeFilenamesJob.js new file mode 100644 index 0000000..652f277 --- /dev/null +++ b/server/jobs/fixNodeFilenamesJob.js @@ -0,0 +1,17 @@ +'use strict'; + +angular.module('ffffng').factory('FixNodeFilenamesJob', function (NodeService, Logger) { + return { + description: 'Makes sure node files (holding fastd key, name, etc.) are correctly named.', + + run: function (callback) { + NodeService.fixNodeFilenames(function (err) { + if (err) { + Logger.tag('nodes', 'fix-filenames').error('Error fixing filenames:', err); + } + + callback(); + }); + } + }; +}); diff --git a/server/jobs/scheduler.js b/server/jobs/scheduler.js index ddb7748..7c5961d 100644 --- a/server/jobs/scheduler.js +++ b/server/jobs/scheduler.js @@ -69,6 +69,7 @@ angular.module('ffffng').factory('Scheduler', function ($injector, Logger, confi try { schedule('0 */1 * * * *', 'MailQueueJob'); + schedule('15 */1 * * * *', 'FixNodeFilenamesJob'); if (config.client.monitoring.enabled) { schedule('30 */15 * * * *', 'NodeInformationRetrievalJob'); diff --git a/server/services/nodeService.js b/server/services/nodeService.js index 3f89d69..24285a9 100644 --- a/server/services/nodeService.js +++ b/server/services/nodeService.js @@ -44,6 +44,14 @@ angular.module('ffffng') return glob.sync(config.server.peersPath + '/' + pattern.toLowerCase()); } + function findFilesInPeersPath() { + var files = glob.sync(config.server.peersPath + '/*'); + + return _.filter(files, function (file) { + return file[0] !== '.' && fs.lstatSync(file).isFile(); + }); + } + function parseNodeFilename(filename) { var parts = _.split(filename, '@', filenameParts.length); var parsed = {}; @@ -88,18 +96,20 @@ angular.module('ffffng') return null; } - function writeNodeFile(isUpdate, token, node, nodeSecrets, callback) { - var filename = - config.server.peersPath + '/' + - ( - node.hostname + '@' + - node.mac + '@' + - (node.key || '') + '@' + - token + '@' + - (nodeSecrets.monitoringToken || '') - ).toLowerCase(); + function toNodeFilename(token, node, nodeSecrets) { + return config.server.peersPath + '/' + + ( + node.hostname + '@' + + node.mac + '@' + + (node.key || '') + '@' + + token + '@' + + (nodeSecrets.monitoringToken || '') + ).toLowerCase(); + } - var data = ''; + function writeNodeFile(isUpdate, token, node, nodeSecrets, callback) { + var filename = toNodeFilename(token, node, nodeSecrets); + var data = ''; _.each(linePrefixes, function (prefix, key) { var value; switch (key) { @@ -401,6 +411,42 @@ angular.module('ffffng') getNodeDataByMonitoringToken: function (monitoringToken, callback) { return getNodeDataByFilePattern({ monitoringToken: monitoringToken }, callback); + }, + + fixNodeFilenames: function (callback) { + var files = findFilesInPeersPath(); + + async.mapLimit( + files, + MAX_PARALLEL_NODES_PARSING, + function (file, fileCallback) { + parseNodeFile(file, function (err, node, nodeSecrets) { + if (err) { + return fileCallback(err); + } + + var expectedFilename = toNodeFilename(node.token, node, nodeSecrets); + if (file !== expectedFilename) { + try { + fs.renameSync(file, expectedFilename); + } catch (e) { + return fileCallback(new Error( + 'Cannot rename file ' + file + ' to ' + expectedFilename + ' => ' + e + )); + } + } + + fileCallback(null); + }); + }, + function (err) { + if (err) { + return callback(err); + } + + return callback(null); + } + ); } }; });