mirror of
https://github.com/simple-login/app.git
synced 2024-11-13 07:31:12 +01:00
118 lines
4.4 KiB
Python
118 lines
4.4 KiB
Python
from flask import render_template, request, redirect, url_for, flash
|
|
from flask_login import login_required, current_user
|
|
|
|
from app.config import EMAIL_SERVERS_WITH_PRIORITY, EMAIL_SERVERS, DKIM_DNS_VALUE
|
|
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
|
|
from app.models import CustomDomain
|
|
|
|
|
|
@dashboard_bp.route("/domains/<int:custom_domain_id>", 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"))
|
|
|
|
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)
|
|
|
|
if mx_domains != EMAIL_SERVERS:
|
|
mx_ok = False
|
|
mx_errors = get_mx_domains(custom_domain.domain, keep_priority=True)
|
|
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(
|
|
"dashboard.domain_detail", custom_domain_id=custom_domain.id
|
|
)
|
|
)
|
|
elif request.form.get("form-name") == "check-spf":
|
|
spf_domains = get_spf_domain(custom_domain.domain)
|
|
for email_server in EMAIL_SERVERS:
|
|
email_server = email_server[:-1] # remove the trailing .
|
|
if email_server not in spf_domains:
|
|
flash(
|
|
f"{email_server} is not included in your SPF record.", "warning"
|
|
)
|
|
spf_ok = False
|
|
|
|
if spf_ok:
|
|
custom_domain.spf_verified = True
|
|
db.session.commit()
|
|
flash("The SPF is setup correctly", "success")
|
|
return redirect(
|
|
url_for(
|
|
"dashboard.domain_detail", custom_domain_id=custom_domain.id
|
|
)
|
|
)
|
|
else:
|
|
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(
|
|
"dashboard.domain_detail", custom_domain_id=custom_domain.id
|
|
)
|
|
)
|
|
else:
|
|
dkim_ok = False
|
|
dkim_errors = get_txt_record(f"dkim._domainkey.{custom_domain.domain}")
|
|
|
|
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 successfully", "success")
|
|
|
|
return redirect(url_for("dashboard.custom_domain"))
|
|
|
|
spf_include_records = []
|
|
for priority, email_server in EMAIL_SERVERS_WITH_PRIORITY:
|
|
spf_include_records.append(f"include:{email_server[:-1]}")
|
|
|
|
spf_record = f"v=spf1 {' '.join(spf_include_records)} -all"
|
|
|
|
dkim_record = f"v=DKIM1; k=rsa; p={DKIM_DNS_VALUE}"
|
|
|
|
return render_template(
|
|
"dashboard/domain_detail.html",
|
|
custom_domain=custom_domain,
|
|
EMAIL_SERVERS_WITH_PRIORITY=EMAIL_SERVERS_WITH_PRIORITY,
|
|
spf_record=spf_record,
|
|
dkim_record=dkim_record,
|
|
mx_errors=mx_errors,
|
|
mx_ok=mx_ok,
|
|
spf_errors=spf_errors,
|
|
spf_ok=spf_ok,
|
|
dkim_errors=dkim_errors,
|
|
dkim_ok=dkim_ok,
|
|
)
|