random route stuff
This commit is contained in:
parent
d27ff64a74
commit
9a63772401
8 changed files with 143 additions and 124 deletions
182
lib/routes.js
182
lib/routes.js
|
|
@ -2,10 +2,10 @@
|
|||
|
||||
const morgan = require('morgan')
|
||||
const helmet = require('helmet')
|
||||
const RateLimit = require('express-rate-limit')
|
||||
const bodyParser = require('body-parser')
|
||||
const BigNumber = require('bignumber.js')
|
||||
const _ = require('lodash/fp')
|
||||
const express = require('express')
|
||||
|
||||
const options = require('./options')
|
||||
const logger = require('./logger')
|
||||
|
|
@ -17,9 +17,7 @@ const settingsLoader = require('./settings-loader')
|
|||
const plugins = require('./plugins')
|
||||
const helpers = require('./route-helpers')
|
||||
const poller = require('./poller')
|
||||
const T = require('./time')
|
||||
|
||||
module.exports = {init}
|
||||
const argv = require('minimist')(process.argv.slice(2))
|
||||
|
||||
const CLOCK_SKEW = 60 * 1000
|
||||
const REQUEST_TTL = 3 * 60 * 1000
|
||||
|
|
@ -27,6 +25,8 @@ const REQUEST_TTL = 3 * 60 * 1000
|
|||
const pids = {}
|
||||
const reboots = {}
|
||||
|
||||
const devMode = argv.dev || options.http
|
||||
|
||||
function poll (req, res, next) {
|
||||
const deviceId = req.deviceId
|
||||
const deviceTime = req.deviceTime
|
||||
|
|
@ -153,7 +153,7 @@ function ca (req, res) {
|
|||
|
||||
return pairing.authorizeCaDownload(token)
|
||||
.then(ca => res.json({ca}))
|
||||
.catch(() => res.status(408).end())
|
||||
.catch(() => res.sendStatus(403))
|
||||
}
|
||||
|
||||
function pair (req, res, next) {
|
||||
|
|
@ -320,107 +320,91 @@ function authorize (req, res, next) {
|
|||
return next()
|
||||
}
|
||||
|
||||
throw httpError('Unauthorized', 403)
|
||||
return res.sendStatus(403)
|
||||
})
|
||||
.catch(next)
|
||||
}
|
||||
|
||||
function init (opts) {
|
||||
const skip = options.logLevel === 'debug'
|
||||
? () => false
|
||||
: (req, res) => _.includes(req.path, ['/poll', '/state']) && res.statusCode === 200
|
||||
const skip = options.logLevel === 'debug'
|
||||
? () => false
|
||||
: (req, res) => _.includes(req.path, ['/poll', '/state']) && res.statusCode === 200
|
||||
|
||||
const app = opts.app
|
||||
const localApp = opts.localApp
|
||||
const configRequiredRoutes = [
|
||||
'/poll',
|
||||
'/trade',
|
||||
'/send',
|
||||
'/cash_out',
|
||||
'/dispense_ack',
|
||||
'/event',
|
||||
'/verify_user',
|
||||
'/verify_transaction',
|
||||
'/phone_code'
|
||||
]
|
||||
|
||||
const authMiddleware = opts.devMode
|
||||
? (req, res, next) => next()
|
||||
: authorize
|
||||
const app = express()
|
||||
const localApp = express()
|
||||
|
||||
const configRequiredRoutes = [
|
||||
'/poll',
|
||||
'/trade',
|
||||
'/send',
|
||||
'/cash_out',
|
||||
'/dispense_ack',
|
||||
'/event',
|
||||
'/verify_user',
|
||||
'/verify_transaction',
|
||||
'/phone_code'
|
||||
]
|
||||
app.use(helmet({noCache: true}))
|
||||
app.use(bodyParser.json())
|
||||
app.use(morgan('dev', {skip}))
|
||||
|
||||
const limiter = new RateLimit({
|
||||
windowMs: T.minute,
|
||||
max: 10,
|
||||
delayMs: 0,
|
||||
delayAfter: 0,
|
||||
keyGenerator: () => 'everybody'
|
||||
// These two have their own authorization
|
||||
app.post('/pair', populateDeviceId, pair)
|
||||
app.get('/ca', ca)
|
||||
|
||||
app.use(populateDeviceId)
|
||||
if (!devMode) app.use(authorize)
|
||||
app.use(configRequiredRoutes, populateSettings)
|
||||
app.use(filterOldRequests)
|
||||
app.post('*', cacheAction)
|
||||
|
||||
app.get('/poll', poll)
|
||||
app.post('/trade', trade)
|
||||
app.post('/send', send)
|
||||
app.post('/state', stateChange)
|
||||
app.post('/cash_out', cashOut)
|
||||
app.post('/dispense_ack', dispenseAck)
|
||||
|
||||
app.post('/event', deviceEvent)
|
||||
app.post('/verify_user', verifyUser)
|
||||
app.post('/verify_transaction', verifyTx)
|
||||
|
||||
app.post('/phone_code', phoneCode)
|
||||
app.post('/update_phone', updatePhone)
|
||||
app.get('/phone_tx', fetchPhoneTx)
|
||||
app.post('/register_redeem/:txId', registerRedeem)
|
||||
app.get('/await_dispense/:txId', waitForDispense)
|
||||
app.post('/dispense', dispense)
|
||||
|
||||
app.use(errorHandler)
|
||||
|
||||
localApp.get('/pid', (req, res) => {
|
||||
const deviceId = req.query.device_id
|
||||
const pidRec = pids[deviceId]
|
||||
res.json(pidRec)
|
||||
})
|
||||
|
||||
localApp.post('/reboot', (req, res) => {
|
||||
const pid = req.body.pid
|
||||
const deviceId = req.body.deviceId
|
||||
|
||||
if (!deviceId || !pid) {
|
||||
return res.sendStatus(400)
|
||||
}
|
||||
|
||||
reboots[deviceId] = pid
|
||||
res.sendStatus(200)
|
||||
})
|
||||
|
||||
localApp.post('/dbChange', (req, res, next) => {
|
||||
return settingsLoader.loadLatest()
|
||||
.then(poller.reload)
|
||||
.then(() => logger.info('Config reloaded'))
|
||||
.catch(err => {
|
||||
logger.error(err)
|
||||
res.sendStatus(500)
|
||||
})
|
||||
|
||||
app.use(morgan('dev', {skip}))
|
||||
app.use(helmet())
|
||||
app.use(populateDeviceId)
|
||||
app.use(configRequiredRoutes, populateSettings)
|
||||
app.use(bodyParser.json())
|
||||
app.use(filterOldRequests)
|
||||
app.post('*', cacheAction)
|
||||
|
||||
app.post('/pair', limiter, pair)
|
||||
app.get('/ca', limiter, ca)
|
||||
|
||||
app.get('/poll', authMiddleware, poll)
|
||||
|
||||
app.post('/trade', authMiddleware, trade)
|
||||
app.post('/send', authMiddleware, send)
|
||||
app.post('/state', authMiddleware, stateChange)
|
||||
app.post('/cash_out', authMiddleware, cashOut)
|
||||
app.post('/dispense_ack', authMiddleware, dispenseAck)
|
||||
|
||||
app.post('/event', authMiddleware, deviceEvent)
|
||||
app.post('/verify_user', authMiddleware, verifyUser)
|
||||
app.post('/verify_transaction', authMiddleware, verifyTx)
|
||||
|
||||
app.post('/phone_code', authMiddleware, phoneCode)
|
||||
app.post('/update_phone', authMiddleware, updatePhone)
|
||||
app.get('/phone_tx', authMiddleware, fetchPhoneTx)
|
||||
app.post('/register_redeem/:txId', authMiddleware, registerRedeem)
|
||||
app.get('/await_dispense/:txId', authMiddleware, waitForDispense)
|
||||
app.post('/dispense', authMiddleware, dispense)
|
||||
|
||||
app.use('*', errorHandler)
|
||||
|
||||
localApp.get('/pid', (req, res) => {
|
||||
const deviceId = req.query.device_id
|
||||
const pidRec = pids[deviceId]
|
||||
res.json(pidRec)
|
||||
})
|
||||
|
||||
localApp.post('/reboot', (req, res) => {
|
||||
const pid = req.body.pid
|
||||
const deviceId = req.body.deviceId
|
||||
|
||||
if (!deviceId || !pid) {
|
||||
return res.sendStatus(400)
|
||||
}
|
||||
|
||||
reboots[deviceId] = pid
|
||||
res.sendStatus(200)
|
||||
})
|
||||
|
||||
localApp.post('/dbChange', (req, res, next) => {
|
||||
return settingsLoader.loadLatest()
|
||||
.then(poller.reload)
|
||||
.then(() => logger.info('Config reloaded'))
|
||||
.catch(err => {
|
||||
logger.error(err)
|
||||
res.sendStatus(500)
|
||||
})
|
||||
})
|
||||
|
||||
setInterval(pruneIdempotents, 60000)
|
||||
|
||||
return app
|
||||
}
|
||||
})
|
||||
|
||||
function populateDeviceId (req, res, next) {
|
||||
const deviceId = ((typeof req.connection.getPeerCertificate === 'function' &&
|
||||
|
|
@ -449,3 +433,7 @@ function populateSettings (req, res, next) {
|
|||
.then(() => next())
|
||||
.catch(next)
|
||||
}
|
||||
|
||||
setInterval(pruneIdempotents, 60000)
|
||||
|
||||
module.exports = {app, localApp}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue