From e5e8db6c38aa76f4585f0c81b804435aeb427ca8 Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Wed, 29 Jun 2022 22:17:47 +0800 Subject: [PATCH 1/7] Add cacheable-lookup --- package-lock.json | 14 ++++++++++++++ package.json | 1 + server/uptime-kuma-server.js | 5 +++++ 3 files changed, 20 insertions(+) diff --git a/package-lock.json b/package-lock.json index 9d64290b..2ac4a758 100644 --- a/package-lock.json +++ b/package-lock.json @@ -22,6 +22,7 @@ "bcryptjs": "~2.4.3", "bootstrap": "5.1.3", "bree": "~7.1.5", + "cacheable-lookup": "^6.0.4", "chardet": "^1.3.0", "chart.js": "~3.6.2", "chartjs-adapter-dayjs": "~1.0.0", @@ -4770,6 +4771,14 @@ "node": ">= 0.8" } }, + "node_modules/cacheable-lookup": { + "version": "6.0.4", + "resolved": "https://registry.npmjs.org/cacheable-lookup/-/cacheable-lookup-6.0.4.tgz", + "integrity": "sha512-mbcDEZCkv2CZF4G01kr8eBd/5agkt9oCqz75tJMSIsquvRZ2sL6Hi5zGVKi/0OSC9oO1GHfJ2AV0ZIOY9vye0A==", + "engines": { + "node": ">=10.6.0" + } + }, "node_modules/call-bind": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/call-bind/-/call-bind-1.0.2.tgz", @@ -19882,6 +19891,11 @@ "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.0.0.tgz", "integrity": "sha512-pMhOfFDPiv9t5jjIXkHosWmkSyQbvsgEVNkz0ERHbuLh2T/7j4Mqqpz523Fe8MVY89KC6Sh/QfS2sM+SjgFDcw==" }, + "cacheable-lookup": { + "version": "6.0.4", + "resolved": "https://registry.npmjs.org/cacheable-lookup/-/cacheable-lookup-6.0.4.tgz", + "integrity": "sha512-mbcDEZCkv2CZF4G01kr8eBd/5agkt9oCqz75tJMSIsquvRZ2sL6Hi5zGVKi/0OSC9oO1GHfJ2AV0ZIOY9vye0A==" + }, "call-bind": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/call-bind/-/call-bind-1.0.2.tgz", diff --git a/package.json b/package.json index 08eed263..022e7d8d 100644 --- a/package.json +++ b/package.json @@ -74,6 +74,7 @@ "bcryptjs": "~2.4.3", "bootstrap": "5.1.3", "bree": "~7.1.5", + "cacheable-lookup": "^6.0.4", "chardet": "^1.3.0", "chart.js": "~3.6.2", "chartjs-adapter-dayjs": "~1.0.0", diff --git a/server/uptime-kuma-server.js b/server/uptime-kuma-server.js index 605ba533..6f7be7ba 100644 --- a/server/uptime-kuma-server.js +++ b/server/uptime-kuma-server.js @@ -7,6 +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"); /** * `module.exports` (alias: `server`) should be inside this class, in order to avoid circular dependency issue. @@ -71,6 +72,10 @@ class UptimeKumaServer { } } + const cacheable = new CacheableLookup(); + cacheable.install(http.globalAgent); + cacheable.install(https.globalAgent); + this.io = new Server(this.httpServer); } From 25d711e6834d88c10d21099e53ab333f758b8fc0 Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Mon, 18 Jul 2022 22:06:25 +0800 Subject: [PATCH 2/7] Fix jsdoc data type --- server/util-server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/util-server.js b/server/util-server.js index f6a0e396..deacb3a9 100644 --- a/server/util-server.js +++ b/server/util-server.js @@ -384,7 +384,7 @@ exports.checkCertificate = function (res) { /** * Check if the provided status code is within the accepted ranges - * @param {string} status The status code to check + * @param {number} status The status code to check * @param {string[]} acceptedCodes An array of accepted status codes * @returns {boolean} True if status code within range, false otherwise * @throws {Error} Will throw an error if the provided status code is not a valid range string or code string From 2073f0c28476bb46fb953ecefb9622273e8819d9 Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Mon, 18 Jul 2022 22:33:35 +0800 Subject: [PATCH 3/7] Bind cacheable-lookup to custom http agent --- server/cacheable-dns-http-agent.js | 50 ++++++++++++++++++++++++++++++ server/model/monitor.js | 6 +++- server/uptime-kuma-server.js | 6 ++-- 3 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 server/cacheable-dns-http-agent.js diff --git a/server/cacheable-dns-http-agent.js b/server/cacheable-dns-http-agent.js new file mode 100644 index 00000000..0eb7a6ee --- /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 5c97785f..bcb5a8b7 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 6f7be7ba..e93e2b78 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); } From 65d71e5db0653361c55e191313e491269f6b61af Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Mon, 18 Jul 2022 23:14:16 +0800 Subject: [PATCH 4/7] Fix mssqlQuery keep adding error listener, which causes memory leak. Also it is not necessary since the error catched in the promise .catch(..). --- server/util-server.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/server/util-server.js b/server/util-server.js index deacb3a9..ec68c2f3 100644 --- a/server/util-server.js +++ b/server/util-server.js @@ -238,10 +238,6 @@ exports.dnsResolve = function (hostname, resolverServer, resolverPort, rrtype) { */ exports.mssqlQuery = function (connectionString, query) { return new Promise((resolve, reject) => { - mssql.on("error", err => { - reject(err); - }); - mssql.connect(connectionString).then(pool => { return pool.request() .query(query); From 9cd060c6c3fba9912bced3643a268d84b24f42f1 Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Mon, 18 Jul 2022 23:27:05 +0800 Subject: [PATCH 5/7] socks-proxy-agent 6.2.X is a breaking change, freeze to 6.1.1. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 8693135a..93c249f4 100644 --- a/package.json +++ b/package.json @@ -99,7 +99,7 @@ "redbean-node": "0.1.4", "socket.io": "~4.4.1", "socket.io-client": "~4.4.1", - "socks-proxy-agent": "^6.1.1", + "socks-proxy-agent": "6.1.1", "tar": "^6.1.11", "tcp-ping": "~0.1.1", "thirty-two": "~1.0.2" From 8f7b7e74c955eea2d4107d1658f94530101aede9 Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Mon, 18 Jul 2022 23:28:19 +0800 Subject: [PATCH 6/7] socks-proxy-agent 6.2.X is a breaking change, freeze to 6.1.1. --- package-lock.json | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/package-lock.json b/package-lock.json index b0e55246..e64f9fa7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -47,7 +47,7 @@ "redbean-node": "0.1.4", "socket.io": "~4.4.1", "socket.io-client": "~4.4.1", - "socks-proxy-agent": "^6.1.1", + "socks-proxy-agent": "6.1.1", "tar": "^6.1.11", "tcp-ping": "~0.1.1", "thirty-two": "~1.0.2" @@ -14306,13 +14306,13 @@ } }, "node_modules/socks-proxy-agent": { - "version": "6.2.1", - "resolved": "https://registry.npmjs.org/socks-proxy-agent/-/socks-proxy-agent-6.2.1.tgz", - "integrity": "sha512-a6KW9G+6B3nWZ1yB8G7pJwL3ggLy1uTzKAgCb7ttblwqdz9fMGJUuTy3uFzEP48FAs9FLILlmzDlE2JJhVQaXQ==", + "version": "6.1.1", + "resolved": "https://registry.npmjs.org/socks-proxy-agent/-/socks-proxy-agent-6.1.1.tgz", + "integrity": "sha512-t8J0kG3csjA4g6FTbsMOWws+7R7vuRC8aQ/wy3/1OWmsgwA68zs/+cExQ0koSitUDXqhufF/YJr9wtNMZHw5Ew==", "dependencies": { "agent-base": "^6.0.2", - "debug": "^4.3.3", - "socks": "^2.6.2" + "debug": "^4.3.1", + "socks": "^2.6.1" }, "engines": { "node": ">= 10" @@ -27095,13 +27095,13 @@ } }, "socks-proxy-agent": { - "version": "6.2.1", - "resolved": "https://registry.npmjs.org/socks-proxy-agent/-/socks-proxy-agent-6.2.1.tgz", - "integrity": "sha512-a6KW9G+6B3nWZ1yB8G7pJwL3ggLy1uTzKAgCb7ttblwqdz9fMGJUuTy3uFzEP48FAs9FLILlmzDlE2JJhVQaXQ==", + "version": "6.1.1", + "resolved": "https://registry.npmjs.org/socks-proxy-agent/-/socks-proxy-agent-6.1.1.tgz", + "integrity": "sha512-t8J0kG3csjA4g6FTbsMOWws+7R7vuRC8aQ/wy3/1OWmsgwA68zs/+cExQ0koSitUDXqhufF/YJr9wtNMZHw5Ew==", "requires": { "agent-base": "^6.0.2", - "debug": "^4.3.3", - "socks": "^2.6.2" + "debug": "^4.3.1", + "socks": "^2.6.1" } }, "sortablejs": { From 17ed051401f3889e8be041084301f05517a33526 Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Mon, 18 Jul 2022 23:32:45 +0800 Subject: [PATCH 7/7] Add CacheableDnsHttpAgent.install() --- package.json | 2 +- server/cacheable-dns-http-agent.js | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 93c249f4..7a18dbdf 100644 --- a/package.json +++ b/package.json @@ -68,7 +68,7 @@ "badge-maker": "^3.3.1", "bcryptjs": "~2.4.3", "bree": "~7.1.5", - "cacheable-lookup": "^6.0.4", + "cacheable-lookup": "~6.0.4", "chardet": "^1.3.0", "check-password-strength": "^2.0.5", "cheerio": "^1.0.0-rc.10", diff --git a/server/cacheable-dns-http-agent.js b/server/cacheable-dns-http-agent.js index 0eb7a6ee..56e8430e 100644 --- a/server/cacheable-dns-http-agent.js +++ b/server/cacheable-dns-http-agent.js @@ -17,6 +17,10 @@ class CacheableDnsHttpAgent { this.cacheable.install(https.globalAgent); } + static install(agent) { + this.cacheable.install(agent); + } + /** * @var {https.AgentOptions} agentOptions * @return {https.Agent}