flnxi-Wu/license-server/scripts/generate-keys.ts
WuYongJie ba98bf22cc feat: 添加许可证系统、Agent部署模块和Harbor镜像管理
- 新增许可证服务器 (license-server/)
- 新增 Agent 部署模块 (flnxi-agent/)
- 添加许可证验证和激活页面
- 添加 Harbor 镜像同步脚本
- 更新素材系统支持许可证验证
2025-12-15 21:19:46 +08:00

68 lines
1.8 KiB
TypeScript
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.

import * as crypto from 'crypto';
import * as fs from 'fs';
import * as path from 'path';
// 生成 RSA 密钥对
const { publicKey, privateKey } = crypto.generateKeyPairSync('rsa', {
modulusLength: 2048,
publicKeyEncoding: {
type: 'spki',
format: 'pem',
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem',
},
});
// 生成 AES 密钥(32字节)
const aesKey = crypto.randomBytes(32).toString('hex');
// 生成 JWT 密钥
const jwtSecret = crypto.randomBytes(32).toString('hex');
// 创建 keys 目录
const keysDir = path.join(__dirname, '..', 'keys');
if (!fs.existsSync(keysDir)) {
fs.mkdirSync(keysDir, { recursive: true });
}
// 保存密钥文件
fs.writeFileSync(path.join(keysDir, 'private.key'), privateKey);
fs.writeFileSync(path.join(keysDir, 'public.key'), publicKey);
// 生成 .env 内容
const envContent = `# 数据库连接
DATABASE_URL="postgresql://postgres:password@localhost:5432/license_server?schema=public"
# RSA 私钥(用于签名许可证)
RSA_PRIVATE_KEY="${privateKey.replace(/\n/g, '\\n')}"
# RSA 公钥(嵌入客户端用于验证签名)
RSA_PUBLIC_KEY="${publicKey.replace(/\n/g, '\\n')}"
# AES 加密密钥
AES_SECRET_KEY="${aesKey}"
# JWT 密钥
JWT_SECRET="${jwtSecret}"
# 管理员初始密码
ADMIN_INITIAL_PASSWORD="admin123"
`;
fs.writeFileSync(path.join(__dirname, '..', '.env'), envContent);
console.log('✅ 密钥生成完成!');
console.log('');
console.log('文件已生成:');
console.log(' - keys/private.key (RSA 私钥)');
console.log(' - keys/public.key (RSA 公钥)');
console.log(' - .env (环境变量配置)');
console.log('');
console.log('⚠️ 请妥善保管私钥,不要提交到代码仓库!');
console.log('');
console.log('📋 客户端公钥(复制到素材系统):');
console.log('─'.repeat(50));
console.log(publicKey);