From 32e0b129fe78730c47d96a04481c70597ab58944 Mon Sep 17 00:00:00 2001 From: Fallenbagel <98979876+Fallenbagel@users.noreply.github.com> Date: Tue, 22 Oct 2024 05:20:14 +0800 Subject: [PATCH 1/9] docs(aur): add disclaimer about being maintained by third-party (#1044) --- docs/getting-started/aur.mdx | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/getting-started/aur.mdx b/docs/getting-started/aur.mdx index a67a0b24b..025118c8f 100644 --- a/docs/getting-started/aur.mdx +++ b/docs/getting-started/aur.mdx @@ -6,6 +6,10 @@ sidebar_position: 4 # AUR (Arch User Repository) +:::note Disclaimer +This AUR package is not maintained by us but by a third party. Please refer to the maintainer for any issues. +::: + :::info This method is not recommended for most users. It is intended for advanced users who are using Arch Linux or an Arch-based distribution. ::: From 0bbcfcbd5e03137aba35ceb07e42f623aefa41d7 Mon Sep 17 00:00:00 2001 From: Gauthier Date: Thu, 24 Oct 2024 18:11:25 +0200 Subject: [PATCH 2/9] fix: cache Jellyfin/Emby avatars from API (#1045) * fix: cache Jellyfin/Emby avatars from API Previously, avatars were cached using image links from Jellyfin/Emby. Now, avatar images are obtained directly from the API to avoid some configuration bugs. * fix: update avatar on new login --- server/lib/imageproxy.ts | 21 ++++++-- server/routes/auth.ts | 45 ++-------------- server/routes/avatarproxy.ts | 59 ++++++++++++++++----- server/routes/settings/index.ts | 5 +- server/routes/user/index.ts | 7 +-- src/components/Common/CachedImage/index.tsx | 7 +-- 6 files changed, 73 insertions(+), 71 deletions(-) diff --git a/server/lib/imageproxy.ts b/server/lib/imageproxy.ts index badfe94f2..04e320a0b 100644 --- a/server/lib/imageproxy.ts +++ b/server/lib/imageproxy.ts @@ -135,6 +135,7 @@ class ImageProxy { private cacheVersion; private key; private baseUrl; + private headers: HeadersInit | null = null; constructor( key: string, @@ -142,6 +143,7 @@ class ImageProxy { options: { cacheVersion?: number; rateLimitOptions?: RateLimitOptions; + headers?: HeadersInit; } = {} ) { this.cacheVersion = options.cacheVersion ?? 1; @@ -155,9 +157,13 @@ class ImageProxy { } else { this.fetch = fetch; } + this.headers = options.headers || null; } - public async getImage(path: string): Promise { + public async getImage( + path: string, + fallbackPath?: string + ): Promise { const cacheKey = this.getCacheKey(path); const imageResponse = await this.get(cacheKey); @@ -166,7 +172,11 @@ class ImageProxy { const newImage = await this.set(path, cacheKey); if (!newImage) { - throw new Error('Failed to load image'); + if (fallbackPath) { + return await this.getImage(fallbackPath); + } else { + throw new Error('Failed to load image'); + } } return newImage; @@ -247,7 +257,12 @@ class ImageProxy { : '/' : '') + (path.startsWith('/') ? path.slice(1) : path); - const response = await this.fetch(href); + const response = await this.fetch(href, { + headers: this.headers || undefined, + }); + if (!response.ok) { + return null; + } const arrayBuffer = await response.arrayBuffer(); const buffer = Buffer.from(arrayBuffer); diff --git a/server/routes/auth.ts b/server/routes/auth.ts index 560f04d57..70e674f97 100644 --- a/server/routes/auth.ts +++ b/server/routes/auth.ts @@ -6,7 +6,6 @@ import { UserType } from '@server/constants/user'; import { getRepository } from '@server/datasource'; import { User } from '@server/entity/User'; import { startJobs } from '@server/job/schedule'; -import ImageProxy from '@server/lib/imageproxy'; import { Permission } from '@server/lib/permissions'; import { getSettings } from '@server/lib/settings'; import logger from '@server/logger'; @@ -15,7 +14,6 @@ import { ApiError } from '@server/types/error'; import { getHostname } from '@server/utils/getHostname'; import * as EmailValidator from 'email-validator'; import { Router } from 'express'; -import gravatarUrl from 'gravatar-url'; import net from 'net'; const authRoutes = Router(); @@ -328,12 +326,7 @@ authRoutes.post('/jellyfin', async (req, res, next) => { jellyfinDeviceId: deviceId, jellyfinAuthToken: account.AccessToken, permissions: Permission.ADMIN, - avatar: account.User.PrimaryImageTag - ? `/Users/${account.User.Id}/Images/Primary/?tag=${account.User.PrimaryImageTag}&quality=90` - : gravatarUrl(body.email || account.User.Name, { - default: 'mm', - size: 200, - }), + avatar: `/avatarproxy/${account.User.Id}`, userType: UserType.EMBY, }); @@ -347,12 +340,7 @@ authRoutes.post('/jellyfin', async (req, res, next) => { jellyfinDeviceId: deviceId, jellyfinAuthToken: account.AccessToken, permissions: Permission.ADMIN, - avatar: account.User.PrimaryImageTag - ? `/Users/${account.User.Id}/Images/Primary/?tag=${account.User.PrimaryImageTag}&quality=90` - : gravatarUrl(body.email || account.User.Name, { - default: 'mm', - size: 200, - }), + avatar: `/avatarproxy/${account.User.Id}`, userType: UserType.JELLYFIN, }); @@ -401,27 +389,7 @@ authRoutes.post('/jellyfin', async (req, res, next) => { jellyfinUsername: account.User.Name, } ); - // Update the users avatar with their jellyfin profile pic (incase it changed) - if (account.User.PrimaryImageTag) { - const avatar = `/Users/${account.User.Id}/Images/Primary/?tag=${account.User.PrimaryImageTag}&quality=90`; - if (avatar !== user.avatar) { - const avatarProxy = new ImageProxy('avatar', ''); - avatarProxy.clearCachedImage(user.avatar); - } - user.avatar = avatar; - } else { - const avatar = gravatarUrl(user.email || account.User.Name, { - default: 'mm', - size: 200, - }); - - if (avatar !== user.avatar) { - const avatarProxy = new ImageProxy('avatar', ''); - avatarProxy.clearCachedImage(user.avatar); - } - - user.avatar = avatar; - } + user.avatar = `/avatarproxy/${account.User.Id}`; user.jellyfinUsername = account.User.Name; if (user.username === account.User.Name) { @@ -459,12 +427,7 @@ authRoutes.post('/jellyfin', async (req, res, next) => { jellyfinUserId: account.User.Id, jellyfinDeviceId: deviceId, permissions: settings.main.defaultPermissions, - avatar: account.User.PrimaryImageTag - ? `/Users/${account.User.Id}/Images/Primary/?tag=${account.User.PrimaryImageTag}&quality=90` - : gravatarUrl(body.email || account.User.Name, { - default: 'mm', - size: 200, - }), + avatar: `/avatarproxy/${account.User.Id}`, userType: settings.main.mediaServerType === MediaServerType.JELLYFIN ? UserType.JELLYFIN diff --git a/server/routes/avatarproxy.ts b/server/routes/avatarproxy.ts index e6f6f3b54..2d72e2f19 100644 --- a/server/routes/avatarproxy.ts +++ b/server/routes/avatarproxy.ts @@ -1,21 +1,39 @@ import { MediaServerType } from '@server/constants/server'; +import { getRepository } from '@server/datasource'; +import { User } from '@server/entity/User'; import ImageProxy from '@server/lib/imageproxy'; import { getSettings } from '@server/lib/settings'; import logger from '@server/logger'; +import { getAppVersion } from '@server/utils/appVersion'; import { getHostname } from '@server/utils/getHostname'; import { Router } from 'express'; +import gravatarUrl from 'gravatar-url'; const router = Router(); -const avatarImageProxy = new ImageProxy('avatar', ''); -// Proxy avatar images -router.get('/*', async (req, res) => { - let imagePath = ''; +let _avatarImageProxy: ImageProxy | null = null; +async function initAvatarImageProxy() { + if (!_avatarImageProxy) { + const userRepository = getRepository(User); + const admin = await userRepository.findOne({ + where: { id: 1 }, + select: ['id', 'jellyfinUserId', 'jellyfinDeviceId'], + order: { id: 'ASC' }, + }); + const deviceId = admin?.jellyfinDeviceId; + const authToken = getSettings().jellyfin.apiKey; + _avatarImageProxy = new ImageProxy('avatar', '', { + headers: { + 'X-Emby-Authorization': `MediaBrowser Client="Jellyseerr", Device="Jellyseerr", DeviceId="${deviceId}", Version="${getAppVersion()}", Token="${authToken}"`, + }, + }); + } + return _avatarImageProxy; +} + +router.get('/:jellyfinUserId', async (req, res) => { try { - const jellyfinAvatar = req.url.match( - /(\/Users\/\w+\/Images\/Primary\/?\?tag=\w+&quality=90)$/ - )?.[1]; - if (!jellyfinAvatar) { + if (!req.params.jellyfinUserId.match(/^[a-f0-9]{32}$/)) { const mediaServerType = getSettings().main.mediaServerType; throw new Error( `Provided URL is not ${ @@ -26,10 +44,28 @@ router.get('/*', async (req, res) => { ); } - const imageUrl = new URL(jellyfinAvatar, getHostname()); - imagePath = imageUrl.toString(); + const avatarImageCache = await initAvatarImageProxy(); - const imageData = await avatarImageProxy.getImage(imagePath); + const user = await getRepository(User).findOne({ + where: { jellyfinUserId: req.params.jellyfinUserId }, + }); + + const fallbackUrl = gravatarUrl(user?.email || 'none', { + default: 'mm', + size: 200, + }); + const jellyfinAvatarUrl = `${getHostname()}/UserImage?UserId=${ + req.params.jellyfinUserId + }`; + let imageData = await avatarImageCache.getImage( + jellyfinAvatarUrl, + fallbackUrl + ); + + if (imageData.meta.extension === 'json') { + // this is a 404 + imageData = await avatarImageCache.getImage(fallbackUrl); + } res.writeHead(200, { 'Content-Type': `image/${imageData.meta.extension}`, @@ -42,7 +78,6 @@ router.get('/*', async (req, res) => { res.end(imageData.imageBuffer); } catch (e) { logger.error('Failed to proxy avatar image', { - imagePath, errorMessage: e.message, }); } diff --git a/server/routes/settings/index.ts b/server/routes/settings/index.ts index 3d6b6b0d3..c5a070d2d 100644 --- a/server/routes/settings/index.ts +++ b/server/routes/settings/index.ts @@ -32,7 +32,6 @@ import { getHostname } from '@server/utils/getHostname'; import { Router } from 'express'; import rateLimit from 'express-rate-limit'; import fs from 'fs'; -import gravatarUrl from 'gravatar-url'; import { escapeRegExp, merge, omit, set, sortBy } from 'lodash'; import { rescheduleJob } from 'node-schedule'; import path from 'path'; @@ -395,9 +394,7 @@ settingsRoutes.get('/jellyfin/users', async (req, res) => { const users = resp.users.map((user) => ({ username: user.Name, id: user.Id, - thumb: user.PrimaryImageTag - ? `/Users/${user.Id}/Images/Primary/?tag=${user.PrimaryImageTag}&quality=90` - : gravatarUrl(user.Name, { default: 'mm', size: 200 }), + thumb: `/avatarproxy/${user.Id}`, email: user.Name, })); diff --git a/server/routes/user/index.ts b/server/routes/user/index.ts index 83ad0910b..2a29c0374 100644 --- a/server/routes/user/index.ts +++ b/server/routes/user/index.ts @@ -539,12 +539,7 @@ router.post( ).toString('base64'), email: jellyfinUser?.Name, permissions: settings.main.defaultPermissions, - avatar: jellyfinUser?.PrimaryImageTag - ? `/Users/${jellyfinUser.Id}/Images/Primary/?tag=${jellyfinUser.PrimaryImageTag}&quality=90` - : gravatarUrl(jellyfinUser?.Name ?? '', { - default: 'mm', - size: 200, - }), + avatar: `/avatarproxy/${jellyfinUser?.Id}`, userType: settings.main.mediaServerType === MediaServerType.JELLYFIN ? UserType.JELLYFIN diff --git a/src/components/Common/CachedImage/index.tsx b/src/components/Common/CachedImage/index.tsx index b01a47bb4..a6d2fb001 100644 --- a/src/components/Common/CachedImage/index.tsx +++ b/src/components/Common/CachedImage/index.tsx @@ -25,11 +25,8 @@ const CachedImage = ({ src, type, ...props }: CachedImageProps) => { ? src.replace(/^https:\/\/image\.tmdb\.org\//, '/imageproxy/') : src; } else if (type === 'avatar') { - // jellyfin avatar (in any) - const jellyfinAvatar = src.match( - /(\/Users\/\w+\/Images\/Primary\/?\?tag=\w+&quality=90)$/ - )?.[1]; - imageUrl = jellyfinAvatar ? `/avatarproxy` + jellyfinAvatar : src; + // jellyfin avatar (if any) + imageUrl = src; } else { return null; } From 326001c3ecc92dc730f327130a71e797882a62b9 Mon Sep 17 00:00:00 2001 From: Gauthier Date: Thu, 24 Oct 2024 18:12:42 +0200 Subject: [PATCH 3/9] feat: add more logs to migrations and create a settings backup (#1036) * feat: add more logs to migrations and create a settings backup * fix: avoid backup to be replaced at next startup * fix: resolve review comments * fix: try to fix CodeQL warnings --- .gitignore | 1 + server/lib/settings/migrator.ts | 54 +++++++++++++++++++++++++++------ 2 files changed, 45 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 9a8925ab0..c417acb09 100644 --- a/.gitignore +++ b/.gitignore @@ -34,6 +34,7 @@ yarn-error.log* # database config/db/*.sqlite3* config/settings.json +config/settings.old.json # logs config/logs/*.log* diff --git a/server/lib/settings/migrator.ts b/server/lib/settings/migrator.ts index 002e5516d..6f61e5082 100644 --- a/server/lib/settings/migrator.ts +++ b/server/lib/settings/migrator.ts @@ -1,7 +1,7 @@ /* eslint-disable no-console */ import type { AllSettings } from '@server/lib/settings'; import logger from '@server/logger'; -import fs from 'fs'; +import fs from 'fs/promises'; import path from 'path'; const migrationsDir = path.join(__dirname, 'migrations'); @@ -10,19 +10,46 @@ export const runMigrations = async ( settings: AllSettings, SETTINGS_PATH: string ): Promise => { - const migrations = fs - .readdirSync(migrationsDir) - .filter((file) => file.endsWith('.js') || file.endsWith('.ts')) - // eslint-disable-next-line @typescript-eslint/no-var-requires - .map((file) => require(path.join(migrationsDir, file)).default); - let migrated = settings; try { + // we read old backup and create a backup of currents settings + const BACKUP_PATH = SETTINGS_PATH.replace('.json', '.old.json'); + let oldBackup: Buffer | null = null; + try { + oldBackup = await fs.readFile(BACKUP_PATH); + } catch { + /* empty */ + } + await fs.writeFile(BACKUP_PATH, JSON.stringify(settings, undefined, ' ')); + + const migrations = (await fs.readdir(migrationsDir)).filter( + (file) => file.endsWith('.js') || file.endsWith('.ts') + ); + const settingsBefore = JSON.stringify(migrated); for (const migration of migrations) { - migrated = await migration(migrated); + try { + logger.debug(`Checking migration '${migration}'...`, { + label: 'Settings Migrator', + }); + const { default: migrationFn } = await import( + path.join(migrationsDir, migration) + ); + const newSettings = await migrationFn(migrated); + if (JSON.stringify(migrated) !== JSON.stringify(newSettings)) { + logger.debug(`Migration '${migration}' has been applied.`, { + label: 'Settings Migrator', + }); + } + migrated = newSettings; + } catch (e) { + logger.error(`Error while running migration '${migration}'`, { + label: 'Settings Migrator', + }); + throw e; + } } const settingsAfter = JSON.stringify(migrated); @@ -30,12 +57,19 @@ export const runMigrations = async ( if (settingsBefore !== settingsAfter) { // a migration occured // we check that the new config will be saved - fs.writeFileSync(SETTINGS_PATH, JSON.stringify(migrated, undefined, ' ')); - const fileSaved = JSON.parse(fs.readFileSync(SETTINGS_PATH, 'utf-8')); + await fs.writeFile( + SETTINGS_PATH, + JSON.stringify(migrated, undefined, ' ') + ); + const fileSaved = JSON.parse(await fs.readFile(SETTINGS_PATH, 'utf-8')); if (JSON.stringify(fileSaved) !== settingsAfter) { // something went wrong while saving file throw new Error('Unable to save settings after migration.'); } + } else if (oldBackup) { + // no migration occured + // we save the old backup (to avoid settings.json and settings.old.json being the same) + await fs.writeFile(BACKUP_PATH, oldBackup.toString()); } } catch (e) { logger.error( From f2b63156d1d4aa903eb261d2c80c059c39d9091b Mon Sep 17 00:00:00 2001 From: Gauthier Date: Thu, 24 Oct 2024 18:13:11 +0200 Subject: [PATCH 4/9] feat: add a warning if permissions are missing from config folder (#1030) --- overseerr-api.yml | 3 +++ server/index.ts | 7 +++++++ server/routes/index.ts | 7 ++++++- server/utils/appDataVolume.ts | 11 ++++++++++- 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/overseerr-api.yml b/overseerr-api.yml index 96a4520a7..ef3ccf8b3 100644 --- a/overseerr-api.yml +++ b/overseerr-api.yml @@ -1988,6 +1988,9 @@ paths: appDataPath: type: string example: /app/config + appDataPermissions: + type: boolean + example: true /settings/main: get: summary: Get main settings diff --git a/server/index.ts b/server/index.ts index 965903618..092b93fef 100644 --- a/server/index.ts +++ b/server/index.ts @@ -21,6 +21,7 @@ import clearCookies from '@server/middleware/clearcookies'; import routes from '@server/routes'; import avatarproxy from '@server/routes/avatarproxy'; import imageproxy from '@server/routes/imageproxy'; +import { appDataPermissions } from '@server/utils/appDataVolume'; import { getAppVersion } from '@server/utils/appVersion'; import restartFlag from '@server/utils/restartFlag'; import { getClientIp } from '@supercharge/request-ip'; @@ -51,6 +52,12 @@ const dev = process.env.NODE_ENV !== 'production'; const app = next({ dev }); const handle = app.getRequestHandler(); +if (!appDataPermissions()) { + logger.error( + 'Something went wrong while checking config folder! Please ensure the config folder is set up properly.\nhttps://docs.jellyseerr.dev/getting-started' + ); +} + app .prepare() .then(async () => { diff --git a/server/routes/index.ts b/server/routes/index.ts index c7c8389e0..120e2e86b 100644 --- a/server/routes/index.ts +++ b/server/routes/index.ts @@ -17,7 +17,11 @@ import { mapProductionCompany } from '@server/models/Movie'; import { mapNetwork } from '@server/models/Tv'; import settingsRoutes from '@server/routes/settings'; import watchlistRoutes from '@server/routes/watchlist'; -import { appDataPath, appDataStatus } from '@server/utils/appDataVolume'; +import { + appDataPath, + appDataPermissions, + appDataStatus, +} from '@server/utils/appDataVolume'; import { getAppVersion, getCommitTag } from '@server/utils/appVersion'; import restartFlag from '@server/utils/restartFlag'; import { isPerson } from '@server/utils/typeHelpers'; @@ -93,6 +97,7 @@ router.get('/status/appdata', (_req, res) => { return res.status(200).json({ appData: appDataStatus(), appDataPath: appDataPath(), + appDataPermissions: appDataPermissions(), }); }); diff --git a/server/utils/appDataVolume.ts b/server/utils/appDataVolume.ts index 73c80b2c5..837f7f669 100644 --- a/server/utils/appDataVolume.ts +++ b/server/utils/appDataVolume.ts @@ -1,4 +1,4 @@ -import { existsSync } from 'fs'; +import { accessSync, existsSync } from 'fs'; import path from 'path'; const CONFIG_PATH = process.env.CONFIG_DIRECTORY @@ -14,3 +14,12 @@ export const appDataStatus = (): boolean => { export const appDataPath = (): string => { return CONFIG_PATH; }; + +export const appDataPermissions = (): boolean => { + try { + accessSync(CONFIG_PATH); + return true; + } catch (err) { + return false; + } +}; From d331798b28a7bd32a27fc0ccbad2354be2e15b02 Mon Sep 17 00:00:00 2001 From: Gauthier Date: Thu, 24 Oct 2024 18:34:01 +0200 Subject: [PATCH 5/9] fix: remove language profiles dropdown for Sonarr v4 (#1000) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently, the language profiles removed with Sonarr v4 are still available for compatibility reasons. However, Jellyseerr still queries and displays language profiles (marking them as “Deprecated”). This PR hides and does not query language profiles unless Sonarr v3 is used. fix #207 --- server/routes/service.ts | 6 +- server/routes/settings/sonarr.ts | 11 +- src/components/Settings/SonarrModal/index.tsx | 206 +++++++++--------- 3 files changed, 118 insertions(+), 105 deletions(-) diff --git a/server/routes/service.ts b/server/routes/service.ts index 083e1eb57..8f6c92b0d 100644 --- a/server/routes/service.ts +++ b/server/routes/service.ts @@ -123,9 +123,13 @@ serviceRoutes.get<{ sonarrId: string }>( }); try { + const systemStatus = await sonarr.getSystemStatus(); + const sonarrMajorVersion = Number(systemStatus.version.split('.')[0]); + const profiles = await sonarr.getProfiles(); const rootFolders = await sonarr.getRootFolders(); - const languageProfiles = await sonarr.getLanguageProfiles(); + const languageProfiles = + sonarrMajorVersion <= 3 ? await sonarr.getLanguageProfiles() : null; const tags = await sonarr.getTags(); return res.status(200).json({ diff --git a/server/routes/settings/sonarr.ts b/server/routes/settings/sonarr.ts index 358d07002..8c74fa20a 100644 --- a/server/routes/settings/sonarr.ts +++ b/server/routes/settings/sonarr.ts @@ -43,13 +43,14 @@ sonarrRoutes.post('/test', async (req, res, next) => { url: SonarrAPI.buildUrl(req.body, '/api/v3'), }); - const urlBase = await sonarr - .getSystemStatus() - .then((value) => value.urlBase) - .catch(() => req.body.baseUrl); + const systemStatus = await sonarr.getSystemStatus(); + const sonarrMajorVersion = Number(systemStatus.version.split('.')[0]); + + const urlBase = systemStatus.urlBase; const profiles = await sonarr.getProfiles(); const folders = await sonarr.getRootFolders(); - const languageProfiles = await sonarr.getLanguageProfiles(); + const languageProfiles = + sonarrMajorVersion <= 3 ? await sonarr.getLanguageProfiles() : null; const tags = await sonarr.getTags(); return res.status(200).json({ diff --git a/src/components/Settings/SonarrModal/index.tsx b/src/components/Settings/SonarrModal/index.tsx index ed6d1f564..f2bcd4dbc 100644 --- a/src/components/Settings/SonarrModal/index.tsx +++ b/src/components/Settings/SonarrModal/index.tsx @@ -86,10 +86,12 @@ interface TestResponse { id: number; path: string; }[]; - languageProfiles: { - id: number; - name: string; - }[]; + languageProfiles: + | { + id: number; + name: string; + }[] + | null; tags: { id: number; label: string; @@ -112,7 +114,7 @@ const SonarrModal = ({ onClose, sonarr, onSave }: SonarrModalProps) => { const [testResponse, setTestResponse] = useState({ profiles: [], rootFolders: [], - languageProfiles: [], + languageProfiles: null, tags: [], }); const SonarrSettingsSchema = Yup.object().shape({ @@ -137,9 +139,11 @@ const SonarrModal = ({ onClose, sonarr, onSave }: SonarrModalProps) => { activeProfileId: Yup.string().required( intl.formatMessage(messages.validationProfileRequired) ), - activeLanguageProfileId: Yup.number().required( - intl.formatMessage(messages.validationLanguageProfileRequired) - ), + activeLanguageProfileId: testResponse.languageProfiles + ? Yup.number().required( + intl.formatMessage(messages.validationLanguageProfileRequired) + ) + : Yup.number(), externalUrl: Yup.string() .url(intl.formatMessage(messages.validationApplicationUrl)) .test( @@ -658,54 +662,56 @@ const SonarrModal = ({ onClose, sonarr, onSave }: SonarrModalProps) => { )} -
- -
-
- - - {testResponse.languageProfiles.length > 0 && - testResponse.languageProfiles.map((language) => ( - - ))} - + {testResponse.languageProfiles && ( +
+ +
+
+ + + {testResponse.languageProfiles.length > 0 && + testResponse.languageProfiles.map((language) => ( + + ))} + +
+ {errors.activeLanguageProfileId && + touched.activeLanguageProfileId && ( +
+ {errors.activeLanguageProfileId} +
+ )}
- {errors.activeLanguageProfileId && - touched.activeLanguageProfileId && ( -
- {errors.activeLanguageProfileId} -
- )}
-
+ )}
-
- -
-
- - - {testResponse.languageProfiles.length > 0 && - testResponse.languageProfiles.map((language) => ( - - ))} - + {testResponse.languageProfiles && ( +
+ +
+
+ + + {testResponse.languageProfiles.length > 0 && + testResponse.languageProfiles.map((language) => ( + + ))} + +
+ {errors.activeAnimeLanguageProfileId && + touched.activeAnimeLanguageProfileId && ( +
+ {errors.activeAnimeLanguageProfileId} +
+ )}
- {errors.activeAnimeLanguageProfileId && - touched.activeAnimeLanguageProfileId && ( -
- {errors.activeAnimeLanguageProfileId} -
- )}
-
+ )}
+
+ +
+
+ +
+ {errors.httpProxy && + touched.httpProxy && + typeof errors.httpProxy === 'string' && ( +
{errors.httpProxy}
+ )} +
+
From f2ed101e522561dab8563b744d908ff036c957c5 Mon Sep 17 00:00:00 2001 From: Gauthier Date: Thu, 31 Oct 2024 15:51:57 +0100 Subject: [PATCH 7/9] fix: use fs/promises for settings (#1057) * fix: use fs/promises for settings This PR switches from synchronous operations with the 'fs' module to asynchronous operations with the 'fs/promises' module. It also corrects a small error with hostname migration. * fix: add missing merge function of default and current config * refactor: add more logs to migration --- server/api/jellyfin.ts | 2 +- server/api/plexapi.ts | 2 +- server/lib/scanners/plex/index.ts | 2 +- server/lib/settings/index.ts | 81 +++++++++---------- .../migrations/0001_migrate_hostname.ts | 11 +-- .../migrations/0002_migrate_apitokens.ts | 10 ++- server/lib/settings/migrator.ts | 47 ++++++----- server/routes/auth.ts | 4 +- server/routes/settings/index.ts | 26 +++--- server/routes/settings/notifications.ts | 40 ++++----- server/routes/settings/radarr.ts | 12 +-- server/routes/settings/sonarr.ts | 12 +-- 12 files changed, 128 insertions(+), 121 deletions(-) diff --git a/server/api/jellyfin.ts b/server/api/jellyfin.ts index f65503477..7b45cdaf7 100644 --- a/server/api/jellyfin.ts +++ b/server/api/jellyfin.ts @@ -410,7 +410,7 @@ class JellyfinAPI extends ExternalAPI { ).AccessToken; } catch (e) { logger.error( - `Something went wrong while creating an API key the Jellyfin server: ${e.message}`, + `Something went wrong while creating an API key from the Jellyfin server: ${e.message}`, { label: 'Jellyfin API' } ); diff --git a/server/api/plexapi.ts b/server/api/plexapi.ts index f6b8f3cb0..10d5d1d2a 100644 --- a/server/api/plexapi.ts +++ b/server/api/plexapi.ts @@ -180,7 +180,7 @@ class PlexAPI { settings.plex.libraries = []; } - settings.save(); + await settings.save(); } public async getLibraryContents( diff --git a/server/lib/scanners/plex/index.ts b/server/lib/scanners/plex/index.ts index f074872bb..f6049630c 100644 --- a/server/lib/scanners/plex/index.ts +++ b/server/lib/scanners/plex/index.ts @@ -129,7 +129,7 @@ class PlexScanner }); settings.plex.libraries = newLibraries; - settings.save(); + await settings.save(); } } else { for (const library of this.libraries) { diff --git a/server/lib/settings/index.ts b/server/lib/settings/index.ts index 360aeb29d..d0e6166d0 100644 --- a/server/lib/settings/index.ts +++ b/server/lib/settings/index.ts @@ -2,7 +2,7 @@ import { MediaServerType } from '@server/constants/server'; import { Permission } from '@server/lib/permissions'; import { runMigrations } from '@server/lib/settings/migrator'; import { randomUUID } from 'crypto'; -import fs from 'fs'; +import fs from 'fs/promises'; import { merge } from 'lodash'; import path from 'path'; import webpush from 'web-push'; @@ -481,10 +481,6 @@ class Settings { } get main(): MainSettings { - if (!this.data.main.apiKey) { - this.data.main.apiKey = this.generateApiKey(); - this.save(); - } return this.data.main; } @@ -586,29 +582,20 @@ class Settings { } get clientId(): string { - if (!this.data.clientId) { - this.data.clientId = randomUUID(); - this.save(); - } - return this.data.clientId; } get vapidPublic(): string { - this.generateVapidKeys(); - return this.data.vapidPublic; } get vapidPrivate(): string { - this.generateVapidKeys(); - return this.data.vapidPrivate; } - public regenerateApiKey(): MainSettings { + public async regenerateApiKey(): Promise { this.main.apiKey = this.generateApiKey(); - this.save(); + await this.save(); return this.main; } @@ -620,15 +607,6 @@ class Settings { } } - private generateVapidKeys(force = false): void { - if (!this.data.vapidPublic || !this.data.vapidPrivate || force) { - const vapidKeys = webpush.generateVAPIDKeys(); - this.data.vapidPrivate = vapidKeys.privateKey; - this.data.vapidPublic = vapidKeys.publicKey; - this.save(); - } - } - /** * Settings Load * @@ -643,30 +621,51 @@ class Settings { return this; } - if (!fs.existsSync(SETTINGS_PATH)) { - this.save(); + let data; + try { + data = await fs.readFile(SETTINGS_PATH, 'utf-8'); + } catch { + await this.save(); } - const data = fs.readFileSync(SETTINGS_PATH, 'utf-8'); if (data) { const parsedJson = JSON.parse(data); - this.data = await runMigrations(parsedJson, SETTINGS_PATH); - - this.data = merge(this.data, parsedJson); - - if (process.env.API_KEY) { - if (this.main.apiKey != process.env.API_KEY) { - this.main.apiKey = process.env.API_KEY; - } - } - - this.save(); + const migratedData = await runMigrations(parsedJson, SETTINGS_PATH); + this.data = merge(this.data, migratedData); } + + // generate keys and ids if it's missing + let change = false; + if (!this.data.main.apiKey) { + this.data.main.apiKey = this.generateApiKey(); + change = true; + } else if (process.env.API_KEY) { + if (this.main.apiKey != process.env.API_KEY) { + this.main.apiKey = process.env.API_KEY; + } + } + if (!this.data.clientId) { + this.data.clientId = randomUUID(); + change = true; + } + if (!this.data.vapidPublic || !this.data.vapidPrivate) { + const vapidKeys = webpush.generateVAPIDKeys(); + this.data.vapidPrivate = vapidKeys.privateKey; + this.data.vapidPublic = vapidKeys.publicKey; + change = true; + } + if (change) { + await this.save(); + } + return this; } - public save(): void { - fs.writeFileSync(SETTINGS_PATH, JSON.stringify(this.data, undefined, ' ')); + public async save(): Promise { + await fs.writeFile( + SETTINGS_PATH, + JSON.stringify(this.data, undefined, ' ') + ); } } diff --git a/server/lib/settings/migrations/0001_migrate_hostname.ts b/server/lib/settings/migrations/0001_migrate_hostname.ts index c514ac2db..ddc8211cf 100644 --- a/server/lib/settings/migrations/0001_migrate_hostname.ts +++ b/server/lib/settings/migrations/0001_migrate_hostname.ts @@ -1,15 +1,14 @@ import type { AllSettings } from '@server/lib/settings'; const migrateHostname = (settings: any): AllSettings => { - const oldJellyfinSettings = settings.jellyfin; - if (oldJellyfinSettings && oldJellyfinSettings.hostname) { - const { hostname } = oldJellyfinSettings; + if (settings.jellyfin?.hostname) { + const { hostname } = settings.jellyfin; const protocolMatch = hostname.match(/^(https?):\/\//i); const useSsl = protocolMatch && protocolMatch[1].toLowerCase() === 'https'; const remainingUrl = hostname.replace(/^(https?):\/\//i, ''); const urlMatch = remainingUrl.match(/^([^:]+)(:([0-9]+))?(\/.*)?$/); - delete oldJellyfinSettings.hostname; + delete settings.jellyfin.hostname; if (urlMatch) { const [, ip, , port, urlBase] = urlMatch; settings.jellyfin = { @@ -21,9 +20,7 @@ const migrateHostname = (settings: any): AllSettings => { }; } } - if (settings.jellyfin && settings.jellyfin.hostname) { - delete settings.jellyfin.hostname; - } + return settings; }; diff --git a/server/lib/settings/migrations/0002_migrate_apitokens.ts b/server/lib/settings/migrations/0002_migrate_apitokens.ts index 46340433b..0149c3e37 100644 --- a/server/lib/settings/migrations/0002_migrate_apitokens.ts +++ b/server/lib/settings/migrations/0002_migrate_apitokens.ts @@ -27,8 +27,14 @@ const migrateApiTokens = async (settings: any): Promise => { admin.jellyfinDeviceId ); jellyfinClient.setUserId(admin.jellyfinUserId ?? ''); - const apiKey = await jellyfinClient.createApiToken('Jellyseerr'); - settings.jellyfin.apiKey = apiKey; + try { + const apiKey = await jellyfinClient.createApiToken('Jellyseerr'); + settings.jellyfin.apiKey = apiKey; + } catch { + throw new Error( + "Failed to create Jellyfin API token from admin account. Please check your network configuration or edit your settings.json by adding an 'apiKey' field inside of the 'jellyfin' section to fix this issue." + ); + } } return settings; }; diff --git a/server/lib/settings/migrator.ts b/server/lib/settings/migrator.ts index 6f61e5082..801140000 100644 --- a/server/lib/settings/migrator.ts +++ b/server/lib/settings/migrator.ts @@ -1,4 +1,3 @@ -/* eslint-disable no-console */ import type { AllSettings } from '@server/lib/settings'; import logger from '@server/logger'; import fs from 'fs/promises'; @@ -15,9 +14,9 @@ export const runMigrations = async ( try { // we read old backup and create a backup of currents settings const BACKUP_PATH = SETTINGS_PATH.replace('.json', '.old.json'); - let oldBackup: Buffer | null = null; + let oldBackup: string | null = null; try { - oldBackup = await fs.readFile(BACKUP_PATH); + oldBackup = await fs.readFile(BACKUP_PATH, 'utf-8'); } catch { /* empty */ } @@ -37,7 +36,7 @@ export const runMigrations = async ( const { default: migrationFn } = await import( path.join(migrationsDir, migration) ); - const newSettings = await migrationFn(migrated); + const newSettings = await migrationFn(structuredClone(migrated)); if (JSON.stringify(migrated) !== JSON.stringify(newSettings)) { logger.debug(`Migration '${migration}' has been applied.`, { label: 'Settings Migrator', @@ -45,10 +44,20 @@ export const runMigrations = async ( } migrated = newSettings; } catch (e) { - logger.error(`Error while running migration '${migration}'`, { - label: 'Settings Migrator', - }); - throw e; + // we stop jellyseerr if the migration failed + logger.error( + `Error while running migration '${migration}': ${e.message}`, + { + label: 'Settings Migrator', + } + ); + logger.error( + 'A common cause for this error is a permission issue with your configuration folder, a network issue or a corrupted database.', + { + label: 'Settings Migrator', + } + ); + process.exit(); } } @@ -72,22 +81,18 @@ export const runMigrations = async ( await fs.writeFile(BACKUP_PATH, oldBackup.toString()); } } catch (e) { + // we stop jellyseerr if the migration failed logger.error( `Something went wrong while running settings migrations: ${e.message}`, - { label: 'Settings Migrator' } + { + label: 'Settings Migrator', + } ); - // we stop jellyseerr if the migration failed - console.log( - '====================================================================' - ); - console.log( - ' SOMETHING WENT WRONG WHILE RUNNING SETTINGS MIGRATIONS ' - ); - console.log( - ' Please check that your configuration folder is properly set up ' - ); - console.log( - '====================================================================' + logger.error( + 'A common cause for this issue is a permission error of your configuration folder.', + { + label: 'Settings Migrator', + } ); process.exit(); } diff --git a/server/routes/auth.ts b/server/routes/auth.ts index 70e674f97..d38ae2211 100644 --- a/server/routes/auth.ts +++ b/server/routes/auth.ts @@ -87,7 +87,7 @@ authRoutes.post('/plex', async (req, res, next) => { }); settings.main.mediaServerType = MediaServerType.PLEX; - settings.save(); + await settings.save(); startJobs(); await userRepository.save(user); @@ -366,7 +366,7 @@ authRoutes.post('/jellyfin', async (req, res, next) => { settings.jellyfin.urlBase = body.urlBase ?? ''; settings.jellyfin.useSsl = body.useSsl ?? false; settings.jellyfin.apiKey = apiKey; - settings.save(); + await settings.save(); startJobs(); await userRepository.save(user); diff --git a/server/routes/settings/index.ts b/server/routes/settings/index.ts index c5a070d2d..bc8c5ef7c 100644 --- a/server/routes/settings/index.ts +++ b/server/routes/settings/index.ts @@ -69,19 +69,19 @@ settingsRoutes.get('/main', (req, res, next) => { res.status(200).json(filteredMainSettings(req.user, settings.main)); }); -settingsRoutes.post('/main', (req, res) => { +settingsRoutes.post('/main', async (req, res) => { const settings = getSettings(); settings.main = merge(settings.main, req.body); - settings.save(); + await settings.save(); return res.status(200).json(settings.main); }); -settingsRoutes.post('/main/regenerate', (req, res, next) => { +settingsRoutes.post('/main/regenerate', async (req, res, next) => { const settings = getSettings(); - const main = settings.regenerateApiKey(); + const main = await settings.regenerateApiKey(); if (!req.user) { return next({ status: 500, message: 'User missing from request.' }); @@ -118,7 +118,7 @@ settingsRoutes.post('/plex', async (req, res, next) => { settings.plex.machineId = result.MediaContainer.machineIdentifier; settings.plex.name = result.MediaContainer.friendlyName; - settings.save(); + await settings.save(); } catch (e) { logger.error('Something went wrong testing Plex connection', { label: 'API', @@ -231,7 +231,7 @@ settingsRoutes.get('/plex/library', async (req, res) => { ...library, enabled: enabledLibraries.includes(library.id), })); - settings.save(); + await settings.save(); return res.status(200).json(settings.plex.libraries); }); @@ -282,7 +282,7 @@ settingsRoutes.post('/jellyfin', async (req, res, next) => { Object.assign(settings.jellyfin, req.body); settings.jellyfin.serverId = result.Id; settings.jellyfin.name = result.ServerName; - settings.save(); + await settings.save(); } catch (e) { if (e instanceof ApiError) { logger.error('Something went wrong testing Jellyfin connection', { @@ -370,7 +370,7 @@ settingsRoutes.get('/jellyfin/library', async (req, res, next) => { ...library, enabled: enabledLibraries.includes(library.id), })); - settings.save(); + await settings.save(); return res.status(200).json(settings.jellyfin.libraries); }); @@ -434,7 +434,7 @@ settingsRoutes.post('/tautulli', async (req, res, next) => { throw new Error('Tautulli version not supported'); } - settings.save(); + await settings.save(); } catch (e) { logger.error('Something went wrong testing Tautulli connection', { label: 'API', @@ -695,7 +695,7 @@ settingsRoutes.post<{ jobId: JobId }>( settingsRoutes.post<{ jobId: JobId }>( '/jobs/:jobId/schedule', - (req, res, next) => { + async (req, res, next) => { const scheduledJob = scheduledJobs.find( (job) => job.id === req.params.jobId ); @@ -709,7 +709,7 @@ settingsRoutes.post<{ jobId: JobId }>( if (result) { settings.jobs[scheduledJob.id].schedule = req.body.schedule; - settings.save(); + await settings.save(); scheduledJob.cronSchedule = req.body.schedule; @@ -766,11 +766,11 @@ settingsRoutes.post<{ cacheId: AvailableCacheIds }>( settingsRoutes.post( '/initialize', isAuthenticated(Permission.ADMIN), - (_req, res) => { + async (_req, res) => { const settings = getSettings(); settings.public.initialized = true; - settings.save(); + await settings.save(); return res.status(200).json(settings.public); } diff --git a/server/routes/settings/notifications.ts b/server/routes/settings/notifications.ts index be2fd89a8..5b2e1715b 100644 --- a/server/routes/settings/notifications.ts +++ b/server/routes/settings/notifications.ts @@ -31,11 +31,11 @@ notificationRoutes.get('/discord', (_req, res) => { res.status(200).json(settings.notifications.agents.discord); }); -notificationRoutes.post('/discord', (req, res) => { +notificationRoutes.post('/discord', async (req, res) => { const settings = getSettings(); settings.notifications.agents.discord = req.body; - settings.save(); + await settings.save(); res.status(200).json(settings.notifications.agents.discord); }); @@ -65,11 +65,11 @@ notificationRoutes.get('/slack', (_req, res) => { res.status(200).json(settings.notifications.agents.slack); }); -notificationRoutes.post('/slack', (req, res) => { +notificationRoutes.post('/slack', async (req, res) => { const settings = getSettings(); settings.notifications.agents.slack = req.body; - settings.save(); + await settings.save(); res.status(200).json(settings.notifications.agents.slack); }); @@ -99,11 +99,11 @@ notificationRoutes.get('/telegram', (_req, res) => { res.status(200).json(settings.notifications.agents.telegram); }); -notificationRoutes.post('/telegram', (req, res) => { +notificationRoutes.post('/telegram', async (req, res) => { const settings = getSettings(); settings.notifications.agents.telegram = req.body; - settings.save(); + await settings.save(); res.status(200).json(settings.notifications.agents.telegram); }); @@ -133,11 +133,11 @@ notificationRoutes.get('/pushbullet', (_req, res) => { res.status(200).json(settings.notifications.agents.pushbullet); }); -notificationRoutes.post('/pushbullet', (req, res) => { +notificationRoutes.post('/pushbullet', async (req, res) => { const settings = getSettings(); settings.notifications.agents.pushbullet = req.body; - settings.save(); + await settings.save(); res.status(200).json(settings.notifications.agents.pushbullet); }); @@ -167,11 +167,11 @@ notificationRoutes.get('/pushover', (_req, res) => { res.status(200).json(settings.notifications.agents.pushover); }); -notificationRoutes.post('/pushover', (req, res) => { +notificationRoutes.post('/pushover', async (req, res) => { const settings = getSettings(); settings.notifications.agents.pushover = req.body; - settings.save(); + await settings.save(); res.status(200).json(settings.notifications.agents.pushover); }); @@ -201,11 +201,11 @@ notificationRoutes.get('/email', (_req, res) => { res.status(200).json(settings.notifications.agents.email); }); -notificationRoutes.post('/email', (req, res) => { +notificationRoutes.post('/email', async (req, res) => { const settings = getSettings(); settings.notifications.agents.email = req.body; - settings.save(); + await settings.save(); res.status(200).json(settings.notifications.agents.email); }); @@ -235,11 +235,11 @@ notificationRoutes.get('/webpush', (_req, res) => { res.status(200).json(settings.notifications.agents.webpush); }); -notificationRoutes.post('/webpush', (req, res) => { +notificationRoutes.post('/webpush', async (req, res) => { const settings = getSettings(); settings.notifications.agents.webpush = req.body; - settings.save(); + await settings.save(); res.status(200).json(settings.notifications.agents.webpush); }); @@ -284,7 +284,7 @@ notificationRoutes.get('/webhook', (_req, res) => { res.status(200).json(response); }); -notificationRoutes.post('/webhook', (req, res, next) => { +notificationRoutes.post('/webhook', async (req, res, next) => { const settings = getSettings(); try { JSON.parse(req.body.options.jsonPayload); @@ -300,7 +300,7 @@ notificationRoutes.post('/webhook', (req, res, next) => { authHeader: req.body.options.authHeader, }, }; - settings.save(); + await settings.save(); res.status(200).json(settings.notifications.agents.webhook); } catch (e) { @@ -351,11 +351,11 @@ notificationRoutes.get('/lunasea', (_req, res) => { res.status(200).json(settings.notifications.agents.lunasea); }); -notificationRoutes.post('/lunasea', (req, res) => { +notificationRoutes.post('/lunasea', async (req, res) => { const settings = getSettings(); settings.notifications.agents.lunasea = req.body; - settings.save(); + await settings.save(); res.status(200).json(settings.notifications.agents.lunasea); }); @@ -385,11 +385,11 @@ notificationRoutes.get('/gotify', (_req, res) => { res.status(200).json(settings.notifications.agents.gotify); }); -notificationRoutes.post('/gotify', (req, res) => { +notificationRoutes.post('/gotify', async (req, res) => { const settings = getSettings(); settings.notifications.agents.gotify = req.body; - settings.save(); + await settings.save(); res.status(200).json(settings.notifications.agents.gotify); }); diff --git a/server/routes/settings/radarr.ts b/server/routes/settings/radarr.ts index c2b0a6f52..efa586658 100644 --- a/server/routes/settings/radarr.ts +++ b/server/routes/settings/radarr.ts @@ -12,7 +12,7 @@ radarrRoutes.get('/', (_req, res) => { res.status(200).json(settings.radarr); }); -radarrRoutes.post('/', (req, res) => { +radarrRoutes.post('/', async (req, res) => { const settings = getSettings(); const newRadarr = req.body as RadarrSettings; @@ -31,7 +31,7 @@ radarrRoutes.post('/', (req, res) => { } settings.radarr = [...settings.radarr, newRadarr]; - settings.save(); + await settings.save(); return res.status(201).json(newRadarr); }); @@ -76,7 +76,7 @@ radarrRoutes.post< radarrRoutes.put<{ id: string }, RadarrSettings, RadarrSettings>( '/:id', - (req, res, next) => { + async (req, res, next) => { const settings = getSettings(); const radarrIndex = settings.radarr.findIndex( @@ -102,7 +102,7 @@ radarrRoutes.put<{ id: string }, RadarrSettings, RadarrSettings>( ...req.body, id: Number(req.params.id), } as RadarrSettings; - settings.save(); + await settings.save(); return res.status(200).json(settings.radarr[radarrIndex]); } @@ -134,7 +134,7 @@ radarrRoutes.get<{ id: string }>('/:id/profiles', async (req, res, next) => { ); }); -radarrRoutes.delete<{ id: string }>('/:id', (req, res, next) => { +radarrRoutes.delete<{ id: string }>('/:id', async (req, res, next) => { const settings = getSettings(); const radarrIndex = settings.radarr.findIndex( @@ -146,7 +146,7 @@ radarrRoutes.delete<{ id: string }>('/:id', (req, res, next) => { } const removed = settings.radarr.splice(radarrIndex, 1); - settings.save(); + await settings.save(); return res.status(200).json(removed[0]); }); diff --git a/server/routes/settings/sonarr.ts b/server/routes/settings/sonarr.ts index 8c74fa20a..84bf4d793 100644 --- a/server/routes/settings/sonarr.ts +++ b/server/routes/settings/sonarr.ts @@ -12,7 +12,7 @@ sonarrRoutes.get('/', (_req, res) => { res.status(200).json(settings.sonarr); }); -sonarrRoutes.post('/', (req, res) => { +sonarrRoutes.post('/', async (req, res) => { const settings = getSettings(); const newSonarr = req.body as SonarrSettings; @@ -31,7 +31,7 @@ sonarrRoutes.post('/', (req, res) => { } settings.sonarr = [...settings.sonarr, newSonarr]; - settings.save(); + await settings.save(); return res.status(201).json(newSonarr); }); @@ -73,7 +73,7 @@ sonarrRoutes.post('/test', async (req, res, next) => { } }); -sonarrRoutes.put<{ id: string }>('/:id', (req, res) => { +sonarrRoutes.put<{ id: string }>('/:id', async (req, res) => { const settings = getSettings(); const sonarrIndex = settings.sonarr.findIndex( @@ -101,12 +101,12 @@ sonarrRoutes.put<{ id: string }>('/:id', (req, res) => { ...req.body, id: Number(req.params.id), } as SonarrSettings; - settings.save(); + await settings.save(); return res.status(200).json(settings.sonarr[sonarrIndex]); }); -sonarrRoutes.delete<{ id: string }>('/:id', (req, res) => { +sonarrRoutes.delete<{ id: string }>('/:id', async (req, res) => { const settings = getSettings(); const sonarrIndex = settings.sonarr.findIndex( @@ -120,7 +120,7 @@ sonarrRoutes.delete<{ id: string }>('/:id', (req, res) => { } const removed = settings.sonarr.splice(sonarrIndex, 1); - settings.save(); + await settings.save(); return res.status(200).json(removed[0]); }); From ca838a00fa4acb0ccdfbac8be4cf7fde493346f7 Mon Sep 17 00:00:00 2001 From: Gauthier Date: Thu, 31 Oct 2024 16:10:45 +0100 Subject: [PATCH 8/9] feat: add bypass list, bypass local addresses and username/password to proxy setting (#1059) * fix: use fs/promises for settings This PR switches from synchronous operations with the 'fs' module to asynchronous operations with the 'fs/promises' module. It also corrects a small error with hostname migration. * fix: add missing merge function of default and current config * feat: add bypass list, bypass local addresses and username/password to proxy setting This PR adds more options to the proxy setting, like username/password authentication, bypass list of domains and bypass local addresses. The UX is taken from *arrs. * fix: add error handling for proxy creating * fix: remove logs --- server/index.ts | 6 +- server/lib/settings/index.ts | 24 +- server/utils/customProxyAgent.ts | 111 +++++++++ server/utils/restartFlag.ts | 2 +- .../Settings/SettingsMain/index.tsx | 216 ++++++++++++++++-- 5 files changed, 333 insertions(+), 26 deletions(-) create mode 100644 server/utils/customProxyAgent.ts diff --git a/server/index.ts b/server/index.ts index f37d7522c..cd65d566a 100644 --- a/server/index.ts +++ b/server/index.ts @@ -23,6 +23,7 @@ import avatarproxy from '@server/routes/avatarproxy'; import imageproxy from '@server/routes/imageproxy'; import { appDataPermissions } from '@server/utils/appDataVolume'; import { getAppVersion } from '@server/utils/appVersion'; +import createCustomProxyAgent from '@server/utils/customProxyAgent'; import restartFlag from '@server/utils/restartFlag'; import { getClientIp } from '@supercharge/request-ip'; import { TypeormStore } from 'connect-typeorm/out'; @@ -38,7 +39,6 @@ import dns from 'node:dns'; import net from 'node:net'; import path from 'path'; import swaggerUi from 'swagger-ui-express'; -import { ProxyAgent, setGlobalDispatcher } from 'undici'; import YAML from 'yamljs'; if (process.env.forceIpv4First === 'true') { @@ -76,8 +76,8 @@ app restartFlag.initializeSettings(settings.main); // Register HTTP proxy - if (settings.main.httpProxy) { - setGlobalDispatcher(new ProxyAgent(settings.main.httpProxy)); + if (settings.main.proxy.enabled) { + await createCustomProxyAgent(settings.main.proxy); } // Migrate library types diff --git a/server/lib/settings/index.ts b/server/lib/settings/index.ts index d0e6166d0..29447f534 100644 --- a/server/lib/settings/index.ts +++ b/server/lib/settings/index.ts @@ -99,6 +99,17 @@ interface Quota { quotaDays?: number; } +export interface ProxySettings { + enabled: boolean; + hostname: string; + port: number; + useSsl: boolean; + user: string; + password: string; + bypassFilter: string; + bypassLocalAddresses: boolean; +} + export interface MainSettings { apiKey: string; applicationTitle: string; @@ -119,7 +130,7 @@ export interface MainSettings { mediaServerType: number; partialRequestsEnabled: boolean; locale: string; - httpProxy: string; + proxy: ProxySettings; } interface PublicSettings { @@ -326,7 +337,16 @@ class Settings { mediaServerType: MediaServerType.NOT_CONFIGURED, partialRequestsEnabled: true, locale: 'en', - httpProxy: '', + proxy: { + enabled: false, + hostname: '', + port: 8080, + useSsl: false, + user: '', + password: '', + bypassFilter: '', + bypassLocalAddresses: true, + }, }, plex: { name: '', diff --git a/server/utils/customProxyAgent.ts b/server/utils/customProxyAgent.ts new file mode 100644 index 000000000..3b6223685 --- /dev/null +++ b/server/utils/customProxyAgent.ts @@ -0,0 +1,111 @@ +import type { ProxySettings } from '@server/lib/settings'; +import logger from '@server/logger'; +import type { Dispatcher } from 'undici'; +import { Agent, ProxyAgent, setGlobalDispatcher } from 'undici'; + +export default async function createCustomProxyAgent( + proxySettings: ProxySettings +) { + const defaultAgent = new Agent(); + + const skipUrl = (url: string) => { + const hostname = new URL(url).hostname; + + if (proxySettings.bypassLocalAddresses && isLocalAddress(hostname)) { + return true; + } + + for (const address of proxySettings.bypassFilter.split(',')) { + const trimmedAddress = address.trim(); + if (!trimmedAddress) { + continue; + } + + if (trimmedAddress.startsWith('*')) { + const domain = trimmedAddress.slice(1); + if (hostname.endsWith(domain)) { + return true; + } + } else if (hostname === trimmedAddress) { + return true; + } + } + + return false; + }; + + const noProxyInterceptor = ( + dispatch: Dispatcher['dispatch'] + ): Dispatcher['dispatch'] => { + return (opts, handler) => { + const url = opts.origin?.toString(); + return url && skipUrl(url) + ? defaultAgent.dispatch(opts, handler) + : dispatch(opts, handler); + }; + }; + + const token = + proxySettings.user && proxySettings.password + ? `Basic ${Buffer.from( + `${proxySettings.user}:${proxySettings.password}` + ).toString('base64')}` + : undefined; + + try { + const proxyAgent = new ProxyAgent({ + uri: + (proxySettings.useSsl ? 'https://' : 'http://') + + proxySettings.hostname + + ':' + + proxySettings.port, + token, + interceptors: { + Client: [noProxyInterceptor], + }, + }); + + setGlobalDispatcher(proxyAgent); + } catch (e) { + logger.error('Failed to connect to the proxy: ' + e.message, { + label: 'Proxy', + }); + setGlobalDispatcher(defaultAgent); + return; + } + + try { + const res = await fetch('https://www.google.com', { method: 'HEAD' }); + if (res.ok) { + logger.debug('HTTP(S) proxy connected successfully', { label: 'Proxy' }); + } else { + logger.error('Proxy responded, but with a non-OK status: ' + res.status, { + label: 'Proxy', + }); + setGlobalDispatcher(defaultAgent); + } + } catch (e) { + logger.error( + 'Failed to connect to the proxy: ' + e.message + ': ' + e.cause, + { label: 'Proxy' } + ); + setGlobalDispatcher(defaultAgent); + } +} + +function isLocalAddress(hostname: string) { + if (hostname === 'localhost' || hostname === '127.0.0.1') { + return true; + } + + const privateIpRanges = [ + /^10\./, // 10.x.x.x + /^172\.(1[6-9]|2[0-9]|3[0-1])\./, // 172.16.x.x - 172.31.x.x + /^192\.168\./, // 192.168.x.x + ]; + if (privateIpRanges.some((regex) => regex.test(hostname))) { + return true; + } + + return false; +} diff --git a/server/utils/restartFlag.ts b/server/utils/restartFlag.ts index bb5f011d5..18d03ea64 100644 --- a/server/utils/restartFlag.ts +++ b/server/utils/restartFlag.ts @@ -14,7 +14,7 @@ class RestartFlag { return ( this.settings.csrfProtection !== settings.csrfProtection || this.settings.trustProxy !== settings.trustProxy || - this.settings.httpProxy !== settings.httpProxy + this.settings.proxy.enabled !== settings.proxy.enabled ); } } diff --git a/src/components/Settings/SettingsMain/index.tsx b/src/components/Settings/SettingsMain/index.tsx index b4fdea783..2d1e0219f 100644 --- a/src/components/Settings/SettingsMain/index.tsx +++ b/src/components/Settings/SettingsMain/index.tsx @@ -55,8 +55,17 @@ const messages = defineMessages('components.Settings.SettingsMain', { validationApplicationUrlTrailingSlash: 'URL must not end in a trailing slash', partialRequestsEnabled: 'Allow Partial Series Requests', locale: 'Display Language', - httpProxy: 'HTTP Proxy', - httpProxyTip: 'Tooltip to write', + proxyEnabled: 'HTTP(S) Proxy', + proxyHostname: 'Proxy Hostname', + proxyPort: 'Proxy Port', + proxySsl: 'Use SSL For Proxy', + proxyUser: 'Proxy Username', + proxyPassword: 'Proxy Password', + proxyBypassFilter: 'Proxy Ignored Addresses', + proxyBypassFilterTip: + "Use ',' as a separator, and '*.' as a wildcard for subdomains", + proxyBypassLocalAddresses: 'Bypass Proxy for Local Addresses', + validationProxyPort: 'You must provide a valid port', }); const SettingsMain = () => { @@ -84,9 +93,12 @@ const SettingsMain = () => { intl.formatMessage(messages.validationApplicationUrlTrailingSlash), (value) => !value || !value.endsWith('/') ), - httpProxy: Yup.string().url( - intl.formatMessage(messages.validationApplicationUrl) - ), + proxyPort: Yup.number().when('proxyEnabled', { + is: (proxyEnabled: boolean) => proxyEnabled, + then: Yup.number().required( + intl.formatMessage(messages.validationProxyPort) + ), + }), }); const regenerate = async () => { @@ -142,7 +154,14 @@ const SettingsMain = () => { partialRequestsEnabled: data?.partialRequestsEnabled, trustProxy: data?.trustProxy, cacheImages: data?.cacheImages, - httpProxy: data?.httpProxy, + proxyEnabled: data?.proxy?.enabled, + proxyHostname: data?.proxy?.hostname, + proxyPort: data?.proxy?.port, + proxySsl: data?.proxy?.useSsl, + proxyUser: data?.proxy?.user, + proxyPassword: data?.proxy?.password, + proxyBypassFilter: data?.proxy?.bypassFilter, + proxyBypassLocalAddresses: data?.proxy?.bypassLocalAddresses, }} enableReinitialize validationSchema={MainSettingsSchema} @@ -164,7 +183,16 @@ const SettingsMain = () => { partialRequestsEnabled: values.partialRequestsEnabled, trustProxy: values.trustProxy, cacheImages: values.cacheImages, - httpProxy: values.httpProxy, + proxy: { + enabled: values.proxyEnabled, + hostname: values.proxyHostname, + port: values.proxyPort, + useSsl: values.proxySsl, + user: values.proxyUser, + password: values.proxyPassword, + bypassFilter: values.proxyBypassFilter, + bypassLocalAddresses: values.proxyBypassLocalAddresses, + }, }), }); if (!res.ok) throw new Error(); @@ -445,27 +473,175 @@ const SettingsMain = () => {
-
+ {values.proxyEnabled && ( + <> +
+ +
+
+ +
+ {errors.proxyHostname && + touched.proxyHostname && + typeof errors.proxyHostname === 'string' && ( +
{errors.proxyHostname}
+ )} +
+
+
+ +
+
+ +
+ {errors.proxyPort && + touched.proxyPort && + typeof errors.proxyPort === 'string' && ( +
{errors.proxyPort}
+ )} +
+
+
+ +
+ { + setFieldValue('proxySsl', !values.proxySsl); + }} + /> +
+
+
+ +
+
+ +
+ {errors.proxyUser && + touched.proxyUser && + typeof errors.proxyUser === 'string' && ( +
{errors.proxyUser}
+ )} +
+
+
+ +
+
+ +
+ {errors.proxyPassword && + touched.proxyPassword && + typeof errors.proxyPassword === 'string' && ( +
{errors.proxyPassword}
+ )} +
+
+
+ +
+
+ +
+ {errors.proxyBypassFilter && + touched.proxyBypassFilter && + typeof errors.proxyBypassFilter === 'string' && ( +
+ {errors.proxyBypassFilter} +
+ )} +
+
+
+ +
+ { + setFieldValue( + 'proxyBypassLocalAddresses', + !values.proxyBypassLocalAddresses + ); + }} + /> +
+
+ + )}
From cf59102ef91fa0e907cc6369b0fe60b503c823ca Mon Sep 17 00:00:00 2001 From: Fallenbagel <98979876+Fallenbagel@users.noreply.github.com> Date: Sun, 3 Nov 2024 14:35:20 +0800 Subject: [PATCH 9/9] fix(externalapi): extract basic auth and pass it through header (#1062) This commit adds extraction of basic authentication credentials from the URL and then pass the credentials as the `Authorization` header. And then credentials are removed from the URL before being passed to fetch. This is done because fetch request cannot be constructed using a URL with credentials fix #1027 --- server/api/externalapi.ts | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/server/api/externalapi.ts b/server/api/externalapi.ts index 0dfddefc9..0dc1f967d 100644 --- a/server/api/externalapi.ts +++ b/server/api/externalapi.ts @@ -32,13 +32,27 @@ class ExternalAPI { this.fetch = fetch; } - this.baseUrl = baseUrl; - this.params = params; + const url = new URL(baseUrl); + this.defaultHeaders = { 'Content-Type': 'application/json', Accept: 'application/json', + ...((url.username || url.password) && { + Authorization: `Basic ${Buffer.from( + `${url.username}:${url.password}` + ).toString('base64')}`, + }), ...options.headers, }; + + if (url.username || url.password) { + url.username = ''; + url.password = ''; + baseUrl = url.toString(); + } + + this.baseUrl = baseUrl; + this.params = params; this.cache = options.nodeCache; }