From 1742f517789751a0e8142ffe645c822839cbca94 Mon Sep 17 00:00:00 2001 From: Franziska Kunsmann Date: Tue, 14 Dec 2021 13:24:26 +0100 Subject: [PATCH] libs.tools.resolve_identifier: add support for named networks --- configs/as8881.txt | 665 +++++++++++++++++++++++++++++++++++++++++++++ libs/firewall.py | 35 +++ libs/tools.py | 15 +- 3 files changed, 707 insertions(+), 8 deletions(-) create mode 100644 configs/as8881.txt create mode 100644 libs/firewall.py diff --git a/configs/as8881.txt b/configs/as8881.txt new file mode 100644 index 0000000..5cfd733 --- /dev/null +++ b/configs/as8881.txt @@ -0,0 +1,665 @@ +104.151.0.0/17 +109.250.0.0/16 +109.250.0.0/20 +109.250.112.0/20 +109.250.128.0/20 +109.250.144.0/20 +109.250.16.0/20 +109.250.160.0/20 +109.250.176.0/20 +109.250.192.0/19 +109.250.224.0/19 +109.250.32.0/20 +109.250.48.0/20 +109.250.64.0/19 +109.250.80.0/22 +109.250.84.0/22 +109.250.88.0/22 +109.250.92.0/22 +109.250.96.0/20 +134.101.0.0/21 +149.233.32.0/19 +153.94.0.0/20 +153.97.113.0/24 +153.97.114.0/24 +156.67.7.0/24 +159.255.168.0/23 +159.255.170.0/24 +159.255.171.0/24 +160.60.0.0/16 +160.60.0.0/24 +160.60.1.0/24 +160.60.2.0/24 +160.60.3.0/24 +185.108.228.0/23 +185.128.68.0/22 +185.139.76.0/22 +185.151.200.0/24 +185.151.201.0/24 +185.151.203.0/24 +185.158.48.0/22 +185.199.205.0/24 +185.235.232.0/22 +185.8.230.0/23 +192.166.0.0/21 +192.166.4.0/24 +192.166.75.0/24 +192.166.84.0/22 +192.166.87.0/24 +192.166.88.0/21 +193.101.220.0/23 +193.101.4.0/23 +193.101.5.0/24 +193.105.230.0/24 +193.111.212.0/22 +193.111.212.0/24 +193.141.24.0/24 +193.163.13.0/25 +193.163.13.128/25 +193.186.107.0/24 +193.186.108.0/24 +193.219.15.0/24 +193.22.120.0/21 +193.22.120.0/24 +193.22.121.0/24 +193.22.122.0/24 +193.22.123.0/24 +193.22.124.0/24 +193.22.125.0/24 +193.22.126.0/24 +193.22.127.0/24 +193.22.180.0/22 +193.22.3.0/24 +193.26.240.0/22 +193.26.242.0/24 +193.28.72.0/21 +193.29.232.0/21 +193.29.232.0/24 +193.29.237.0/24 +193.29.238.0/24 +193.29.240.0/24 +193.29.243.0/24 +193.29.246.0/24 +193.30.132.0/24 +193.96.238.0/24 +193.98.229.0/24 +193.98.40.0/22 +193.99.160.0/21 +194.113.252.0/23 +194.113.253.0/24 +194.115.26.0/24 +194.120.182.0/23 +194.120.182.0/24 +194.120.183.0/24 +194.120.184.0/24 +194.120.189.0/24 +194.127.140.0/22 +194.127.144.0/21 +194.127.203.0/24 +194.139.55.0/24 +194.145.230.0/24 +194.156.216.0/21 +194.156.232.0/23 +194.156.233.0/24 +194.174.168.0/22 +194.180.53.0/24 +194.180.64.0/20 +194.187.112.0/24 +194.39.185.0/24 +194.39.190.0/24 +194.39.87.0/24 +194.42.108.0/23 +194.59.34.0/24 +194.59.35.0/24 +194.6.239.0/24 +194.76.106.0/24 +194.76.107.0/24 +194.76.151.0/24 +194.88.25.0/24 +194.9.190.0/24 +194.99.0.0/21 +194.99.113.0/24 +195.149.80.0/23 +195.167.208.0/20 +195.191.20.0/23 +195.202.32.0/19 +195.226.160.0/19 +195.226.96.0/19 +195.234.139.0/24 +195.238.233.0/24 +195.244.10.0/23 +195.64.176.0/23 +195.93.158.0/23 +212.204.0.0/19 +212.7.128.0/19 +212.8.0.0/19 +212.80.224.0/19 +212.80.224.0/20 +212.80.240.0/20 +212.93.0.0/19 +213.138.32.0/19 +213.139.128.0/19 +213.182.128.0/19 +213.30.192.0/18 +217.117.96.0/20 +217.117.97.0/24 +217.117.98.0/24 +217.199.64.0/20 +217.27.192.0/20 +217.65.16.0/20 +217.66.35.0/24 +217.66.36.0/24 +217.66.37.0/24 +217.66.38.0/24 +217.66.39.0/24 +217.66.43.0/24 +217.66.44.0/24 +217.66.47.0/24 +217.78.128.0/20 +217.9.32.0/19 +217.9.96.0/19 +45.13.15.0/24 +46.142.0.0/16 +46.142.0.0/20 +46.142.112.0/20 +46.142.128.0/20 +46.142.144.0/20 +46.142.16.0/20 +46.142.160.0/20 +46.142.176.0/20 +46.142.194.0/24 +46.142.214.0/24 +46.142.224.0/19 +46.142.32.0/20 +46.142.48.0/20 +46.142.64.0/20 +46.142.80.0/20 +46.142.96.0/20 +46.189.0.0/17 +46.189.116.0/24 +62.214.0.0/16 +62.214.213.0/24 +62.214.224.0/19 +62.217.32.0/19 +62.220.0.0/19 +62.72.64.0/19 +62.72.88.0/22 +62.72.92.0/23 +62.72.94.0/24 +77.74.136.0/21 +77.87.190.0/24 +80.242.160.0/19 +82.119.160.0/19 +82.140.0.0/18 +82.140.48.0/21 +82.144.32.0/19 +82.144.34.0/24 +82.144.35.0/24 +82.144.36.0/24 +82.144.37.0/24 +82.145.0.0/19 +82.194.96.0/19 +82.207.128.0/17 +82.207.192.0/19 +82.207.224.0/21 +82.207.232.0/22 +82.207.236.0/24 +82.207.240.0/20 +82.207.244.0/24 +82.207.245.0/24 +82.207.246.0/24 +82.207.247.0/24 +82.207.248.0/24 +82.207.249.0/24 +82.207.250.0/24 +82.207.251.0/24 +82.207.252.0/24 +82.207.253.0/24 +82.207.254.0/24 +82.207.255.0/24 +83.135.0.0/16 +83.135.0.0/22 +83.135.112.0/20 +83.135.128.0/20 +83.135.144.0/20 +83.135.16.0/22 +83.135.160.0/21 +83.135.164.0/22 +83.135.168.0/21 +83.135.176.0/22 +83.135.180.0/22 +83.135.184.0/21 +83.135.192.0/20 +83.135.20.0/24 +83.135.208.0/20 +83.135.21.0/24 +83.135.22.0/24 +83.135.224.0/22 +83.135.23.0/24 +83.135.230.0/23 +83.135.232.0/21 +83.135.24.0/24 +83.135.240.0/22 +83.135.244.0/24 +83.135.245.0/24 +83.135.248.0/24 +83.135.249.0/24 +83.135.25.0/24 +83.135.250.0/24 +83.135.251.0/24 +83.135.252.0/24 +83.135.253.0/24 +83.135.254.0/24 +83.135.255.0/24 +83.135.26.0/24 +83.135.27.0/24 +83.135.28.0/24 +83.135.29.0/24 +83.135.30.0/24 +83.135.31.0/24 +83.135.32.0/19 +83.135.4.0/22 +83.135.64.0/20 +83.135.8.0/21 +83.135.80.0/20 +83.135.96.0/20 +83.243.48.0/22 +83.243.52.0/22 +84.19.192.0/19 +84.19.192.0/20 +84.19.208.0/20 +87.122.0.0/15 +87.122.0.0/16 +87.122.0.0/20 +87.122.112.0/20 +87.122.128.0/21 +87.122.136.0/22 +87.122.144.0/20 +87.122.16.0/22 +87.122.160.0/20 +87.122.176.0/21 +87.122.184.0/24 +87.122.185.0/24 +87.122.186.0/24 +87.122.187.0/24 +87.122.188.0/24 +87.122.189.0/24 +87.122.190.0/24 +87.122.191.0/24 +87.122.192.0/20 +87.122.20.0/22 +87.122.208.0/20 +87.122.224.0/20 +87.122.24.0/21 +87.122.240.0/20 +87.122.32.0/19 +87.122.64.0/20 +87.122.80.0/20 +87.122.96.0/20 +87.123.0.0/16 +87.123.0.0/20 +87.123.112.0/20 +87.123.128.0/20 +87.123.144.0/20 +87.123.16.0/20 +87.123.160.0/20 +87.123.176.0/20 +87.123.192.0/20 +87.123.208.0/22 +87.123.216.0/21 +87.123.224.0/20 +87.123.240.0/22 +87.123.244.0/22 +87.123.248.0/22 +87.123.252.0/24 +87.123.253.0/24 +87.123.254.0/24 +87.123.255.0/24 +87.123.32.0/20 +87.123.48.0/20 +87.123.64.0/20 +87.123.80.0/20 +87.123.96.0/20 +88.130.0.0/16 +88.130.0.0/20 +88.130.112.0/20 +88.130.130.0/23 +88.130.132.0/22 +88.130.136.0/21 +88.130.144.0/21 +88.130.152.0/24 +88.130.153.0/24 +88.130.154.0/24 +88.130.155.0/24 +88.130.156.0/22 +88.130.156.0/24 +88.130.157.0/24 +88.130.158.0/24 +88.130.159.0/24 +88.130.16.0/20 +88.130.160.0/21 +88.130.172.0/22 +88.130.176.0/21 +88.130.180.0/24 +88.130.181.0/24 +88.130.182.0/24 +88.130.183.0/24 +88.130.184.0/24 +88.130.185.0/24 +88.130.186.0/24 +88.130.187.0/24 +88.130.188.0/24 +88.130.189.0/24 +88.130.190.0/24 +88.130.191.0/24 +88.130.192.0/21 +88.130.200.0/21 +88.130.208.0/21 +88.130.216.0/21 +88.130.216.0/22 +88.130.220.0/24 +88.130.221.0/24 +88.130.222.0/24 +88.130.223.0/24 +88.130.32.0/20 +88.130.48.0/24 +88.130.49.0/24 +88.130.50.0/24 +88.130.51.0/24 +88.130.52.0/24 +88.130.53.0/24 +88.130.54.0/24 +88.130.55.0/24 +88.130.56.0/24 +88.130.57.0/24 +88.130.58.0/24 +88.130.59.0/24 +88.130.60.0/24 +88.130.61.0/24 +88.130.62.0/24 +88.130.63.0/24 +88.130.64.0/20 +88.130.80.0/20 +88.130.96.0/20 +89.244.0.0/14 +89.244.0.0/16 +89.244.112.0/21 +89.244.120.0/21 +89.244.120.0/22 +89.244.124.0/24 +89.244.125.0/24 +89.244.126.0/24 +89.244.127.0/24 +89.244.160.0/20 +89.244.164.0/22 +89.244.168.0/21 +89.244.176.0/20 +89.244.192.0/20 +89.244.208.0/20 +89.244.224.0/20 +89.244.240.0/20 +89.244.64.0/21 +89.244.72.0/22 +89.244.76.0/22 +89.244.80.0/20 +89.244.96.0/20 +89.245.0.0/16 +89.245.0.0/20 +89.245.16.0/20 +89.245.160.0/20 +89.245.176.0/21 +89.245.184.0/24 +89.245.185.0/24 +89.245.186.0/24 +89.245.187.0/24 +89.245.188.0/24 +89.245.189.0/24 +89.245.190.0/24 +89.245.191.0/24 +89.245.192.0/20 +89.245.208.0/20 +89.245.32.0/20 +89.245.48.0/20 +89.245.64.0/20 +89.245.80.0/20 +89.245.96.0/20 +89.246.0.0/16 +89.246.0.0/20 +89.246.104.0/23 +89.246.106.0/24 +89.246.107.0/24 +89.246.108.0/24 +89.246.109.0/24 +89.246.110.0/24 +89.246.111.0/24 +89.246.112.0/22 +89.246.116.0/22 +89.246.120.0/24 +89.246.121.0/24 +89.246.122.0/24 +89.246.123.0/24 +89.246.124.0/22 +89.246.16.0/20 +89.246.160.0/20 +89.246.176.0/22 +89.246.180.0/22 +89.246.184.0/21 +89.246.192.0/19 +89.246.32.0/20 +89.246.48.0/21 +89.246.56.0/21 +89.246.96.0/21 +89.247.0.0/16 +89.247.0.0/20 +89.247.112.0/21 +89.247.120.0/22 +89.247.124.0/24 +89.247.125.0/24 +89.247.126.0/24 +89.247.127.0/24 +89.247.144.0/22 +89.247.152.0/21 +89.247.16.0/20 +89.247.160.0/22 +89.247.164.0/22 +89.247.168.0/21 +89.247.192.0/20 +89.247.208.0/21 +89.247.216.0/22 +89.247.224.0/21 +89.247.232.0/22 +89.247.236.0/22 +89.247.240.0/21 +89.247.240.0/22 +89.247.252.0/24 +89.247.253.0/24 +89.247.254.0/24 +89.247.255.0/24 +89.247.32.0/20 +89.247.48.0/20 +89.247.64.0/20 +89.247.80.0/20 +89.247.96.0/20 +89.27.128.0/17 +89.27.153.0/24 +91.194.180.0/23 +91.195.104.0/23 +91.198.67.0/24 +91.199.158.0/24 +91.201.128.0/22 +91.204.10.0/24 +91.206.214.0/23 +91.208.159.0/24 +91.208.191.0/24 +91.208.212.0/24 +91.217.145.0/24 +91.220.125.0/24 +91.223.2.0/24 +91.223.41.0/24 +91.229.3.0/24 +92.116.0.0/15 +92.116.0.0/20 +92.116.112.0/21 +92.116.120.0/21 +92.116.128.0/18 +92.116.16.0/20 +92.116.192.0/19 +92.116.200.0/21 +92.116.224.0/20 +92.116.240.0/20 +92.116.32.0/20 +92.116.48.0/20 +92.116.64.0/18 +92.116.96.0/19 +92.117.0.0/20 +92.117.128.0/19 +92.117.16.0/20 +92.117.160.0/19 +92.117.192.0/21 +92.117.200.0/21 +92.117.208.0/20 +92.117.224.0/19 +92.117.224.0/21 +92.117.232.0/21 +92.117.240.0/21 +92.117.248.0/21 +92.117.64.0/20 +92.117.80.0/20 +94.134.0.0/15 +94.134.0.0/18 +94.134.100.0/22 +94.134.104.0/21 +94.134.112.0/21 +94.134.120.0/24 +94.134.121.0/24 +94.134.122.0/24 +94.134.123.0/24 +94.134.124.0/24 +94.134.125.0/24 +94.134.126.0/24 +94.134.127.0/24 +94.134.128.0/20 +94.134.144.0/22 +94.134.148.0/22 +94.134.152.0/21 +94.134.160.0/21 +94.134.168.0/22 +94.134.172.0/22 +94.134.176.0/20 +94.134.176.0/21 +94.134.192.0/20 +94.134.208.0/21 +94.134.216.0/21 +94.134.224.0/19 +94.134.64.0/20 +94.134.80.0/22 +94.134.84.0/24 +94.134.85.0/24 +94.134.86.0/24 +94.134.87.0/24 +94.134.88.0/24 +94.134.89.0/24 +94.134.90.0/24 +94.134.91.0/24 +94.134.92.0/24 +94.134.93.0/24 +94.134.94.0/24 +94.134.95.0/24 +94.134.96.0/22 +2001:1438:1000::/36 +2001:1438:2000::/36 +2001:1438:3000::/36 +2001:1438:4000::/36 +2001:1438::/32 +2001:1438:f000::/36 +2001:1438:fff:10::/64 +2001:1438:fff:11::/64 +2001:1438:fff:12::/64 +2001:1438:fff:3::/64 +2001:1438:fff:4::/64 +2001:1438:fff:5::/64 +2001:1438:fff:6::/64 +2001:1438:fff:7::/64 +2001:1438:fff:8::/64 +2001:1438:fff:9::/64 +2001:1438:fff:a::/64 +2001:1438:fff:b::/64 +2001:1438:fff:c::/64 +2001:1438:fff:d::/64 +2001:1438:fff:e::/64 +2001:1438:fff:f::/64 +2001:16b8:1000::/40 +2001:16b8:100::/40 +2001:16b8:1100::/40 +2001:16b8:1200::/40 +2001:16b8:1300::/40 +2001:16b8:1400::/40 +2001:16b8:2000::/40 +2001:16b8:200::/40 +2001:16b8:2100::/40 +2001:16b8:2200::/40 +2001:16b8:2300::/40 +2001:16b8:2400::/40 +2001:16b8:2500::/40 +2001:16b8:2600::/40 +2001:16b8:2700::/40 +2001:16b8:2800::/40 +2001:16b8:2900::/40 +2001:16b8:2a00::/40 +2001:16b8:2b00::/40 +2001:16b8:2c00::/40 +2001:16b8:2d00::/40 +2001:16b8:2e00::/40 +2001:16b8:300::/40 +2001:16b8:4000::/40 +2001:16b8:400::/40 +2001:16b8:4100::/40 +2001:16b8:4200::/40 +2001:16b8:4300::/40 +2001:16b8:4500::/40 +2001:16b8:4600::/40 +2001:16b8:500::/40 +2001:16b8:6000::/40 +2001:16b8:600::/40 +2001:16b8:6100::/40 +2001:16b8:6200::/40 +2001:16b8:6300::/40 +2001:16b8:6400::/40 +2001:16b8:6500::/40 +2001:16b8:6600::/40 +2001:16b8:6700::/40 +2001:16b8:6800::/40 +2001:16b8:700::/40 +2001:16b8:800::/40 +2001:16b8:900::/40 +2001:16b8::/32 +2001:16b8::/40 +2001:16b8:a00::/40 +2001:16b8:b00::/40 +2001:678:274::/48 +2001:678:c74::/48 +2001:67c:27ac::/48 +2001:67c:2878::/48 +2001:67c:2e8c::/48 +2001:67c:660::/48 +2001:7b0::/32 +2001:9e8:2000::/35 +2001:9e8:4000::/35 +2001:9e8:6000::/35 +2001:9e8:8000::/35 +2001:9e8::/32 +2001:9e8::/35 +2001:9e8:a000::/35 +2001:9e8:c000::/35 +2001:9e8:e000::/35 +2a00:1900::/32 +2a00:fb8:2000::/35 +2a00:fb8:4000::/35 +2a00:fb8:6000::/35 +2a00:fb8::/29 +2a00:fb8::/32 +2a00:fb8::/35 +2a03:3fc0:2000::/48 +2a0a:ed40::/29 +2a0d:240::/29 +2a0d:ad00::/29 diff --git a/libs/firewall.py b/libs/firewall.py new file mode 100644 index 0000000..4b981a4 --- /dev/null +++ b/libs/firewall.py @@ -0,0 +1,35 @@ +from os.path import abspath, dirname, join +from ipaddress import ip_network, IPv4Network + +REPO_PATH = dirname(dirname(abspath(__file__))) + +def generate_ip_list_from_routes(filename): + # generated using: + # whois -i origin as8881 | awk '/^route/ {print $2}' > configs/as8881.txt + with open(join(REPO_PATH, 'configs', f'{filename}.txt')) as f: + networks = f.read().splitlines() + + result = { + 'ipv4': set(), + 'ipv6': set(), + } + + for line in networks: + line = line.strip() + + if not line or line.startswith('#'): + continue + + ip = ip_network(line) + + if isinstance(ip, IPv4Network): + result['ipv4'].add(ip) + else: + result['ipv6'].add(ip) + + return result + + +named_networks = { + 'versatel': generate_ip_list_from_routes('as8881'), +} diff --git a/libs/tools.py b/libs/tools.py index d96feec..eacbc9d 100644 --- a/libs/tools.py +++ b/libs/tools.py @@ -22,14 +22,13 @@ def resolve_identifier(repo, identifier): return {'ipv4': {ip}, 'ipv6': set()} else: return {'ipv4': set(), 'ipv6': {ip}} - except Exception as e: - io.stderr('{x} {t} Exception while resolving "{i}": {e}'.format( - x=red('✘'), - t=bold('libs.tools.resolve_identifier'), - i=identifier, - e=str(e), - )) - raise + except ValueError: + try: + return repo.libs.firewall.named_networks[identifier] + except KeyError: + raise BundleError( + f'libs.tools.resolve_identifier(): Could not resolve {identifier}' + ) found_ips = set() for node in nodes: