diff --git a/server/cacheable-dns-http-agent.js b/server/cacheable-dns-http-agent.js new file mode 100644 index 000000000..0eb7a6ee2 --- /dev/null +++ b/server/cacheable-dns-http-agent.js @@ -0,0 +1,50 @@ +const https = require("https"); +const http = require("http"); +const CacheableLookup = require("cacheable-lookup"); + +class CacheableDnsHttpAgent { + + static cacheable = new CacheableLookup(); + + static httpAgentList = {}; + static httpsAgentList = {}; + + /** + * Register cacheable to global agents + */ + static registerGlobalAgent() { + this.cacheable.install(http.globalAgent); + this.cacheable.install(https.globalAgent); + } + + /** + * @var {https.AgentOptions} agentOptions + * @return {https.Agent} + */ + static getHttpsAgent(agentOptions) { + let key = JSON.stringify(agentOptions); + if (!(key in this.httpsAgentList)) { + this.httpsAgentList[key] = new https.Agent(agentOptions); + this.cacheable.install(this.httpsAgentList[key]); + } + return this.httpsAgentList[key]; + } + + /** + * @var {http.AgentOptions} agentOptions + * @return {https.Agents} + */ + static getHttpAgent(agentOptions) { + let key = JSON.stringify(agentOptions); + if (!(key in this.httpAgentList)) { + this.httpAgentList[key] = new http.Agent(agentOptions); + this.cacheable.install(this.httpAgentList[key]); + } + return this.httpAgentList[key]; + } + +} + +module.exports = { + CacheableDnsHttpAgent, +}; diff --git a/server/model/monitor.js b/server/model/monitor.js index 5c97785f9..bcb5a8b72 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -16,6 +16,7 @@ const { demoMode } = require("../config"); const version = require("../../package.json").version; const apicache = require("../modules/apicache"); const { UptimeKumaServer } = require("../uptime-kuma-server"); +const { CacheableDnsHttpAgent } = require("../cacheable-dns-http-agent"); /** * status: @@ -434,10 +435,13 @@ class Monitor extends BeanModel { "Accept": "*/*", "User-Agent": "Uptime-Kuma/" + version, }, - httpsAgent: new https.Agent({ + httpsAgent: CacheableDnsHttpAgent.getHttpsAgent({ maxCachedSessions: 0, // Use Custom agent to disable session reuse (https://github.com/nodejs/node/issues/3940) rejectUnauthorized: !this.getIgnoreTls(), }), + httpAgent: CacheableDnsHttpAgent.getHttpAgent({ + maxCachedSessions: 0, + }), maxRedirects: this.maxredirects, validateStatus: (status) => { return checkStatusCode(status, this.getAcceptedStatuscodes()); diff --git a/server/uptime-kuma-server.js b/server/uptime-kuma-server.js index 6f7be7ba2..e93e2b787 100644 --- a/server/uptime-kuma-server.js +++ b/server/uptime-kuma-server.js @@ -7,7 +7,7 @@ const { R } = require("redbean-node"); const { log } = require("../src/util"); const Database = require("./database"); const util = require("util"); -const CacheableLookup = require("cacheable-lookup"); +const { CacheableDnsHttpAgent } = require("./cacheable-dns-http-agent"); /** * `module.exports` (alias: `server`) should be inside this class, in order to avoid circular dependency issue. @@ -72,9 +72,7 @@ class UptimeKumaServer { } } - const cacheable = new CacheableLookup(); - cacheable.install(http.globalAgent); - cacheable.install(https.globalAgent); + CacheableDnsHttpAgent.registerGlobalAgent(); this.io = new Server(this.httpServer); }