from netaddr import IPNetwork
from utils_v2.string import json

src_network = IPNetwork("100.64.0.0/24")
src_subnets = list(src_network.subnet(28))
print("SUBNET:", json.to_string(src_subnets, default=str))
print("COUNT:", len(src_subnets))


dst_network = IPNetwork("100.127.0.0/28")
dst_subnets = list(dst_network.subnet(32))
print("SUBNET:", json.to_string(dst_subnets, default=str))
print("COUNT:", len(dst_subnets))


final_json = [
    {
        "action": "src-nat",
        "chain": "srcnat",
        "disabled": "false",
        "src-address": str(i[0]),
        "to-addresses": str(i[1])
    } for i in zip(src_subnets, dst_subnets)
]
print("SUBNET:", json.to_string(final_json, default=str))

import asyncio
import httpx

http_client = httpx.AsyncClient(
    limits = httpx.Limits(
        max_connections = 100,  # ............ Maximum number of connections allowed in the pool.
        max_keepalive_connections = 50,  # ... Maximum number of connections that can be kept alive.
    ),
    timeout = httpx.Timeout(
        pool = 120.0,  # .... Time to wait for a free connection from the pool.
        connect = 2.5,  # ... Time to wait for establishing a connection to the server.
        write = 10.0,  # .... Time to wait for sending data.
        read = 9.9  # ....... Time to wait for receiving data.
    )
)


async def configure_nat():

    tasks = [
        http_client.put(
            url = r"http://102.210.175.251/rest/ip/firewall/nat",
            auth = httpx.BasicAuth(username = "easyfi", password = "easyfi"),
            json = i
        ) for i in final_json
    ]
    print("Async NAT")
    results = await asyncio.gather(*tasks)
    print(results)

# asyncio.run(configure_nat())
