Skip to content

Commit

Permalink
Make cacheExpirySeconds reloadable
Browse files Browse the repository at this point in the history
  • Loading branch information
willnode committed Sep 11, 2024
1 parent 5f85cc7 commit 5a27748
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 42 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
78 changes: 37 additions & 41 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
isExceedLabelLimit,
validateCAARecords,
isExceedHostLimit,
isHttpCodeAllowed
isHttpCodeAllowed,
getExpiryDate
} from "./util.js";

const MAX_DATA_SIZE = 10 * 1024; // 10 KB
Expand All @@ -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 });
}
Expand Down Expand Up @@ -80,7 +76,7 @@ async function buildCache(host) {
url,
expand,
blacklisted: isHostBlacklisted(host),
expire: Date.now() + cacheExpirySeconds * 1000,
expire: getExpiryDate(),
httpStatus: parseInt(httpStatus),
};
}
Expand Down Expand Up @@ -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;
}

Expand Down
13 changes: 13 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, boolean>}
*/
Expand All @@ -30,6 +31,17 @@ let blacklistMap = null;
* @type {Record<string, boolean> | 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<string, any>}
Expand Down Expand Up @@ -83,6 +95,7 @@ export function clearConfig() {
blacklistMap = null;
useLocalDNS = null;
blacklistRedirectUrl = null;
cacheExpirySeconds = null;
}

/**
Expand Down

0 comments on commit 5a27748

Please sign in to comment.