Rewrite whitelist resolve logic to skip unresolved hosts from whitelist

8aa7ed863583808be13b96d0373f697a9d196521

Cohee <18619528+Cohee1207@users.noreply.github.com>

1 files changed, +18 -8Showing whitespace changes
src/middleware/whitelist.js+18 -8
@@ -11,6 +11,7 @@ import { color, getConfigValue, safeReadFileSync } from '../util.js';
1111
1212const whitelistPath = path.join(process.cwd(), './whitelist.txt');
1313const enableForwardedWhitelist = getConfigValue('enableForwardedWhitelist', false, 'boolean');
14+/** @type {string[]} */
1415let whitelist = getConfigValue('whitelist', []);
1516
1617if (fs.existsSync(whitelistPath)) {
@@ -83,24 +84,33 @@ function isIpFormat(entry) {
8384 * This function will modify the whitelist array in place.
8485 */
8586async function resolveHostnames() {
86- for (let i = 0; i < whitelist.length; i++) {
87+ const resolvedWhitelist = [];
87- try {
88+
88- const entry = whitelist[i];
89+ const promises = whitelist.map(async (entry) => {
90+ if (!entry || typeof entry !== 'string') {
91+ return;
92+ }
8993
9094 // Skip if entry appears to be an IP address, CIDR notation, or IP wildcard
9195 if (isIpFormat(entry)) {
92- continue;
96+ resolvedWhitelist.push(entry);
97+ return;
9398 }
9499
95100 if (isValidHostname(entry)) {
101+ try {
96102 const result = await dns.promises.lookup(entry);
97103 console.info(`Resolved whitelist hostname ${color.green(entry)} to IPv${result.family} address ${color.green(result.address)}`);
98- whitelist[i] = result.address;
104+ resolvedWhitelist.push(result.address);
99- }
105+ } catch (e) {
100- } catch {
106+ console.warn(`Failed to resolve whitelist hostname ${color.red(entry)}: ${e.message}`);
101- // Ignore errors when resolving hostnames
102107 }
108+ } else {
109+ resolvedWhitelist.push(entry);
103110 }
111+ });
112+
113+ await Promise.allSettled(promises);
104114}
105115
106116/**