diff --git a/backend/web.py b/backend/web.py index a63ab305fb7a8a27263072932049e13e00f3a308..9ab8cf12310336ac8653f07635aeb7d8a83b0f58 100644 --- a/backend/web.py +++ b/backend/web.py @@ -1,52 +1,20 @@ #!/usr/bin/env python3 import ssl -from rich.console import Console from cryptography import x509 import socket import tls_utils -from tls_utils import TLSDetails - -class SSLHandler: - def __init__(self, host: str, port: int, context: ssl.SSLContext): - self.host = host - self.port = port - self.context = context +from generic_handler import GenericHandler +class SSLHandler(GenericHandler): def connect(self, verification: bool) -> int: if verification: with self.context.wrap_socket(socket.socket(), server_hostname=self.host) as s: s.connect((self.host, self.port)) cert = s.getpeercert() - return tls_utils.get_validity_days(cert)[1] + return tls_utils.check_cert_validity(cert)[1] else: pem_cert = ssl.get_server_certificate((self.host, self.port), timeout=5) cert = x509.load_pem_x509_certificate(pem_cert.encode()) not_after = cert.not_valid_after_utc.timestamp() - return tls_utils.get_expiry_timestamps(not_after)[1] - -class SSLVerificator: - def __init__(self, context: ssl.SSLContext): - self.context = context - - def connect(self, domain: str, port: int) -> TLSDetails: - handler = SSLHandler(domain, port, self.context) - try: - expiry = handler.connect(True) - return TLSDetails(domain_name=domain, expires_in_days=expiry) - except ssl.SSLCertVerificationError as e: - if e.verify_code == tls_utils.EXPIRED: - expiry = handler.connect(False) - return TLSDetails(domain_name=domain, expires_in_days=expiry) - elif e.verify_code == tls_utils.REVOKED: - return TLSDetails(domain_name=domain, error_message="was revoked.") - elif e.verify_code == tls_utils.SELF_SIGNED: - return TLSDetails(domain_name=domain, error_message="is self-signed.") - elif e.verify_code == tls_utils.ROOT_NOT_TRUSTED: - return TLSDetails(domain_name=domain, error_message="invalid: root not trusted.") - else: - return TLSDetails(domain_name=domain, error_message="failed verification: " + e.verify_message + ".") - except ssl.SSLError as e: - return TLSDetails(domain_name=domain, error_message="could not establish a secure connection: " + e.reason + ".") - except Exception as e: - return TLSDetails(domain_name=domain, error_message="could not connect: " + str(e) + ".") \ No newline at end of file + return tls_utils.compare_expiry_timestamps(not_after)[1] \ No newline at end of file