Skip to content

Commit

Permalink
Add support for IPv6 encapsulation of IPv4 IP addresses (un33k#51)
Browse files Browse the repository at this point in the history
* add deprecation warning, up version Django to latest, update travis.ci

* style fix

* drop 3.4 support for 2.1.3

* add ip clean up utils

* add unit test for IPv6 encapsulation of IPv4

* clean up and rename formatter

* fix sponsors links
  • Loading branch information
un33k authored May 23, 2019
1 parent 9d82700 commit d9797f5
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
File renamed without changes.
20 changes: 20 additions & 0 deletions ipware/tests/tests_ipv6.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,23 @@ def test_meta_proxy_trusted_ips_proxy_count_more_error_ignore_fallback(self):
}
result = get_client_ip(request, proxy_count=1, proxy_trusted_ips=['74dc::02bb'])
self.assertEqual(result, (None, False))


class IPv6EncapsulationOfIPv4TestCase(TestCase):
"""IPv6 Encapsulation of IPv4 - IP address Test"""

def test_ipv6_encapsulation_of_ipv4_private(self):
request = HttpRequest()
request.META = {
'HTTP_X_FORWARDED_FOR': '::ffff:127.0.0.1',
}
result = get_client_ip(request)
self.assertEqual(result, ('127.0.0.1', False))

def test_ipv6_encapsulation_of_ipv4_public(self):
request = HttpRequest()
request.META = {
'HTTP_X_FORWARDED_FOR': '::ffff:177.139.233.139',
}
result = get_client_ip(request)
self.assertEqual(result, ('177.139.233.139', True))
15 changes: 13 additions & 2 deletions ipware/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
from . import defaults as defs


def cleanup_ip(ip):
"""
Given ip address string, it cleans it up
"""
ip = ip.strip().lower()
if (ip.startswith('::ffff:')):
return ip.replace('::ffff:', '')
return ip


def is_valid_ipv4(ip_str):
"""
Check the validity of an IPv4 address
Expand Down Expand Up @@ -94,8 +104,9 @@ def get_ip_info(ip_str):
"""
ip = None
is_routable_ip = False
if is_valid_ip(ip_str):
ip = ip_str
clean_ip = cleanup_ip(ip_str)
if is_valid_ip(clean_ip):
ip = clean_ip
is_routable_ip = is_public_ip(ip)
return ip, is_routable_ip

Expand Down
2 changes: 2 additions & 0 deletions requirement-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Django>=2.1.3
pycodestyle==2.5.0

0 comments on commit d9797f5

Please sign in to comment.