Skip to content

Commit

Permalink
Fix getIp (#853)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChaituVR authored Aug 28, 2023
1 parent fe86da9 commit 15629f1
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
8 changes: 5 additions & 3 deletions src/helpers/rateLimit.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import rateLimit from 'express-rate-limit';
import RedisStore from 'rate-limit-redis';
import { createClient } from 'redis';
import { getIp, rpcError } from '../utils';
import { getIp, rpcError, sha256 } from '../utils';

let client;

Expand All @@ -18,12 +18,14 @@ let client;
await client.connect();
})();

const hashedIp = (req): string => sha256(getIp(req)).slice(0, 7);

export default rateLimit({
windowMs: 20 * 1e3,
max: 60,
standardHeaders: true,
legacyHeaders: false,
keyGenerator: (req) => getIp(req),
keyGenerator: (req) => hashedIp(req),
skip: (req, res) => {
const keycardData = res.locals.keycardData;
if (keycardData?.valid && !keycardData.rateLimited) {
Expand All @@ -35,7 +37,7 @@ export default rateLimit({
handler: (req, res) => {
const { id = null } = req.body;

console.log(`too many requests ${getIp(req).slice(0, 7)}`);
console.log(`too many requests ${hashedIp(req)}`);
rpcError(
res,
429,
Expand Down
3 changes: 1 addition & 2 deletions src/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import express from 'express';
import snapshot from '@snapshot-labs/strategies';
import { getAddress } from '@ethersproject/address';
import scores from './scores';
import { clone, formatStrategies, rpcSuccess, rpcError, blockNumByNetwork, getIp } from './utils';
import { clone, formatStrategies, rpcSuccess, rpcError, blockNumByNetwork } from './utils';
import { version } from '../package.json';
import { getVp, validate } from './methods';
import disabled from './disabled.json';
Expand Down Expand Up @@ -153,7 +153,6 @@ router.post('/api/scores', async (req, res) => {
network,
space,
snapshot,
getIp(req),
JSON.stringify(strategies),
JSON.stringify(errorMessage).slice(0, 256),
requestId
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,5 @@ export function getIp(req) {
''
).split(',');

return sha256(ips[0].trim()).substring(0, 10);
return ips[0].trim();
}

0 comments on commit 15629f1

Please sign in to comment.