mirror of
https://ghfast.top/https://github.com/aeroxw/tick-stock-panel.git
synced 2026-09-12 17:54:15 +08:00
* fix(concurrency): 共享缓存/任务表加锁, 全局限速, depth 原子写, 认证热路径缓存 修复多线程下的竞态与阻塞: - overview/strategy_cache/PanelCache/StrategyMonitor._watching 四处共享状态加锁, 消除 "dict/OrderedDict mutated" 与丢更新/半写读取 - strategy_cache/depth parquet 改临时文件 + os.replace 原子写 - rate_limits 改进程级共享时间轴限速, 并发同步不再聚合超过单能力 rpm; scheduler 令牌账目与 sleep 分离, sleep 不再独占锁串行化其他请求 - auth.is_configured() 内存缓存, 认证中间件不再每请求读盘阻塞事件循环 - api/backtest 任务清理/取消全程持 _jobs_lock, 并用 Semaphore(2) 限并发重回测 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * perf(data): limit_ladder 去 N+1 全市场重算, 指标裁剪, factor 向量化 - limit_ladder 前一日 consecutive 改窄读单日 parquet 存储列 (谓词/投影下推), 替代 range(1,10) 逐日 _load_enriched_for_date 全市场指标重算 (最坏 9x) - compute_indicators 新增可选 needed 裁剪 (默认 None 行为逐位不变, 已对照验证), factor 只算所需因子列 - factor._calc_period_return 用 Polars join 替代 Python 逐行 price_map 循环, _add_groups 去 map_elements 改纯表达式 (输出逐位一致) - screener ext value_map 按 parquet mtime 记忆化, 免每请求磁盘重读 (DuckDB 过滤仍用隔离 :memory: 连接, 不扩大注入面) Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * refactor(backend): 报表存储去重, 删死代码, DuckDB 视图重建收敛, 管道失败如实标记 - 三份近乎逐字复制的 *_reports.py 收敛到共享 JsonReportStore (原子写 + 锁), 各模块公有 API/id 格式/上限/落盘 schema 完全保持不变 - 删除 ext_pull.py 中字节相同的死 _run_loop (Python 只绑第二个) 及无用 import - 13 张 DuckDB 视图重建收敛为唯一权威 repository.rebuild_views(), daily_pipeline 与 /api/data/clear 改为调用 (修好 clear 路径漏挂视图的漂移) - daily_pipeline 累积 stage_errors 并在末尾抛出, 部分失败不再误报成功; free/None 模式的能力门控跳过不计入失败 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(frontend): SSE 连接态, 路由代码分割, 查询失效修复, 三态与无障碍 - 实时行情 SSE: 连接态 store + 指数退避 + 断线徽标/toast (避免静默丢告警); 回测 SSE 断线有界重连 + 可重试, 不再永久卡住进度条 - router 全部 React.lazy + Suspense, vite manualChunks 拆图表库 (echarts 变独立 1MB 懒加载 chunk, 首屏包显著减小) - 修 Data 清库后其它页显示旧数据 (改回广域失效); 修 Watchlist kline 失效键 永不匹配; query key 收敛到 QK 工厂 (新增 strategyDetail) - Monitor/Analysis/StockAnalysis/ExtPages/CustomSignals 补 loading 门控与 error/empty 三态区分 - 新增共享 Modal 原语 (焦点陷阱/ESC/焦点还原/aria), 改造 3 个高频弹窗; Toast/AlertToast 加 aria-live 与键盘可达; Watchlist/LimitUpLadder 卡片 memo 修复本轮 review 发现的缺陷: - Modal 焦点 effect 依赖 onClose 致每次输入抢焦点 → 改 ref 只装一次 - StrategySettingsDialog 删除确认框被 Modal 面板裁剪 → 移出作兄弟节点 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(quant): 修正 ST 板块限价套错 与 因子 Sharpe 年化频率 两个不报错但会算错数的领域 bug: 1. ST 5% 涨跌停限幅被无条件套到创业板/科创板 ST 股: 注册制改革后 创业板(300/301)、科创板(688/689) 的风险警示股仍执行 20%, 北交所 30%, 只有主板 ST 才是 5%。原代码 _is_st 先判且覆盖板块限幅, 导致 创业板/科创板 ST 的涨停价按 5% 计算 → +5% 被误报涨停、真 +20% 涨停被漏报, 污染 signal_limit_up / consecutive_limit_ups / 连板梯队 / near_limit_up。 修正: ST 5% 仅在 ~(创业板|科创板|北交所) 时生效 (EOD + 盘中两条路径 + near_limit_up)。 2. 因子回测 Sharpe 一律乘 √252, 但 group_nav 每点是一个调仓周期收益: 月频调仓下是月收益, 乘 √252 会把 Sharpe 高估 √(252/12) ≈ 4.6x (周频 ≈2.2x), 使无效因子显示成明星因子, 废掉"先筛无效指标"的用途。 修正: 年化系数按 config.rebalance 取 √252/√52/√12。 新增 tests/test_st_limit_and_sharpe.py (5 例) 覆盖两处修正。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
215 lines
7.1 KiB
Python
215 lines
7.1 KiB
Python
"""访问密码认证 — 单用户, 自托管场景。
|
|
|
|
设计:
|
|
- 密码用 PBKDF2-HMAC-SHA256 哈希(标准库 hashlib, 无新依赖), 加随机 salt。
|
|
即使 auth.json 泄露, 也无法逆向出明文密码。
|
|
- 会话用随机 token(token_urlsafe), 内存 + 文件双存(支持多进程/重启不丢失)。
|
|
- 存储: data/user_data/auth.json (chmod 0600), 仿 secrets_store 模式。
|
|
|
|
安全要点:
|
|
- 设密码接口必须限制本机/内网(见 auth router), 防黑客抢占域名抢先设密码。
|
|
- 登录限流: 错5次锁5分钟(见 auth router 内存计数)。
|
|
- 单密码, 不做多用户(避免重构全项目数据层)。
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import json
|
|
import logging
|
|
import os
|
|
import secrets as _secrets
|
|
import threading
|
|
import time
|
|
from pathlib import Path
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# PBKDF2 参数(NIST 推荐, 单次校验 ~100ms, 兼顾安全与响应)
|
|
_PBKDF2_ITER = 200_000
|
|
_SALT_LEN = 16
|
|
_TOKEN_BYTES = 32
|
|
|
|
# 会话有效期: 30 天(自托管单用户, 长一点减少重登频率)
|
|
SESSION_TTL = 30 * 24 * 3600
|
|
|
|
_lock = threading.Lock()
|
|
# 内存中的有效会话: { token: expire_ts }。进程重启后从磁盘恢复。
|
|
_sessions: dict[str, float] = {}
|
|
|
|
# 「是否已设密码」缓存: 每个 /api/ 请求都要判定, auth_middleware 原先每次 read_text
|
|
# 磁盘 (阻塞事件循环)。此处懒加载缓存, set_password 后失效重算 (仍返回最新真值)。
|
|
_configured_cache: bool | None = None
|
|
|
|
|
|
def _path() -> Path:
|
|
from app.config import settings
|
|
p = settings.data_dir / "user_data" / "auth.json"
|
|
p.parent.mkdir(parents=True, exist_ok=True)
|
|
return p
|
|
|
|
|
|
def _load() -> dict:
|
|
p = _path()
|
|
if p.exists():
|
|
try:
|
|
return json.loads(p.read_text(encoding="utf-8"))
|
|
except Exception as e: # noqa: BLE001
|
|
logger.warning("auth.json malformed: %s", e)
|
|
return {}
|
|
|
|
|
|
def _save(data: dict) -> None:
|
|
p = _path()
|
|
p.write_text(json.dumps(data, indent=2, ensure_ascii=False), encoding="utf-8")
|
|
try:
|
|
os.chmod(p, 0o600)
|
|
except OSError:
|
|
pass
|
|
|
|
|
|
def _hash_password(password: str, salt: bytes | None = None) -> tuple[str, str]:
|
|
"""返回 (salt_hex, hash_hex)。salt 为 None 时生成新 salt。"""
|
|
if salt is None:
|
|
salt = os.urandom(_SALT_LEN)
|
|
dk = hashlib.pbkdf2_hmac("sha256", password.encode("utf-8"), salt, _PBKDF2_ITER)
|
|
return salt.hex(), dk.hex()
|
|
|
|
|
|
def _verify_password(password: str, salt_hex: str, hash_hex: str) -> bool:
|
|
"""恒定时间比较, 防时序攻击。"""
|
|
try:
|
|
salt = bytes.fromhex(salt_hex)
|
|
expected = bytes.fromhex(hash_hex)
|
|
except ValueError:
|
|
return False
|
|
actual = hashlib.pbkdf2_hmac("sha256", password.encode("utf-8"), salt, _PBKDF2_ITER)
|
|
return _secrets.compare_digest(actual, expected)
|
|
|
|
|
|
# ================================================================
|
|
# 密码管理
|
|
# ================================================================
|
|
|
|
def is_configured() -> bool:
|
|
"""是否已设置访问密码 (带缓存)。
|
|
|
|
热路径 (auth_middleware 每请求调用): 命中缓存则不碰磁盘, 不阻塞事件循环。
|
|
首次或 set_password 失效后, 懒加载重读一次 auth.json, 保证返回最新真值。
|
|
"""
|
|
global _configured_cache
|
|
if _configured_cache is None:
|
|
d = _load()
|
|
_configured_cache = bool(d.get("password_hash"))
|
|
return _configured_cache
|
|
|
|
|
|
def set_password(password: str) -> None:
|
|
"""设置/修改访问密码。清空所有现有会话(强制重新登录)。"""
|
|
global _configured_cache
|
|
if len(password) < 6:
|
|
raise ValueError("密码至少 6 位")
|
|
salt_hex, hash_hex = _hash_password(password)
|
|
with _lock:
|
|
_sessions.clear() # 改密码 = 旧会话全部失效
|
|
_save({
|
|
"password_hash": hash_hex,
|
|
"password_salt": salt_hex,
|
|
"updated_at": int(time.time()),
|
|
"sessions": {}, # 清空持久化会话
|
|
})
|
|
_configured_cache = None # 失效缓存, 下次 is_configured 重读最新真值
|
|
logger.info("access password set")
|
|
|
|
|
|
def bootstrap_from_env() -> bool:
|
|
"""首次初始化: 若环境变量 AUTH_PASSWORD 已配置且尚未设过密码, 则用它设密码。
|
|
|
|
公网服务器部署场景: 避免每次都要 SSH 端口转发才能设首个密码。
|
|
明文密码只在内存/配置中, 经 set_password() 哈希后写入 auth.json (chmod 0600)。
|
|
一旦设置成功, 后续重启不再覆盖 (用户改密码走 UI, 不受环境变量影响)。
|
|
|
|
Returns:
|
|
True 表示本次用环境变量初始化了密码; False 表示无需初始化。
|
|
"""
|
|
from app.config import settings
|
|
|
|
pwd = (settings.auth_password or "").strip()
|
|
if not pwd:
|
|
return False
|
|
if is_configured():
|
|
# 已设过密码, 不覆盖 (避免环境变量反复重置用户在 UI 改的密码)
|
|
return False
|
|
try:
|
|
set_password(pwd)
|
|
logger.info("access password bootstrapped from AUTH_PASSWORD env (one-time)")
|
|
return True
|
|
except ValueError as e:
|
|
# 密码不合规 (< 6 位), 记日志但不阻断启动
|
|
logger.warning("AUTH_PASSWORD bootstrap skipped: %s", e)
|
|
return False
|
|
|
|
|
|
def verify_and_create_session(password: str) -> str | None:
|
|
"""验证密码, 成功则创建会话并返回 token, 失败返回 None。"""
|
|
d = _load()
|
|
if not d.get("password_hash"):
|
|
return None
|
|
if not _verify_password(password, d.get("password_salt", ""), d["password_hash"]):
|
|
return None
|
|
token = _secrets.token_urlsafe(_TOKEN_BYTES)
|
|
expire = time.time() + SESSION_TTL
|
|
with _lock:
|
|
_sessions[token] = expire
|
|
_persist_sessions_locked()
|
|
return token
|
|
|
|
|
|
def revoke_session(token: str) -> None:
|
|
"""注销会话(登出)。"""
|
|
with _lock:
|
|
_sessions.pop(token, None)
|
|
_persist_sessions_locked()
|
|
|
|
|
|
def is_valid_session(token: str) -> bool:
|
|
"""检查会话是否有效(存在且未过期)。过期则清理。"""
|
|
if not token:
|
|
return False
|
|
with _lock:
|
|
expire = _sessions.get(token)
|
|
if expire is None:
|
|
return False
|
|
if time.time() > expire:
|
|
_sessions.pop(token, None)
|
|
_persist_sessions_locked()
|
|
return False
|
|
return True
|
|
|
|
|
|
def _persist_sessions_locked() -> None:
|
|
"""把当前内存会话写回 auth.json(需持锁调用)。"""
|
|
d = _load()
|
|
d["sessions"] = {t: exp for t, exp in _sessions.items()}
|
|
_save(d)
|
|
|
|
|
|
def _restore_sessions() -> None:
|
|
"""启动时从 auth.json 恢复未过期会话(支持进程重启不丢登录态)。"""
|
|
with _lock:
|
|
d = _load()
|
|
now = time.time()
|
|
saved = d.get("sessions") or {}
|
|
for token, expire in saved.items():
|
|
if isinstance(expire, (int, float)) and expire > now:
|
|
_sessions[token] = expire
|
|
if len(_sessions) != len(saved):
|
|
# 有过期会话被清理, 落盘一次
|
|
_persist_sessions_locked()
|
|
|
|
|
|
# 模块加载时恢复会话
|
|
try:
|
|
_restore_sessions()
|
|
except Exception as e: # noqa: BLE001
|
|
logger.warning("restore sessions failed: %s", e)
|