chore: server code formatting
This commit is contained in:
parent
aedabcbdee
commit
68517170e2
234 changed files with 9824 additions and 6195 deletions
|
|
@ -2,7 +2,8 @@ const pairing = require('../pairing')
|
|||
const logger = require('../logger')
|
||||
|
||||
const authorize = function (req, res, next) {
|
||||
return pairing.isPaired(req.deviceId)
|
||||
return pairing
|
||||
.isPaired(req.deviceId)
|
||||
.then(deviceName => {
|
||||
if (deviceName) {
|
||||
req.deviceName = deviceName
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
const pairing = require('../pairing')
|
||||
const logger = require('../logger')
|
||||
|
||||
function ca (req, res) {
|
||||
function ca(req, res) {
|
||||
const token = req.query.token
|
||||
|
||||
return pairing.authorizeCaDownload(token)
|
||||
return pairing
|
||||
.authorizeCaDownload(token)
|
||||
.then(ca => res.json({ ca }))
|
||||
.catch(error => {
|
||||
logger.error(error.message)
|
||||
|
|
|
|||
|
|
@ -1,9 +1,7 @@
|
|||
const logger = require('../logger')
|
||||
|
||||
function errorHandler (err, req, res, next) {
|
||||
const statusCode = err.name === 'HTTPError'
|
||||
? err.code || 500
|
||||
: 500
|
||||
function errorHandler(err, req, res) {
|
||||
const statusCode = err.name === 'HTTPError' ? err.code || 500 : 500
|
||||
|
||||
const json = { error: err.message }
|
||||
|
||||
|
|
|
|||
|
|
@ -5,19 +5,23 @@ const CLOCK_SKEW = 60 * 1000
|
|||
const REQUEST_TTL = 3 * 60 * 1000
|
||||
const THROTTLE_CLOCK_SKEW = 60 * 1000
|
||||
|
||||
function filterOldRequests (req, res, next) {
|
||||
function filterOldRequests(req, res, next) {
|
||||
const deviceTime = req.deviceTime
|
||||
const deviceId = req.deviceId
|
||||
const timestamp = Date.now()
|
||||
const delta = timestamp - Date.parse(deviceTime)
|
||||
|
||||
const shouldTrigger = !state.canLogClockSkewMap[deviceId] ||
|
||||
const shouldTrigger =
|
||||
!state.canLogClockSkewMap[deviceId] ||
|
||||
timestamp - state.canLogClockSkewMap[deviceId] >= THROTTLE_CLOCK_SKEW
|
||||
|
||||
if (delta > CLOCK_SKEW && shouldTrigger) {
|
||||
state.canLogClockSkewMap[deviceId] = timestamp
|
||||
logger.error('Clock skew with lamassu-machine[%s] too high [%ss], adjust lamassu-machine clock',
|
||||
req.deviceName, (delta / 1000).toFixed(2))
|
||||
logger.error(
|
||||
'Clock skew with lamassu-machine[%s] too high [%ss], adjust lamassu-machine clock',
|
||||
req.deviceName,
|
||||
(delta / 1000).toFixed(2),
|
||||
)
|
||||
}
|
||||
|
||||
if (delta > REQUEST_TTL) return res.status(408).json({ error: 'stale' })
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
const { getOperatorId } = require('../operator')
|
||||
|
||||
function findOperatorId (req, res, next) {
|
||||
function findOperatorId(req, res, next) {
|
||||
return getOperatorId('middleware')
|
||||
.then(operatorId => {
|
||||
res.locals.operatorId = operatorId
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
const crypto = require('crypto')
|
||||
|
||||
function sha256 (buf) {
|
||||
function sha256(buf) {
|
||||
if (!buf) return null
|
||||
const hash = crypto.createHash('sha256')
|
||||
|
||||
|
|
@ -9,10 +9,13 @@ function sha256 (buf) {
|
|||
}
|
||||
|
||||
const populateDeviceId = function (req, res, next) {
|
||||
const peerCert = req.socket.getPeerCertificate ? req.socket.getPeerCertificate() : null
|
||||
const peerCert = req.socket.getPeerCertificate
|
||||
? req.socket.getPeerCertificate()
|
||||
: null
|
||||
const deviceId = peerCert?.raw ? sha256(peerCert.raw) : null
|
||||
|
||||
if (!deviceId) return res.status(500).json({ error: 'Unable to find certificate' })
|
||||
|
||||
if (!deviceId)
|
||||
return res.status(500).json({ error: 'Unable to find certificate' })
|
||||
req.deviceId = deviceId
|
||||
req.deviceTime = req.get('date')
|
||||
|
||||
|
|
|
|||
|
|
@ -3,57 +3,74 @@ const state = require('./state')
|
|||
const newSettingsLoader = require('../new-settings-loader')
|
||||
const logger = require('../logger')
|
||||
|
||||
db.connect({ direct: true }).then(sco => {
|
||||
sco.client.on('notification', data => {
|
||||
const parsedData = JSON.parse(data.payload)
|
||||
return reload(parsedData.operatorId)
|
||||
db.connect({ direct: true })
|
||||
.then(sco => {
|
||||
sco.client.on('notification', data => {
|
||||
const parsedData = JSON.parse(data.payload)
|
||||
return reload(parsedData.operatorId)
|
||||
})
|
||||
return sco.none('LISTEN $1:name', 'reload')
|
||||
})
|
||||
return sco.none('LISTEN $1:name', 'reload')
|
||||
}).catch(console.error)
|
||||
.catch(console.error)
|
||||
|
||||
db.connect({ direct: true }).then(sco => {
|
||||
sco.client.on('notification', data => {
|
||||
const parsedData = JSON.parse(data.payload)
|
||||
return machineAction(parsedData.action, parsedData.value)
|
||||
db.connect({ direct: true })
|
||||
.then(sco => {
|
||||
sco.client.on('notification', data => {
|
||||
const parsedData = JSON.parse(data.payload)
|
||||
return machineAction(parsedData.action, parsedData.value)
|
||||
})
|
||||
return sco.none('LISTEN $1:name', 'machineAction')
|
||||
})
|
||||
return sco.none('LISTEN $1:name', 'machineAction')
|
||||
}).catch(console.error)
|
||||
.catch(console.error)
|
||||
|
||||
function machineAction (type, value) {
|
||||
function machineAction(type, value) {
|
||||
const deviceId = value.deviceId
|
||||
const operatorId = value.operatorId
|
||||
const pid = state.pids?.[operatorId]?.[deviceId]?.pid
|
||||
|
||||
switch (type) {
|
||||
case 'reboot':
|
||||
logger.debug(`Rebooting machine '${deviceId}' from operator ${operatorId}`)
|
||||
logger.debug(
|
||||
`Rebooting machine '${deviceId}' from operator ${operatorId}`,
|
||||
)
|
||||
state.reboots[operatorId] = { [deviceId]: pid }
|
||||
break
|
||||
case 'shutdown':
|
||||
logger.debug(`Shutting down machine '${deviceId}' from operator ${operatorId}`)
|
||||
logger.debug(
|
||||
`Shutting down machine '${deviceId}' from operator ${operatorId}`,
|
||||
)
|
||||
state.shutdowns[operatorId] = { [deviceId]: pid }
|
||||
break
|
||||
case 'restartServices':
|
||||
logger.debug(`Restarting services of machine '${deviceId}' from operator ${operatorId}`)
|
||||
logger.debug(
|
||||
`Restarting services of machine '${deviceId}' from operator ${operatorId}`,
|
||||
)
|
||||
state.restartServicesMap[operatorId] = { [deviceId]: pid }
|
||||
break
|
||||
case 'emptyUnit':
|
||||
logger.debug(`Emptying units from machine '${deviceId}' from operator ${operatorId}`)
|
||||
logger.debug(
|
||||
`Emptying units from machine '${deviceId}' from operator ${operatorId}`,
|
||||
)
|
||||
state.emptyUnit[operatorId] = { [deviceId]: pid }
|
||||
break
|
||||
case 'refillUnit':
|
||||
logger.debug(`Refilling recyclers from machine '${deviceId}' from operator ${operatorId}`)
|
||||
logger.debug(
|
||||
`Refilling recyclers from machine '${deviceId}' from operator ${operatorId}`,
|
||||
)
|
||||
state.refillUnit[operatorId] = { [deviceId]: pid }
|
||||
break
|
||||
case 'diagnostics':
|
||||
logger.debug(`Running diagnostics on machine '${deviceId}' from operator ${operatorId}`)
|
||||
logger.debug(
|
||||
`Running diagnostics on machine '${deviceId}' from operator ${operatorId}`,
|
||||
)
|
||||
state.diagnostics[operatorId] = { [deviceId]: pid }
|
||||
break
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
function reload (operatorId) {
|
||||
function reload(operatorId) {
|
||||
state.needsSettingsReload[operatorId] = true
|
||||
}
|
||||
|
||||
|
|
@ -73,11 +90,14 @@ const populateSettings = function (req, res, next) {
|
|||
// 4. There's no cached config, cache and send the latest config
|
||||
|
||||
if (versionId) {
|
||||
const cachedVersionedSettings = settingsCache.get(`${operatorId}-v${versionId}`)
|
||||
const cachedVersionedSettings = settingsCache.get(
|
||||
`${operatorId}-v${versionId}`,
|
||||
)
|
||||
|
||||
if (!cachedVersionedSettings) {
|
||||
logger.debug('Fetching a specific config version cached value')
|
||||
return newSettingsLoader.load(versionId)
|
||||
return newSettingsLoader
|
||||
.load(versionId)
|
||||
.then(settings => {
|
||||
settingsCache.set(`${operatorId}-v${versionId}`, settings)
|
||||
req.settings = settings
|
||||
|
|
@ -94,16 +114,22 @@ const populateSettings = function (req, res, next) {
|
|||
const operatorSettings = settingsCache.get(`${operatorId}-latest`)
|
||||
|
||||
if (!!needsSettingsReload[operatorId] || !operatorSettings) {
|
||||
!!needsSettingsReload[operatorId]
|
||||
? logger.debug('Fetching and caching a new latest config value, as a reload was requested')
|
||||
: logger.debug('Fetching the latest config version because there\'s no cached value')
|
||||
needsSettingsReload[operatorId]
|
||||
? logger.debug(
|
||||
'Fetching and caching a new latest config value, as a reload was requested',
|
||||
)
|
||||
: logger.debug(
|
||||
"Fetching the latest config version because there's no cached value",
|
||||
)
|
||||
|
||||
return newSettingsLoader.loadLatest()
|
||||
return newSettingsLoader
|
||||
.loadLatest()
|
||||
.then(settings => {
|
||||
const versionId = settings.version
|
||||
settingsCache.set(`${operatorId}-latest`, settings)
|
||||
settingsCache.set(`${operatorId}-v${versionId}`, settings)
|
||||
if (!!needsSettingsReload[operatorId]) delete needsSettingsReload[operatorId]
|
||||
if (needsSettingsReload[operatorId])
|
||||
delete needsSettingsReload[operatorId]
|
||||
req.settings = settings
|
||||
})
|
||||
.then(() => next())
|
||||
|
|
|
|||
|
|
@ -12,20 +12,24 @@ const rejectIncompatibleMachines = function (req, res, next) {
|
|||
const machineMajor = semver.major(machineVersion)
|
||||
|
||||
if (serverMajor - machineMajor > 1) {
|
||||
logger.error(`Machine version too old: ${machineVersion} deviceId: ${deviceId}`)
|
||||
logger.error(
|
||||
`Machine version too old: ${machineVersion} deviceId: ${deviceId}`,
|
||||
)
|
||||
return res.status(400).json({
|
||||
error: 'Machine version too old'
|
||||
error: 'Machine version too old',
|
||||
})
|
||||
}
|
||||
|
||||
if (serverMajor < machineMajor) {
|
||||
logger.error(`Machine version too new: ${machineVersion} deviceId: ${deviceId}`)
|
||||
logger.error(
|
||||
`Machine version too new: ${machineVersion} deviceId: ${deviceId}`,
|
||||
)
|
||||
return res.status(400).json({
|
||||
error: 'Machine version too new'
|
||||
error: 'Machine version too new',
|
||||
})
|
||||
}
|
||||
|
||||
next()
|
||||
}
|
||||
|
||||
module.exports = rejectIncompatibleMachines
|
||||
module.exports = rejectIncompatibleMachines
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ module.exports = (function () {
|
|||
needsSettingsReload: {},
|
||||
settingsCache: new NodeCache({
|
||||
stdTTL: SETTINGS_CACHE_REFRESH,
|
||||
checkperiod: SETTINGS_CACHE_REFRESH // Clear cache every hour
|
||||
checkperiod: SETTINGS_CACHE_REFRESH, // Clear cache every hour
|
||||
}),
|
||||
canLogClockSkewMap: {},
|
||||
canGetLastSeenMap: {},
|
||||
|
|
@ -18,6 +18,6 @@ module.exports = (function () {
|
|||
emptyUnit: {},
|
||||
refillUnit: {},
|
||||
diagnostics: {},
|
||||
mnemonic: null
|
||||
mnemonic: null,
|
||||
}
|
||||
}())
|
||||
})()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue