testing new rate limit middleware

This commit is contained in:
Chris Troutner
2021-03-07 12:42:10 -08:00
parent e6d4bb5443
commit 72ba3c86c7
5 changed files with 49 additions and 10 deletions
+8
View File
@@ -7,6 +7,9 @@ const express = require('express')
const RateLimits = require('./middleware/route-ratelimit')
const rateLimits = new RateLimits()
const RateLimits2 = require('./middleware/route-ratelimit2')
const rateLimits2 = new RateLimits2()
const path = require('path')
const logger = require('morgan')
const wlogger = require('./util/winston-logging')
@@ -103,6 +106,8 @@ const USE_RATE_LIMITS = process.env.USE_RATE_LIMITS
: true
const auth = new AuthMW()
console.log(`USE_RATE_LIMITS: ${USE_RATE_LIMITS}`)
if (USE_RATE_LIMITS) {
// Inspect the header for a JWT token.
app.use(`/${v4prefix}/`, jwtAuth.getTokenFromHeaders)
@@ -111,6 +116,9 @@ if (USE_RATE_LIMITS) {
// Handles Anonymous and Basic Authorization schemes used by passport.js
app.use(`/${v4prefix}/`, auth.mw())
// Experimental rate limits
app.use(`/${v4prefix}/`, rateLimits2.applyRateLimits)
// Rate limit on all v4 routes
// Establish and enforce rate limits.
app.use(`/${v4prefix}/`, rateLimits.rateLimitByResource)
+10 -7
View File
@@ -45,10 +45,10 @@ const rateLimitOptions = {
}
// Constants
const ANON_LIMITS = config.anonRateLimit
const WHITELIST_RATE_LIMIT = config.whitelistRateLimit
// const ANON_LIMITS = config.anonRateLimit
// const WHITELIST_RATE_LIMIT = config.whitelistRateLimit
const WHITELIST_DOMAINS = config.whitelistDomains
const INTERNAL_RATE_LIMIT = 1
// const INTERNAL_RATE_LIMIT = 1
class RateLimits {
constructor () {
@@ -56,7 +56,7 @@ class RateLimits {
this.jwt = jwt
this.rateLimiter = new RateLimiterRedis(rateLimitOptions)
this.config = config
_this.config = config
}
// This is the main middleware funciton of this library. All other functions
@@ -134,15 +134,18 @@ class RateLimits {
// Retrieve the origin.
const origin = req.get('origin')
console.log(`origin: ${origin}`)
// console.log(`WHITELIST_DOMAINS: ${JSON.stringify(WHITELIST_DOMAINS, null, 2)}`)
for (let i = 0; i < WHITELIST_DOMAINS.length; i++) {
const thisDomain = WHITELIST_DOMAINS[i]
if (origin.toString().indexOf(thisDomain) > -1) {
return true
}
// if (origin.toString().indexOf(thisDomain) > -1) {
// return true
// }
if (origin.includes(thisDomain)) return true
}
return retVal