From 6a42673229dcf5a2299b945ab68dd312096f36e7 Mon Sep 17 00:00:00 2001 From: Son NK <> Date: Sun, 3 May 2020 11:19:14 +0200 Subject: [PATCH] remove the copy button, use CNAME for DKIM --- .../dashboard/domain_detail/dns.html | 51 ++++++++++--------- app/dashboard/views/domain_detail.py | 19 +++---- app/dns_utils.py | 15 ++++++ 3 files changed, 53 insertions(+), 32 deletions(-) diff --git a/app/dashboard/templates/dashboard/domain_detail/dns.html b/app/dashboard/templates/dashboard/domain_detail/dns.html index 30657e51..4a6d9a24 100644 --- a/app/dashboard/templates/dashboard/domain_detail/dns.html +++ b/app/dashboard/templates/dashboard/domain_detail/dns.html @@ -28,19 +28,21 @@
Add the following MX DNS record to your domain.
- Please note that there's a point (.) at the end target addresses.
+ Please note that there's a point (.) at the end target addresses. + This is to make sure the absolute address is used. +
Also some domain registrars (Namecheap, CloudFlare, etc) might use @ for the root domain.
{% for priority, email_server in EMAIL_SERVERS_WITH_PRIORITY %}
- Domain: {{ custom_domain.domain }} or @
+ Record: MX
+ Domain: {{ custom_domain.domain }} or @
Priority: {{ priority }}
- Target: {{ email_server }} - + Target: {{ email_server }}
{% endfor %} @@ -93,18 +95,18 @@ Setting up SPF is highly recommended to reduce the chance your emails ending up in the recipient's Spam folder. -
Add the following TXT DNS record to your domain
+
Add the following TXT DNS record to your domain.
- Domain: {{ custom_domain.domain }} or @
+ Record: TXT
+ Domain: {{ custom_domain.domain }} or @
Value: - + {{ spf_record }} -
@@ -158,18 +160,21 @@ Setting up DKIM is highly recommended to reduce the chance your emails ending up in the recipient's Spam folder. -
Add the following TXT DNS record to your domain
+
Add the following CNAME DNS record to your domain.
- Domain: dkim._domainkey.{{ custom_domain.domain }}
+ Record: CNAME
+ Domain: dkim._domainkey.{{ custom_domain.domain }}
Value: - - {{ dkim_record }} + + {{ dkim_cname }} -
@@ -189,7 +194,7 @@
Your DNS is not correctly set. {% if dkim_errors %} - The TXT record we obtain for + The CNAME record we obtain for dkim._domainkey.{{ custom_domain.domain }} is:
diff --git a/app/dashboard/views/domain_detail.py b/app/dashboard/views/domain_detail.py index b8cdbcad..544875e4 100644 --- a/app/dashboard/views/domain_detail.py +++ b/app/dashboard/views/domain_detail.py @@ -8,6 +8,7 @@ from app.dns_utils import ( get_spf_domain, get_dkim_record, get_txt_record, + get_cname_record, ) from app.extensions import db from app.models import CustomDomain, Alias @@ -21,6 +22,11 @@ def domain_detail_dns(custom_domain_id): flash("You cannot see this page", "warning") return redirect(url_for("dashboard.index")) + spf_record = f"v=spf1 include:{EMAIL_DOMAIN} -all" + + # hardcode the DKIM selector here + dkim_cname = f"dkim._domainkey.{EMAIL_DOMAIN}" + mx_ok = spf_ok = dkim_ok = True mx_errors = spf_errors = dkim_errors = [] @@ -67,9 +73,8 @@ def domain_detail_dns(custom_domain_id): spf_errors = get_txt_record(custom_domain.domain) elif request.form.get("form-name") == "check-dkim": - dkim_record = get_dkim_record(custom_domain.domain) - correct_dkim_record = f"v=DKIM1; k=rsa; p={DKIM_DNS_VALUE}" - if dkim_record == correct_dkim_record: + dkim_record = get_cname_record(custom_domain.domain) + if dkim_record == dkim_cname: flash("The DKIM is setup correctly.", "success") custom_domain.dkim_verified = True db.session.commit() @@ -80,13 +85,9 @@ def domain_detail_dns(custom_domain_id): ) ) else: - flash("DKIM: the TXT record is not correctly set", "warning") + flash("DKIM: the CNAME record is not correctly set", "warning") dkim_ok = False - dkim_errors = get_txt_record(f"dkim._domainkey.{custom_domain.domain}") - - spf_record = f"v=spf1 include:{EMAIL_DOMAIN} -all" - - dkim_record = f"v=DKIM1; k=rsa; p={DKIM_DNS_VALUE}" + dkim_errors = [dkim_record or "[Empty]"] return render_template( "dashboard/domain_detail/dns.html", diff --git a/app/dns_utils.py b/app/dns_utils.py index 1dbae850..052c71d5 100644 --- a/app/dns_utils.py +++ b/app/dns_utils.py @@ -1,3 +1,5 @@ +from typing import Optional + import dns.resolver @@ -10,6 +12,19 @@ def _get_dns_resolver(): return my_resolver +def get_cname_record(hostname) -> Optional[str]: + """Return the CNAME record if exists for a domain""" + try: + answers = _get_dns_resolver().query(hostname, "CNAME") + except Exception: + return None + + for a in answers: + return a + + return None + + def get_mx_domains(hostname) -> [(int, str)]: """return list of (priority, domain name). domain name ends with a "." at the end.