SillyTavern/recover.js

69 lines
1.7 KiB
JavaScript
Raw Normal View History

2024-10-10 23:28:17 +02:00
import fs from 'node:fs';
2024-10-11 09:43:29 +02:00
import process from 'node:process';
2024-10-10 21:37:22 +02:00
import yaml from 'yaml';
import storage from 'node-persist';
2024-10-10 23:41:08 +02:00
import {
initUserStorage,
getPasswordSalt,
getPasswordHash,
toKey,
} from './src/users.js';
2024-04-12 20:31:43 +02:00
const userAccount = process.argv[2];
const userPassword = process.argv[3];
if (!userAccount) {
console.error('A tool for recovering lost SillyTavern accounts. Uses a "dataRoot" setting from config.yaml file.');
console.error('Usage: node recover.js [account] (password)');
console.error('Example: node recover.js admin password');
process.exit(1);
}
async function initStorage() {
const config = yaml.parse(fs.readFileSync('config.yaml', 'utf8'));
const dataRoot = config.dataRoot;
if (!dataRoot) {
console.error('No "dataRoot" setting found in config.yaml file.');
process.exit(1);
}
2024-10-10 23:41:08 +02:00
await initUserStorage(dataRoot);
2024-04-12 20:31:43 +02:00
}
async function main() {
await initStorage();
/**
* @type {import('./src/users').User}
*/
2024-10-10 23:41:08 +02:00
const user = await storage.get(toKey(userAccount));
2024-04-12 20:31:43 +02:00
if (!user) {
console.error(`User "${userAccount}" not found.`);
process.exit(1);
}
2024-10-10 23:41:08 +02:00
if (!user.enabled) {
2024-04-12 20:31:43 +02:00
console.log('User is disabled. Enabling...');
user.enabled = true;
}
if (userPassword) {
console.log('Setting new password...');
2024-10-10 23:41:08 +02:00
const salt = getPasswordSalt();
const passwordHash = getPasswordHash(userPassword, salt);
2024-04-12 20:31:43 +02:00
user.password = passwordHash;
user.salt = salt;
} else {
console.log('Setting an empty password...');
user.password = '';
user.salt = '';
}
2024-10-10 23:41:08 +02:00
await storage.setItem(toKey(userAccount), user);
2024-04-12 20:31:43 +02:00
console.log('User recovered. A program will exit now.');
}
main();