aboutsummaryrefslogtreecommitdiff
path: root/tools/aio/modules/dns.py
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2023-12-11 15:02:08 +0800
committercrupest <crupest@outlook.com>2024-03-25 22:25:21 +0800
commit77c5f0d98f8318c8ec99fee64591b0701e270224 (patch)
tree56d7f83e4d4d736890d8d3999379a459b5d96ccf /tools/aio/modules/dns.py
parent52566293e75055513d397bf3ad64af969cd1f185 (diff)
downloadcrupest-77c5f0d98f8318c8ec99fee64591b0701e270224.tar.gz
crupest-77c5f0d98f8318c8ec99fee64591b0701e270224.tar.bz2
crupest-77c5f0d98f8318c8ec99fee64591b0701e270224.zip
tools(aio): move aio and related scripts.
Diffstat (limited to 'tools/aio/modules/dns.py')
-rw-r--r--tools/aio/modules/dns.py42
1 files changed, 42 insertions, 0 deletions
diff --git a/tools/aio/modules/dns.py b/tools/aio/modules/dns.py
new file mode 100644
index 0000000..5006d5f
--- /dev/null
+++ b/tools/aio/modules/dns.py
@@ -0,0 +1,42 @@
+from os.path import *
+from io import StringIO
+import re
+from .nginx import *
+
+
+def generate_dns_zone(domain: str, ip: str, /, ttl: str | int = 600, *, enable_mail: bool = True, dkim: str | None = None) -> str:
+ result = f"$ORIGIN {domain}.\n\n"
+ result += "; A records\n"
+ result += f"@ {ttl} IN A {ip}\n"
+ subdomains = list_subdomain_names()
+ for subdomain in subdomains:
+ result += f"{subdomain} {ttl} IN A {ip}\n"
+
+ if enable_mail:
+ result += "\n; MX records\n"
+ result += f"@ {ttl} IN MX 10 mail.{domain}.\n"
+ result += "\n; SPF record\n"
+ result += f"@ {ttl} IN TXT \"v=spf1 mx ~all\"\n"
+ if dkim is not None:
+ result += "\n; DKIM record\n"
+ result += f"mail._domainkey {ttl} IN TEXT \"{dkim}\""
+ result += "\n; DMARC record\n"
+ result += "_dmarc {ttl} IN TXT \"v=DMARC1; p=none; rua=mailto:dmarc.report@{domain}; ruf=mailto:dmarc.report@{domain}; sp=none; ri=86400\"\n"
+ return result
+
+
+def get_dkim_from_mailserver(domain: str) -> str | None:
+ dkim_path = join(data_dir, "dms/config/opendkim/keys", domain, "mail.txt")
+ if not exists(dkim_path):
+ return None
+
+ p = subprocess.run(["sudo", "cat", dkim_path],
+ capture_output=True, check=True)
+ value = ""
+ for match in re.finditer("\"(.*)\"", p.stdout.decode('utf-8')):
+ value += match.group(1)
+ return value
+
+
+def generate_dns_zone_with_dkim(domain: str, ip: str, /, ttl: str | int = 600) -> str:
+ return generate_dns_zone(domain, ip, ttl, enable_mail=True, dkim=get_dkim_from_mailserver(domain))