DPR-blocked-sites/runners/1_ips_from_domains_txt.py

33 lines
811 B
Python

import ipaddress
import socket
from collections import defaultdict
from utils import prettyprint
ips = []
dict = defaultdict(list)
def main(file_data: list):
for line in open('domains.txt'):
l = line.strip()
if l.startswith('#') or l == '':
continue
domain_or_range, comment, *_ = [x.strip() for x in l.split('#')] + [None]
try:
ipaddress.ip_network(domain_or_range)
dict[domain_or_range].append(comment)
except ValueError:
hostname_data = socket.gethostbyname_ex(domain_or_range)
for ip in hostname_data[2]:
dict[ip + '/32'].append(l)
ips = list(dict.keys())
for ip in ips:
file_data.append(prettyprint(ip, ', '.join(dict[ip])) + '\n')
return file_data