问题修复

This commit is contained in:
2026-07-20 11:29:46 +08:00
parent 2ddae8b2d6
commit 22cdb730d7
5 changed files with 44 additions and 45 deletions
+1
View File
@@ -668,6 +668,7 @@ async def _render_script(domain: str, server_name: str, os: str, db: AsyncSessio
cert_dir=d.cert_dir,
check_cmd=d.check_cmd,
reload_cmd=d.reload_cmd,
acme_config_id=d.acme_config_id or "",
)
return script, server, d
+27 -31
View File
@@ -28,19 +28,30 @@ async def verify_token(
raise HTTPException(status_code=401, detail="Invalid authorization header")
token = authorization[7:]
result = await db.execute(select(Server).where(Server.token == token))
server = result.scalar_one_or_none()
server = result.scalars().first()
if not server:
raise HTTPException(status_code=401, detail="Invalid token")
return server
@router.get("/version")
async def get_version(domain: str, db: AsyncSession = Depends(get_db)):
"""返回当前证书版本号(无需认证,方便客户端轻量检查)"""
result = await db.execute(select(Domain).where(Domain.domain == domain))
d = result.scalar_one_or_none()
async def _find_domain(db: AsyncSession, domain: str, server_id: int | None = None, acme_config_id: int | None = None) -> Domain:
"""根据条件查找域名记录,acme_config_id 精确匹配"""
conditions = [Domain.domain == domain]
if server_id is not None:
conditions.append(Domain.server_id == server_id)
if acme_config_id is not None:
conditions.append(Domain.acme_config_id == acme_config_id)
result = await db.execute(select(Domain).where(*conditions))
d = result.scalars().first()
if not d:
raise HTTPException(status_code=404, detail="Domain not found")
return d
@router.get("/version")
async def get_version(domain: str, acme_config_id: int | None = None, db: AsyncSession = Depends(get_db)):
"""返回当前证书版本号(无需认证,方便客户端轻量检查)"""
d = await _find_domain(db, domain, acme_config_id=acme_config_id)
return PlainTextResponse(d.version)
@@ -52,16 +63,12 @@ def _cert_store_dir(domain: str) -> str:
@router.get("/cert/fullchain")
async def get_fullchain(
domain: str,
acme_config_id: int | None = None,
server: Server = Depends(verify_token),
db: AsyncSession = Depends(get_db),
):
"""下载 fullchain.pem"""
result = await db.execute(
select(Domain).where(Domain.domain == domain, Domain.server_id == server.id)
)
d = result.scalar_one_or_none()
if not d:
raise HTTPException(status_code=404, detail="Domain not found for this server")
await _find_domain(db, domain, server_id=server.id, acme_config_id=acme_config_id)
cert_path = Path(settings.cert_dir) / _cert_store_dir(domain) / "fullchain.pem"
if not cert_path.exists():
raise HTTPException(status_code=404, detail="Certificate file not found")
@@ -71,16 +78,12 @@ async def get_fullchain(
@router.get("/cert/private")
async def get_private_key(
domain: str,
acme_config_id: int | None = None,
server: Server = Depends(verify_token),
db: AsyncSession = Depends(get_db),
):
"""下载 private.key"""
result = await db.execute(
select(Domain).where(Domain.domain == domain, Domain.server_id == server.id)
)
d = result.scalar_one_or_none()
if not d:
raise HTTPException(status_code=404, detail="Domain not found for this server")
await _find_domain(db, domain, server_id=server.id, acme_config_id=acme_config_id)
key_path = Path(settings.cert_dir) / _cert_store_dir(domain) / "private.key"
if not key_path.exists():
raise HTTPException(status_code=404, detail="Private key file not found")
@@ -91,22 +94,18 @@ async def get_private_key(
async def generate_script(
domain: str,
server_name: str,
acme_config_id: int | None = None,
db: AsyncSession = Depends(get_db),
):
"""根据 server_name 生成对应的部署脚本"""
# 查找服务器
result = await db.execute(select(Server).where(Server.name == server_name))
server = result.scalar_one_or_none()
server = result.scalars().first()
if not server:
raise HTTPException(status_code=404, detail="Server not found")
# 查找域名配置
result = await db.execute(
select(Domain).where(Domain.domain == domain, Domain.server_id == server.id)
)
d = result.scalar_one_or_none()
if not d:
raise HTTPException(status_code=404, detail="Domain not found for this server")
d = await _find_domain(db, domain, server_id=server.id, acme_config_id=acme_config_id)
# 选择模板
if server.platform == "windows":
@@ -122,6 +121,7 @@ async def generate_script(
cert_dir=d.cert_dir,
check_cmd=d.check_cmd,
reload_cmd=d.reload_cmd,
acme_config_id=d.acme_config_id or "",
)
media_type = "text/plain" if server.platform == "windows" else "application/x-sh"
@@ -134,16 +134,12 @@ async def report_deploy(
status: str,
message: str = "",
hostname: str = "",
acme_config_id: int | None = None,
server: Server = Depends(verify_token),
db: AsyncSession = Depends(get_db),
):
"""客户端上报部署结果"""
result = await db.execute(
select(Domain).where(Domain.domain == domain, Domain.server_id == server.id)
)
d = result.scalar_one_or_none()
if not d:
raise HTTPException(status_code=404, detail="Domain not found for this server")
d = await _find_domain(db, domain, server_id=server.id, acme_config_id=acme_config_id)
log = DeployLog(domain_id=d.id, server_id=server.id, hostname=hostname, status=status, message=message)
db.add(log)