From 647734a3805711eadb3bfa226264c22f7bc0538f Mon Sep 17 00:00:00 2001 From: Sazonov Andrey Date: Wed, 25 Mar 2026 17:22:07 +0300 Subject: [PATCH] fixik --- redirector.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/redirector.py b/redirector.py index 58af714..8da7e94 100644 --- a/redirector.py +++ b/redirector.py @@ -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')