diff --git a/config.conf b/config.conf index 3ea6ec6..1e0b54d 100644 --- a/config.conf +++ b/config.conf @@ -14,4 +14,16 @@ pathToCaddyCert=/var/lib/caddy/.local/share/caddy/certificates/acme-v02.api.lets pathToSaveFolder=/var/lib/caddy-to-dsm/certs/ #Адрес NAS -urlNas=http://stadidisk.local \ No newline at end of file +urlNas=http://stadidisk.local + +#Загрузка сертификатов производится через ssh? +is_load_cert_from_ssh=1 + +#Имя хоста для подключения +ssh_host=cudy-router + +#Путь на удаленном хосте до сертификатов +path_to_remote_ssh_cert=/etc/caddy/data/caddy/certificates/acme-v02.api.letsencrypt.org-directory/ + +#Путь куда копировать сертификаты с хоста +path_to_copy_ssh_cert=/opt/caddy-to-dsm/certs-from-ssh/ \ No newline at end of file diff --git a/main.py b/main.py index 2b8aa9c..6a8a881 100644 --- a/main.py +++ b/main.py @@ -5,8 +5,89 @@ from dsm_worker import DSMWorker import logging from pathlib import Path import shutil +import subprocess +import shlex +import re +import tempfile + from structs import CertInfo, DSMAuthInfo +def sync_caddy_certs_ssh(settings): + """ + Загружает сертификаты Caddy с OpenWrt через SSH. + + Синхронизируются только домены из settings.list_domains. + Сертификаты сохраняются локально для существующей + логики проверки и импорта в DSM. + """ + + # Папка с сертификатами на Cudy router + remote_root = settings.path_to_remote_ssh_cert + local_root = settings.path_to_caddy_cert + ssh_host = settings.ssh_host + local_root.mkdir(parents=True, exist_ok=True) + local_root.chmod(0o700) + + for domain in settings.list_domains: + + if not re.fullmatch(r"[a-zA-Z0-9][a-zA-Z0-9.-]*", domain): + raise ValueError(f"Invalid domain: {domain}") + + logging.info(f"Downloading certificate: {domain}") + + # Временный каталог, чтобы не записать неполную пару + with tempfile.TemporaryDirectory( + prefix=".caddy-sync-", + dir=local_root + ) as temp_dir: + + temp_path = Path(temp_dir) + + for extension in ("crt", "key"): + + filename = f"{domain}.{extension}" + remote_file = f"{remote_root}/{domain}/{filename}" + + command = [ + "ssh", + "-T", + "-o", "BatchMode=yes", + "-o", "ConnectTimeout=5", + ssh_host, + "cat " + shlex.quote(remote_file) + ] + + result = subprocess.run( + command, + capture_output=True, + timeout=30, + check=True + ) + + if not result.stdout: + raise RuntimeError(f"Empty certificate file: {remote_file}") + + local_file = temp_path / filename + local_file.write_bytes(result.stdout) + local_file.chmod(0o600) + + # Только после получения обоих файлов + # заменяем локальные копии + domain_dir = local_root / domain + domain_dir.mkdir(parents=True, exist_ok=True) + domain_dir.chmod(0o700) + + for extension in ("crt", "key"): + + filename = f"{domain}.{extension}" + + (temp_path / filename).replace( + domain_dir / filename + ) + + logging.info(f"Certificate downloaded: {domain}") + + def main(): """ Запускает основной процесс проверки и обновления сертификатов. @@ -24,6 +105,14 @@ def main(): service_worker = ServiceFunctions() settings = service_worker.load_config() + + # Получаем актуальные сертификаты Caddy с Cudy + if settings.is_load_cert_from_ssh: + sync_caddy_certs_ssh(settings) + #если мы копируем по ssh то указываем папку с сертификатами куда скопировали их с удаленного домена + settings.path_to_caddy_cert = settings.path_to_copy_ssh_cert + + #Проверяем все ли папки нужны if service_worker.remove_unnecessary_dsm_certs(settings.path_to_save_folder, settings.list_domains): logging.info(f"DSM certificate folders checked successfully") @@ -36,8 +125,7 @@ def main(): #Ищем в папке Caddy те сертификаты которые необходимо обновить, они приведены в настройках list_candidate_update_certs = service_worker.search_caddy_cert(settings.path_to_caddy_cert, settings.list_domains) - list_update_certs = service_worker.check_update_certs(settings.path_to_save_folder, - settings.path_to_caddy_cert, + list_update_certs = service_worker.check_update_certs(settings.path_to_save_folder, settings.path_to_caddy_cert, list_candidate_update_certs) if not list_update_certs: @@ -102,113 +190,5 @@ def main(): - - - - - - - # #### Тест CaddyWorker ################## - # pem_cert = None - # - # print(f"search_cert_caddy") - # cert_path = worker.search_cert_caddy( - # "D:/Work/Programming/Python/CaddyToDSM/TestData/certs", - # "wud.stadidisk.ru" - # ) - # - # print(f"Path to cert: {cert_path}") - # print(f"\n") - # - # if cert_path: - # print(f"get_cert_info") - # cert_info = worker.get_cert_info(cert_path) - # service_worker.save_cert_info(cert_info, Path("D:/Work/Programming/Python/CaddyToDSM/TestData/pems")) - # - # - # 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}") - # print(f"Dir cert: {cert_info.dir_cert}") - # print(f"\n") - # - # print(f"convert_to_dsm") - # pem_cert = worker.convert_to_dsm(cert_info.dir_cert, - # "D:/Work/Programming/Python/CaddyToDSM/TestData/pems") - # - # if(pem_cert): - # print(f"Cert Name: {pem_cert.certificate_name}") - # print(f"Path to private key: {pem_cert.private_key_path}") - # print(f"Path to pem: {pem_cert.private_key_path}") - # print(f"Path to fullchain: {pem_cert.private_key_path}") - # print(f"\n") - # else: - # print("Convert Certificate Fail") - # print(f"\n") - # - # else: - # print("Certificate not found") - # print(f"\n") - # - # - # - # ######### Тест DSM worker ################# - # - # dsm_worker = DSMWorker("http://stadidisk.local") - # auth_info = dsm_worker.auth_in_DSM(Path("D:/Work/Programming/Python/CaddyToDSM/TestData/.secret")) - # print("Starting dsm_worker\n") - # - # if dsm_worker.logout_dsm(): - # print("Logout Success\n") - # else: - # print("Logout Fail\n") - # - # if auth_info: - # print(f"Session id: {auth_info.sid}") - # print(f"Token: {auth_info.token}") - # print("\n") - # - # print("Starting get_list_certs\n") - # list_certs = dsm_worker.get_list_certs() - # - # print("Starting get_list_services\n") - # list_services = dsm_worker.get_list_services(list_certs) - # - # if list_services: - # print(f"Service List: {list_services}\n") - # else: - # print("Service list empty\n") - # - # if list_certs: - # print(f"Cert list: {list_certs}\n") - # - # print("Starting dsm_worker\n") - # dsm_cert_info = dsm_worker.import_cert(pem_cert) - # if dsm_cert_info: - # print(f"Cert Import Success\n") - # - # isSetCert = dsm_worker.set_cert_on_service(list_services, dsm_cert_info) - # - # if isSetCert: - # print(f"Cert Set Success\n") - # else: - # print("Cert Set Fail\n") - # - # else: - # print(f"Cert Import Fail\n") - # - # else: - # print("Cert list is empty\n") - # - # else: - # print(f"Error getting auth data") - # print("\n") - - - - - - if __name__ == "__main__": main() \ No newline at end of file diff --git a/service_functions.py b/service_functions.py index 0367e9d..a80b90d 100644 --- a/service_functions.py +++ b/service_functions.py @@ -127,6 +127,10 @@ class ServiceFunctions: "pathToCaddyCert", "pathToSaveFolder", "urlNas", + "path_to_remote_ssh_cert", + "is_load_cert_from_ssh", + "ssh_host", + "path_to_copy_ssh_cert" ] if not config.has_section("Settings"): @@ -147,6 +151,10 @@ class ServiceFunctions: credentials = Path(config.get("Settings", "pathToCredentials")), path_to_save_folder = Path(config.get("Settings", "pathToSaveFolder")), nas_url = config.get("Settings", "urlNas"), + path_to_remote_ssh_cert=Path(config.get("Settings", "path_to_remote_ssh_cert")), + is_load_cert_from_ssh=bool(config.get("Settings", "is_load_cert_from_ssh")), + ssh_host=config.get("Settings", "ssh_host"), + path_to_copy_ssh_cert=Path(config.get("Settings", "path_to_copy_ssh_cert")) ) return appl_settings diff --git a/structs.py b/structs.py index 4bd9948..f175df2 100644 --- a/structs.py +++ b/structs.py @@ -36,4 +36,10 @@ class ApplSettings: credentials : Path path_to_caddy_cert: Path path_to_save_folder: Path - nas_url: str \ No newline at end of file + nas_url: str + path_to_remote_ssh_cert: Path + path_to_copy_ssh_cert : Path + ssh_host: str + is_load_cert_from_ssh : bool = False + +