Update module imports
| @@ -1,7 +1,12 @@ | ||
| 1 | 1 | import fs from 'node:fs'; |
| 2 | 2 | import yaml from 'yaml'; |
| 3 | 3 | import storage from 'node-persist'; |
| 4 | -import * as users from './src/users.js'; | |
| 4 | +import { | |
| 5 | + initUserStorage, | |
| 6 | + getPasswordSalt, | |
| 7 | + getPasswordHash, | |
| 8 | + toKey, | |
| 9 | +} from './src/users.js'; | |
| 5 | 10 | |
| 6 | 11 | const userAccount = process.argv[2]; |
| 7 | 12 | const userPassword = process.argv[3]; |
| @@ -22,7 +27,7 @@ async function initStorage() { | ||
| 22 | 27 | process.exit(1); |
| 23 | 28 | } |
| 24 | 29 | |
| 25 | 30 | await users.initUserStorage(dataRoot); |
| 26 | 31 | } |
| 27 | 32 | |
| 28 | 33 | async function main() { |
| @@ -31,22 +36,22 @@ async function main() { | ||
| 31 | 36 | /** |
| 32 | 37 | * @type {import('./src/users').User} |
| 33 | 38 | */ |
| 34 | 39 | const user = await storage.get(users.toKey(userAccount)); |
| 35 | 40 | |
| 36 | 41 | if (!user) { |
| 37 | 42 | console.error(`User "${userAccount}" not found.`); |
| 38 | 43 | process.exit(1); |
| 39 | 44 | } |
| 40 | 45 | |
| 41 | 46 | if (!user.enabled) { |
| 42 | 47 | console.log('User is disabled. Enabling...'); |
| 43 | 48 | user.enabled = true; |
| 44 | 49 | } |
| 45 | 50 | |
| 46 | 51 | if (userPassword) { |
| 47 | 52 | console.log('Setting new password...'); |
| 48 | 53 | const salt = users.getPasswordSalt(); |
| 49 | 54 | const passwordHash = users.getPasswordHash(userPassword, salt); |
| 50 | 55 | user.password = passwordHash; |
| 51 | 56 | user.salt = salt; |
| 52 | 57 | } else { |
| @@ -55,7 +60,7 @@ async function main() { | ||
| 55 | 60 | user.salt = ''; |
| 56 | 61 | } |
| 57 | 62 | |
| 58 | 63 | await storage.setItem(users.toKey(userAccount), user); |
| 59 | 64 | console.log('User recovered. A program will exit now.'); |
| 60 | 65 | } |
| 61 | 66 | |
| @@ -36,13 +36,27 @@ util.inspect.defaultOptions.maxStringLength = null; | ||
| 36 | 36 | util.inspect.defaultOptions.depth = 4; |
| 37 | 37 | |
| 38 | 38 | // local library imports |
| 39 | 39 | import *{ asloadPlugins loader} from './src/plugin-loader.js'; |
| 40 | -import * as userModule from './src/users.js'; | |
| 40 | +import { | |
| 41 | + initUserStorage, | |
| 42 | + getCsrfSecret, | |
| 43 | + getCookieSecret, | |
| 44 | + getCookieSessionName, | |
| 45 | + getAllEnabledUsers, | |
| 46 | + ensurePublicDirectoriesExist, | |
| 47 | + getUserDirectoriesList, | |
| 48 | + migrateSystemPrompts, | |
| 49 | + migrateUserData, | |
| 50 | + requireLoginMiddleware, | |
| 51 | + setUserDataMiddleware, | |
| 52 | + shouldRedirectToLogin, | |
| 53 | + tryAutoLogin, | |
| 54 | + router as userDataRouter, | |
| 55 | +} from './src/users.js'; | |
| 41 | 56 | import basicAuthMiddleware from './src/middleware/basicAuth.js'; |
| 42 | 57 | import whitelistMiddleware from './src/middleware/whitelist.js'; |
| 43 | 58 | import multerMonkeyPatch from './src/middleware/multerMonkeyPatch.js'; |
| 44 | 59 | import initRequestProxy from './src/request-proxy.js'; |
| 45 | -import * as contentManager from './src/endpoints/content-manager.js'; | |
| 46 | 60 | import { |
| 47 | 61 | getVersion, |
| 48 | 62 | getConfigValue, |
| @@ -81,7 +95,7 @@ import { router as worldInfoRouter } from './src/endpoints/worldinfo.js'; | ||
| 81 | 95 | import { router as statsRouter, init as statsInit, onExit as statsOnExit } from './src/endpoints/stats.js'; |
| 82 | 96 | import { router as backgroundsRouter } from './src/endpoints/backgrounds.js'; |
| 83 | 97 | import { router as spritesRouter } from './src/endpoints/sprites.js'; |
| 84 | 98 | import { router as contentManagerRouter, checkForNewContent } from './src/endpoints/content-manager.js'; |
| 85 | 99 | import { router as settingsRouter, init as settingsInit } from './src/endpoints/settings.js'; |
| 86 | 100 | import { router as stableDiffusionRouter } from './src/endpoints/stable-diffusion.js'; |
| 87 | 101 | import { router as hordeRouter } from './src/endpoints/horde.js'; |
| @@ -349,21 +363,21 @@ function getSessionCookieAge() { | ||
| 349 | 363 | } |
| 350 | 364 | |
| 351 | 365 | app.use(cookieSession({ |
| 352 | 366 | name: userModule.getCookieSessionName(), |
| 353 | 367 | sameSite: 'strict', |
| 354 | 368 | httpOnly: true, |
| 355 | 369 | maxAge: getSessionCookieAge(), |
| 356 | 370 | secret: userModule.getCookieSecret(), |
| 357 | 371 | })); |
| 358 | 372 | |
| 359 | 373 | app.use(userModule.setUserDataMiddleware); |
| 360 | 374 | |
| 361 | 375 | // CSRF Protection // |
| 362 | 376 | if (!disableCsrf) { |
| 363 | 377 | const COOKIES_SECRET = userModule.getCookieSecret(); |
| 364 | 378 | |
| 365 | 379 | const { generateToken, doubleCsrfProtection } = doubleCsrf({ |
| 366 | 380 | getSecret: userModule.getCsrfSecret, |
| 367 | 381 | cookieName: 'X-CSRF-Token', |
| 368 | 382 | cookieOptions: { |
| 369 | 383 | httpOnly: true, |
| @@ -394,7 +408,7 @@ if (!disableCsrf) { | ||
| 394 | 408 | // Static files |
| 395 | 409 | // Host index page |
| 396 | 410 | app.get('/', (request, response) => { |
| 397 | 411 | if (userModule.shouldRedirectToLogin(request)) { |
| 398 | 412 | const query = request.url.split('?')[1]; |
| 399 | 413 | const redirectUrl = query ? `/login?${query}` : '/login'; |
| 400 | 414 | return response.redirect(redirectUrl); |
| @@ -411,7 +425,7 @@ app.get('/login', async (request, response) => { | ||
| 411 | 425 | } |
| 412 | 426 | |
| 413 | 427 | try { |
| 414 | 428 | const autoLogin = await userModule.tryAutoLogin(request, basicAuthMode); |
| 415 | 429 | |
| 416 | 430 | if (autoLogin) { |
| 417 | 431 | return response.redirect('/'); |
| @@ -430,7 +444,7 @@ app.use(express.static(process.cwd() + '/public', {})); | ||
| 430 | 444 | app.use('/api/users', usersPublicRouter); |
| 431 | 445 | |
| 432 | 446 | // Everything below this line requires authentication |
| 433 | 447 | app.use(userModule.requireLoginMiddleware); |
| 434 | 448 | app.get('/api/ping', (_, response) => response.sendStatus(204)); |
| 435 | 449 | |
| 436 | 450 | // File uploads |
| @@ -438,7 +452,7 @@ app.use(multer({ dest: uploadsPath, limits: { fieldSize: 10 * 1024 * 1024 } }).s | ||
| 438 | 452 | app.use(multerMonkeyPatch); |
| 439 | 453 | |
| 440 | 454 | // User data mount |
| 441 | 455 | app.use('/', userModule.routeruserDataRouter); |
| 442 | 456 | // Private endpoints |
| 443 | 457 | app.use('/api/users', usersPrivateRouter); |
| 444 | 458 | // Admin endpoints |
| @@ -625,15 +639,15 @@ const preSetupTasks = async function () { | ||
| 625 | 639 | } |
| 626 | 640 | console.log(); |
| 627 | 641 | |
| 628 | 642 | const directories = await userModule.getUserDirectoriesList(); |
| 629 | 643 | await contentManager.checkForNewContent(directories); |
| 630 | 644 | await ensureThumbnailCache(); |
| 631 | 645 | cleanUploads(); |
| 632 | 646 | |
| 633 | 647 | await settingsInit(); |
| 634 | 648 | await statsInit(); |
| 635 | 649 | |
| 636 | 650 | const cleanupPlugins = await loadPluginsinitializePlugins(); |
| 637 | 651 | const consoleTitle = process.title; |
| 638 | 652 | |
| 639 | 653 | let isExiting = false; |
| @@ -740,10 +754,10 @@ const postSetupTasks = async function (v6Failed, v4Failed) { | ||
| 740 | 754 | * Loads server plugins from a directory. |
| 741 | 755 | * @returns {Promise<Function>} Function to be run on server exit |
| 742 | 756 | */ |
| 743 | 757 | async function loadPluginsinitializePlugins() { |
| 744 | 758 | try { |
| 745 | 759 | const pluginDirectory = path.join(serverDirectory, 'plugins'); |
| 746 | 760 | const cleanupPlugins = await loader.loadPlugins(app, pluginDirectory); |
| 747 | 761 | return cleanupPlugins; |
| 748 | 762 | } catch { |
| 749 | 763 | console.log('Plugin loading failed.'); |
| @@ -883,7 +897,7 @@ async function verifySecuritySettings() { | ||
| 883 | 897 | logSecurityAlert('Your SillyTavern is currently insecurely open to the public. Enable whitelisting, basic authentication or user accounts.'); |
| 884 | 898 | } |
| 885 | 899 | |
| 886 | 900 | const users = await userModule.getAllEnabledUsers(); |
| 887 | 901 | const unprotectedUsers = users.filter(x => !x.password); |
| 888 | 902 | const unprotectedAdminUsers = unprotectedUsers.filter(x => x.admin); |
| 889 | 903 | |
| @@ -901,10 +915,10 @@ async function verifySecuritySettings() { | ||
| 901 | 915 | } |
| 902 | 916 | |
| 903 | 917 | // User storage module needs to be initialized before starting the server |
| 904 | 918 | userModule.initUserStorage(dataRoot) |
| 905 | 919 | .then(userModule.ensurePublicDirectoriesExist) |
| 906 | 920 | .then(userModule.migrateUserData) |
| 907 | 921 | .then(userModule.migrateSystemPrompts) |
| 908 | 922 | .then(verifySecuritySettings) |
| 909 | 923 | .then(preSetupTasks) |
| 910 | 924 | .finally(startServer); |
| @@ -16,7 +16,7 @@ import { AVATAR_WIDTH, AVATAR_HEIGHT } from '../constants.js'; | ||
| 16 | 16 | import { jsonParser, urlencodedParser } from '../express-common.js'; |
| 17 | 17 | import { deepMerge, humanizedISO8601DateTime, tryParse, extractFileFromZipBuffer } from '../util.js'; |
| 18 | 18 | import { TavernCardValidator } from '../validator/TavernCardValidator.js'; |
| 19 | 19 | import *{ asparse, characterCardParserwrite } from '../character-card-parser.js'; |
| 20 | 20 | import { readWorldInfoFile } from './worldinfo.js'; |
| 21 | 21 | import { invalidateThumbnail } from './thumbnails.js'; |
| 22 | 22 | import { importRisuSprites } from './sprites.js'; |
| @@ -38,7 +38,7 @@ async function readCharacterData(inputFile, inputFormat = 'png') { | ||
| 38 | 38 | return characterDataCache.get(cacheKey); |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 | const result = characterCardParser.parse(inputFile, inputFormat); |
| 42 | 42 | characterDataCache.set(cacheKey, result); |
| 43 | 43 | return result; |
| 44 | 44 | } |
| @@ -77,7 +77,7 @@ async function writeCharacterData(inputFile, data, outputFile, request, crop = u | ||
| 77 | 77 | const inputImage = await getInputImage(); |
| 78 | 78 | |
| 79 | 79 | // Get the chunks |
| 80 | 80 | const outputImage = characterCardParser.write(inputImage, data); |
| 81 | 81 | const outputImagePath = path.join(request.user.directories.characters, `${outputFile}.png`); |
| 82 | 82 | |
| 83 | 83 | writeFileAtomicSync(outputImagePath, outputImage); |
| @@ -5,15 +5,16 @@ import { Buffer } from 'node:buffer'; | ||
| 5 | 5 | import express from 'express'; |
| 6 | 6 | import fetch from 'node-fetch'; |
| 7 | 7 | import sanitize from 'sanitize-filename'; |
| 8 | +import { sync as writeFileAtomicSync } from 'write-file-atomic'; | |
| 8 | 9 | |
| 9 | 10 | import { getConfigValue, color } from '../util.js'; |
| 10 | 11 | import { jsonParser } from '../express-common.js'; |
| 11 | 12 | import { sync as writeFileAtomicSyncwrite } from 'write../character-filecard-atomicparser.js'; |
| 13 | + | |
| 12 | 14 | const contentDirectory = path.join(process.cwd(), 'default/content'); |
| 13 | 15 | const scaffoldDirectory = path.join(process.cwd(), 'default/scaffold'); |
| 14 | 16 | const contentIndexPath = path.join(contentDirectory, 'index.json'); |
| 15 | 17 | const scaffoldIndexPath = path.join(scaffoldDirectory, 'index.json'); |
| 16 | -import * as characterCardParser from '../character-card-parser.js'; | |
| 17 | 18 | |
| 18 | 19 | const WHITELIST_GENERIC_URL_DOWNLOAD_SOURCES = getConfigValue('whitelistImportDomains', []); |
| 19 | 20 | |
| @@ -397,7 +398,7 @@ async function downloadPygmalionCharacter(id) { | ||
| 397 | 398 | const avatarResult = await fetch(avatarUrl); |
| 398 | 399 | const avatarBuffer = await avatarResult.buffer(); |
| 399 | 400 | |
| 400 | 401 | const cardBuffer = characterCardParser.write(avatarBuffer, JSON.stringify(characterData)); |
| 401 | 402 | |
| 402 | 403 | return { |
| 403 | 404 | buffer: cardBuffer, |
| @@ -8,9 +8,8 @@ import express from 'express'; | ||
| 8 | 8 | import { jsonParser } from '../express-common.js'; |
| 9 | 9 | import { getUserAvatar, toKey, getPasswordHash, getPasswordSalt, createBackupArchive, ensurePublicDirectoriesExist, toAvatarKey } from '../users.js'; |
| 10 | 10 | import { SETTINGS_FILE } from '../constants.js'; |
| 11 | 11 | import *{ ascheckForNewContent, contentManagerCONTENT_TYPES } from './content-manager.js'; |
| 12 | 12 | import { color, Cache } from '../util.js'; |
| 13 | -import { checkForNewContent } from './content-manager.js'; | |
| 14 | 13 | |
| 15 | 14 | const RESET_CACHE = new Cache(5 * 60 * 1000); |
| 16 | 15 | |
| @@ -168,7 +167,7 @@ router.post('/reset-settings', jsonParser, async (request, response) => { | ||
| 168 | 167 | |
| 169 | 168 | const pathToFile = path.join(request.user.directories.root, SETTINGS_FILE); |
| 170 | 169 | await fsPromises.rm(pathToFile, { force: true }); |
| 171 | 170 | await contentManager.checkForNewContent([request.user.directories], [contentManager.CONTENT_TYPES.SETTINGS]); |
| 172 | 171 | |
| 173 | 172 | return response.sendStatus(204); |
| 174 | 173 | } catch (error) { |