Skip to content

Commit

Permalink
feat(api-node): add request-ip (#20967)
Browse files Browse the repository at this point in the history
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
  • Loading branch information
hongbo-miao and renovate[bot] authored Dec 1, 2024
1 parent b3c7fd8 commit 928a7c6
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 56 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ The diagram illustrates the repository's architecture, which is considered overl
- **Network Error Logging** - HTTP header `NEL`
- **response-time** - HTTP header `X-Response-Time`
- **connect-timeout** - Request timeout
- **request-ip** - IP address retrieving
- **Terminus** - Health check and graceful shutdown
- **pino** - Logging
- **dotenv-flow** - Environment variables loading
Expand Down
129 changes: 75 additions & 54 deletions api-node/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion api-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"pino-http": "10.3.0",
"rate-limiter-flexible": "5.0.4",
"report-to": "1.1.0",
"request-ip": "3.3.0",
"response-time": "2.3.3",
"serve-favicon": "2.5.0",
"spdy": "4.0.2",
Expand All @@ -98,7 +99,7 @@
"@types/cookie-parser": "1.4.8",
"@types/cors": "2.8.17",
"@types/dotenv-flow": "3.3.3",
"@types/express": "4.17.21",
"@types/express": "5.0.0",
"@types/express-list-endpoints": "6.0.3",
"@types/graphql-depth-limit": "1.1.6",
"@types/graphql-upload": "8.0.12",
Expand All @@ -108,6 +109,7 @@
"@types/lodash.uniq": "4.5.9",
"@types/multer": "1.4.12",
"@types/node": "22.10.1",
"@types/request-ip": "0.0.41",
"@types/response-time": "2.3.8",
"@types/serve-favicon": "2.5.7",
"@types/spdy": "3.4.9",
Expand Down
2 changes: 2 additions & 0 deletions api-node/src/security/middlewares/authMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { expressjwt as jwt } from 'express-jwt';
import config from '../../config';

const authMiddleware = (): RequestHandler => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
return jwt({
secret: config.jwtSecret,
algorithms: ['HS256'],
Expand Down
4 changes: 3 additions & 1 deletion api-node/src/security/middlewares/rateLimitMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NextFunction, Request, RequestHandler, Response } from 'express';
import Redis from 'ioredis';
import { BurstyRateLimiter, RateLimiterMemory, RateLimiterRedis } from 'rate-limiter-flexible';
import { getClientIp } from 'request-ip';

const BURST_POINTS_RATE = 2.5;
const BURST_DURATION_RATE = 10;
Expand Down Expand Up @@ -54,8 +55,9 @@ const rateLimitMiddleware = (
const rateLimiter = new BurstyRateLimiter(redisRateLimiter, burstRedisRateLimiter);

return (req: Request, res: Response, next: NextFunction) => {
const ip = getClientIp(req) || 'unknown';
return rateLimiter
.consume(req.ip)
.consume(ip)
.then(() => {
next();
})
Expand Down

0 comments on commit 928a7c6

Please sign in to comment.