Rewrite whitelist resolve logic to skip unresolved hosts from whitelist

8aa7ed863583808be13b96d0373f697a9d196521

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

1 files changed, +22 -12Ignore whitespace
src/middleware/whitelist.js+22 -12
@@ -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- const entry = whitelist[i];
8988
90- // Skip if entry appears to be an IP address, CIDR notation, or IP wildcard
89+ const promises = whitelist.map(async (entry) => {
91- if (isIpFormat(entry)) {
90+ if (!entry || typeof entry !== 'string') {
92- continue;
91+ return;
9392 }
93+
94+ // Skip if entry appears to be an IP address, CIDR notation, or IP wildcard
95+ if (isIpFormat(entry)) {
96+ resolvedWhitelist.push(entry);
97+ return;
98+ }
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);
105+ } catch (e) {
106+ console.warn(`Failed to resolve whitelist hostname ${color.red(entry)}: ${e.message}`);
99107 }
100108 } catchelse {
101- // Ignore errors when resolving hostnames
109+ resolvedWhitelist.push(entry);
102110 }
103111 });
112+
113+ await Promise.allSettled(promises);
104114}
105115
106116/**