From 0bb24856c425484befe64430088b17fbd5f9ba4c Mon Sep 17 00:00:00 2001 From: Bryan Cerrati Date: Tue, 4 Mar 2025 15:56:37 -0500 Subject: [PATCH] Refresh Button --- app/routes/domain.py | 37 +++++++++++++++++-------------------- app/routes/ssl.py | 33 ++++++++++++--------------------- app/templates/domains.html | 5 +++-- app/templates/ssl.html | 1 + app/utils.py | 15 ++++++++++++++- 5 files changed, 47 insertions(+), 44 deletions(-) diff --git a/app/routes/domain.py b/app/routes/domain.py index d960412..7d40fe3 100644 --- a/app/routes/domain.py +++ b/app/routes/domain.py @@ -2,7 +2,7 @@ from flask import Blueprint, render_template, request, redirect, url_for import whois from app.models import Domain from app import db -from app.utils import get_ssl_expiration +from app.utils import get_ssl_expiration, get_whois_expiration domain_bp = Blueprint('domain', __name__) @@ -12,7 +12,7 @@ def home(): @domain_bp.route('/domains') def domains(): - # Show only non-ssl_only domains on the normal Domains page. + # Only show domains that were added normally (ssl_only is False) domains_list = Domain.query.filter_by(ssl_only=False).all() return render_template('domains.html', domains=domains_list) @@ -22,23 +22,10 @@ def add_domain(): domain_list = request.form.getlist('domain') for domain_name in domain_list: if domain_name.strip(): - # Perform WHOIS lookup - try: - w = whois.whois(domain_name) - exp_date = w.expiration_date - if isinstance(exp_date, list): - exp_date = exp_date[0] - if exp_date and hasattr(exp_date, "strftime"): - whois_exp_date_str = exp_date.strftime("%m/%d/%Y") - else: - whois_exp_date_str = str(exp_date) - except Exception as e: - whois_exp_date_str = f"Error: {e}" - - # Perform SSL lookup using our utility function + # WHOIS lookup + whois_exp_date_str = get_whois_expiration(domain_name) + # SSL lookup ssl_exp_date_str = get_ssl_expiration(domain_name) - - # Only add if not already present. if not Domain.query.filter_by(domain=domain_name).first(): new_domain = Domain( domain=domain_name, @@ -50,11 +37,21 @@ def add_domain(): db.session.commit() return redirect(url_for('domain.domains')) return render_template('add_domain.html') - + @domain_bp.route('/delete/', methods=['GET']) def delete_domain(domain_id): domain_to_delete = Domain.query.get_or_404(domain_id) db.session.delete(domain_to_delete) db.session.commit() - # Redirect back to the Domains page (or SSL page if needed) + return redirect(url_for('domain.domains')) + +@domain_bp.route('/domains/refresh') +def refresh_domains(): + # Refresh only non-SSL-only domains + domains_list = Domain.query.filter_by(ssl_only=False).all() + for d in domains_list: + d.expiration_date = get_whois_expiration(d.domain) + # Optionally update SSL expiration as well if desired: + d.ssl_expiration_date = get_ssl_expiration(d.domain) + db.session.commit() return redirect(url_for('domain.domains')) diff --git a/app/routes/ssl.py b/app/routes/ssl.py index 915aa8c..75fc99d 100644 --- a/app/routes/ssl.py +++ b/app/routes/ssl.py @@ -1,7 +1,7 @@ from flask import Blueprint, render_template, request, redirect, url_for from app.models import Domain from app import db -from app.utils import get_ssl_expiration +from app.utils import get_ssl_expiration, get_whois_expiration ssl_bp = Blueprint('ssl', __name__) @@ -9,12 +9,6 @@ ssl_bp = Blueprint('ssl', __name__) def ssl_page(): # Get all domains (both normal and ssl_only) domains = Domain.query.all() - # Update each domain's SSL expiration (optional, to refresh if needed) - for d in domains: - current_ssl_exp = get_ssl_expiration(d.domain) - if d.ssl_expiration_date != current_ssl_exp: - d.ssl_expiration_date = current_ssl_exp - db.session.commit() return render_template('ssl.html', domains=domains) @ssl_bp.route('/ssl/add', methods=['GET', 'POST']) @@ -23,21 +17,8 @@ def add_ssl_domain(): domain_list = request.form.getlist('domain') for domain_name in domain_list: if domain_name.strip(): - # Both lookups are performed when adding an SSL-only domain. - try: - w = whois.whois(domain_name) - exp_date = w.expiration_date - if isinstance(exp_date, list): - exp_date = exp_date[0] - if exp_date and hasattr(exp_date, "strftime"): - whois_exp_date_str = exp_date.strftime("%m/%d/%Y") - else: - whois_exp_date_str = str(exp_date) - except Exception as e: - whois_exp_date_str = f"Error: {e}" - + whois_exp_date_str = get_whois_expiration(domain_name) ssl_exp_date_str = get_ssl_expiration(domain_name) - if not Domain.query.filter_by(domain=domain_name).first(): new_domain = Domain( domain=domain_name, @@ -49,3 +30,13 @@ def add_ssl_domain(): db.session.commit() return redirect(url_for('ssl.ssl_page')) return render_template('add_ssl_domain.html') + +@ssl_bp.route('/ssl/refresh') +def refresh_ssl(): + # Refresh all domains (both normal and ssl_only) + domains = Domain.query.all() + for d in domains: + d.expiration_date = get_whois_expiration(d.domain) + d.ssl_expiration_date = get_ssl_expiration(d.domain) + db.session.commit() + return redirect(url_for('ssl.ssl_page')) diff --git a/app/templates/domains.html b/app/templates/domains.html index ccabe4a..2c43bcb 100644 --- a/app/templates/domains.html +++ b/app/templates/domains.html @@ -1,12 +1,13 @@ {% extends "base.html" %} {% block title %}Domains{% endblock %} {% block content %} -

Domains List

+

Domain List

+ - + diff --git a/app/templates/ssl.html b/app/templates/ssl.html index f5fa7aa..38ac142 100644 --- a/app/templates/ssl.html +++ b/app/templates/ssl.html @@ -2,6 +2,7 @@ {% block title %}SSL Certificates{% endblock %} {% block content %}

SSL Certificates

+
DomainExpiration DateDomain Expiration Action
diff --git a/app/utils.py b/app/utils.py index 4df8a42..f5a2c6b 100644 --- a/app/utils.py +++ b/app/utils.py @@ -1,4 +1,5 @@ import socket, ssl, datetime +import whois def get_ssl_expiration(domain, port=443): try: @@ -8,9 +9,21 @@ def get_ssl_expiration(domain, port=443): cert = ssock.getpeercert() notAfter = cert.get('notAfter') if notAfter: - # Example certificate expiry format: "Jun 1 12:00:00 2025 GMT" + # Example format: "Jun 1 12:00:00 2025 GMT" exp_date = datetime.datetime.strptime(notAfter, '%b %d %H:%M:%S %Y %Z') return exp_date.strftime("%m/%d/%Y") return "NO SSL" except Exception: return "NO SSL" + +def get_whois_expiration(domain): + try: + w = whois.whois(domain) + exp_date = w.expiration_date + if isinstance(exp_date, list): + exp_date = exp_date[0] + if exp_date and hasattr(exp_date, "strftime"): + return exp_date.strftime("%m/%d/%Y") + return str(exp_date) + except Exception: + return "Error"