Skip to content

Commit

Permalink
Add is_local_ip function & remove is_local parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Lekuruu committed Jan 3, 2024
1 parent 7c3a249 commit a1b9896
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion helpers/external/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from functools import cache

import config
import struct
import socket
import app

@dataclass(slots=True)
Expand All @@ -36,9 +38,37 @@ def download_database():
with open(f'{config.DATA_PATH}/geolite.mmdb', 'wb') as f:
f.write(response.content)

def is_local_ip(ip: str) -> bool:
if ':' in ip:
# TODO: IPv6 parsing
return False

private = (
[ 2130706432, 4278190080 ], # 127.0.0.0
[ 3232235520, 4294901760 ], # 192.168.0.0
[ 2886729728, 4293918720 ], # 172.16.0.0
[ 167772160, 4278190080 ], # 10.0.0.0
)

f = struct.unpack(
'!I',
socket.inet_pton(
socket.AF_INET,
ip
)
)[0]

for net in private:
if (f & net[1]) == net[0]:
return True

return False

@cache
def fetch_geolocation(ip: str, is_local: bool = False) -> Geolocation:
def fetch_geolocation(ip: str) -> Geolocation:
try:
is_local = is_local_ip(ip)

if is_local:
if not (geo := fetch_web(ip, is_local)):
return Geolocation()
Expand Down

0 comments on commit a1b9896

Please sign in to comment.