import requests
from dns import resolver, rdatatype

__API_KEY = 'YXJkQU1VVkRoSGZyV0FP'  # fill in your actual api key
__API_URL = 'https://dns.it53.nl:8443/api/v1/servers/localhost/zones'
__MASTER_DNS = 'ns1.it53.nl'  # fill in your actual master DNS

headers = {
    'X-API-Key': __API_KEY,
}

response = requests.get(__API_URL, headers=headers)

data = response.json()

for zone in data:
    zone_name = zone['name'].rstrip('.')

    try:
        answers = resolver.resolve(zone_name, 'NS')
        ns_records = [rdata.to_text().rstrip('.') for rdata in answers]  # strip ending '.'
        if __MASTER_DNS not in ns_records:
            print(f'🚨 {zone_name} does not point to master DNS: {__MASTER_DNS}')
            continue
    except Exception as e:
        print(f'🚨 Error resolving NS for {zone_name}: {e}')
        continue

    try:
        soa_answer = resolver.resolve(zone_name, 'SOA')
        if __MASTER_DNS not in soa_answer.rrset.to_text():
            print(f'🚨 SOA record of {zone_name} is inconsistent with master DNS: {__MASTER_DNS}')
        else:
            print(f'✅ DNS consistency check passed for {zone_name}.')
    except Exception as e:
        print(f'🚨 Error resolving SOA for {zone_name}: {e}')
