-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathip.py
33 lines (25 loc) · 1 KB
/
ip.py
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
from typing import Iterable, Literal, Optional, Tuple
from django.conf import settings
from django.http import HttpRequest
from python_ipware import IpWare
def get_client_ip(
request: HttpRequest,
proxy_order: Literal['left-most', 'right-most'] = 'left-most',
proxy_count: Optional[int] = None,
proxy_trusted_ips: Optional[Iterable[str]] = None,
request_header_order: Optional[Iterable[str]] = None,
) -> Tuple[str, bool]:
leftmost = proxy_order == 'left-most'
request_header_order = getattr(settings, 'IPWARE_META_PRECEDENCE_ORDER', request_header_order)
# Instantiate IpWare with values from the function arguments
ipw = IpWare(precedence=request_header_order,
leftmost=leftmost,
proxy_count=proxy_count,
proxy_list=proxy_trusted_ips)
ip, _ = ipw.get_client_ip(request.META, True)
client_ip = None
routable = False
if ip:
client_ip = str(ip)
routable = ip.is_global
return client_ip, routable