完善
This commit is contained in:
+26
-13
@@ -250,10 +250,10 @@ class AcmeService:
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
def issue_certificate(self, domain: str) -> tuple[bool, str]:
|
||||
def issue_certificate(self, domain: str) -> tuple[bool, str, dict, str]:
|
||||
"""
|
||||
申请证书(使用 certbot + DNS-01 验证)
|
||||
返回 (成功?, 日志信息)
|
||||
返回 (成功?, 摘要信息, 证书详情dict, certbot原始输出)
|
||||
|
||||
泛域名 *.example.com 会自动同时申请裸域名 example.com
|
||||
"""
|
||||
@@ -264,23 +264,36 @@ class AcmeService:
|
||||
|
||||
success, output = self._run_certbot(domain, "certonly")
|
||||
|
||||
cert_info = {}
|
||||
if success:
|
||||
cert_path = domain_dir / "fullchain.pem"
|
||||
if cert_path.exists():
|
||||
cert_data = cert_path.read_bytes()
|
||||
cert = x509.load_pem_x509_certificate(cert_data)
|
||||
not_after = cert.not_valid_after_utc
|
||||
san = []
|
||||
try:
|
||||
san_ext = cert.extensions.get_extension_for_class(x509.SubjectAlternativeName)
|
||||
san = san_ext.value.get_values_for_type(x509.DNSName)
|
||||
except x509.ExtensionNotFound:
|
||||
pass
|
||||
return True, f"Certificate issued, expires: {not_after.strftime('%Y-%m-%d %H:%M:%S')}, SAN: {san}"
|
||||
cert_data = cert_path.read_bytes()
|
||||
cert = x509.load_pem_x509_certificate(cert_data)
|
||||
not_before = getattr(cert, "not_valid_before_utc", None) or cert.not_valid_before
|
||||
not_after = getattr(cert, "not_valid_after_utc", None) or cert.not_valid_after
|
||||
san = []
|
||||
try:
|
||||
san_ext = cert.extensions.get_extension_for_class(x509.SubjectAlternativeName)
|
||||
san = san_ext.value.get_values_for_type(x509.DNSName)
|
||||
except x509.ExtensionNotFound:
|
||||
pass
|
||||
cert_info = {
|
||||
"not_before": not_before.isoformat(),
|
||||
"not_after": not_after.isoformat(),
|
||||
"serial_number": str(cert.serial_number),
|
||||
"san": [str(s) for s in san],
|
||||
"issuer": cert.issuer.rfc4514_string(),
|
||||
}
|
||||
msg = f"签发成功,过期时间: {not_after.strftime('%Y-%m-%d %H:%M:%S')}, SAN: {san}"
|
||||
return True, msg, cert_info, output
|
||||
except Exception as e:
|
||||
return True, f"签发成功但解析证书失败: {e}", cert_info, output
|
||||
|
||||
return success, output
|
||||
return success, output, cert_info, output
|
||||
|
||||
def renew_certificate(self, domain: str) -> tuple[bool, str]:
|
||||
def renew_certificate(self, domain: str) -> tuple[bool, str, dict, str]:
|
||||
"""续签证书"""
|
||||
return self.issue_certificate(domain)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user