Skip to content

Commit

Permalink
Merge pull request #71 from ipinfo/umar/check-bogon-locally
Browse files Browse the repository at this point in the history
check bogon locally
  • Loading branch information
UmanShahzad authored Nov 23, 2022
2 parents c302987 + 032adb8 commit 4011c4e
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 0 deletions.
66 changes: 66 additions & 0 deletions ipinfo/bogon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from ipaddress import ip_network, ip_address as IP


def is_bogon(ip_address):
for network in BOGON_NETWORKS:
if IP(ip_address) in network:
return True
return False


BOGON_NETWORKS = [
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"),
]
8 changes: 8 additions & 0 deletions ipinfo/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
cache_key,
)
from . import handler_utils
from .bogon import is_bogon


class Handler:
Expand Down Expand Up @@ -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)]
Expand Down
8 changes: 8 additions & 0 deletions ipinfo/handler_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
cache_key,
)
from . import handler_utils
from .bogon import is_bogon


class AsyncHandler:
Expand Down Expand Up @@ -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)]
Expand Down
12 changes: 12 additions & 0 deletions tests/handler_async_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'}
13 changes: 13 additions & 0 deletions tests/handler_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'}

0 comments on commit 4011c4e

Please sign in to comment.