2019-12-27 23:50:09 +01:00
|
|
|
from flask import render_template, request, redirect, url_for, flash
|
|
|
|
from flask_login import login_required, current_user
|
|
|
|
|
2019-12-30 19:34:38 +01:00
|
|
|
from app.config import EMAIL_SERVERS_WITH_PRIORITY, DKIM_DNS_VALUE, EMAIL_DOMAIN
|
2019-12-27 23:50:09 +01:00
|
|
|
from app.dashboard.base import dashboard_bp
|
|
|
|
from app.dns_utils import (
|
|
|
|
get_mx_domains,
|
|
|
|
get_spf_domain,
|
|
|
|
get_dkim_record,
|
|
|
|
get_txt_record,
|
|
|
|
)
|
|
|
|
from app.extensions import db
|
2019-12-30 17:52:08 +01:00
|
|
|
from app.models import CustomDomain, GenEmail
|
2019-12-27 23:50:09 +01:00
|
|
|
|
|
|
|
|
2019-12-30 17:52:08 +01:00
|
|
|
@dashboard_bp.route("/domains/<int:custom_domain_id>/dns", methods=["GET", "POST"])
|
2019-12-27 23:50:09 +01:00
|
|
|
@login_required
|
2019-12-30 17:52:08 +01:00
|
|
|
def domain_detail_dns(custom_domain_id):
|
2019-12-27 23:50:09 +01:00
|
|
|
# only premium user can see custom domain
|
|
|
|
if not current_user.is_premium():
|
|
|
|
flash("Only premium user can add custom domains", "warning")
|
|
|
|
return redirect(url_for("dashboard.index"))
|
|
|
|
|
|
|
|
custom_domain = CustomDomain.get(custom_domain_id)
|
|
|
|
if not custom_domain or custom_domain.user_id != current_user.id:
|
|
|
|
flash("You cannot see this page", "warning")
|
|
|
|
return redirect(url_for("dashboard.index"))
|
|
|
|
|
|
|
|
mx_ok = spf_ok = dkim_ok = True
|
|
|
|
mx_errors = spf_errors = dkim_errors = []
|
|
|
|
|
|
|
|
if request.method == "POST":
|
|
|
|
if request.form.get("form-name") == "check-mx":
|
|
|
|
mx_domains = get_mx_domains(custom_domain.domain)
|
|
|
|
|
2019-12-30 19:34:38 +01:00
|
|
|
if sorted(mx_domains) != sorted(EMAIL_SERVERS_WITH_PRIORITY):
|
2019-12-27 23:50:09 +01:00
|
|
|
mx_ok = False
|
2019-12-30 19:34:38 +01:00
|
|
|
# build mx_errors to show to user
|
|
|
|
mx_errors = [
|
|
|
|
f"{priority} {domain}" for (priority, domain) in mx_domains
|
|
|
|
]
|
2019-12-27 23:50:09 +01:00
|
|
|
else:
|
|
|
|
flash(
|
|
|
|
"Your domain is verified. Now it can be used to create custom alias",
|
|
|
|
"success",
|
|
|
|
)
|
|
|
|
custom_domain.verified = True
|
|
|
|
db.session.commit()
|
|
|
|
return redirect(
|
|
|
|
url_for(
|
2019-12-30 21:11:31 +01:00
|
|
|
"dashboard.domain_detail_dns", custom_domain_id=custom_domain.id
|
2019-12-27 23:50:09 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
elif request.form.get("form-name") == "check-spf":
|
|
|
|
spf_domains = get_spf_domain(custom_domain.domain)
|
2019-12-30 19:34:38 +01:00
|
|
|
if EMAIL_DOMAIN in spf_domains:
|
2019-12-27 23:50:09 +01:00
|
|
|
custom_domain.spf_verified = True
|
|
|
|
db.session.commit()
|
|
|
|
flash("The SPF is setup correctly", "success")
|
|
|
|
return redirect(
|
|
|
|
url_for(
|
2019-12-30 21:11:31 +01:00
|
|
|
"dashboard.domain_detail_dns", custom_domain_id=custom_domain.id
|
2019-12-27 23:50:09 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
else:
|
2019-12-30 19:34:38 +01:00
|
|
|
flash(f"{EMAIL_DOMAIN} is not included in your SPF record.", "warning")
|
|
|
|
spf_ok = False
|
2019-12-27 23:50:09 +01:00
|
|
|
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:
|
|
|
|
flash("The DKIM is setup correctly.", "success")
|
|
|
|
custom_domain.dkim_verified = True
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
return redirect(
|
|
|
|
url_for(
|
2019-12-30 21:11:31 +01:00
|
|
|
"dashboard.domain_detail_dns", custom_domain_id=custom_domain.id
|
2019-12-27 23:50:09 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
dkim_ok = False
|
|
|
|
dkim_errors = get_txt_record(f"dkim._domainkey.{custom_domain.domain}")
|
|
|
|
|
2019-12-30 19:34:38 +01:00
|
|
|
spf_record = f"v=spf1 include:{EMAIL_DOMAIN} -all"
|
2019-12-27 23:50:09 +01:00
|
|
|
|
|
|
|
dkim_record = f"v=DKIM1; k=rsa; p={DKIM_DNS_VALUE}"
|
|
|
|
|
2019-12-30 18:58:44 +01:00
|
|
|
return render_template(
|
|
|
|
"dashboard/domain_detail/dns.html",
|
|
|
|
EMAIL_SERVERS_WITH_PRIORITY=EMAIL_SERVERS_WITH_PRIORITY,
|
|
|
|
**locals(),
|
|
|
|
)
|
2019-12-30 17:52:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
@dashboard_bp.route("/domains/<int:custom_domain_id>/info", methods=["GET", "POST"])
|
|
|
|
@login_required
|
|
|
|
def domain_detail(custom_domain_id):
|
|
|
|
# only premium user can see custom domain
|
|
|
|
if not current_user.is_premium():
|
|
|
|
flash("Only premium user can add custom domains", "warning")
|
|
|
|
return redirect(url_for("dashboard.index"))
|
|
|
|
|
|
|
|
custom_domain = CustomDomain.get(custom_domain_id)
|
|
|
|
if not custom_domain or custom_domain.user_id != current_user.id:
|
|
|
|
flash("You cannot see this page", "warning")
|
|
|
|
return redirect(url_for("dashboard.index"))
|
|
|
|
|
2019-12-30 18:20:49 +01:00
|
|
|
if request.method == "POST":
|
|
|
|
if request.form.get("form-name") == "switch-catch-all":
|
|
|
|
custom_domain.catch_all = not custom_domain.catch_all
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
if custom_domain.catch_all:
|
|
|
|
flash(
|
|
|
|
f"The catch-all has been enabled for {custom_domain.domain}",
|
|
|
|
"success",
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
flash(
|
|
|
|
f"The catch-all has been disabled for {custom_domain.domain}",
|
|
|
|
"warning",
|
|
|
|
)
|
|
|
|
return redirect(
|
|
|
|
url_for("dashboard.domain_detail", custom_domain_id=custom_domain.id)
|
|
|
|
)
|
2019-12-30 18:51:55 +01:00
|
|
|
elif request.form.get("form-name") == "delete":
|
|
|
|
name = custom_domain.domain
|
|
|
|
CustomDomain.delete(custom_domain_id)
|
|
|
|
db.session.commit()
|
|
|
|
flash(f"Domain {name} has been deleted", "success")
|
|
|
|
|
|
|
|
return redirect(url_for("dashboard.custom_domain"))
|
2019-12-30 18:20:49 +01:00
|
|
|
|
2019-12-30 17:52:08 +01:00
|
|
|
nb_alias = GenEmail.filter_by(custom_domain_id=custom_domain.id).count()
|
|
|
|
|
|
|
|
return render_template("dashboard/domain_detail/info.html", **locals())
|