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
12const whitelistPath = path.join(process.cwd(), './whitelist.txt');12const whitelistPath = path.join(process.cwd(), './whitelist.txt');
13const enableForwardedWhitelist = getConfigValue('enableForwardedWhitelist', false, 'boolean');13const enableForwardedWhitelist = getConfigValue('enableForwardedWhitelist', false, 'boolean');
14/** @type {string[]} */
14let whitelist = getConfigValue('whitelist', []);15let whitelist = getConfigValue('whitelist', []);
1516
16if (fs.existsSync(whitelistPath)) {17if (fs.existsSync(whitelistPath)) {
@@ -83,24 +84,33 @@ function isIpFormat(entry) {
83 * This function will modify the whitelist array in place.84 * This function will modify the whitelist array in place.
84 */85 */
85async function resolveHostnames() {86async 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 wildcard89 const promises = whitelist.map(async (entry) => {
91 if (isIpFormat(entry)) {90 if (!entry || typeof entry !== 'string') {
92 continue;91 return;
93 }92 }
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
95 if (isValidHostname(entry)) {100 if (isValidHostname(entry)) {
101 try {
96 const result = await dns.promises.lookup(entry);102 const result = await dns.promises.lookup(entry);
97 console.info(`Resolved whitelist hostname ${color.green(entry)} to IPv${result.family} address ${color.green(result.address)}`);103 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}`);
99 }107 }
100 } catch {108 } else {
101 // Ignore errors when resolving hostnames109 resolvedWhitelist.push(entry);
102 }110 }
103 }111 });
112
113 await Promise.allSettled(promises);
104}114}
105115
106/**116/**