From d2fe417715d2116a33bf6468bb37b272a2be9edb Mon Sep 17 00:00:00 2001 From: missytake Date: Wed, 13 Dec 2023 04:18:25 +0100 Subject: [PATCH] DNS: try other resolvers if the first doesn't have it --- cmdeploy/src/cmdeploy/cmdeploy.py | 20 +++++++---- cmdeploy/src/cmdeploy/dns.py | 56 +++++++++++++++++-------------- 2 files changed, 45 insertions(+), 31 deletions(-) diff --git a/cmdeploy/src/cmdeploy/cmdeploy.py b/cmdeploy/src/cmdeploy/cmdeploy.py index e41a9475..e7aef07a 100644 --- a/cmdeploy/src/cmdeploy/cmdeploy.py +++ b/cmdeploy/src/cmdeploy/cmdeploy.py @@ -45,11 +45,11 @@ def init_cmd(args, out): to_print = ["Now you should add %dnsentry% at your DNS provider:\n"] if not ipaddress: entries += 1 - to_print.append(f"\tA\t{args.chatmail_domain}\t\t") + to_print.append(f"\tA\t{args.chatmail_domain}.\t\t") if not mta_ipadress or mta_ipadress != ipaddress: entries += 1 to_print.append( - f"\tCNAME\tmta-sts.{args.chatmail_domain}\t{args.chatmail_domain}." + f"\tCNAME\tmta-sts.{args.chatmail_domain}.\t{args.chatmail_domain}." ) if entries == 1: singular = "this entry" @@ -126,11 +126,19 @@ def dns_cmd(args, out): print() if not dns.check_ptr_record(ipv4, args.config.mail_domain): - print(f"You should add a PTR/reverse DNS entry for {ipv4}, with the value: {args.config.mail_domain}.") - print("You can do so at your hosting provider (maybe this isn't your DNS provider).\n") + print( + f"You should add a PTR/reverse DNS entry for {ipv4}, with the value: {args.config.mail_domain}." + ) + print( + "You can do so at your hosting provider (maybe this isn't your DNS provider).\n" + ) if not dns.check_ptr_record(ipv6, args.config.mail_domain): - print(f"You should add a PTR/reverse DNS entry for {ipv6}, with the value: {args.config.mail_domain}.") - print("You can do so at your hosting provider (maybe this isn't your DNS provider).\n") + print( + f"You should add a PTR/reverse DNS entry for {ipv6}, with the value: {args.config.mail_domain}." + ) + print( + "You can do so at your hosting provider (maybe this isn't your DNS provider).\n" + ) to_print = [] with open(template, "r") as f: diff --git a/cmdeploy/src/cmdeploy/dns.py b/cmdeploy/src/cmdeploy/dns.py index e01ded67..d470cdeb 100644 --- a/cmdeploy/src/cmdeploy/dns.py +++ b/cmdeploy/src/cmdeploy/dns.py @@ -1,7 +1,11 @@ import requests from ipaddress import ip_address -url = "https://dns.nextdns.io/dns-query" +resolvers = [ + "https://dns.nextdns.io/dns-query", + "https://dns.google/resolve", + "https://cloudflare-dns.com/dns-query", +] dns_types = { "A": 1, "AAAA": 28, @@ -20,36 +24,38 @@ class DNS: def get(self, typ: str, domain: str) -> str: """Get a DNS entry""" - r = self.session.get( - url, - params={"name": domain, "type": typ}, - headers={"accept": "application/dns-json"}, - ) + for url in resolvers: + r = self.session.get( + url, + params={"name": domain, "type": typ}, + headers={"accept": "application/dns-json"}, + ) - j = r.json() - if "Answer" in j: - for answer in j["Answer"]: - if answer["type"] == dns_types[typ]: - return answer["data"] + j = r.json() + if "Answer" in j: + for answer in j["Answer"]: + if answer["type"] == dns_types[typ]: + return answer["data"] return "" def resolve_mx(self, domain: str) -> (str, str): """Resolve an MX entry""" - r = self.session.get( - url, - params={"name": domain, "type": "MX"}, - headers={"accept": "application/dns-json"}, - ) + for url in resolvers: + r = self.session.get( + url, + params={"name": domain, "type": "MX"}, + headers={"accept": "application/dns-json"}, + ) - j = r.json() - if "Answer" in j: - result = (0, None) - for answer in j["Answer"]: - if answer["type"] == dns_types["MX"]: - prio, server_name = answer["data"].split() - if int(prio) > result[0]: - result = (int(prio), server_name) - return result + j = r.json() + if "Answer" in j: + result = (0, None) + for answer in j["Answer"]: + if answer["type"] == dns_types["MX"]: + prio, server_name = answer["data"].split() + if int(prio) > result[0]: + result = (int(prio), server_name) + return result return None, None def resolve(self, domain: str) -> str: