Skip to content

Commit 4011c4e

Browse files
authored
Merge pull request #71 from ipinfo/umar/check-bogon-locally
check bogon locally
2 parents c302987 + 032adb8 commit 4011c4e

File tree

5 files changed

+107
-0
lines changed

5 files changed

+107
-0
lines changed

ipinfo/bogon.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
from ipaddress import ip_network, ip_address as IP
2+
3+
4+
def is_bogon(ip_address):
5+
for network in BOGON_NETWORKS:
6+
if IP(ip_address) in network:
7+
return True
8+
return False
9+
10+
11+
BOGON_NETWORKS = [
12+
ip_network("0.0.0.0/8"),
13+
ip_network("10.0.0.0/8"),
14+
ip_network("100.64.0.0/10"),
15+
ip_network("127.0.0.0/8"),
16+
ip_network("169.254.0.0/16"),
17+
ip_network("172.16.0.0/12"),
18+
ip_network("192.0.0.0/24"),
19+
ip_network("192.0.2.0/24"),
20+
ip_network("192.168.0.0/16"),
21+
ip_network("198.18.0.0/15"),
22+
ip_network("198.51.100.0/24"),
23+
ip_network("203.0.113.0/24"),
24+
ip_network("224.0.0.0/4"),
25+
ip_network("240.0.0.0/4"),
26+
ip_network("255.255.255.255/32"),
27+
ip_network("::/128"),
28+
ip_network("::1/128"),
29+
ip_network("::ffff:0:0/96"),
30+
ip_network("::/96"),
31+
ip_network("100::/64"),
32+
ip_network("2001:10::/28"),
33+
ip_network("2001:db8::/32"),
34+
ip_network("fc00::/7"),
35+
ip_network("fe80::/10"),
36+
ip_network("fec0::/10"),
37+
ip_network("ff00::/8"),
38+
ip_network("2002::/24"),
39+
ip_network("2002:a00::/24"),
40+
ip_network("2002:7f00::/24"),
41+
ip_network("2002:a9fe::/32"),
42+
ip_network("2002:ac10::/28"),
43+
ip_network("2002:c000::/40"),
44+
ip_network("2002:c000:200::/40"),
45+
ip_network("2002:c0a8::/32"),
46+
ip_network("2002:c612::/31"),
47+
ip_network("2002:c633:6400::/40"),
48+
ip_network("2002:cb00:7100::/40"),
49+
ip_network("2002:e000::/20"),
50+
ip_network("2002:f000::/20"),
51+
ip_network("2002:ffff:ffff::/48"),
52+
ip_network("2001::/40"),
53+
ip_network("2001:0:a00::/40"),
54+
ip_network("2001:0:7f00::/40"),
55+
ip_network("2001:0:a9fe::/48"),
56+
ip_network("2001:0:ac10::/44"),
57+
ip_network("2001:0:c000::/56"),
58+
ip_network("2001:0:c000:200::/56"),
59+
ip_network("2001:0:c0a8::/48"),
60+
ip_network("2001:0:c612::/47"),
61+
ip_network("2001:0:c633:6400::/56"),
62+
ip_network("2001:0:cb00:7100::/56"),
63+
ip_network("2001:0:e000::/36"),
64+
ip_network("2001:0:f000::/36"),
65+
ip_network("2001:0:ffff:ffff::/64"),
66+
]

ipinfo/handler.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
cache_key,
2929
)
3030
from . import handler_utils
31+
from .bogon import is_bogon
3132

3233

3334
class Handler:
@@ -109,6 +110,13 @@ def getDetails(self, ip_address=None, timeout=None):
109110
):
110111
ip_address = ip_address.exploded
111112

113+
# check if bogon.
114+
if is_bogon(ip_address):
115+
details = {}
116+
details["ip"] = ip_address
117+
details["bogon"] = True
118+
return Details(details)
119+
112120
# check cache first.
113121
try:
114122
cached_ipaddr = self.cache[cache_key(ip_address)]

ipinfo/handler_async.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
cache_key,
3030
)
3131
from . import handler_utils
32+
from .bogon import is_bogon
3233

3334

3435
class AsyncHandler:
@@ -134,6 +135,13 @@ async def getDetails(self, ip_address=None, timeout=None):
134135
):
135136
ip_address = ip_address.exploded
136137

138+
# check if bogon.
139+
if is_bogon(ip_address):
140+
details = {}
141+
details["ip"] = ip_address
142+
details["bogon"] = True
143+
return Details(details)
144+
137145
# check cache first.
138146
try:
139147
cached_ipaddr = self.cache[cache_key(ip_address)]

tests/handler_async_test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,15 @@ async def test_get_batch_details_total_timeout(batch_size):
148148
ips, batch_size=batch_size, timeout_total=0.001
149149
)
150150
await handler.deinit()
151+
152+
153+
#############
154+
# BOGON TESTS
155+
#############
156+
157+
158+
async def test_bogon_details():
159+
token = os.environ.get("IPINFO_TOKEN", "")
160+
handler = AsyncHandler(token)
161+
details = await handler.getDetails("127.0.0.1")
162+
assert details.all == {'bogon': True, 'ip': '127.0.0.1'}

tests/handler_test.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,16 @@ def test_get_map():
150150
handler = Handler()
151151
mapUrl = handler.getMap(open("tests/map-ips.txt").read().splitlines())
152152
print(f"got URL={mapUrl}")
153+
154+
155+
#############
156+
# BOGON TESTS
157+
#############
158+
159+
160+
def test_bogon_details():
161+
token = os.environ.get("IPINFO_TOKEN", "")
162+
handler = Handler(token)
163+
details = handler.getDetails("127.0.0.1")
164+
assert isinstance(details, Details)
165+
assert details.all == {'bogon': True, 'ip': '127.0.0.1'}

0 commit comments

Comments
 (0)