-
Notifications
You must be signed in to change notification settings - Fork 2
/
globalvars.py
31 lines (25 loc) · 939 Bytes
/
globalvars.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# globals.py
from fastapi import HTTPException
import commonfuncs as cf
ipTracker = {}
defaultRateLimit = 5
def logIP(X_Forwarded_For, action, limit=defaultRateLimit):
global ipTracker
if not X_Forwarded_For:
X_Forwarded_For = 'localhost'
if not ipTracker.get(X_Forwarded_For, False):
ipTracker[X_Forwarded_For] = {action: cf.getEpochTime()}
return
else:
if not ipTracker[X_Forwarded_For].get(action, False):
ipTracker[X_Forwarded_For][action] = cf.getEpochTime()
return
else:
# check last usage
lastHit = ipTracker[X_Forwarded_For][action]
age = cf.checkAge(lastHit)
if age <= limit:
raise HTTPException(status_code=429, detail=f"Rate limited, pls try after {limit} secs")
else:
ipTracker[X_Forwarded_For][action] = cf.getEpochTime()
return