Skip to content

Commit

Permalink
feat: proxy setting (Fallenbagel#1031)
Browse files Browse the repository at this point in the history
* feat: add a proxy option into settings

* feat: add a proxy option into settings

* fix: use undici proxy agent
  • Loading branch information
gauthier-th authored and thibodelanghe committed Dec 18, 2024
1 parent 40627ac commit fba2f00
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 40 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@
"sqlite3": "5.1.4",
"swagger-ui-express": "4.6.2",
"swr": "2.2.5",
"typeorm": "0.3.12",
"typeorm": "0.3.11",
"undici": "^6.20.1",
"web-push": "3.5.0",
"winston": "3.8.2",
"winston-daily-rotate-file": "4.7.1",
Expand Down
65 changes: 27 additions & 38 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ 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') {
Expand Down Expand Up @@ -74,6 +75,11 @@ app
const settings = await getSettings().load();
restartFlag.initializeSettings(settings.main);

// Register HTTP proxy
if (settings.main.httpProxy) {
setGlobalDispatcher(new ProxyAgent(settings.main.httpProxy));
}

// Migrate library types
if (
settings.plex.libraries.length > 1 &&
Expand Down
2 changes: 2 additions & 0 deletions server/lib/settings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export interface MainSettings {
mediaServerType: number;
partialRequestsEnabled: boolean;
locale: string;
httpProxy: string;
}

interface PublicSettings {
Expand Down Expand Up @@ -325,6 +326,7 @@ class Settings {
mediaServerType: MediaServerType.NOT_CONFIGURED,
partialRequestsEnabled: true,
locale: 'en',
httpProxy: '',
},
plex: {
name: '',
Expand Down
3 changes: 2 additions & 1 deletion server/utils/restartFlag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class RestartFlag {

return (
this.settings.csrfProtection !== settings.csrfProtection ||
this.settings.trustProxy !== settings.trustProxy
this.settings.trustProxy !== settings.trustProxy ||
this.settings.httpProxy !== settings.httpProxy
);
}
}
Expand Down
29 changes: 29 additions & 0 deletions src/components/Settings/SettingsMain/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ 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',
});

const SettingsMain = () => {
Expand Down Expand Up @@ -82,6 +84,9 @@ const SettingsMain = () => {
intl.formatMessage(messages.validationApplicationUrlTrailingSlash),
(value) => !value || !value.endsWith('/')
),
httpProxy: Yup.string().url(
intl.formatMessage(messages.validationApplicationUrl)
),
});

const regenerate = async () => {
Expand Down Expand Up @@ -137,6 +142,7 @@ const SettingsMain = () => {
partialRequestsEnabled: data?.partialRequestsEnabled,
trustProxy: data?.trustProxy,
cacheImages: data?.cacheImages,
httpProxy: data?.httpProxy,
}}
enableReinitialize
validationSchema={MainSettingsSchema}
Expand All @@ -158,6 +164,7 @@ const SettingsMain = () => {
partialRequestsEnabled: values.partialRequestsEnabled,
trustProxy: values.trustProxy,
cacheImages: values.cacheImages,
httpProxy: values.httpProxy,
}),
});
if (!res.ok) throw new Error();
Expand Down Expand Up @@ -437,6 +444,28 @@ const SettingsMain = () => {
/>
</div>
</div>
<div className="form-row">
<label htmlFor="httpProxy" className="checkbox-label">
<span className="mr-2">
{intl.formatMessage(messages.httpProxy)}
</span>
<SettingsBadge badgeType="advanced" className="mr-2" />
<SettingsBadge badgeType="restartRequired" />
<span className="label-tip">
{intl.formatMessage(messages.httpProxyTip)}
</span>
</label>
<div className="form-input-area">
<div className="form-input-field">
<Field id="httpProxy" name="httpProxy" type="text" />
</div>
{errors.httpProxy &&
touched.httpProxy &&
typeof errors.httpProxy === 'string' && (
<div className="error">{errors.httpProxy}</div>
)}
</div>
</div>
<div className="actions">
<div className="flex justify-end">
<span className="ml-3 inline-flex rounded-md shadow-sm">
Expand Down

0 comments on commit fba2f00

Please sign in to comment.