From ca8d3cad15f60b6541d0c39b39a6faf8a000645d Mon Sep 17 00:00:00 2001 From: Alex Root Junior Date: Thu, 16 Nov 2017 18:36:49 +0200 Subject: [PATCH] Provided customisation of allowed IPs list. // Webhook --- aiogram/dispatcher/webhook.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/aiogram/dispatcher/webhook.py b/aiogram/dispatcher/webhook.py index f91f4bdd..20a95f27 100644 --- a/aiogram/dispatcher/webhook.py +++ b/aiogram/dispatcher/webhook.py @@ -28,7 +28,8 @@ WEBHOOK_REQUEST = 'WEBHOOK_REQUEST' TELEGRAM_IP_LOWER = ipaddress.IPv4Address('149.154.167.197') TELEGRAM_IP_UPPER = ipaddress.IPv4Address('149.154.167.233') -LOCALHOST_IP = ipaddress.IPv4Address('127.0.0.1') + +allowed_ips = set() def _check_ip(ip: str) -> bool: @@ -39,7 +40,21 @@ def _check_ip(ip: str) -> bool: :return: """ address = ipaddress.IPv4Address(ip) - return TELEGRAM_IP_LOWER <= address <= TELEGRAM_IP_UPPER or address == LOCALHOST_IP + return address in allowed_ips + + +def allow_ip(*ips: str): + """ + Allow ip address. + + :param ips: + :return: + """ + allowed_ips.update(ipaddress.IPv4Address(ip) for ip in ips) + + +# Allow access from Telegram servers +allow_ip(*(ip for ip in range(int(TELEGRAM_IP_LOWER), int(TELEGRAM_IP_UPPER) + 1))) class WebhookRequestHandler(web.View):