DPR-blocked-sites/runners/y_google_ranges.py
Maxim Slipenko b748fb1c96 Добавляет IP-диапазоны Google (#1)
Co-authored-by: Maxim Slipenko <no-reply@maxim.slipenko.com>
Reviewed-on: #1
2022-09-16 15:15:04 +00:00

34 lines
909 B
Python

from collections import defaultdict
from utils import prettyprint
import ipaddress
new_ips_data = defaultdict(list)
cloud_ranges_file = open("google_ranges.txt", "r")
cloud_ranges = [x.strip() for x in cloud_ranges_file.readlines()]
def process_list(item: str):
l = item.strip()
if l.startswith('#') or l == '':
return True
[range, comment] = [x.strip() for x in l.split('#')]
for cloud_range in cloud_ranges:
if ipaddress.ip_network(range).subnet_of(ipaddress.ip_network(cloud_range)):
new_ips_data[cloud_range].append(f'{comment} ({range})')
return False
return True
def main(file_data: list[str]):
file_data = list(filter(process_list, file_data))
ips = list(new_ips_data.keys())
for ip in ips:
file_data.append(prettyprint(ip, 'google_range :: ' + ', '.join(new_ips_data[ip])) + '\n')
return file_data