Refresh Button

main
Bryan Cerrati 2025-03-04 15:56:37 -05:00
parent 58496200ca
commit 0bb24856c4
5 changed files with 47 additions and 44 deletions

View File

@ -2,7 +2,7 @@ from flask import Blueprint, render_template, request, redirect, url_for
import whois import whois
from app.models import Domain from app.models import Domain
from app import db 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__) domain_bp = Blueprint('domain', __name__)
@ -12,7 +12,7 @@ def home():
@domain_bp.route('/domains') @domain_bp.route('/domains')
def 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() domains_list = Domain.query.filter_by(ssl_only=False).all()
return render_template('domains.html', domains=domains_list) return render_template('domains.html', domains=domains_list)
@ -22,23 +22,10 @@ def add_domain():
domain_list = request.form.getlist('domain') domain_list = request.form.getlist('domain')
for domain_name in domain_list: for domain_name in domain_list:
if domain_name.strip(): if domain_name.strip():
# Perform WHOIS lookup # WHOIS lookup
try: whois_exp_date_str = get_whois_expiration(domain_name)
w = whois.whois(domain_name) # SSL lookup
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
ssl_exp_date_str = get_ssl_expiration(domain_name) 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(): if not Domain.query.filter_by(domain=domain_name).first():
new_domain = Domain( new_domain = Domain(
domain=domain_name, domain=domain_name,
@ -50,11 +37,21 @@ def add_domain():
db.session.commit() db.session.commit()
return redirect(url_for('domain.domains')) return redirect(url_for('domain.domains'))
return render_template('add_domain.html') return render_template('add_domain.html')
@domain_bp.route('/delete/<int:domain_id>', methods=['GET']) @domain_bp.route('/delete/<int:domain_id>', methods=['GET'])
def delete_domain(domain_id): def delete_domain(domain_id):
domain_to_delete = Domain.query.get_or_404(domain_id) domain_to_delete = Domain.query.get_or_404(domain_id)
db.session.delete(domain_to_delete) db.session.delete(domain_to_delete)
db.session.commit() 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')) return redirect(url_for('domain.domains'))

View File

@ -1,7 +1,7 @@
from flask import Blueprint, render_template, request, redirect, url_for from flask import Blueprint, render_template, request, redirect, url_for
from app.models import Domain from app.models import Domain
from app import db 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__) ssl_bp = Blueprint('ssl', __name__)
@ -9,12 +9,6 @@ ssl_bp = Blueprint('ssl', __name__)
def ssl_page(): def ssl_page():
# Get all domains (both normal and ssl_only) # Get all domains (both normal and ssl_only)
domains = Domain.query.all() 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) return render_template('ssl.html', domains=domains)
@ssl_bp.route('/ssl/add', methods=['GET', 'POST']) @ssl_bp.route('/ssl/add', methods=['GET', 'POST'])
@ -23,21 +17,8 @@ def add_ssl_domain():
domain_list = request.form.getlist('domain') domain_list = request.form.getlist('domain')
for domain_name in domain_list: for domain_name in domain_list:
if domain_name.strip(): if domain_name.strip():
# Both lookups are performed when adding an SSL-only domain. whois_exp_date_str = get_whois_expiration(domain_name)
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}"
ssl_exp_date_str = get_ssl_expiration(domain_name) ssl_exp_date_str = get_ssl_expiration(domain_name)
if not Domain.query.filter_by(domain=domain_name).first(): if not Domain.query.filter_by(domain=domain_name).first():
new_domain = Domain( new_domain = Domain(
domain=domain_name, domain=domain_name,
@ -49,3 +30,13 @@ def add_ssl_domain():
db.session.commit() db.session.commit()
return redirect(url_for('ssl.ssl_page')) return redirect(url_for('ssl.ssl_page'))
return render_template('add_ssl_domain.html') 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'))

View File

@ -1,12 +1,13 @@
{% extends "base.html" %} {% extends "base.html" %}
{% block title %}Domains{% endblock %} {% block title %}Domains{% endblock %}
{% block content %} {% block content %}
<h1>Domains List</h1> <h1>Domain List</h1>
<button onclick="window.location.href='{{ url_for('domain.refresh_domains') }}'">Refresh Domains</button>
<table border="1" cellpadding="5" cellspacing="0"> <table border="1" cellpadding="5" cellspacing="0">
<thead> <thead>
<tr> <tr>
<th>Domain</th> <th>Domain</th>
<th>Expiration Date</th> <th>Domain Expiration</th>
<th>Action</th> <th>Action</th>
</tr> </tr>
</thead> </thead>

View File

@ -2,6 +2,7 @@
{% block title %}SSL Certificates{% endblock %} {% block title %}SSL Certificates{% endblock %}
{% block content %} {% block content %}
<h1>SSL Certificates</h1> <h1>SSL Certificates</h1>
<button onclick="window.location.href='{{ url_for('ssl.refresh_ssl') }}'">Refresh SSL</button>
<table border="1" cellpadding="5" cellspacing="0"> <table border="1" cellpadding="5" cellspacing="0">
<thead> <thead>
<tr> <tr>

View File

@ -1,4 +1,5 @@
import socket, ssl, datetime import socket, ssl, datetime
import whois
def get_ssl_expiration(domain, port=443): def get_ssl_expiration(domain, port=443):
try: try:
@ -8,9 +9,21 @@ def get_ssl_expiration(domain, port=443):
cert = ssock.getpeercert() cert = ssock.getpeercert()
notAfter = cert.get('notAfter') notAfter = cert.get('notAfter')
if 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') exp_date = datetime.datetime.strptime(notAfter, '%b %d %H:%M:%S %Y %Z')
return exp_date.strftime("%m/%d/%Y") return exp_date.strftime("%m/%d/%Y")
return "NO SSL" return "NO SSL"
except Exception: except Exception:
return "NO SSL" 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"