diff --git a/cmdeploy/src/cmdeploy/cmdeploy.py b/cmdeploy/src/cmdeploy/cmdeploy.py index 0f78c64b..75528b31 100644 --- a/cmdeploy/src/cmdeploy/cmdeploy.py +++ b/cmdeploy/src/cmdeploy/cmdeploy.py @@ -15,6 +15,7 @@ from pathlib import Path from termcolor import colored from chatmaild.config import read_config, write_initial_config +import cmdeploy.dns # @@ -33,10 +34,29 @@ def init_cmd_options(parser): def init_cmd(args, out): """Initialize chatmail config file.""" if args.inipath.exists(): - out.red(f"Path exists, not modifying: {args.inipath}") - raise SystemExit(1) - write_initial_config(args.inipath, args.chatmail_domain) - out.green(f"created config file for {args.chatmail_domain} in {args.inipath}") + print(f"Path exists, not modifying: {args.inipath}") + else: + write_initial_config(args.inipath, args.chatmail_domain) + out.green(f"created config file for {args.chatmail_domain} in {args.inipath}") + ipaddress, _ = cmdeploy.dns.resolve(args.chatmail_domain) + mta_ipadress, _ = cmdeploy.dns.resolve("mta-sts." + args.chatmail_domain) + entries = 0 + 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") + if not mta_ipadress: + entries += 1 + to_print.append(f"\tCNAME\tmta-sts.{args.chatmail_domain}\t{args.chatmail_domain}.") + if entries == 1: + singular = "this entry" + elif entries == 2: + singular = "these entries" + else: + return + to_print[0] = to_print[0].replace("%dnsentry%", singular) + for line in to_print: + print(line) def run_cmd_options(parser): diff --git a/cmdeploy/src/cmdeploy/dns.py b/cmdeploy/src/cmdeploy/dns.py new file mode 100644 index 00000000..2e9b780c --- /dev/null +++ b/cmdeploy/src/cmdeploy/dns.py @@ -0,0 +1,34 @@ +import requests + +dns_types = { + "A": 1, + "AAAA": 28, + "CNAME": 5, +} + + +def resolve(domain: str) -> (str, str): + result, typ = get("A", domain), "A" + if not result: + result = get("CNAME", domain) + if result: + result, typ = get("A", result[:-1]), "A" + if not result: + result, typ = get("AAAA", domain), "AAAA" + return result, typ + + +def get(typ: str, domain: str) -> str: + """Get a DNS entry""" + url = "https://dns.nextdns.io/dns-query" + r = requests.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"]