api/ioutils/request.py

50 lines
1.6 KiB
Python
Raw Normal View History

2024-01-15 13:32:24 +01:00
from piracyshield_component.log.logger import Logger
from piracyshield_component.exception import ApplicationException
from .response import ResponseHandler
import time
from collections import deque
from tornado.locks import Lock
from interceptors.logger import LoggerInterceptor
from interceptors.security.blacklist import BlacklistInterceptor
from interceptors.security.rate_limit import RateLimitInterceptor
from ioutils.errors import ErrorCode, ErrorMessage
2024-01-15 13:32:24 +01:00
class RequestHandler(ResponseHandler):
"""
Requests gateway.
The requestor gets blocked here before proceding any further.
Interceptors are implemented to allow certain features to take part of the action.
2024-01-15 13:32:24 +01:00
Current actived interceptors:
- security: takes care of the IP addresses exceeding limits (ex. applies the ban rules when triggered).
- rate limit: the IP will get a 429 when trying to perform too many requests on a given timeframe.
"""
2024-01-15 13:32:24 +01:00
interceptors = [
LoggerInterceptor,
BlacklistInterceptor,
RateLimitInterceptor
]
2024-01-15 13:32:24 +01:00
async def prepare(self):
await self.run_interceptors()
2024-01-15 13:32:24 +01:00
async def run_interceptors(self):
for interceptor in self.interceptors:
try:
await interceptor().execute(self)
2024-01-15 13:32:24 +01:00
except Exception as InterceptorException:
self.error(
status_code = InterceptorException.status_code,
error_code = InterceptorException.error_code,
message = InterceptorException.error_message
)
2024-01-15 13:32:24 +01:00
return