Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
899e8092da | ||
|
|
9d9089ad50 | ||
|
|
f41d3e50f2 | ||
|
|
4840099b70 | ||
|
|
11e201bfe6 |
+16
@@ -4,6 +4,10 @@ wud.stadidisk.ru
|
|||||||
|
|
||||||
#Настройки необходимые для работы приложения
|
#Настройки необходимые для работы приложения
|
||||||
[Settings]
|
[Settings]
|
||||||
|
# Источник списка: list или caddyfile
|
||||||
|
domainsSource=caddyfile
|
||||||
|
proxyTargetHost=stadidisk.local
|
||||||
|
|
||||||
#Путь к файлу с логином/паролем для авторизации
|
#Путь к файлу с логином/паролем для авторизации
|
||||||
pathToCredentials=/etc/caddy-to-dsm/.secret
|
pathToCredentials=/etc/caddy-to-dsm/.secret
|
||||||
|
|
||||||
@@ -15,3 +19,15 @@ pathToSaveFolder=/var/lib/caddy-to-dsm/certs/
|
|||||||
|
|
||||||
#Адрес NAS
|
#Адрес NAS
|
||||||
urlNas=http://stadidisk.local
|
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/
|
||||||
@@ -5,8 +5,89 @@ from dsm_worker import DSMWorker
|
|||||||
import logging
|
import logging
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import shutil
|
import shutil
|
||||||
|
import subprocess
|
||||||
|
import shlex
|
||||||
|
import re
|
||||||
|
import tempfile
|
||||||
|
|
||||||
from structs import CertInfo, DSMAuthInfo
|
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():
|
def main():
|
||||||
"""
|
"""
|
||||||
Запускает основной процесс проверки и обновления сертификатов.
|
Запускает основной процесс проверки и обновления сертификатов.
|
||||||
@@ -24,6 +105,15 @@ def main():
|
|||||||
|
|
||||||
service_worker = ServiceFunctions()
|
service_worker = ServiceFunctions()
|
||||||
settings = service_worker.load_config()
|
settings = service_worker.load_config()
|
||||||
|
|
||||||
|
# Получаем актуальные сертификаты Caddy с Cudy
|
||||||
|
if settings.is_load_cert_from_ssh:
|
||||||
|
#если мы копируем по ssh то указываем папку с сертификатами куда скопировали их с удаленного домена
|
||||||
|
settings.path_to_caddy_cert = settings.path_to_copy_ssh_cert
|
||||||
|
sync_caddy_certs_ssh(settings)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#Проверяем все ли папки нужны
|
#Проверяем все ли папки нужны
|
||||||
if service_worker.remove_unnecessary_dsm_certs(settings.path_to_save_folder, settings.list_domains):
|
if service_worker.remove_unnecessary_dsm_certs(settings.path_to_save_folder, settings.list_domains):
|
||||||
logging.info(f"DSM certificate folders checked successfully")
|
logging.info(f"DSM certificate folders checked successfully")
|
||||||
@@ -36,8 +126,7 @@ def main():
|
|||||||
#Ищем в папке Caddy те сертификаты которые необходимо обновить, они приведены в настройках
|
#Ищем в папке Caddy те сертификаты которые необходимо обновить, они приведены в настройках
|
||||||
list_candidate_update_certs = service_worker.search_caddy_cert(settings.path_to_caddy_cert, settings.list_domains)
|
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,
|
list_update_certs = service_worker.check_update_certs(settings.path_to_save_folder, settings.path_to_caddy_cert,
|
||||||
settings.path_to_caddy_cert,
|
|
||||||
list_candidate_update_certs)
|
list_candidate_update_certs)
|
||||||
|
|
||||||
if not list_update_certs:
|
if not list_update_certs:
|
||||||
@@ -102,113 +191,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__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
+163
-1
@@ -6,6 +6,7 @@ from dataclasses import asdict
|
|||||||
import logging
|
import logging
|
||||||
from configparser import ConfigParser
|
from configparser import ConfigParser
|
||||||
from caddy_worker import CaddyWorker
|
from caddy_worker import CaddyWorker
|
||||||
|
import subprocess
|
||||||
|
|
||||||
class ServiceFunctions:
|
class ServiceFunctions:
|
||||||
|
|
||||||
@@ -96,6 +97,124 @@ class ServiceFunctions:
|
|||||||
password=password,
|
password=password,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def get_caddy_proxy_domains(self, proxy_target_host: str, ssh_host: str) -> list[str]:
|
||||||
|
"""
|
||||||
|
Получает домены из Caddyfile, у которых reverse_proxy
|
||||||
|
направлен на указанный upstream-хост.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from urllib.parse import urlsplit
|
||||||
|
|
||||||
|
command = [
|
||||||
|
"ssh",
|
||||||
|
"-T",
|
||||||
|
"-o", "BatchMode=yes",
|
||||||
|
"-o", "ConnectTimeout=5",
|
||||||
|
ssh_host,
|
||||||
|
"/usr/bin/caddy adapt "
|
||||||
|
"--config /etc/caddy/Caddyfile "
|
||||||
|
"--adapter caddyfile"
|
||||||
|
]
|
||||||
|
|
||||||
|
try:
|
||||||
|
result = subprocess.run(
|
||||||
|
command,
|
||||||
|
capture_output=True,
|
||||||
|
text=True,
|
||||||
|
timeout=30,
|
||||||
|
check=True
|
||||||
|
)
|
||||||
|
except (subprocess.CalledProcessError,
|
||||||
|
subprocess.TimeoutExpired) as exc:
|
||||||
|
raise RuntimeError(
|
||||||
|
"Failed to read Caddyfile from Cudy"
|
||||||
|
) from exc
|
||||||
|
|
||||||
|
config = json.loads(result.stdout)
|
||||||
|
target = proxy_target_host.strip().lower().rstrip(".")
|
||||||
|
|
||||||
|
if not target:
|
||||||
|
raise ValueError("proxyTargetHost is empty")
|
||||||
|
|
||||||
|
def is_target_upstream(dial: str) -> bool:
|
||||||
|
if not isinstance(dial, str):
|
||||||
|
return False
|
||||||
|
|
||||||
|
address = dial.strip()
|
||||||
|
|
||||||
|
if address.startswith("tcp/"):
|
||||||
|
address = address[4:]
|
||||||
|
|
||||||
|
try:
|
||||||
|
parsed = urlsplit(
|
||||||
|
address if "://" in address
|
||||||
|
else "//" + address
|
||||||
|
)
|
||||||
|
hostname = (parsed.hostname or "").lower().rstrip(".")
|
||||||
|
except ValueError:
|
||||||
|
return False
|
||||||
|
|
||||||
|
return hostname == target
|
||||||
|
|
||||||
|
def has_target_proxy(node) -> bool:
|
||||||
|
if isinstance(node, dict):
|
||||||
|
|
||||||
|
if node.get("handler") == "reverse_proxy":
|
||||||
|
for upstream in node.get("upstreams", []):
|
||||||
|
if is_target_upstream(
|
||||||
|
upstream.get("dial", "")
|
||||||
|
):
|
||||||
|
return True
|
||||||
|
|
||||||
|
return any(
|
||||||
|
has_target_proxy(value)
|
||||||
|
for value in node.values()
|
||||||
|
)
|
||||||
|
|
||||||
|
if isinstance(node, list):
|
||||||
|
return any(
|
||||||
|
has_target_proxy(item)
|
||||||
|
for item in node
|
||||||
|
)
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
servers = (
|
||||||
|
config.get("apps", {})
|
||||||
|
.get("http", {})
|
||||||
|
.get("servers", {})
|
||||||
|
)
|
||||||
|
|
||||||
|
domains = set()
|
||||||
|
|
||||||
|
for server in servers.values():
|
||||||
|
for route in server.get("routes", []):
|
||||||
|
|
||||||
|
if not has_target_proxy(route.get("handle", [])):
|
||||||
|
continue
|
||||||
|
|
||||||
|
for matcher in route.get("match", []):
|
||||||
|
for host in matcher.get("host", []):
|
||||||
|
host = host.lower().rstrip(".")
|
||||||
|
|
||||||
|
if "*" not in host:
|
||||||
|
domains.add(host)
|
||||||
|
|
||||||
|
if not domains:
|
||||||
|
raise RuntimeError(
|
||||||
|
f"No reverse_proxy domains found for {target}"
|
||||||
|
)
|
||||||
|
|
||||||
|
result_domains = sorted(domains)
|
||||||
|
|
||||||
|
logging.info(
|
||||||
|
"Domains proxied to %s: %s",
|
||||||
|
target,
|
||||||
|
", ".join(result_domains)
|
||||||
|
)
|
||||||
|
|
||||||
|
return result_domains
|
||||||
|
|
||||||
|
|
||||||
def load_config(self):
|
def load_config(self):
|
||||||
|
|
||||||
@@ -127,6 +246,10 @@ class ServiceFunctions:
|
|||||||
"pathToCaddyCert",
|
"pathToCaddyCert",
|
||||||
"pathToSaveFolder",
|
"pathToSaveFolder",
|
||||||
"urlNas",
|
"urlNas",
|
||||||
|
"path_to_remote_ssh_cert",
|
||||||
|
"is_load_cert_from_ssh",
|
||||||
|
"ssh_host",
|
||||||
|
"path_to_copy_ssh_cert"
|
||||||
]
|
]
|
||||||
|
|
||||||
if not config.has_section("Settings"):
|
if not config.has_section("Settings"):
|
||||||
@@ -139,7 +262,42 @@ class ServiceFunctions:
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
domains = list(config["ListDomains"].keys())
|
# Выбираем источник списка доменов
|
||||||
|
domains_source = config.get("Settings", "domainsSource", fallback="list").strip().lower()
|
||||||
|
ssh_host = config.get("Settings", "ssh_host").strip()
|
||||||
|
if domains_source == "caddyfile":
|
||||||
|
|
||||||
|
proxy_target_host = config.get(
|
||||||
|
"Settings",
|
||||||
|
"proxyTargetHost",
|
||||||
|
fallback="stadidisk.local"
|
||||||
|
)
|
||||||
|
|
||||||
|
domains = self.get_caddy_proxy_domains(
|
||||||
|
proxy_target_host,
|
||||||
|
ssh_host
|
||||||
|
)
|
||||||
|
|
||||||
|
elif domains_source == "list":
|
||||||
|
|
||||||
|
if not config.has_section("ListDomains"):
|
||||||
|
raise ValueError(
|
||||||
|
"Missing [ListDomains] section"
|
||||||
|
)
|
||||||
|
|
||||||
|
domains = list(
|
||||||
|
config["ListDomains"].keys()
|
||||||
|
)
|
||||||
|
|
||||||
|
else:
|
||||||
|
raise ValueError(
|
||||||
|
f"Unknown domainsSource: {domains_source}"
|
||||||
|
)
|
||||||
|
|
||||||
|
if not domains:
|
||||||
|
raise RuntimeError(
|
||||||
|
"Domain list is empty, synchronization aborted"
|
||||||
|
)
|
||||||
|
|
||||||
appl_settings = ApplSettings(
|
appl_settings = ApplSettings(
|
||||||
list_domains = domains,
|
list_domains = domains,
|
||||||
@@ -147,6 +305,10 @@ class ServiceFunctions:
|
|||||||
credentials = Path(config.get("Settings", "pathToCredentials")),
|
credentials = Path(config.get("Settings", "pathToCredentials")),
|
||||||
path_to_save_folder = Path(config.get("Settings", "pathToSaveFolder")),
|
path_to_save_folder = Path(config.get("Settings", "pathToSaveFolder")),
|
||||||
nas_url = config.get("Settings", "urlNas"),
|
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=config.getboolean("Settings", "is_load_cert_from_ssh"),
|
||||||
|
ssh_host=ssh_host,
|
||||||
|
path_to_copy_ssh_cert=Path(config.get("Settings", "path_to_copy_ssh_cert"))
|
||||||
)
|
)
|
||||||
return appl_settings
|
return appl_settings
|
||||||
|
|
||||||
|
|||||||
@@ -37,3 +37,9 @@ class ApplSettings:
|
|||||||
path_to_caddy_cert: Path
|
path_to_caddy_cert: Path
|
||||||
path_to_save_folder: Path
|
path_to_save_folder: Path
|
||||||
nas_url: str
|
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
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user