diff --git a/lib/middlewares/rejectIncompatbleMachines.js b/lib/middlewares/rejectIncompatbleMachines.js new file mode 100644 index 00000000..f105ea84 --- /dev/null +++ b/lib/middlewares/rejectIncompatbleMachines.js @@ -0,0 +1,24 @@ +const semver = require('semver') +const version = require('../../package.json').version +const logger = require('../logger') + +const rejectIncompatibleMachines = function (req, res, next) { + const machineVersion = req.query.version + const deviceId = req.deviceId + + if (!machineVersion) return next() + + const serverMajor = semver.major(version) + const machineMajor = semver.major(machineVersion) + + if (serverMajor - machineMajor > 1) { + logger.error(`Machine version too old: ${machineVersion} deviceId: ${deviceId}`) + return res.status(400).json({ + error: 'Machine version too old' + }) + } + + next() +} + +module.exports = rejectIncompatibleMachines \ No newline at end of file diff --git a/lib/routes.js b/lib/routes.js index 91302dfd..110f9686 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -22,7 +22,7 @@ const logsRoutes = require('./routes/logsRoutes') const pairingRoutes = require('./routes/pairingRoutes') const diagnosticsRoutes = require('./routes/diagnosticsRoutes') const performanceRoutes = require('./routes/performanceRoutes') -const pollingRoutes = require('./routes/pollingRoutes') +const rejectIncompatibleMachines = require('./middlewares/rejectIncompatbleMachines') const stateRoutes = require('./routes/stateRoutes') const termsAndConditionsRoutes = require('./routes/termsAndConditionsRoutes') const { router: txRoutes } = require('./routes/txRoutes') @@ -81,7 +81,6 @@ const loadRoutes = async () => { // other app routes app.use('/graphql', recordPing) - app.use('/poll', pollingRoutes) app.use('/terms_conditions', termsAndConditionsRoutes) app.use('/state', stateRoutes) app.use('/cashbox', cashboxRoutes) @@ -103,6 +102,9 @@ const loadRoutes = async () => { app.use('/probe', probeRoutes) + // Not all requests have the machine version on the url + // rejecting poll is enough to render the machine "stuck" + app.use(rejectIncompatibleMachines) await graphQLServer.start() app.use('/graphql', express.json(), diff --git a/lib/routes/pollingRoutes.js b/lib/routes/pollingRoutes.js deleted file mode 100644 index a433f950..00000000 --- a/lib/routes/pollingRoutes.js +++ /dev/null @@ -1,15 +0,0 @@ -const express = require('express') -const nmd = require('nano-markdown') -const _ = require('lodash/fp') - -const router = express.Router() - -const version = require('../../package.json').version - -function poll (req, res) { - return res.json({ version }) -} - -router.get('/', poll) - -module.exports = router diff --git a/package-lock.json b/package-lock.json index c8abac1c..1e0ffa4e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -86,7 +86,7 @@ "promise-sequential": "^1.1.1", "queue-promise": "^2.2.1", "request-promise": "^4.2.6", - "semver": "^7.1.3", + "semver": "^7.7.1", "serve-static": "^1.12.4", "talisman": "^0.20.0", "telnyx": "^1.25.5", @@ -18226,6 +18226,7 @@ "version": "7.7.1", "resolved": "https://registry.npmjs.org/semver/-/semver-7.7.1.tgz", "integrity": "sha512-hlq8tAfn0m/61p4BVRcPzIGr6LKiMwo4VM6dGi6pt4qcRkmNzTcWq6eCEjEh+qXjkMDvPlOFFSGwQjoEa6gyMA==", + "license": "ISC", "bin": { "semver": "bin/semver.js" }, diff --git a/package.json b/package.json index ca3a7045..8c3c876e 100644 --- a/package.json +++ b/package.json @@ -83,7 +83,7 @@ "promise-sequential": "^1.1.1", "queue-promise": "^2.2.1", "request-promise": "^4.2.6", - "semver": "^7.1.3", + "semver": "^7.7.1", "serve-static": "^1.12.4", "talisman": "^0.20.0", "telnyx": "^1.25.5",