87 lines
2.7 KiB
TypeScript
87 lines
2.7 KiB
TypeScript
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');
|
||
|
||
// 主库加密密钥
|
||
const mainDbEncryptionKey = crypto.randomBytes(32).toString('hex');
|
||
|
||
// Docker / 本地默认数据库参数
|
||
const postgresUser = 'license';
|
||
const postgresPassword = crypto.randomBytes(18).toString('base64url');
|
||
const postgresDb = 'license_db';
|
||
const mainDatabaseUrl = `postgresql://${postgresUser}:${postgresPassword}@localhost:5432/${postgresDb}?schema=main`;
|
||
const projectDatabaseUrl = `postgresql://${postgresUser}:${postgresPassword}@localhost:5432/${postgresDb}?schema=public`;
|
||
const adminInitialPassword = crypto.randomBytes(18).toString('base64url');
|
||
|
||
// 创建 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 = `# 数据库连接
|
||
POSTGRES_USER="${postgresUser}"
|
||
POSTGRES_PASSWORD="${postgresPassword}"
|
||
POSTGRES_DB="${postgresDb}"
|
||
|
||
MAIN_DATABASE_URL="${mainDatabaseUrl}"
|
||
MAIN_DB_ENCRYPTION_KEY="${mainDbEncryptionKey}"
|
||
DATABASE_URL="${projectDatabaseUrl}"
|
||
|
||
# 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="${adminInitialPassword}"
|
||
`;
|
||
|
||
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);
|
||
console.log('');
|
||
console.log('📋 许可证服务初始管理员密码已写入 license-server/.env 的 ADMIN_INITIAL_PASSWORD,不在终端日志中输出明文。');
|