mirror of
https://github.com/fullstack-cash/bch-api.git
synced 2026-09-22 01:02:05 -07:00
Added logging to new rate limiter
This commit is contained in:
@@ -4,6 +4,8 @@ const express = require("express")
|
|||||||
const RateLimit = require("express-rate-limit")
|
const RateLimit = require("express-rate-limit")
|
||||||
const axios = require("axios")
|
const axios = require("axios")
|
||||||
|
|
||||||
|
const wlogger = require("../util/winston-logging")
|
||||||
|
|
||||||
const jwt = require("jsonwebtoken")
|
const jwt = require("jsonwebtoken")
|
||||||
const KeyEncoder = require("key-encoder").default
|
const KeyEncoder = require("key-encoder").default
|
||||||
const keyEncoder = new KeyEncoder("secp256k1")
|
const keyEncoder = new KeyEncoder("secp256k1")
|
||||||
@@ -64,6 +66,7 @@ class RateLimits {
|
|||||||
will be downgraded to 0 on-the-fly. Indexer endpoints will effectively be
|
will be downgraded to 0 on-the-fly. Indexer endpoints will effectively be
|
||||||
downgraded to the anonymous access tier.
|
downgraded to the anonymous access tier.
|
||||||
*/
|
*/
|
||||||
|
// CT 2/7/20: Older rate-limiting code that does not scale well.
|
||||||
async routeRateLimit(req, res, next) {
|
async routeRateLimit(req, res, next) {
|
||||||
// Disable rate limiting if 0 passed from RATE_LIMIT_MAX_REQUESTS
|
// Disable rate limiting if 0 passed from RATE_LIMIT_MAX_REQUESTS
|
||||||
if (maxRequests === 0) return next()
|
if (maxRequests === 0) return next()
|
||||||
@@ -195,6 +198,7 @@ class RateLimits {
|
|||||||
// It does fine-grane analysis on the data coming from the auth servers and
|
// It does fine-grane analysis on the data coming from the auth servers and
|
||||||
// uses its output to adjust rate limits on-the-fly based on the users
|
// uses its output to adjust rate limits on-the-fly based on the users
|
||||||
// permission level.
|
// permission level.
|
||||||
|
// CT 2/7/20: I believe this is older code that is only used by routeRateLimit.
|
||||||
evalUserPermissioins(req, authData) {
|
evalUserPermissioins(req, authData) {
|
||||||
// console.log(`authData: ${JSON.stringify(authData, null, 2)}`)
|
// console.log(`authData: ${JSON.stringify(authData, null, 2)}`)
|
||||||
|
|
||||||
@@ -269,7 +273,7 @@ class RateLimits {
|
|||||||
|
|
||||||
userId = decoded.id
|
userId = decoded.id
|
||||||
} else {
|
} else {
|
||||||
console.log(`No JWT token found!`)
|
wlogger.debug(`No JWT token found!`)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Code here for the rate limiter is adapted from this example:
|
// Code here for the rate limiter is adapted from this example:
|
||||||
@@ -277,7 +281,7 @@ class RateLimits {
|
|||||||
try {
|
try {
|
||||||
// The resource being consumed: full node, indexer, SLPDB, etc.
|
// The resource being consumed: full node, indexer, SLPDB, etc.
|
||||||
const resource = _this.getResource(req.url)
|
const resource = _this.getResource(req.url)
|
||||||
console.log(`resource: ${resource}`)
|
wlogger.debug(`resource: ${resource}`)
|
||||||
|
|
||||||
let key = userId ? userId : req.ip
|
let key = userId ? userId : req.ip
|
||||||
|
|
||||||
@@ -285,7 +289,7 @@ class RateLimits {
|
|||||||
decoded.resource = resource
|
decoded.resource = resource
|
||||||
const pointsToConsume = _this.calcPoints(decoded)
|
const pointsToConsume = _this.calcPoints(decoded)
|
||||||
|
|
||||||
console.log(
|
wlogger.info(
|
||||||
`User ${key} consuming ${pointsToConsume} point for resource ${resource}.`
|
`User ${key} consuming ${pointsToConsume} point for resource ${resource}.`
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -303,7 +307,7 @@ class RateLimits {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(`Error in route-ratelimit.js/newRateLimit(): `, err)
|
wlogger.error(`Error in route-ratelimit.js/newRateLimit(): `, err)
|
||||||
// throw err
|
// throw err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -324,7 +328,7 @@ class RateLimits {
|
|||||||
const level30Routes = ["insight", "bitcore", "blockbook"]
|
const level30Routes = ["insight", "bitcore", "blockbook"]
|
||||||
const level40Routes = ["slp"]
|
const level40Routes = ["slp"]
|
||||||
|
|
||||||
// console.log(`apiLevel: ${apiLevel}`)
|
wlogger.debug(`apiLevel: ${apiLevel}`)
|
||||||
|
|
||||||
// Only evaluate if user is using a JWT token.
|
// Only evaluate if user is using a JWT token.
|
||||||
if (jwtInfo.id) {
|
if (jwtInfo.id) {
|
||||||
@@ -354,7 +358,7 @@ class RateLimits {
|
|||||||
|
|
||||||
return retVal
|
return retVal
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(`Error in route-ratelimit.js/calcPoints()`)
|
wlogger.error(`Error in route-ratelimit.js/calcPoints()`)
|
||||||
// throw err
|
// throw err
|
||||||
retVal = 30
|
retVal = 30
|
||||||
}
|
}
|
||||||
@@ -368,14 +372,14 @@ class RateLimits {
|
|||||||
// what kind of variations will be seen in production.
|
// what kind of variations will be seen in production.
|
||||||
getResource(url) {
|
getResource(url) {
|
||||||
try {
|
try {
|
||||||
console.log(`url: ${JSON.stringify(url, null, 2)}`)
|
wlogger.debug(`url: ${JSON.stringify(url, null, 2)}`)
|
||||||
|
|
||||||
const splitUrl = url.split("/")
|
const splitUrl = url.split("/")
|
||||||
const resource = splitUrl[1]
|
const resource = splitUrl[1]
|
||||||
|
|
||||||
return resource
|
return resource
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(`Error in getResource().`)
|
wlogger.error(`Error in getResource().`)
|
||||||
throw err
|
throw err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user