diff --git a/server/cacheable-dns-http-agent.js b/server/cacheable-dns-http-agent.js index 56e8430eb..301367918 100644 --- a/server/cacheable-dns-http-agent.js +++ b/server/cacheable-dns-http-agent.js @@ -1,6 +1,8 @@ const https = require("https"); const http = require("http"); const CacheableLookup = require("cacheable-lookup"); +const { Settings } = require("./settings"); +const { log } = require("../src/util"); class CacheableDnsHttpAgent { @@ -9,12 +11,30 @@ class CacheableDnsHttpAgent { static httpAgentList = {}; static httpsAgentList = {}; + static enable = false; + /** - * Register cacheable to global agents + * Register/Disable cacheable to global agents */ - static registerGlobalAgent() { - this.cacheable.install(http.globalAgent); - this.cacheable.install(https.globalAgent); + static async update() { + log.debug("CacheableDnsHttpAgent", "update"); + let isEnable = await Settings.get("dnsCache"); + + if (isEnable !== this.enable) { + log.debug("CacheableDnsHttpAgent", "value changed"); + + if (isEnable) { + log.debug("CacheableDnsHttpAgent", "enable"); + this.cacheable.install(http.globalAgent); + this.cacheable.install(https.globalAgent); + } else { + log.debug("CacheableDnsHttpAgent", "disable"); + this.cacheable.uninstall(http.globalAgent); + this.cacheable.uninstall(https.globalAgent); + } + } + + this.enable = isEnable; } static install(agent) { @@ -26,6 +46,10 @@ class CacheableDnsHttpAgent { * @return {https.Agent} */ static getHttpsAgent(agentOptions) { + if (!this.enable) { + return new https.Agent(agentOptions); + } + let key = JSON.stringify(agentOptions); if (!(key in this.httpsAgentList)) { this.httpsAgentList[key] = new https.Agent(agentOptions); @@ -39,6 +63,10 @@ class CacheableDnsHttpAgent { * @return {https.Agents} */ static getHttpAgent(agentOptions) { + if (!this.enable) { + return new http.Agent(agentOptions); + } + let key = JSON.stringify(agentOptions); if (!(key in this.httpAgentList)) { this.httpAgentList[key] = new http.Agent(agentOptions); diff --git a/server/server.js b/server/server.js index 0bb894f8f..cdfe18dec 100644 --- a/server/server.js +++ b/server/server.js @@ -136,6 +136,7 @@ const { proxySocketHandler } = require("./socket-handlers/proxy-socket-handler") const { dockerSocketHandler } = require("./socket-handlers/docker-socket-handler"); const { maintenanceSocketHandler } = require("./socket-handlers/maintenance-socket-handler"); const { Settings } = require("./settings"); +const { CacheableDnsHttpAgent } = require("./cacheable-dns-http-agent"); app.use(express.json()); @@ -1114,6 +1115,8 @@ let needSetup = false; await setSettings("general", data); server.entryPage = data.entryPage; + await CacheableDnsHttpAgent.update(); + // Also need to apply timezone globally if (data.serverTimezone) { await server.setTimezone(data.serverTimezone); diff --git a/server/uptime-kuma-server.js b/server/uptime-kuma-server.js index 078cc31d9..062375626 100644 --- a/server/uptime-kuma-server.js +++ b/server/uptime-kuma-server.js @@ -83,12 +83,12 @@ class UptimeKumaServer { } } - CacheableDnsHttpAgent.registerGlobalAgent(); - this.io = new Server(this.httpServer); } async initAfterDatabaseReady() { + await CacheableDnsHttpAgent.update(); + process.env.TZ = await this.getTimezone(); dayjs.tz.setDefault(process.env.TZ); log.debug("DEBUG", "Timezone: " + process.env.TZ); diff --git a/src/components/settings/General.vue b/src/components/settings/General.vue index 612ed8029..8a734ab28 100644 --- a/src/components/settings/General.vue +++ b/src/components/settings/General.vue @@ -150,6 +150,46 @@ + +
+ + +
+ + +
+ +
+ + +
+
+