Я предлагаю вам использовать шаблон декоратора для таких случаев, то есть вы добавляете новую опцию конфигурации IP_LIST с некоторым набором адресов, разделенным запятой.
IP_LIST = "127.0.0.1,127.0.0.2,..."
После этого добавьте новую функцию декоратора и украсите любая конечная точка с декоратором.
def ip_verified(fn):
"""
A custom decorator that checks if a client IP is in the list, otherwise block access.
"""
@wraps(fn)
def decorated_view(*args, **kwargs):
ip_list_str = current_app.config['IP_LIST']
ip_list = ip_list_str.split(",") if ip_list_str else []
if request.headers.getlist("X-Forwarded-For"):
remote_ip = request.headers.getlist("X-Forwarded-For")[0]
else:
remote_ip = request.remote_addr
if remote_ip not in ip_list:
return "Not sufficient privileges", 403
return fn(*args, **kwargs)
return decorated_view
@app.route("/your_route", methods=["GET"])
@ip_verified
def your_route():
...