diff --git a/bin/ssu b/bin/ssu new file mode 100755 index 00000000..6d7b4a50 --- /dev/null +++ b/bin/ssu @@ -0,0 +1,83 @@ +#!/usr/bin/env node + +'use strict' + +var wreck = require('wreck') +var argv = process.argv.slice(2) + +var cmd = argv[0] +var fingerprint = argv[1] + +if (!cmd || !fingerprint) { + console.log('Command line utility for lamassu-server') + console.log('\nUsage: ssu reboot ') + console.log('This will remotely reboot your lamassu-machine.') + process.exit(1) +} + +var opts = {json: true} +wreck.get('http://localhost:7070/pid?fingerprint=' + fingerprint, opts, function (err, res, payload) { + if (err) { + console.log('Please make sure that lamassu-server is running on this box.') + process.exit(2) + } + + if (!payload.pid) { + console.log('The requested lamassu-machine is not connected.') + process.exit(3) + } + + var pid = payload.pid + + if (Date.now() - payload.ts > 10000) { + console.log('lamassu-machine is not connected to server.') + process.exit(6) + } + + var opts2 = { + headers: {'Content-Type': 'application/json'}, + payload: JSON.stringify({pid: pid, fingerprint: fingerprint}) + } + + wreck.post('http://localhost:7070/reboot', opts2, function (err2, res) { + if (err2) { + console.log('Please make sure that lamassu-server is running on this box.') + process.exit(2) + } + + if (res.statusCode !== 200) { + console.log('Communication error') + return + } + + console.log('Rebooting...') + + var ts = null + + setTimeout(function () { + if (Date.now() - ts < 10000) { + console.log('lamassu-machine did not reboot but is still contacting server.') + process.exit(4) + } + + console.log('lamassu-machine rebooted but is not coming back up.') + process.exit(5) + }, 30000) + + setInterval(function () { + wreck.get('http://localhost:7070/pid?fingerprint=' + fingerprint, opts, function (err3, res, payload) { + if (err3) { + console.log('lamassu-server appears to be down.') + process.exit(2) + } + + ts = payload.ts + + if (payload.pid !== pid) { + console.log('lamassu-machine is back up!') + process.exit(0) + } + }) + }, 5000) + }) +}) diff --git a/lib/app.js b/lib/app.js index 709f78d2..6488da6c 100644 --- a/lib/app.js +++ b/lib/app.js @@ -54,9 +54,9 @@ module.exports = function(options) { } plugins.configure(config); next(); - }); + }); }; - + var authMiddleware; if (options.https) { @@ -97,8 +97,14 @@ module.exports = function(options) { if (options.mock) logger.info('In mock mode'); + var localApp = express() + localApp.use(express.bodyParser()) + var localServer = http.createServer(localApp) + var localPort = 7070 + routes.init({ app: app, + localApp: localApp, lamassuConfig: lamassuConfig, plugins: plugins, authMiddleware: authMiddleware, @@ -106,5 +112,9 @@ module.exports = function(options) { mock: options.mock }); + localServer.listen(7070, function () { + console.log('lamassu-server is listening on local port %d', localPort) + }) + return server; }; diff --git a/lib/routes.js b/lib/routes.js index 1647cbd2..e2cf2f11 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -17,11 +17,18 @@ module.exports = { var STALE_TICKER = 180000; var STALE_BALANCE = 180000; +var pids = {} +var reboots = {} + function poll(req, res) { var rateRec = plugins.getDeviceRate(); var balanceRec = plugins.getBalance(); + var fingerprint = getFingerprint(req) + var pid = req.query.pid - logger.debug('poll request from: %s', getFingerprint(req)); + if (pid) pids[fingerprint] = {pid: pid, ts: Date.now()} + + logger.debug('poll request from: %s', fingerprint); // `rateRec` and `balanceRec` are both objects, so there's no danger // of misinterpreting rate or balance === 0 as 'Server initializing'. @@ -59,6 +66,9 @@ function poll(req, res) { plugins.pollQueries(session(req), function(err, results) { if (err) return logger.error(err); var cartridges = results.cartridges; + + var reboot = reboots[fingerprint] === pid + var response = { err: null, rate: rate * settings.commission, @@ -70,7 +80,8 @@ function poll(req, res) { cartridges: cartridges, twoWayMode: cartridges ? true : false, zeroConfLimit: settings.zeroConfLimit, - fiatTxLimit: settings.fiatTxLimit + fiatTxLimit: settings.fiatTxLimit, + reboot: reboot }; if (response.idVerificationEnabled) @@ -185,6 +196,7 @@ function init(localConfig) { var authMiddleware = localConfig.authMiddleware; var reloadConfigMiddleware = localConfig.reloadConfigMiddleware; var app = localConfig.app; + var localApp = localConfig.localApp app.get('/poll', authMiddleware, reloadConfigMiddleware, poll); @@ -200,6 +212,26 @@ function init(localConfig) { app.post('/pair', pair); app.get('/raqia', raqia); + localApp.get('/pid', function (req, res) { + var machineFingerprint = req.query.fingerprint + var pidRec = pids[machineFingerprint] + res.json(pidRec) + }) + + localApp.post('/reboot', function (req, res) { + console.log('DEBUG1') + var pid = req.body.pid + var fingerprint = req.body.fingerprint + console.log('pid: %s, fingerprint: %s', pid, fingerprint) + + if (!fingerprint || !pid) { + return res.send(400) + } + + reboots[fingerprint] = pid + res.send(200) + }) + return app; }