Files

141 lines
4.9 KiB
Python

from apps.base.models import KeyValue
from apps.base.utils import ip_limit
from core.database import db_startup_lock
from core.logger import logger
from core.security import (
generate_jwt_secret,
is_config_initialized,
is_valid_jwt_secret,
prepare_security_config,
)
from core.settings import DEFAULT_CONFIG, settings
from core.utils import hash_password
MIN_ADMIN_PASSWORD_LENGTH = 8
SETUP_CONFIG_FIELDS = {
"allowed_file_types",
"code_generate_type",
"enable_chunk",
"error_count",
"error_minute",
"login_count",
"login_minute",
"expire_style",
"max_save_seconds",
"name",
"open_upload",
"upload_count",
"upload_minute",
"upload_size",
}
async def ensure_settings_row() -> None:
initial_security = prepare_security_config(DEFAULT_CONFIG)
_, created = await KeyValue.get_or_create(
key="settings", defaults={"value": initial_security.config}
)
if created:
logger.warning(
"系统尚未初始化,请在浏览器中打开站点并完成管理员密码设置"
)
async def ensure_security_settings() -> None:
config_record = await KeyValue.filter(key="settings").first()
if not config_record:
return
current_config = {**DEFAULT_CONFIG, **(config_record.value or {})}
security_config = prepare_security_config(current_config)
if not security_config.changed:
return
config_record.value = security_config.config
await config_record.save()
if security_config.setup_required:
logger.warning("检测到空密码或旧版默认管理员密码,请通过初始化页面重新设置")
elif security_config.password_hashed:
logger.info("已将管理员密码迁移为哈希存储")
if security_config.jwt_secret_rotated:
logger.info("已生成独立 JWT 签名密钥")
await refresh_settings(force=True)
def _sync_ip_limits() -> None:
ip_limit["error"].minutes = settings.error_minute
ip_limit["error"].count = settings.error_count
ip_limit["metadata"].minutes = settings.error_minute
ip_limit["metadata"].count = settings.error_count
ip_limit["upload"].minutes = settings.upload_minute
ip_limit["upload"].count = settings.upload_count
ip_limit["login"].minutes = settings.login_minute
ip_limit["login"].count = settings.login_count
# Per-process config cache: the middleware refreshes settings on every request,
# which otherwise means one DB read per request. Writes call refresh_settings(force=True)
# so same-process visibility is immediate; other processes converge within the TTL
# (documented deployment is a single worker).
CONFIG_CACHE_TTL_SECONDS = 2.0
_config_cached_until = 0.0
async def refresh_settings(force: bool = False) -> None:
"""从数据库读取最新配置并应用到运行时。
默认带短 TTL 缓存(避免每请求查库);写配置的代码路径必须传 force=True。
"""
global _config_cached_until
import time as _time
if not force and _time.monotonic() < _config_cached_until:
return
config_record = await KeyValue.filter(key="settings").first()
settings.user_config = config_record.value if config_record and config_record.value else {}
unknown = settings.unknown_keys(settings.user_config)
if unknown:
logger.warning("配置中存在未登记的键(将被忽略其类型语义,仅透传): %s", unknown)
_sync_ip_limits()
_config_cached_until = _time.monotonic() + CONFIG_CACHE_TTL_SECONDS
def is_runtime_initialized() -> bool:
return is_config_initialized(dict(settings.items()))
async def initialize_system(
admin_password: str,
site_name: str | None = None,
setup_options: dict | None = None,
) -> None:
password = str(admin_password or "").strip()
if len(password) < MIN_ADMIN_PASSWORD_LENGTH:
raise ValueError(f"管理员密码至少需要 {MIN_ADMIN_PASSWORD_LENGTH} 位")
async with db_startup_lock():
config_record = await KeyValue.filter(key="settings").first()
current_config = {
**DEFAULT_CONFIG,
**(config_record.value if config_record and config_record.value else {}),
}
if is_config_initialized(current_config):
raise ValueError("系统已经初始化,请直接登录后台")
next_config = dict(current_config)
for key, value in (setup_options or {}).items():
if key in SETUP_CONFIG_FIELDS:
next_config[key] = value
normalized_site_name = str(site_name or "").strip()
if normalized_site_name:
next_config["name"] = normalized_site_name[:80]
next_config["admin_token"] = hash_password(password)
if not is_valid_jwt_secret(next_config.get("jwt_secret")):
next_config["jwt_secret"] = generate_jwt_secret()
await KeyValue.update_or_create(key="settings", defaults={"value": next_config})
await refresh_settings(force=True)