summaryrefslogtreecommitdiff
path: root/searx/plugins/limiter.py
blob: 24b14f3b43f02e32647094c0de5b69e5fd0d49b9 (plain)
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
32
33
34
35
36
37
38
# SPDX-License-Identifier: AGPL-3.0-or-later
# lint: pylint
# pyright: basic
"""see :ref:`limiter src`"""

import sys
import flask

from searx import redisdb
from searx.plugins import logger
from searx.botdetection import limiter

name = "Request limiter"
description = "Limit the number of request"
default_on = False
preference_section = 'service'

logger = logger.getChild('limiter')


def pre_request():
    """See :ref:`flask.Flask.before_request`"""
    return limiter.filter_request(flask.request)


def init(app: flask.Flask, settings) -> bool:
    if not settings['server']['limiter'] and not settings['server']['public_instance']:
        return False
    if not redisdb.client():
        logger.error(
            "The limiter requires Redis, please consult the documentation: "
            + "https://docs.searxng.org/admin/searx.botdetection.html#limiter"
        )
        if settings['server']['public_instance']:
            sys.exit(1)
        return False
    app.before_request(pre_request)
    return True