implemented storage of tunnel tokens

This commit is contained in:
tigerbot 2017-05-29 13:41:09 -06:00
parent d9486b8297
commit 8e2e071abf
2 changed files with 43 additions and 4 deletions

View File

@ -171,7 +171,7 @@ module.exports = function (myDeps, conf, overrideHttp) {
}).then(function (result) { }).then(function (result) {
console.log('got a token from the tunnel server?'); console.log('got a token from the tunnel server?');
result.owner = session.id; result.owner = session.id;
deps.tunneler.add(result); return deps.tunneler.add(result);
}); });
/* /*
}); });

View File

@ -1,9 +1,38 @@
'use strict'; 'use strict';
module.exports.create = function (deps, config) { module.exports.create = function (deps, config) {
var PromiseA = require('bluebird');
var fs = PromiseA.promisifyAll(require('fs'));
var stunnel = require('stunnel'); var stunnel = require('stunnel');
var activeTunnels = {}; var activeTunnels = {};
var tokensPath = require('path').join(__dirname, '..', 'var', 'tokens.json');
var storage = {
all: function () {
var tokens;
try {
tokens = require(tokensPath);
} catch (err) {
tokens = {};
}
return PromiseA.resolve(Object.keys(tokens).map(function (key) {
return tokens[key];
}));
}
, save: function (result) {
var tokens;
try {
tokens = require(tokensPath);
} catch (err) {
tokens = {};
}
tokens[result.jwt] = result;
return fs.writeFileAsync(tokensPath, JSON.stringify(tokens), 'utf8');
}
};
function addToken(data) { function addToken(data) {
if (!data.tunnelUrl) { if (!data.tunnelUrl) {
var decoded; var decoded;
@ -11,11 +40,12 @@ module.exports.create = function (deps, config) {
decoded = JSON.parse(new Buffer(data.jwt.split('.')[1], 'base64').toString('ascii')); decoded = JSON.parse(new Buffer(data.jwt.split('.')[1], 'base64').toString('ascii'));
} catch (err) { } catch (err) {
console.warn('invalid web token given to tunnel manager', err); console.warn('invalid web token given to tunnel manager', err);
return; return PromiseA.reject(err);
} }
if (!decoded.aud) { if (!decoded.aud) {
console.warn('tunnel manager given token with no tunnelUrl or audience'); console.warn('tunnel manager given token with no tunnelUrl or audience');
return; var err = new Error('missing tunnelUrl and audience');
return PromiseA.reject(err);
} }
data.tunnelUrl = 'wss://' + decoded.aud + '/'; data.tunnelUrl = 'wss://' + decoded.aud + '/';
} }
@ -50,8 +80,17 @@ module.exports.create = function (deps, config) {
addToken({ jwt: jwt, owner: 'config' }); addToken({ jwt: jwt, owner: 'config' });
}); });
} }
storage.all().then(function (stored) {
stored.forEach(function (result) {
addToken(result);
});
});
return { return {
add: addToken add: function (data) {
return addToken(data).then(function () {
return storage.save(data);
});
}
}; };
}; };