mirror of
https://github.com/fullstack-cash/bch-api.git
synced 2026-09-21 16:52:04 -07:00
Added env var to turn off rate limits for local installation
This commit is contained in:
+1
-1
@@ -22,7 +22,7 @@
|
|||||||
"coverage": "nyc report --reporter=text-lcov | coveralls",
|
"coverage": "nyc report --reporter=text-lcov | coveralls",
|
||||||
"coverage:report": "export NETWORK=mainnet && nyc --reporter=html mocha --timeout 25000 test/v4/",
|
"coverage:report": "export NETWORK=mainnet && nyc --reporter=html mocha --timeout 25000 test/v4/",
|
||||||
"docs": "./node_modules/.bin/apidoc -i src/routes/v4 -o docs",
|
"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"
|
"test:temp2": "mocha test/v4/rate-limit2-unit.js"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
|
|||||||
+11
-6
@@ -100,15 +100,18 @@ app.use('/', logReqInfo)
|
|||||||
const v4prefix = 'v4'
|
const v4prefix = 'v4'
|
||||||
|
|
||||||
// START Rate Limits
|
// 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()
|
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.
|
// Inspect the header for a JWT token.
|
||||||
app.use(`/${v4prefix}/`, jwtAuth.getTokenFromHeaders)
|
app.use(`/${v4prefix}/`, jwtAuth.getTokenFromHeaders)
|
||||||
|
|
||||||
@@ -122,6 +125,8 @@ if (USE_RATE_LIMITS) {
|
|||||||
// Rate limit on all v4 routes
|
// Rate limit on all v4 routes
|
||||||
// Establish and enforce rate limits.
|
// Establish and enforce rate limits.
|
||||||
// app.use(`/${v4prefix}/`, rateLimits.rateLimitByResource)
|
// app.use(`/${v4prefix}/`, rateLimits.rateLimitByResource)
|
||||||
|
} else {
|
||||||
|
console.log('Rate limits are NOT being used')
|
||||||
}
|
}
|
||||||
// END Rate Limits
|
// END Rate Limits
|
||||||
|
|
||||||
|
|||||||
@@ -68,28 +68,6 @@ class RateLimits {
|
|||||||
// support this function.
|
// support this function.
|
||||||
async applyRateLimits (req, res, next) {
|
async applyRateLimits (req, res, next) {
|
||||||
try {
|
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.
|
// Exit if the user has already authenticated with Basic Authentication.
|
||||||
if (req.locals.proLimit) {
|
if (req.locals.proLimit) {
|
||||||
console.log('External call, basic auth, skipping rate limits.')
|
console.log('External call, basic auth, skipping rate limits.')
|
||||||
@@ -390,6 +368,34 @@ class RateLimits {
|
|||||||
throw err
|
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
|
module.exports = RateLimits
|
||||||
|
|||||||
@@ -114,6 +114,7 @@ class Encryption {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// console.log(
|
||||||
wlogger.debug(
|
wlogger.debug(
|
||||||
'Executing encryption/getPublicKey with this address: ',
|
'Executing encryption/getPublicKey with this address: ',
|
||||||
cashAddr
|
cashAddr
|
||||||
@@ -180,6 +181,7 @@ class Encryption {
|
|||||||
publicKey: 'not found'
|
publicKey: 'not found'
|
||||||
})
|
})
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
console.log('Error in encryption.js/getPublicKey().', err)
|
||||||
wlogger.error('Error in encryption.js/getPublicKey().', err)
|
wlogger.error('Error in encryption.js/getPublicKey().', err)
|
||||||
|
|
||||||
return _this.errorHandler(err, res)
|
return _this.errorHandler(err, res)
|
||||||
|
|||||||
Reference in New Issue
Block a user