2019-12-13 13:54:00 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import database
|
|
|
|
import argparse
|
|
|
|
import requests
|
|
|
|
import typing
|
|
|
|
import ipaddress
|
|
|
|
import logging
|
|
|
|
import time
|
|
|
|
|
|
|
|
IPNetwork = typing.Union[ipaddress.IPv4Network, ipaddress.IPv6Network]
|
|
|
|
|
|
|
|
|
|
|
|
def get_ranges(asn: str) -> typing.Iterable[str]:
|
|
|
|
req = requests.get(
|
2021-08-14 23:27:28 +02:00
|
|
|
"https://stat.ripe.net/data/as-routing-consistency/data.json",
|
|
|
|
params={"resource": asn},
|
2019-12-13 13:54:00 +01:00
|
|
|
)
|
|
|
|
data = req.json()
|
2021-08-14 23:27:28 +02:00
|
|
|
for pref in data["data"]["prefixes"]:
|
|
|
|
yield pref["prefix"]
|
2019-12-13 13:54:00 +01:00
|
|
|
|
|
|
|
|
2019-12-17 13:29:02 +01:00
|
|
|
def get_name(asn: str) -> str:
|
|
|
|
req = requests.get(
|
2021-08-14 23:27:28 +02:00
|
|
|
"https://stat.ripe.net/data/as-overview/data.json", params={"resource": asn}
|
2019-12-17 13:29:02 +01:00
|
|
|
)
|
|
|
|
data = req.json()
|
2021-08-14 23:27:28 +02:00
|
|
|
return data["data"]["holder"]
|
2019-12-17 13:29:02 +01:00
|
|
|
|
|
|
|
|
2021-08-14 23:27:28 +02:00
|
|
|
if __name__ == "__main__":
|
2019-12-13 13:54:00 +01:00
|
|
|
|
2021-08-14 23:27:28 +02:00
|
|
|
log = logging.getLogger("feed_asn")
|
2019-12-13 13:54:00 +01:00
|
|
|
|
|
|
|
# Parsing arguments
|
|
|
|
parser = argparse.ArgumentParser(
|
2021-08-14 23:27:28 +02:00
|
|
|
description="Add the IP ranges associated to the AS in the database"
|
|
|
|
)
|
2019-12-13 13:54:00 +01:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
DB = database.Database()
|
|
|
|
|
2021-08-14 23:27:28 +02:00
|
|
|
def add_ranges(
|
|
|
|
path: database.Path,
|
|
|
|
match: database.Match,
|
|
|
|
) -> None:
|
2019-12-16 14:18:03 +01:00
|
|
|
assert isinstance(path, database.AsnPath)
|
2019-12-17 13:29:02 +01:00
|
|
|
assert isinstance(match, database.AsnNode)
|
2019-12-15 22:21:05 +01:00
|
|
|
asn_str = database.Database.unpack_asn(path)
|
2021-08-14 23:27:28 +02:00
|
|
|
DB.enter_step("asn_get_name")
|
2019-12-17 14:09:06 +01:00
|
|
|
name = get_name(asn_str)
|
|
|
|
match.name = name
|
2021-08-14 23:27:28 +02:00
|
|
|
DB.enter_step("asn_get_ranges")
|
2019-12-15 15:56:26 +01:00
|
|
|
for prefix in get_ranges(asn_str):
|
2019-12-13 13:54:00 +01:00
|
|
|
parsed_prefix: IPNetwork = ipaddress.ip_network(prefix)
|
|
|
|
if parsed_prefix.version == 4:
|
2021-08-14 23:27:28 +02:00
|
|
|
DB.set_ip4network(prefix, source=path, updated=int(time.time()))
|
|
|
|
log.info("Added %s from %s (%s)", prefix, path, name)
|
2019-12-13 13:54:00 +01:00
|
|
|
elif parsed_prefix.version == 6:
|
2021-08-14 23:27:28 +02:00
|
|
|
log.warning("Unimplemented prefix version: %s", prefix)
|
2019-12-13 13:54:00 +01:00
|
|
|
else:
|
2021-08-14 23:27:28 +02:00
|
|
|
log.error("Unknown prefix version: %s", prefix)
|
2019-12-13 13:54:00 +01:00
|
|
|
|
2019-12-17 13:29:02 +01:00
|
|
|
for _ in DB.exec_each_asn(add_ranges):
|
2019-12-16 14:18:03 +01:00
|
|
|
pass
|
|
|
|
|
2019-12-15 15:56:26 +01:00
|
|
|
DB.save()
|