From 163f1faa5225ae53181f45fe3dfc3d4d87b1ab1c Mon Sep 17 00:00:00 2001 From: Umar Farooq Date: Mon, 21 Nov 2022 16:13:01 +0500 Subject: [PATCH 1/4] check bogon locally --- ipinfo/bogon.py | 66 +++++++++++++++++++++++++++++++++++++++++ ipinfo/handler.py | 8 +++++ ipinfo/handler_async.py | 8 +++++ 3 files changed, 82 insertions(+) create mode 100644 ipinfo/bogon.py diff --git a/ipinfo/bogon.py b/ipinfo/bogon.py new file mode 100644 index 0000000..e758295 --- /dev/null +++ b/ipinfo/bogon.py @@ -0,0 +1,66 @@ +from ipaddress import ip_network, ip_address as IP + + +def is_bogon(ip_address): + for network in BOGON: + if IP(ip_address) in ip_network(network): + return True + return False + + +BOGON = [ + "0.0.0.0/8", + "10.0.0.0/8", + "100.64.0.0/10", + "127.0.0.0/8", + "169.254.0.0/16", + "172.16.0.0/12", + "192.0.0.0/24", + "192.0.2.0/24", + "192.168.0.0/16", + "198.18.0.0/15", + "198.51.100.0/24", + "203.0.113.0/24", + "224.0.0.0/4", + "240.0.0.0/4", + "255.255.255.255/32", + "::/128", + "::1/128", + "::ffff:0:0/96", + "::/96", + "100::/64", + "2001:10::/28", + "2001:db8::/32", + "fc00::/7", + "fe80::/10", + "fec0::/10", + "ff00::/8", + "2002::/24", + "2002:a00::/24", + "2002:7f00::/24", + "2002:a9fe::/32", + "2002:ac10::/28", + "2002:c000::/40", + "2002:c000:200::/40", + "2002:c0a8::/32", + "2002:c612::/31", + "2002:c633:6400::/40", + "2002:cb00:7100::/40", + "2002:e000::/20", + "2002:f000::/20", + "2002:ffff:ffff::/48", + "2001::/40", + "2001:0:a00::/40", + "2001:0:7f00::/40", + "2001:0:a9fe::/48", + "2001:0:ac10::/44", + "2001:0:c000::/56", + "2001:0:c000:200::/56", + "2001:0:c0a8::/48", + "2001:0:c612::/47", + "2001:0:c633:6400::/56", + "2001:0:cb00:7100::/56", + "2001:0:e000::/36", + "2001:0:f000::/36", + "2001:0:ffff:ffff::/64", +] diff --git a/ipinfo/handler.py b/ipinfo/handler.py index 5948930..698a1d8 100644 --- a/ipinfo/handler.py +++ b/ipinfo/handler.py @@ -28,6 +28,7 @@ cache_key, ) from . import handler_utils +from .bogon import is_bogon class Handler: @@ -109,6 +110,13 @@ def getDetails(self, ip_address=None, timeout=None): ): ip_address = ip_address.exploded + # check if bogon. + if is_bogon(ip_address): + details = {} + details["ip"] = ip_address + details["bogon"] = True + return Details(details) + # check cache first. try: cached_ipaddr = self.cache[cache_key(ip_address)] diff --git a/ipinfo/handler_async.py b/ipinfo/handler_async.py index 145cf15..299e300 100644 --- a/ipinfo/handler_async.py +++ b/ipinfo/handler_async.py @@ -29,6 +29,7 @@ cache_key, ) from . import handler_utils +from .bogon import is_bogon class AsyncHandler: @@ -134,6 +135,13 @@ async def getDetails(self, ip_address=None, timeout=None): ): ip_address = ip_address.exploded + # check if bogon. + if is_bogon(ip_address): + details = {} + details["ip"] = ip_address + details["bogon"] = True + return Details(details) + # check cache first. try: cached_ipaddr = self.cache[cache_key(ip_address)] From c9138e73fd29f32fd9e895dd1fc8074cdd7d20e8 Mon Sep 17 00:00:00 2001 From: Umar Farooq Date: Mon, 21 Nov 2022 16:42:16 +0500 Subject: [PATCH 2/4] fmt --- ipinfo/bogon.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ipinfo/bogon.py b/ipinfo/bogon.py index e758295..5fb4da0 100644 --- a/ipinfo/bogon.py +++ b/ipinfo/bogon.py @@ -2,13 +2,13 @@ def is_bogon(ip_address): - for network in BOGON: + for network in BOGON_NETWORKS: if IP(ip_address) in ip_network(network): return True return False -BOGON = [ +BOGON_NETWORKS = [ "0.0.0.0/8", "10.0.0.0/8", "100.64.0.0/10", From 9b79c8b8f5f098468ed69f8b0a93109fd4b381f2 Mon Sep 17 00:00:00 2001 From: Umar Farooq Date: Tue, 22 Nov 2022 23:39:50 +0500 Subject: [PATCH 3/4] convert to ip_network --- ipinfo/bogon.py | 110 ++++++++++++++++++++++++------------------------ 1 file changed, 55 insertions(+), 55 deletions(-) diff --git a/ipinfo/bogon.py b/ipinfo/bogon.py index 5fb4da0..0b7e376 100644 --- a/ipinfo/bogon.py +++ b/ipinfo/bogon.py @@ -3,64 +3,64 @@ def is_bogon(ip_address): for network in BOGON_NETWORKS: - if IP(ip_address) in ip_network(network): + if IP(ip_address) in network: return True return False BOGON_NETWORKS = [ - "0.0.0.0/8", - "10.0.0.0/8", - "100.64.0.0/10", - "127.0.0.0/8", - "169.254.0.0/16", - "172.16.0.0/12", - "192.0.0.0/24", - "192.0.2.0/24", - "192.168.0.0/16", - "198.18.0.0/15", - "198.51.100.0/24", - "203.0.113.0/24", - "224.0.0.0/4", - "240.0.0.0/4", - "255.255.255.255/32", - "::/128", - "::1/128", - "::ffff:0:0/96", - "::/96", - "100::/64", - "2001:10::/28", - "2001:db8::/32", - "fc00::/7", - "fe80::/10", - "fec0::/10", - "ff00::/8", - "2002::/24", - "2002:a00::/24", - "2002:7f00::/24", - "2002:a9fe::/32", - "2002:ac10::/28", - "2002:c000::/40", - "2002:c000:200::/40", - "2002:c0a8::/32", - "2002:c612::/31", - "2002:c633:6400::/40", - "2002:cb00:7100::/40", - "2002:e000::/20", - "2002:f000::/20", - "2002:ffff:ffff::/48", - "2001::/40", - "2001:0:a00::/40", - "2001:0:7f00::/40", - "2001:0:a9fe::/48", - "2001:0:ac10::/44", - "2001:0:c000::/56", - "2001:0:c000:200::/56", - "2001:0:c0a8::/48", - "2001:0:c612::/47", - "2001:0:c633:6400::/56", - "2001:0:cb00:7100::/56", - "2001:0:e000::/36", - "2001:0:f000::/36", - "2001:0:ffff:ffff::/64", + ip_network("0.0.0.0/8"), + ip_network("10.0.0.0/8"), + ip_network("100.64.0.0/10"), + ip_network("127.0.0.0/8"), + ip_network("169.254.0.0/16"), + ip_network("172.16.0.0/12"), + ip_network("192.0.0.0/24"), + ip_network("192.0.2.0/24"), + ip_network("192.168.0.0/16"), + ip_network("198.18.0.0/15"), + ip_network("198.51.100.0/24"), + ip_network("203.0.113.0/24"), + ip_network("224.0.0.0/4"), + ip_network("240.0.0.0/4"), + ip_network("255.255.255.255/32"), + ip_network("::/128"), + ip_network("::1/128"), + ip_network("::ffff:0:0/96"), + ip_network("::/96"), + ip_network("100::/64"), + ip_network("2001:10::/28"), + ip_network("2001:db8::/32"), + ip_network("fc00::/7"), + ip_network("fe80::/10"), + ip_network("fec0::/10"), + ip_network("ff00::/8"), + ip_network("2002::/24"), + ip_network("2002:a00::/24"), + ip_network("2002:7f00::/24"), + ip_network("2002:a9fe::/32"), + ip_network("2002:ac10::/28"), + ip_network("2002:c000::/40"), + ip_network("2002:c000:200::/40"), + ip_network("2002:c0a8::/32"), + ip_network("2002:c612::/31"), + ip_network("2002:c633:6400::/40"), + ip_network("2002:cb00:7100::/40"), + ip_network("2002:e000::/20"), + ip_network("2002:f000::/20"), + ip_network("2002:ffff:ffff::/48"), + ip_network("2001::/40"), + ip_network("2001:0:a00::/40"), + ip_network("2001:0:7f00::/40"), + ip_network("2001:0:a9fe::/48"), + ip_network("2001:0:ac10::/44"), + ip_network("2001:0:c000::/56"), + ip_network("2001:0:c000:200::/56"), + ip_network("2001:0:c0a8::/48"), + ip_network("2001:0:c612::/47"), + ip_network("2001:0:c633:6400::/56"), + ip_network("2001:0:cb00:7100::/56"), + ip_network("2001:0:e000::/36"), + ip_network("2001:0:f000::/36"), + ip_network("2001:0:ffff:ffff::/64"), ] From 032adb8bbbdff1bb373939e9c6cb3f9c840776a5 Mon Sep 17 00:00:00 2001 From: Umar Farooq Date: Wed, 23 Nov 2022 10:16:56 +0500 Subject: [PATCH 4/4] added test cases --- tests/handler_async_test.py | 12 ++++++++++++ tests/handler_test.py | 13 +++++++++++++ 2 files changed, 25 insertions(+) diff --git a/tests/handler_async_test.py b/tests/handler_async_test.py index a750dff..6fa7e0e 100644 --- a/tests/handler_async_test.py +++ b/tests/handler_async_test.py @@ -148,3 +148,15 @@ async def test_get_batch_details_total_timeout(batch_size): ips, batch_size=batch_size, timeout_total=0.001 ) await handler.deinit() + + +############# +# BOGON TESTS +############# + + +async def test_bogon_details(): + token = os.environ.get("IPINFO_TOKEN", "") + handler = AsyncHandler(token) + details = await handler.getDetails("127.0.0.1") + assert details.all == {'bogon': True, 'ip': '127.0.0.1'} \ No newline at end of file diff --git a/tests/handler_test.py b/tests/handler_test.py index b82d907..d4e79a8 100644 --- a/tests/handler_test.py +++ b/tests/handler_test.py @@ -150,3 +150,16 @@ def test_get_map(): handler = Handler() mapUrl = handler.getMap(open("tests/map-ips.txt").read().splitlines()) print(f"got URL={mapUrl}") + + +############# +# BOGON TESTS +############# + + +def test_bogon_details(): + token = os.environ.get("IPINFO_TOKEN", "") + handler = Handler(token) + details = handler.getDetails("127.0.0.1") + assert isinstance(details, Details) + assert details.all == {'bogon': True, 'ip': '127.0.0.1'}