From 2fa911c04edd41bd29dc0bc8ecc4f600af3fa754 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Tue, 9 Mar 2021 06:47:26 -0800 Subject: [PATCH] Added env var to turn off rate limits for local installation --- package.json | 2 +- src/app.js | 17 ++++++---- src/middleware/route-ratelimit2.js | 50 +++++++++++++++++------------- src/routes/v4/encryption.js | 2 ++ 4 files changed, 42 insertions(+), 29 deletions(-) diff --git a/package.json b/package.json index add77ab..045c5f7 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "coverage": "nyc report --reporter=text-lcov | coveralls", "coverage:report": "export NETWORK=mainnet && nyc --reporter=html mocha --timeout 25000 test/v4/", "docs": "./node_modules/.bin/apidoc -i src/routes/v4 -o docs", - "test:temp1": "export NETWORK=mainnet && export TEST=integration && mocha --exit --timeout 25000 -g '#hydrateUtxosWL' test/v4/integration/", + "test:temp1": "export NETWORK=mainnet && export TEST=integration && mocha --exit --timeout 25000 -g '#hydrateUtxos-' test/v4/integration/", "test:temp2": "mocha test/v4/rate-limit2-unit.js" }, "engines": { diff --git a/src/app.js b/src/app.js index bf8be4e..03e0d1d 100644 --- a/src/app.js +++ b/src/app.js @@ -100,15 +100,18 @@ app.use('/', logReqInfo) const v4prefix = 'v4' // START Rate Limits -// Allow users to turn off rate limits with an environment variable. -const USE_RATE_LIMITS = process.env.USE_RATE_LIMITS - ? process.env.USE_RATE_LIMITS - : true const auth = new AuthMW() -console.log(`USE_RATE_LIMITS: ${USE_RATE_LIMITS}`) +// Allow users to turn off rate limits with an environment variable. +const DO_NOT_USE_RATE_LIMITS = process.env.DO_NOT_USE_RATE_LIMITS || false -if (USE_RATE_LIMITS) { +console.log(`DO_NOT_USE_RATE_LIMITS: ${DO_NOT_USE_RATE_LIMITS}`) + +// Ensure req.locals and res.locals objects exist. +app.use(`/${v4prefix}/`, rateLimits2.populateLocals) + +if (!DO_NOT_USE_RATE_LIMITS) { + console.log('Rate limits are being used') // Inspect the header for a JWT token. app.use(`/${v4prefix}/`, jwtAuth.getTokenFromHeaders) @@ -122,6 +125,8 @@ if (USE_RATE_LIMITS) { // Rate limit on all v4 routes // Establish and enforce rate limits. // app.use(`/${v4prefix}/`, rateLimits.rateLimitByResource) +} else { + console.log('Rate limits are NOT being used') } // END Rate Limits diff --git a/src/middleware/route-ratelimit2.js b/src/middleware/route-ratelimit2.js index 2a4988a..06b7141 100644 --- a/src/middleware/route-ratelimit2.js +++ b/src/middleware/route-ratelimit2.js @@ -68,28 +68,6 @@ class RateLimits { // support this function. async applyRateLimits (req, res, next) { try { - // console.log('Starting applyRateLimits()') - // let userId - // const decoded = {} - - // Create a re*Q*.locals object if not passed in. - // req.locals.proLimit will be true if the user is using Basic Authentication. - if (!req.locals) { - req.locals = { - // default values - jwtToken: '', - proLimit: false, - apiLevel: 0 - } - } - - // Create a re*S*.locals object if it does not exist. - if (!res.locals) { - res.locals = { - rateLimitTriggered: false - } - } - // Exit if the user has already authenticated with Basic Authentication. if (req.locals.proLimit) { console.log('External call, basic auth, skipping rate limits.') @@ -390,6 +368,34 @@ class RateLimits { throw err } } + + // Called when rate limits are not used. + populateLocals (req, res, next) { + try { + // Create a re*Q*.locals object if not passed in. + // req.locals.proLimit will be true if the user is using Basic Authentication. + if (!req.locals) { + req.locals = { + // default values + jwtToken: '', + proLimit: false, + apiLevel: 0 + } + } + + // Create a re*S*.locals object if it does not exist. + if (!res.locals) { + res.locals = { + rateLimitTriggered: false + } + } + + next() + } catch (err) { + console.error('Error in populateLocals(): ', err) + throw err + } + } } module.exports = RateLimits diff --git a/src/routes/v4/encryption.js b/src/routes/v4/encryption.js index a910219..7199ec5 100644 --- a/src/routes/v4/encryption.js +++ b/src/routes/v4/encryption.js @@ -114,6 +114,7 @@ class Encryption { }) } + // console.log( wlogger.debug( 'Executing encryption/getPublicKey with this address: ', cashAddr @@ -180,6 +181,7 @@ class Encryption { publicKey: 'not found' }) } catch (err) { + console.log('Error in encryption.js/getPublicKey().', err) wlogger.error('Error in encryption.js/getPublicKey().', err) return _this.errorHandler(err, res)