| const fs = require('fs'); |
| const path = require('path'); |
| const crypto = require('crypto'); |
| const readline = require('readline'); |
|
|
| const ADMIN_FILE = path.join(__dirname, '../data/admin.json'); |
|
|
| |
| const rl = readline.createInterface({ |
| input: process.stdin, |
| output: process.stdout |
| }); |
|
|
| |
| function generateSalt() { |
| return crypto.randomBytes(16).toString('hex'); |
| } |
|
|
| |
| function hashPassword(password, salt) { |
| return crypto.pbkdf2Sync(password, salt, 1000, 64, 'sha512').toString('hex'); |
| } |
|
|
| |
| function question(query) { |
| return new Promise((resolve) => { |
| rl.question(query, resolve); |
| }); |
| } |
|
|
| async function main() { |
| try { |
| console.log('创建管理员账户\n'); |
|
|
| |
| const username = await question('请输入管理员用户名: '); |
| const password = await question('请输入管理员密码: '); |
|
|
| |
| const salt = generateSalt(); |
| const hash = hashPassword(password, salt); |
|
|
| |
| const adminData = { |
| admin: { |
| username, |
| salt, |
| hash |
| } |
| }; |
|
|
| |
| const dataDir = path.dirname(ADMIN_FILE); |
| if (!fs.existsSync(dataDir)) { |
| fs.mkdirSync(dataDir, { recursive: true }); |
| } |
|
|
| |
| fs.writeFileSync(ADMIN_FILE, JSON.stringify(adminData, null, 2)); |
|
|
| console.log('\n管理员账户创建成功!'); |
| console.log('请妥善保管账户信息,不要将admin.json文件提交到版本控制系统。'); |
|
|
| } catch (error) { |
| console.error('创建管理员账户失败:', error); |
| } finally { |
| rl.close(); |
| } |
| } |
|
|
| main(); |