fix: add force min/max TTL in network settings

This commit is contained in:
gauthier-th
2025-08-19 13:55:08 +02:00
parent f958b11d51
commit aeb8023027
7 changed files with 173 additions and 30 deletions

View File

@@ -25,7 +25,7 @@ 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 dnsCache from '@server/utils/dnsCache';
import { initializeDnsCache } from '@server/utils/dnsCache';
import restartFlag from '@server/utils/restartFlag';
import { getClientIp } from '@supercharge/request-ip';
import axios from 'axios';
@@ -83,7 +83,10 @@ app
// Add DNS caching
if (settings.network.dnsCache) {
dnsCache.initialize();
initializeDnsCache({
forceMinTtl: settings.network.dnsCache.forceMinTtl,
forceMaxTtl: settings.network.dnsCache.forceMaxTtl,
});
}
// Register HTTP proxy

View File

@@ -100,17 +100,6 @@ 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;
@@ -138,12 +127,29 @@ export interface MainSettings {
youtubeUrl: string;
}
export interface ProxySettings {
enabled: boolean;
hostname: string;
port: number;
useSsl: boolean;
user: string;
password: string;
bypassFilter: string;
bypassLocalAddresses: boolean;
}
export interface DnsCacheSettings {
enabled: boolean;
forceMinTtl?: number;
forceMaxTtl?: number;
}
export interface NetworkSettings {
csrfProtection: boolean;
forceIpv4First: boolean;
trustProxy: boolean;
proxy: ProxySettings;
dnsCache: boolean;
dnsCache: DnsCacheSettings;
}
interface PublicSettings {
@@ -543,7 +549,11 @@ class Settings {
bypassFilter: '',
bypassLocalAddresses: true,
},
dnsCache: false,
dnsCache: {
enabled: false,
forceMinTtl: 0,
forceMaxTtl: -1,
},
},
};
if (initialSettings) {

View File

@@ -756,8 +756,8 @@ settingsRoutes.get('/cache', async (_req, res) => {
const tmdbImageCache = await ImageProxy.getImageStats('tmdb');
const avatarImageCache = await ImageProxy.getImageStats('avatar');
const stats = dnsCache.getStats();
const entries = dnsCache.getCacheEntries();
const stats = dnsCache?.getStats();
const entries = dnsCache?.getCacheEntries();
return res.status(200).json({
apiCaches,

View File

@@ -1,9 +1,28 @@
import logger from '@server/logger';
import { DnsCacheManager } from 'dns-caching';
const dnsCache = new DnsCacheManager({
logger: logger,
forceMaxTtl: Number(process.env.DNS_CACHE_FORCE_MAX_TTL) || -1,
forceMinTtl: Number(process.env.DNS_CACHE_FORCE_MIN_TTL) || 0,
});
let dnsCache: DnsCacheManager | undefined;
export function initializeDnsCache({
forceMinTtl,
forceMaxTtl,
}: {
forceMinTtl?: number;
forceMaxTtl?: number;
}) {
if (dnsCache) {
logger.warn('DNS Cache is already initialized', { label: 'DNS Cache' });
return;
}
logger.info('Initializing DNS Cache', { label: 'DNS Cache' });
dnsCache = new DnsCacheManager({
logger,
forceMinTtl: typeof forceMinTtl === 'number' ? forceMinTtl * 1000 : 0,
forceMaxTtl: typeof forceMaxTtl === 'number' ? forceMaxTtl * 1000 : -1,
});
dnsCache.initialize();
}
export default dnsCache;