-
Notifications
You must be signed in to change notification settings - Fork 2
/
__init__.py
58 lines (47 loc) · 1.98 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from typing import Tuple
import logging
import os
import geoip2.database
import ipaddress
from geoip2.errors import AddressNotFoundError
logger = logging.getLogger(__name__)
def str_to_int(s: str):
try:
return int(s)
except ValueError:
return None
def ip_to_coordinates(input: str) -> Tuple[float, float, str, str, str]:
geolite2_city_db_path = f"{os.getenv('GEOLITE2_PATH')}/GeoLite2-City.mmdb"
geolite2_asn_db_path = f"{os.getenv('GEOLITE2_PATH')}/GeoLite2-ASN.mmdb"
try:
input_ip = ipaddress.ip_address(input)
private_ip_coordinates_raw = os.getenv("PRIVATE_IP_LOCATION_INFO")
if input_ip.is_private and private_ip_coordinates_raw:
private_ip_coordinates = private_ip_coordinates_raw.split(",")
if len(private_ip_coordinates) != 5:
logger.error("PRIVATE_IP_LOCATION_INFO does not provide enough information. "
"It should have the format: "
"Longitude,Latitude,City,Country,autonomous_system_organization")
return (
float(private_ip_coordinates[0]),
float(private_ip_coordinates[1]),
private_ip_coordinates[2],
private_ip_coordinates[3],
private_ip_coordinates[4]
)
with geoip2.database.Reader(geolite2_city_db_path) as reader:
city_response = reader.city(input)
with geoip2.database.Reader(geolite2_asn_db_path) as reader:
asn_response = reader.asn(input)
return (
city_response.location.latitude,
city_response.location.longitude,
city_response.city.name,
city_response.country.name,
asn_response.autonomous_system_organization
)
except (ValueError, AddressNotFoundError) as e:
logger.error(f"Error getting coordinates: {e}")
return None, None, None, None, None
def is_tor_exit_node(ip: str) -> bool:
pass