Files
smsweb/config.py
OpenClaw Agent 4e5e93660d Initial commit: SMS Receiver Web Service
Features:
- Receive SMS from TranspondSms Android APP
- HMAC-SHA256 signature verification (optional)
- SQLite database storage
- Web UI with login authentication
- Multiple API tokens support
- Timezone conversion (Asia/Shanghai)
- Search, filter, and statistics
- Auto refresh and session management

Tech Stack:
- Flask 3.0
- SQLite database
- HTML5/CSS3 responsive design
2026-02-06 23:23:49 +00:00

124 lines
3.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
短信转发接收端配置文件
支持从 config.json 加载配置
"""
import os
import json
from typing import Dict, List, Any
class Config:
"""基础配置"""
# 服务器配置
HOST = '0.0.0.0'
PORT = 9518
DEBUG = False
# 安全配置
SECRET_KEY = 'default_secret_key_change_me'
SIGN_VERIFY = True
SIGN_MAX_AGE = 3600000 # 签名最大有效时间毫秒默认1小时
# 登录配置
LOGIN_ENABLED = True
LOGIN_USERNAME = 'admin'
LOGIN_PASSWORD = 'admin123'
SESSION_LIFETIME = 3600 # 会话有效期默认1小时
# 数据库配置
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
DATABASE_PATH = os.path.join(BASE_DIR, 'sms_receiver.db')
# 短信存储配置
MAX_MESSAGES = 10000
AUTO_CLEANUP = True
CLEANUP_DAYS = 30
# Web界面配置
PER_PAGE = 50
REFRESH_INTERVAL = 30
# 日志配置
LOG_LEVEL = 'INFO'
LOG_FILE = 'sms_receiver.log'
# 时区配置
TIMEZONE = 'Asia/Shanghai'
# API Token 配置
API_TOKENS: List[Dict[str, Any]] = []
@classmethod
def load_from_json(cls, json_path: str = 'config.json') -> 'Config':
"""从 JSON 文件加载配置"""
config_obj = cls()
if os.path.exists(json_path):
with open(json_path, 'r', encoding='utf-8') as f:
config_data = json.load(f)
# 服务器配置
if 'server' in config_data:
server = config_data['server']
config_obj.HOST = server.get('host', config_obj.HOST)
config_obj.PORT = server.get('port', config_obj.PORT)
config_obj.DEBUG = server.get('debug', config_obj.DEBUG)
# 安全配置
if 'security' in config_data:
security = config_data['security']
config_obj.LOGIN_ENABLED = security.get('enabled', config_obj.LOGIN_ENABLED)
config_obj.LOGIN_USERNAME = security.get('username', config_obj.LOGIN_USERNAME)
config_obj.LOGIN_PASSWORD = security.get('password', config_obj.LOGIN_PASSWORD)
config_obj.SESSION_LIFETIME = security.get('session_lifetime', config_obj.SESSION_LIFETIME)
config_obj.SECRET_KEY = security.get('secret_key', config_obj.SECRET_KEY)
config_obj.SIGN_VERIFY = security.get('sign_verify', config_obj.SIGN_VERIFY)
config_obj.SIGN_MAX_AGE = security.get('sign_max_age', config_obj.SIGN_MAX_AGE)
# 短信配置
if 'sms' in config_data:
sms = config_data['sms']
config_obj.MAX_MESSAGES = sms.get('max_messages', config_obj.MAX_MESSAGES)
config_obj.AUTO_CLEANUP = sms.get('auto_cleanup', config_obj.AUTO_CLEANUP)
config_obj.CLEANUP_DAYS = sms.get('cleanup_days', config_obj.CLEANUP_DAYS)
# 数据库配置
if 'database' in config_data:
database = config_data['database']
if 'path' in database:
# 如果是绝对路径,直接使用;如果是相对路径,相对于项目目录
db_path = database['path']
if not os.path.isabs(db_path):
db_path = os.path.join(config_obj.BASE_DIR, db_path)
config_obj.DATABASE_PATH = db_path
# 时区配置
if 'timezone' in config_data:
config_obj.TIMEZONE = config_data['timezone']
# API Token 配置
if 'api_tokens' in config_data:
config_obj.API_TOKENS = config_data['api_tokens']
return config_obj
class DevelopmentConfig(Config):
"""开发环境配置"""
DEBUG = True
LOG_LEVEL = 'DEBUG'
class ProductionConfig(Config):
"""生产环境配置"""
DEBUG = False
# 配置映射
config = {
'development': DevelopmentConfig,
'production': ProductionConfig,
'default': DevelopmentConfig
}