use x-real-ip headers

This commit is contained in:
aatish 2018-11-29 19:48:02 +05:45
parent 8594e604fc
commit 4e460726dd
1 changed files with 10 additions and 6 deletions

View File

@ -24,12 +24,16 @@ def block_ipv4(get_response):
return get_response(request)
path = request.path_info.lstrip('/')
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
if x_forwarded_for:
client_ip = x_forwarded_for
else:
client_ip = request.META.get('REMOTE_ADDR')
print(is_ipv6_exempt(path), is_valid_ipv6(client_ip))
real_ip_headers = ['HTTP_X_REAL_IP', 'REMOTE_ADDR', 'HTTP_X_FORWARDED_FOR']
client_ip = None
for header in real_ip_headers:
client_ip = request.META.get(header)
if client_ip:
break
if is_valid_ipv6(client_ip) or is_ipv6_exempt(path):
return get_response(request)
else: