feat: initial commit for HookPilot
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
import './warnings.js';
|
||||
import fs from 'node:fs';
|
||||
import { deleteOtherSessions, getAdmin, MIN_PASSWORD_LENGTH, setAdminCredentials } from './auth/admin.js';
|
||||
import { config, dataPath } from './config.js';
|
||||
import { Db } from './db.js';
|
||||
import { randomToken } from './util/crypto.js';
|
||||
|
||||
const USAGE = `用法:
|
||||
node dist/cli.js reset-password [用户名]
|
||||
|
||||
重置管理员密码并使所有登录会话失效。
|
||||
新密码取自环境变量 HOOKPILOT_NEW_PASSWORD,未设置时随机生成并输出。`;
|
||||
|
||||
async function resetPassword(username: string | undefined): Promise<void> {
|
||||
fs.mkdirSync(config.dataDir, { recursive: true, mode: 0o700 });
|
||||
const db = new Db(dataPath('hookpilot.db'));
|
||||
try {
|
||||
const provided = process.env.HOOKPILOT_NEW_PASSWORD;
|
||||
if (provided !== undefined && provided.length < MIN_PASSWORD_LENGTH) {
|
||||
throw new Error(`HOOKPILOT_NEW_PASSWORD 至少需要 ${MIN_PASSWORD_LENGTH} 个字符`);
|
||||
}
|
||||
const password = provided ?? randomToken(12);
|
||||
const name = username ?? getAdmin(db)?.username ?? config.initialAdminUser;
|
||||
await setAdminCredentials(db, name, password);
|
||||
deleteOtherSessions(db, null);
|
||||
console.log(`管理员 ${name} 的密码已重置。`);
|
||||
if (!provided) console.log(`新密码:${password}`);
|
||||
} finally {
|
||||
db.close();
|
||||
}
|
||||
}
|
||||
|
||||
const [command, arg] = process.argv.slice(2);
|
||||
if (command === 'reset-password') {
|
||||
resetPassword(arg).catch((err) => {
|
||||
console.error(err instanceof Error ? err.message : err);
|
||||
process.exit(1);
|
||||
});
|
||||
} else {
|
||||
console.log(USAGE);
|
||||
process.exit(command ? 1 : 0);
|
||||
}
|
||||
Reference in New Issue
Block a user