@@ -9,11 +9,13 @@ const redis = new Redis({
9
9
token : process . env . UPSTASH_REDIS_REST_TOKEN ! ,
10
10
} )
11
11
12
- const THREE_DAYS_IN_MS = 3 * 24 * 60 * 60 * 1000
12
+ const THIRTY_DAYS_IN_MS = 30 * 24 * 60 * 60 * 1000
13
+ const REDIS_DATA_VERSION = '1'
13
14
14
15
interface RedisResponse {
15
16
value : string
16
17
timestamp : number
18
+ version : string // in case we want to purge all existing data and start fresh
17
19
}
18
20
19
21
async function isIPBlockedInRedis ( ip : string , currentTime : number ) {
@@ -27,9 +29,13 @@ async function isIPBlockedInRedis(ip: string, currentTime: number) {
27
29
let isIPBlocked = false
28
30
if ( redisData ) {
29
31
try {
30
- const { value, timestamp } = redisData
31
- // check if entry is valid and is not more than 3 days old
32
- if ( value === BLOCKED_IP_VALUE && currentTime - timestamp <= THREE_DAYS_IN_MS ) {
32
+ const { value, timestamp, version = '' } = redisData
33
+ // check if entry is valid, version is correct and data is not more than 30 days old
34
+ if (
35
+ value === BLOCKED_IP_VALUE &&
36
+ version === REDIS_DATA_VERSION &&
37
+ currentTime - timestamp <= THIRTY_DAYS_IN_MS
38
+ ) {
33
39
isIPBlocked = true
34
40
}
35
41
} catch ( error ) {
@@ -65,7 +71,7 @@ export async function middleware(request: NextRequest) {
65
71
const isIPFromVPN = await isVPN ( ip )
66
72
if ( isIPFromVPN ) {
67
73
try {
68
- await redis . set ( ip , { value : BLOCKED_IP_VALUE , timestamp : currentTime } )
74
+ await redis . set ( ip , { value : BLOCKED_IP_VALUE , timestamp : currentTime , version : REDIS_DATA_VERSION } )
69
75
} catch ( error ) {
70
76
console . error ( 'Failed to set data in Redis:' , error )
71
77
}
0 commit comments