Skip to content

Commit 163f1fa

Browse files
committed
check bogon locally
1 parent c0927b7 commit 163f1fa

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-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:
6+
if IP(ip_address) in ip_network(network):
7+
return True
8+
return False
9+
10+
11+
BOGON = [
12+
"0.0.0.0/8",
13+
"10.0.0.0/8",
14+
"100.64.0.0/10",
15+
"127.0.0.0/8",
16+
"169.254.0.0/16",
17+
"172.16.0.0/12",
18+
"192.0.0.0/24",
19+
"192.0.2.0/24",
20+
"192.168.0.0/16",
21+
"198.18.0.0/15",
22+
"198.51.100.0/24",
23+
"203.0.113.0/24",
24+
"224.0.0.0/4",
25+
"240.0.0.0/4",
26+
"255.255.255.255/32",
27+
"::/128",
28+
"::1/128",
29+
"::ffff:0:0/96",
30+
"::/96",
31+
"100::/64",
32+
"2001:10::/28",
33+
"2001:db8::/32",
34+
"fc00::/7",
35+
"fe80::/10",
36+
"fec0::/10",
37+
"ff00::/8",
38+
"2002::/24",
39+
"2002:a00::/24",
40+
"2002:7f00::/24",
41+
"2002:a9fe::/32",
42+
"2002:ac10::/28",
43+
"2002:c000::/40",
44+
"2002:c000:200::/40",
45+
"2002:c0a8::/32",
46+
"2002:c612::/31",
47+
"2002:c633:6400::/40",
48+
"2002:cb00:7100::/40",
49+
"2002:e000::/20",
50+
"2002:f000::/20",
51+
"2002:ffff:ffff::/48",
52+
"2001::/40",
53+
"2001:0:a00::/40",
54+
"2001:0:7f00::/40",
55+
"2001:0:a9fe::/48",
56+
"2001:0:ac10::/44",
57+
"2001:0:c000::/56",
58+
"2001:0:c000:200::/56",
59+
"2001:0:c0a8::/48",
60+
"2001:0:c612::/47",
61+
"2001:0:c633:6400::/56",
62+
"2001:0:cb00:7100::/56",
63+
"2001:0:e000::/36",
64+
"2001:0:f000::/36",
65+
"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)]

0 commit comments

Comments
 (0)