This commit is contained in:
Sazonov Andrey 2026-03-25 17:22:07 +03:00
parent 25e693fa04
commit 647734a380

View File

@ -162,6 +162,23 @@ async def resolve_domain(domain, dns_name, server, semaphore):
return []
def normalize_ip(ip):
try:
if "/" in ip:
return ip
addr = ipaddress.ip_address(ip)
if isinstance(addr, ipaddress.IPv4Address):
if addr.packed[-1] == 0:
return f"{addr}/24"
else:
return f"{addr}/32"
return ip
except ValueError:
return ip
async def get_cloudflare_ips():
async with httpx.AsyncClient() as client:
r = await client.get("https://www.cloudflare.com/ips-v4/")
@ -226,9 +243,10 @@ async def main():
logging.info(f"Удалено приватных IP: {before_private - len(all_ips)}")
logging.info(f"Финальных IP: {len(all_ips)}")
normalized_ips = {normalize_ip(ip) for ip in all_ips}
result_file = os.path.join(BASE_DIR, f"{cfg['name']}_result_ips.txt")
with open(result_file, "w") as f:
for ip in sorted(all_ips):
for ip in sorted(normalized_ips):
f.write(ip + "\n")
if not cfg["gateway"] and not cfg["interface"]:
@ -244,7 +262,7 @@ async def main():
f'ip route flush table {cfg["table"]}\n\n'
)
for ip in sorted(all_ips):
for ip in sorted(normalized_ips):
if "/" in ip:
if cfg["gateway"]:
f.write(f'ip route replace {ip} via {cfg["gateway"]} table {cfg["table"]}\n')