From 9211bcdc5080bd33591d89bde8764df5aa1166f0 Mon Sep 17 00:00:00 2001 From: yyh Date: Sat, 22 Mar 2025 22:52:04 +0800 Subject: [PATCH] option apikey --- Dockerfile | 12 +++++++----- backend/config/settings.py | 9 +++++++-- backend/routes/v1/api.py | 8 ++++++-- sms_service.sh | 9 +++++---- 4 files changed, 25 insertions(+), 13 deletions(-) mode change 100644 => 100755 sms_service.sh diff --git a/Dockerfile b/Dockerfile index 1f9429b..8acda87 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,16 +2,18 @@ FROM python:3.9-slim WORKDIR /app +# 安装 git 用于拉取最新代码 +RUN apt-get update && apt-get install -y git && apt-get clean + +# 拉取最新代码(请确保仓库为public,否则需要处理身份验证) +RUN git clone https://github.com/nmhjklnm/sms_server.git . + # 安装依赖 -COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt # 创建数据目录 RUN mkdir -p /app/data -# 复制应用代码 -COPY . . - # 确保数据目录有正确的权限 RUN chmod -R 755 /app/data @@ -19,4 +21,4 @@ RUN chmod -R 755 /app/data EXPOSE 8322 # 运行应用 -CMD ["uvicorn", "backend.main:app", "--host", "0.0.0.0", "--port", "8322"] +CMD ["uvicorn", "backend.main:app", "--host", "0.0.0.0", "--port", "8322"] \ No newline at end of file diff --git a/backend/config/settings.py b/backend/config/settings.py index 8730233..2761e19 100644 --- a/backend/config/settings.py +++ b/backend/config/settings.py @@ -4,8 +4,8 @@ from pydantic_settings import BaseSettings class Settings(BaseSettings): # 基本配置 - database_url: str = os.getenv("DATABASE_URL", "sqlite:///sms_database.db") - api_key: str = os.getenv("API_KEY", "sk-nmhjklnm") + database_url: str = os.getenv("DATABASE_URL", "sqlite:///data/sms_database.db") + api_key: Optional[str] = os.getenv("API_KEY", None) host: str = os.getenv("HOST", "0.0.0.0") port: int = int(os.getenv("PORT", "8322")) log_level: str = os.getenv("LOG_LEVEL", "info").lower() @@ -17,6 +17,11 @@ class Settings(BaseSettings): app_title: str = "SMS 验证码接收服务" app_version: str = "1.0" + @property + def requires_api_key(self) -> bool: + """检查是否需要API密钥验证""" + return self.api_key is not None and self.api_key.strip() != "" + class Config: env_file = ".env" case_sensitive = False diff --git a/backend/routes/v1/api.py b/backend/routes/v1/api.py index 47b6791..f4879c8 100644 --- a/backend/routes/v1/api.py +++ b/backend/routes/v1/api.py @@ -18,8 +18,12 @@ router = APIRouter(prefix="/v1/sms", tags=["sms"]) # API KEY 校验 def check_api_key(x_api_key: Optional[str] = Header(None)): - if (x_api_key != settings.api_key): - raise HTTPException(status_code=401, detail="Unauthorized") + # 如果设置了API_KEY,则进行验证 + if settings.requires_api_key: + if (x_api_key != settings.api_key): + raise HTTPException(status_code=401, detail="Unauthorized") + # 如果没有设置API_KEY,则跳过验证 + return True # 接收短信接口 @router.post("/receive") diff --git a/sms_service.sh b/sms_service.sh old mode 100644 new mode 100755 index a1050c5..1128513 --- a/sms_service.sh +++ b/sms_service.sh @@ -1,9 +1,9 @@ #!/bin/bash # 确保脚本以root权限运行 -if [ "$(id -u)" -ne 0; then - echo "请使用sudo运行此脚本" - exit 1 +if [ "$(id -u)" -ne 0 ]; then + echo "请使用sudo运行此脚本" + exit 1 fi # 创建systemd服务文件 @@ -16,6 +16,7 @@ After=network.target User=root WorkingDirectory=/root/project/sms_server EnvironmentFile=-/root/project/sms_server/.env +Environment="PYTHONPATH=." ExecStart=/bin/bash -c "cd /root/project/sms_server && /usr/bin/poetry run python backend/main.py" Restart=always RestartSec=5 @@ -36,4 +37,4 @@ systemctl start sms-webhook.service echo "SMS Webhook服务已安装并启动" echo "查看状态: systemctl status sms-webhook.service" echo "查看日志: journalctl -u sms-webhook.service" -echo "访问服务: http://your-server-ip:$(grep -oP 'PORT=\K[0-9]+' /root/project/sms_server/.env 2>/dev/null || echo 8322)" \ No newline at end of file +echo "访问服务: http://47.97.31.90:$(grep -oP 'PORT=\K[0-9]+' /root/project/sms_server/.env 2>/dev/null || echo 8322)" \ No newline at end of file