From 3b87209e2690abc84bebf140abbcf7b371aed479 Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Mon, 12 Dec 2022 17:19:22 +0800 Subject: [PATCH] Add configurable dns cache --- server/cacheable-dns-http-agent.js | 36 +++++++++++++++++++++++--- server/server.js | 3 +++ server/uptime-kuma-server.js | 4 +-- src/components/settings/General.vue | 40 +++++++++++++++++++++++++++++ src/languages/en.js | 4 +++ src/languages/zh-HK.js | 3 +++ src/pages/Settings.vue | 4 +++ src/util.js | 2 +- src/util.ts | 2 +- 9 files changed, 90 insertions(+), 8 deletions(-) diff --git a/server/cacheable-dns-http-agent.js b/server/cacheable-dns-http-agent.js index 56e8430e..30136791 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 0bb894f8..cdfe18de 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 078cc31d..06237562 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 612ed802..8a734ab2 100644 --- a/src/components/settings/General.vue +++ b/src/components/settings/General.vue @@ -150,6 +150,46 @@ + +
+ + +
+ + +
+ +
+ + +
+
+