fix rate limiting per engine

This commit is contained in:
Marc Abonce Seguin
2022-06-25 16:12:35 -05:00
parent 85eca4fb22
commit 17b9b334a3
6 changed files with 81 additions and 48 deletions

View File

@@ -1,8 +1,11 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
# pyright: strict
import hmac
from abc import ABC, abstractmethod
from typing import Optional
from searx import get_setting
class SharedDict(ABC):
@abstractmethod
@@ -20,3 +23,23 @@ class SharedDict(ABC):
@abstractmethod
def set_str(self, key: str, value: str, expire: Optional[int] = None):
pass
def incr_counter(self, name: str, limit: int = 0, expire: int = 0) -> int:
# generate dict key from name
m = hmac.new(bytes(name, encoding='utf-8'), digestmod='sha256')
m.update(bytes(get_setting('server.secret_key'), encoding='utf-8'))
key = 'SearXNG_counter_' + m.hexdigest()
# check requests count
count = self.get_int(key)
if count is None:
# initialize counter with expiration time
self.set_int(key, 1, expire)
return 1
elif limit >= count or not limit:
# update counter
new_count = count + 1
self.set_int(key, new_count, expire)
return new_count
else:
return count