Fix recover.js script

a64aae3edfc57b274c472cdc388e1553b6beaa28

Cohee <18619528+Cohee1207@users.noreply.github.com>

2 files changed, +70 -52Showing whitespace changes
recover.js+5 -52
@@ -1,16 +1,9 @@
1import fs from 'node:fs';
2import process from 'node:process';1import process from 'node:process';
3import yaml from 'yaml';2import { setConfigFilePath } from './src/util.js';
4import storage from 'node-persist';
5import {
6 initUserStorage,
7 getPasswordSalt,
8 getPasswordHash,
9 toKey,
10} from './src/users.js';
113
12const userAccount = process.argv[2];4const userAccount = process.argv[2];
13const userPassword = process.argv[3];5const userPassword = process.argv[3];
6const configPath = './config.yaml';
147
15if (!userAccount) {8if (!userAccount) {
16 console.error('A tool for recovering lost SillyTavern accounts. Uses a "dataRoot" setting from config.yaml file.');9 console.error('A tool for recovering lost SillyTavern accounts. Uses a "dataRoot" setting from config.yaml file.');
@@ -19,50 +12,10 @@ if (!userAccount) {
19 process.exit(1);12 process.exit(1);
20}13}
2114
22async function initStorage() {
23 const config = yaml.parse(fs.readFileSync('config.yaml', 'utf8'));
24 const dataRoot = config.dataRoot;
25
26 if (!dataRoot) {
27 console.error('No "dataRoot" setting found in config.yaml file.');
28 process.exit(1);
29 }
30
31 await initUserStorage(dataRoot);
32}
33
34async function main() {15async function main() {
35 await initStorage();16 setConfigFilePath(configPath);
3617 const { recoverPassword } = await import('./src/recover-password.js');
37 /**18 await recoverPassword(configPath, userAccount, userPassword);
38 * @type {import('./src/users').User}
39 */
40 const user = await storage.get(toKey(userAccount));
41
42 if (!user) {
43 console.error(`User "${userAccount}" not found.`);
44 process.exit(1);
45 }
46
47 if (!user.enabled) {
48 console.log('User is disabled. Enabling...');
49 user.enabled = true;
50 }
51
52 if (userPassword) {
53 console.log('Setting new password...');
54 const salt = getPasswordSalt();
55 const passwordHash = getPasswordHash(userPassword, salt);
56 user.password = passwordHash;
57 user.salt = salt;
58 } else {
59 console.log('Setting an empty password...');
60 user.password = '';
61 user.salt = '';
62 }
63
64 await storage.setItem(toKey(userAccount), user);
65 console.log('User recovered. A program will exit now.');
66}19}
6720
68main();21main();
src/recover-password.js+65 -0
@@ -0,0 +1,65 @@
1import fs from 'node:fs';
2import yaml from 'yaml';
3import storage from 'node-persist';
4import {
5 initUserStorage,
6 getPasswordSalt,
7 getPasswordHash,
8 toKey,
9} from './users.js';
10
11/**
12 * Initializes the storage with the data root specified in the config file.
13 * @param {string} configPath - The path to the config file.
14 */
15async function initStorage(configPath) {
16 const config = yaml.parse(fs.readFileSync(configPath, 'utf8'));
17 const dataRoot = config.dataRoot;
18
19 if (!dataRoot) {
20 console.error('No "dataRoot" setting found in config.yaml file.');
21 process.exit(1);
22 }
23
24 await initUserStorage(dataRoot);
25}
26
27/**
28 * Recovers a user account by enabling it and optionally setting a new password.
29 * @param {string} configPath - The path to the config file.
30 * @param {string} userAccount - The username of the account to recover.
31 * @param {string} [userPassword] - The new password for the account. If not provided, sets an empty password.
32 */
33export async function recoverPassword(configPath, userAccount, userPassword) {
34 await initStorage(configPath);
35
36 /**
37 * @type {import('./users').User}
38 */
39 const user = await storage.get(toKey(userAccount));
40
41 if (!user) {
42 console.error(`User "${userAccount}" not found.`);
43 process.exit(1);
44 }
45
46 if (!user.enabled) {
47 console.log('User is disabled. Enabling...');
48 user.enabled = true;
49 }
50
51 if (userPassword) {
52 console.log('Setting new password...');
53 const salt = getPasswordSalt();
54 const passwordHash = getPasswordHash(userPassword, salt);
55 user.password = passwordHash;
56 user.salt = salt;
57 } else {
58 console.log('Setting an empty password...');
59 user.password = '';
60 user.salt = '';
61 }
62
63 await storage.setItem(toKey(userAccount), user);
64 console.log('User recovered. A program will exit now.');
65}