Dev Crypto (#730)

* feat: remove admin mode

* feat: bugfix

* feat(crypto): 添加RSA-OAEP + AES-GCM混合加密服务

- 实现CryptoService加密服务,支持RSA-OAEP-2048 + AES-256-GCM混合加密
- 集成数据库层加密,自动加密存储敏感字段(API密钥、私钥等)
- 支持环境变量DATA_ENCRYPTION_KEY配置数据加密密钥
- 适配SQLite数据库加密存储(从PostgreSQL移植)
- 保持Hyperliquid代理钱包处理兼容性
- 更新.gitignore以正确处理crypto模块代码

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* feat(scripts): 添加加密环境一键设置脚本

- setup_encryption.sh: 一键生成RSA密钥对+数据加密密钥+JWT密钥
- generate_rsa_keys.sh: 专业的RSA-2048密钥对生成工具
- generate_data_key.sh: 生成AES-256数据加密密钥和JWT认证密钥
- ENCRYPTION_README.md: 详细的加密系统说明文档
- 支持自动检测现有密钥并只生成缺失的密钥
- 完善的权限管理和安全验证
- 兼容macOS和Linux的跨平台支持

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* feat(api): 添加加密API端点和Gin框架集成

- 新增CryptoHandler处理加密相关API请求
- 提供/api/crypto/public-key端点获取RSA公钥
- 提供/api/crypto/decrypt端点解密敏感数据
- 适配Gin框架的HTTP处理器格式
- 集成CryptoService到API服务器
- 支持前端加密数据传输和解密

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* feat(web): 添加前端加密服务和两阶段密钥输入组件

- CryptoService: Web Crypto API集成,支持RSA-OAEP加密
- TwoStageKeyModal: 安全的两阶段私钥输入组件,支持剪贴板混淆
- 完善国际化翻译支持加密相关UI文本
- 修复TypeScript类型错误和编译问题
- 支持前端敏感数据加密传输到后端
- 增强用户隐私保护和数据安全

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* feat(auth): 增强JWT认证安全性

- 优先使用环境变量JWT_SECRET而不是数据库配置
- 支持通过.env文件安全配置JWT认证密钥
- 保留数据库配置作为回退机制
- 改进JWT密钥来源日志显示
- 增强系统启动时的安全配置检查
- 支持运行时动态JWT密钥切换

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* feat(docker): 集成加密环境变量到Docker部署

- 添加DATA_ENCRYPTION_KEY环境变量传递到容器
- 添加JWT_SECRET环境变量支持
- 挂载secrets目录使容器可访问RSA密钥文件
- 确保容器内加密服务正常工作
- 解决容器启动失败和加密初始化问题
- 完善Docker Compose加密环境配置

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* feat(start): 集成自动加密环境检测和设置

- 增强check_encryption()函数检测JWT_SECRET和DATA_ENCRYPTION_KEY
- 自动运行setup_encryption.sh当检测到缺失密钥时
- 改进加密状态显示,包含RSA+AES+JWT全套加密信息
- 优化用户体验,提供清晰的加密配置反馈
- 支持一键设置完整加密环境
- 确保容器启动前加密环境就绪

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* feat: format fix

* fix(security): 修复前端模型和交易所配置敏感数据明文传输

- 在handleSaveModelConfig中对API密钥进行RSA-OAEP加密
- 在handleSaveExchangeConfig中对API密钥、Secret密钥和Aster私钥进行加密
- 只有非空敏感数据才进行加密处理
- 添加加密失败错误处理和用户友好提示
- 增加encryptionFailed翻译键的中英文支持
- 使用用户ID和会话ID作为加密上下文增强安全性

这修复了之前敏感数据在网络传输中以明文形式发送的安全漏洞。

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* fix(crypto): 修复后端加密服务集成和缺失的加密端点

- 添加Server结构体缺少的cryptoService字段
- 实现handleUpdateModelConfigsEncrypted处理器用于模型配置加密传输
- 修复handleUpdateExchangeConfigsEncrypted中的函数调用
- 在前端API中添加updateModelConfigsEncrypted方法
- 统一RSA密钥路径从secrets/rsa_key改为keys/rsa_private.key
- 确保前端可以使用加密端点安全传输敏感数据
- 兼容原有加密通信模式和二段输入私钥功能

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

---------

Co-authored-by: icy <icyoung520@gmail.com>
Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Icyoung
2025-11-08 02:03:09 +08:00
committed by GitHub
parent 5c2a2bd77d
commit 079995e458
21 changed files with 2478 additions and 458 deletions

122
start.sh
View File

@@ -76,6 +76,103 @@ check_env() {
print_success "环境变量文件存在"
}
# ------------------------------------------------------------------------
# Validation: Encryption Environment (RSA Keys + Data Encryption Key)
# ------------------------------------------------------------------------
check_encryption() {
local need_setup=false
print_info "检查加密环境..."
# 检查RSA密钥对
if [ ! -f "secrets/rsa_key" ] || [ ! -f "secrets/rsa_key.pub" ]; then
print_warning "RSA密钥对不存在"
need_setup=true
fi
# 检查数据加密密钥
if [ ! -f ".env" ] || ! grep -q "^DATA_ENCRYPTION_KEY=" .env; then
print_warning "数据加密密钥未配置"
need_setup=true
fi
# 检查JWT认证密钥
if [ ! -f ".env" ] || ! grep -q "^JWT_SECRET=" .env; then
print_warning "JWT认证密钥未配置"
need_setup=true
fi
# 如果需要设置加密环境
if [ "$need_setup" = "true" ]; then
print_info "🔐 需要设置加密环境"
print_info "加密环境用于保护敏感数据API密钥、私钥等"
echo ""
# 询问用户是否自动设置
read -p "是否自动设置加密环境?[Y/n]: " auto_setup
auto_setup=${auto_setup:-Y}
if [[ "$auto_setup" =~ ^[Yy]$ ]]; then
print_info "正在设置加密环境..."
# 检查加密设置脚本是否存在
if [ -f "scripts/setup_encryption.sh" ]; then
print_info "正在自动设置加密环境..."
print_info "加密系统将保护: API密钥、私钥、Hyperliquid代理钱包"
echo ""
# 自动运行加密设置脚本
# Y: 继续设置加密环境 | n: 保持现有RSA密钥 | n: 保持现有密钥配置
echo -e "Y\nn\nn" | bash scripts/setup_encryption.sh
if [ $? -eq 0 ]; then
echo ""
print_success "🔐 加密环境设置完成!"
print_info " • RSA-2048密钥对已生成"
print_info " • AES-256数据加密密钥已配置"
print_info " • JWT认证密钥已配置"
print_info " • 所有敏感数据现在都受加密保护"
echo ""
else
print_error "加密环境设置失败"
exit 1
fi
else
print_error "加密设置脚本不存在: scripts/setup_encryption.sh"
print_info "请手动运行: ./scripts/setup_encryption.sh"
exit 1
fi
else
print_warning "跳过加密环境设置"
print_info "手动设置命令: ./scripts/setup_encryption.sh"
print_info "系统将使用未加密模式运行(不推荐)"
fi
else
print_success "🔐 加密环境已配置"
print_info " • RSA密钥对: secrets/rsa_key + secrets/rsa_key.pub"
print_info " • 数据加密密钥: .env (DATA_ENCRYPTION_KEY)"
print_info " • JWT认证密钥: .env (JWT_SECRET)"
print_info " • 加密算法: RSA-OAEP-2048 + AES-256-GCM + HS256"
print_info " • 保护数据: API密钥、私钥、Hyperliquid代理钱包、用户认证"
# 验证密钥文件权限
if [ -f "secrets/rsa_key" ]; then
local perm=$(stat -f "%A" "secrets/rsa_key" 2>/dev/null || stat -c "%a" "secrets/rsa_key" 2>/dev/null)
if [ "$perm" != "600" ]; then
print_warning "修复RSA私钥权限..."
chmod 600 secrets/rsa_key
fi
fi
if [ -f ".env" ]; then
local perm=$(stat -f "%A" ".env" 2>/dev/null || stat -c "%a" ".env" 2>/dev/null)
if [ "$perm" != "600" ]; then
print_warning "修复环境文件权限..."
chmod 600 .env
fi
fi
fi
}
# ------------------------------------------------------------------------
# Validation: Configuration File (config.json) - BASIC SETTINGS ONLY
# ------------------------------------------------------------------------
@@ -274,6 +371,21 @@ update() {
print_success "更新完成"
}
# ------------------------------------------------------------------------
# Encryption: Manual Setup
# ------------------------------------------------------------------------
setup_encryption_manual() {
print_info "🔐 手动设置加密环境"
if [ -f "scripts/setup_encryption.sh" ]; then
bash scripts/setup_encryption.sh
else
print_error "加密设置脚本不存在: scripts/setup_encryption.sh"
print_info "请确保项目文件完整"
exit 1
fi
}
# ------------------------------------------------------------------------
# Help: Usage Information
# ------------------------------------------------------------------------
@@ -290,12 +402,18 @@ show_help() {
echo " status 查看服务状态"
echo " clean 清理所有容器和数据"
echo " update 更新代码并重启"
echo " setup-encryption 设置加密环境RSA密钥+数据加密)"
echo " help 显示此帮助信息"
echo ""
echo "示例:"
echo " ./start.sh start --build # 构建并启动"
echo " ./start.sh logs backend # 查看后端日志"
echo " ./start.sh status # 查看状态"
echo " ./start.sh setup-encryption # 手动设置加密环境"
echo ""
echo "🔐 关于加密:"
echo " 系统自动检测加密环境,首次运行时会自动设置"
echo " 手动设置: ./scripts/setup_encryption.sh"
}
# ------------------------------------------------------------------------
@@ -307,6 +425,7 @@ main() {
case "${1:-start}" in
start)
check_env
check_encryption
check_config
check_database
start "$2"
@@ -329,6 +448,9 @@ main() {
update)
update
;;
setup-encryption)
setup_encryption_manual
;;
help|--help|-h)
show_help
;;