From 1c23d6cec5020356216053fbffe7e38823f7f969 Mon Sep 17 00:00:00 2001 From: Fedorov Dmitriy Date: Wed, 26 Aug 2026 20:53:36 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BB=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D1=8E?= =?UTF-8?q?=20=D0=BF=D0=BE=D0=BB=D1=83=D1=87=D0=B5=D0=BD=D0=B8=D1=8F=20?= =?UTF-8?q?=D0=B8=D0=BD=D1=84=D0=BE=D1=80=D0=BC=D0=B0=D1=86=D0=B8=D0=B8=20?= =?UTF-8?q?=D0=BE=20=D1=81=D0=B5=D1=80=D1=82=D0=B8=D1=84=D0=B8=D0=BA=D0=B0?= =?UTF-8?q?=D1=82=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- caddy_worker.py | 56 +++++++++++++++++++++++++++++++++---------------- main.py | 13 ++++++++++-- structs.py | 3 ++- 3 files changed, 51 insertions(+), 21 deletions(-) diff --git a/caddy_worker.py b/caddy_worker.py index 45e62e9..314fd9a 100644 --- a/caddy_worker.py +++ b/caddy_worker.py @@ -21,7 +21,14 @@ class CaddyWorker: if crt_path.is_file(): logging.info(f"Found certificate file: {crt_path}") - return crt_path + + key_path = cert_folder / f"{site_name}.key" + if key_path.is_file(): + logging.info(f"Found key file: {key_path}") + return crt_path + else: + logging.info(f"No key file: {key_path}") + return None logging.info(f"No certificate file: {cert_folder}") return None @@ -31,20 +38,33 @@ class CaddyWorker: - # def GetCertInfo(self, pathToCrtFile) -> CertInfo: - # path = Path(pathToCrtFile) - # - # with open(path, "rb") as file: - # cert_data = file.read() - # - # certificate = x509.load_der_x509_certificate(cert_data) - # - # fingerprint = certificate.fingerprint( - # hashes.SHA256() - # ).hex(":").upper() - # - # return CertInfo( - # fingerprint=fingerprint, - # expires_at=certificate.not_valid_after_utc, - # subject=certificate.subject.rfc4514_string() - # ) \ No newline at end of file + def get_cert_info(self, path_to_crt_file) -> CertInfo | None: + path = Path(path_to_crt_file) + if not path.is_file(): + logging.error(f"Certificate file not found: {path}") + return None + + try: + with open(path, "rb") as file: + cert_data = file.read() + except OSError as e: + logging.error(f"Cannot open certificate file {path}: {e}") + return None + + try: + certificate = x509.load_pem_x509_certificate(cert_data) + except ValueError as e: + logging.error(f"Invalid certificate {path}: {e}") + return None + + fingerprint = certificate.fingerprint( + hashes.SHA256() + ).hex(":").upper() + + domain_name =certificate.subject.get_attributes_for_oid(x509.NameOID.COMMON_NAME)[0].value + return CertInfo( + domain_name=domain_name, + fingerprint=fingerprint, + expires_at=certificate.not_valid_after_utc, + path_to_key=path_to_crt_file.with_suffix(".key"), + ) diff --git a/main.py b/main.py index 8c452cf..014aa62 100644 --- a/main.py +++ b/main.py @@ -5,13 +5,22 @@ from caddy_worker import CaddyWorker def main(): worker = CaddyWorker() - result = worker.search_cert_caddy( + cert_path = worker.search_cert_caddy( "D:/Work/Programming/Python/CaddyToDSM/TestData/certs", "wud.stadidisk.ru" ) - print(f"Result: {result}") + print(f"Path to cert: {cert_path}") + if cert_path: + cert_info = worker.get_cert_info(cert_path) + + print(f"Domain: {cert_info.domain_name}") + print(f"Fingerprint: {cert_info.fingerprint}") + print(f"Expires at: {cert_info.expires_at}") + print(f"Key path: {cert_info.path_to_key}") + else: + print("Certificate not found") if __name__ == "__main__": main() \ No newline at end of file diff --git a/structs.py b/structs.py index a21ff78..fab1f5c 100644 --- a/structs.py +++ b/structs.py @@ -4,9 +4,10 @@ from datetime import datetime @dataclass class CertInfo: + domain_name: str fingerprint: str expires_at: datetime - subject: str + path_to_key: str @dataclass