Changeset View
Changeset View
Standalone View
Standalone View
looper/utils.py
| from typing import Optional | |||||
| import re | import re | ||||
| from django.http import HttpRequest | |||||
| IPV4_WITH_PORT = re.compile(r"([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+):[0-9]+") | IPV4_WITH_PORT = re.compile(r"([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+):[0-9]+") | ||||
| """Regexp matching an IPv4 address with a port number.""" | """Regexp matching an IPv4 address with a port number.""" | ||||
| IPV6_WITH_PORT = re.compile(r"\[([0-9:]+)\]:[0-9]+") | IPV6_WITH_PORT = re.compile(r"\[([0-9:]+)\]:[0-9]+") | ||||
| """Regexp matching an IPv6 address with a port number.""" | """Regexp matching an IPv6 address with a port number.""" | ||||
| def get_client_ip(request): | def get_client_ip(request: HttpRequest) -> str: | ||||
| """Returns the IP of the request, accounting for the possibility of being | """Returns the IP of the request, accounting for the possibility of being | ||||
| behind a proxy. | behind a proxy. | ||||
| """ | """ | ||||
| x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR', None) | x_forwarded_for: Optional[str] = request.META.get('HTTP_X_FORWARDED_FOR', None) | ||||
| if x_forwarded_for: | if x_forwarded_for: | ||||
| # X_FORWARDED_FOR returns client1, proxy1, proxy2,... | # X_FORWARDED_FOR returns client1, proxy1, proxy2,... | ||||
| remote_addr = x_forwarded_for.split(', ', 1)[0].strip() | remote_addr = x_forwarded_for.split(', ', 1)[0].strip() | ||||
| return _remove_port_nr(remote_addr) | return _remove_port_nr(remote_addr) | ||||
| remote_addr = request.META.get('REMOTE_ADDR', '') | remote_addr = request.META.get('REMOTE_ADDR', '') | ||||
| if not remote_addr: | if not remote_addr: | ||||
| return '' | return '' | ||||
| ▲ Show 20 Lines • Show All 42 Lines • Show Last 20 Lines | |||||