From 5a277486c12b29e3a69ab29d138df35fe5ea29e7 Mon Sep 17 00:00:00 2001 From: Wildan M Date: Wed, 11 Sep 2024 17:09:03 +0700 Subject: [PATCH] Make cacheExpirySeconds reloadable --- package.json | 2 +- src/client.js | 78 ++++++++++++++++++++++++--------------------------- src/util.js | 13 +++++++++ 3 files changed, 51 insertions(+), 42 deletions(-) diff --git a/package.json b/package.json index a64965f..4058a2b 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ }, "scripts": { "test": "(cd test && go run .)", - "start": "NODE_ENV=production node --env-file=.env app.js" + "start": "NODE_ENV=production node app.js" }, "keywords": [], "author": "Wildan Mubarok", diff --git a/src/client.js b/src/client.js index 923321b..537ed6e 100644 --- a/src/client.js +++ b/src/client.js @@ -11,7 +11,8 @@ import { isExceedLabelLimit, validateCAARecords, isExceedHostLimit, - isHttpCodeAllowed + isHttpCodeAllowed, + getExpiryDate } from "./util.js"; const MAX_DATA_SIZE = 10 * 1024; // 10 KB @@ -29,11 +30,6 @@ const MAX_DATA_SIZE = 10 * 1024; // 10 KB */ let resolveCache = new LRUCache({ max: 10000 }); -/** - * @type {int | 86400} - */ -const cacheExpirySeconds = parseInt(process.env.CACHE_EXPIRY_SECONDS, 10) || 86400; - function pruneCache() { resolveCache = new LRUCache({ max: 10000 }); } @@ -80,7 +76,7 @@ async function buildCache(host) { url, expand, blacklisted: isHostBlacklisted(host), - expire: Date.now() + cacheExpirySeconds * 1000, + expire: getExpiryDate(), httpStatus: parseInt(httpStatus), }; } @@ -133,45 +129,45 @@ const listener = async function (req, res) { res.write("ok"); return; case '/flushcache': - if (req.method === 'POST') { - let body = ''; - let totalSize = 0; - - req.on('data', chunk => { - totalSize += chunk.length; - // Disconnect if the data stream is too large - if (totalSize > MAX_DATA_SIZE) { - req.destroy(); + if (req.method !== 'POST') { + res.writeHead(405, { 'Content-Type': 'text/plain' }); + res.write("Method Not Allowed"); + return; + } + let body = ''; + let totalSize = 0; + + req.on('data', chunk => { + totalSize += chunk.length; + // Disconnect if the data stream is too large + if (totalSize > MAX_DATA_SIZE) { + req.destroy(); + return; + } + + body += chunk.toString(); + }); + + req.on('end', () => { + if (totalSize <= MAX_DATA_SIZE) { + const parsedData = querystring.parse(body); + const domain = parsedData.domain; + + if (!domain || typeof domain !== 'string') { return; } - body += chunk.toString(); - }); - - req.on('end', () => { - if (totalSize <= MAX_DATA_SIZE) { - const parsedData = querystring.parse(body); - const domain = parsedData.domain; - - if (!domain) { - return; - } - - if (validator.isFQDN(domain)) { - const cacheExists = resolveCache.get(domain); - if (cacheExists !== null && cacheExists !== undefined && cacheExists !== '') { - // Remove the cache entry - resolveCache.delete(domain); - } + if (validator.isFQDN(domain)) { + const cacheExists = resolveCache.get(domain); + if (cacheExists) { + // Remove the cache entry + resolveCache.delete(domain); } } - }); - res.writeHead(200, { 'Content-Type': 'text/plain' }); - res.write("Cache cleared"); - return; - } - res.writeHead(405, {'Content-Type': 'text/plain'}); - res.write("Method Not Allowed"); + } + }); + res.writeHead(200, { 'Content-Type': 'text/plain' }); + res.write("Cache cleared"); return; } diff --git a/src/util.js b/src/util.js index e898bda..a8b9617 100644 --- a/src/util.js +++ b/src/util.js @@ -7,6 +7,7 @@ import forge from "node-forge"; const recordParamDestUrl = 'forward-domain'; const recordParamHttpStatus = 'http-status'; const caaRegex = /^0 issue (")?letsencrypt\.org(;validationmethods=http-01)?\1$/; + /** * @type {Record} */ @@ -30,6 +31,17 @@ let blacklistMap = null; * @type {Record | null} */ let whitelistMap = null; +/** + * @type {number | null} + */ +let cacheExpirySeconds = null; + +export function getExpiryDate() { + if (cacheExpirySeconds === null) { + cacheExpirySeconds = parseInt(process.env.CACHE_EXPIRY_SECONDS || '86400') + } + return Date.now() + cacheExpirySeconds * 1000; +} /** * @returns {Record} @@ -83,6 +95,7 @@ export function clearConfig() { blacklistMap = null; useLocalDNS = null; blacklistRedirectUrl = null; + cacheExpirySeconds = null; } /**