diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index bb45b66..0000000 Binary files a/.DS_Store and /dev/null differ diff --git a/app/main/utils.py b/app/main/utils.py index 81813ff..934e635 100644 --- a/app/main/utils.py +++ b/app/main/utils.py @@ -1,14 +1,18 @@ +import socket +import ssl import subprocess import os import traceback import requests -from flask import current_app, render_template +from flask import app, current_app, render_template from app.models import Posts, MeetingNotes, Monitor, News, EventPosts, Events, Users, Documents from app import db, mail from datetime import datetime from sqlalchemy.exc import SQLAlchemyError from app.constants import file_types from flask_mail import Message +import datetime + class VirusDetectedException(Exception): @@ -462,6 +466,31 @@ def send_website_down_email(id, msg.html = email mail.send(msg) +def get_certificate_expiry_date_time(url): + # Use requests to get the certificate + with requests.get(url, stream=True) as response: + cert = response.raw.connection.sock.getpeercert() + exp_date_text = cert['notAfter'] + # Parse the date with timezone information + expiry_date = datetime.datetime.strptime(exp_date_text, '%b %d %H:%M:%S %Y %Z') + # Assuming the certificate's time zone is UTC + expiry_date = expiry_date.replace(tzinfo=datetime.timezone.utc) + return expiry_date + +def is_certificate_expired(url): + try: + expiry_date = get_certificate_expiry_date_time(url) + # Get the current time in UTC + current_date = datetime.datetime.now(datetime.timezone.utc) + time_remaining = expiry_date - current_date + if time_remaining.days < 0: + return True, "Certificate has expired." + elif time_remaining.days < 7: + return False, f"Certificate is expiring soon: {time_remaining.days} days remaining." + else: + return False, f"Certificate is valid: {time_remaining.days} days remaining." + except Exception as e: + return True, f"Error checking certificate: {e}" def ping_website(monitor_info): """ diff --git a/app/main/views.py b/app/main/views.py index f1ed657..b020fa8 100644 --- a/app/main/views.py +++ b/app/main/views.py @@ -1,4 +1,6 @@ -from flask import render_template, redirect, url_for, session, request as flask_request, jsonify, current_app, flash, send_file, send_from_directory +import socket +import ssl +from flask import app, render_template, redirect, url_for, session, request as flask_request, jsonify, current_app, flash, send_file, send_from_directory from flask_login import login_required, current_user from app.models import Users, Posts, EventPosts, Documents, Monitor from . import main @@ -10,7 +12,7 @@ get_rooms_by_division, create_document, allowed_file, - VirusDetectedException, + VirusDetectedException, is_certificate_expired, scan_file, process_documents_search, process_posts_search, @@ -37,6 +39,7 @@ def index(): Queries for the next 4 events from today forward to display in the Calendar section :return: HTML template for home page """ + posts = Posts.query.filter_by(deleted=False).order_by(Posts.date_created.desc()).limit(20).all() events = EventPosts.query.filter(Posts.deleted == False, EventPosts.event_date >= datetime.utcnow()).order_by( EventPosts.event_date.asc()).limit(4).all() @@ -856,4 +859,4 @@ def monitor(): websites = Monitor.query.order_by(Monitor.id.asc()).all() return render_template('monitor.html', websites=websites, - site_refresh_rate=current_app.config['FRONTEND_REFRESH_RATE']) + site_refresh_rate=current_app.config['FRONTEND_REFRESH_RATE']) diff --git a/app/models.py b/app/models.py index f5a3218..e26c356 100644 --- a/app/models.py +++ b/app/models.py @@ -667,3 +667,4 @@ class Monitor(db.Model): last_success_timestamp = db.Column(db.DateTime) response_header = db.Column(db.String) use_ssl = db.Column(db.Boolean) + expiration_date = db.Column(db.Date) diff --git a/app/static/js/monitor.js b/app/static/js/monitor.js index d97e428..a7f33f3 100644 --- a/app/static/js/monitor.js +++ b/app/static/js/monitor.js @@ -20,6 +20,8 @@ function WebsiteMonitorFunction () { $('#website-' + websiteID + '-check').show(); $('#website-' + websiteID + '-warning').hide(); $('#website-' + websiteID + '-x').hide(); + + } else { tableRow.removeClass('warning'); tableRow.removeClass('success'); @@ -35,7 +37,14 @@ function WebsiteMonitorFunction () { $('#website-' + websiteID + '-time-2').html(data['most_recent_success']); $('#website-' + websiteID + '-status-code').html(data['status_code']); $('#modalBody-' + websiteID).html(data['reason']); + + if (data['is_expired']) { + $('#website-' + websiteID + '-expiration-status').html('Expired'); + } else { + $('#website-' + websiteID + '-expiration-status').html('Valid'); + } }, + error: function (data) { tableRow.removeClass('danger'); tableRow.removeClass('success'); diff --git a/app/templates/email/monitor_email_alert.html b/app/templates/email/monitor_email_alert.html index 2657128..b002626 100644 --- a/app/templates/email/monitor_email_alert.html +++ b/app/templates/email/monitor_email_alert.html @@ -34,5 +34,13 @@