mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Rewrite whitelist resolve logic to skip unresolved hosts from whitelist
This commit is contained in:
@@ -11,6 +11,7 @@ import { color, getConfigValue, safeReadFileSync } from '../util.js';
|
|||||||
|
|
||||||
const whitelistPath = path.join(process.cwd(), './whitelist.txt');
|
const whitelistPath = path.join(process.cwd(), './whitelist.txt');
|
||||||
const enableForwardedWhitelist = getConfigValue('enableForwardedWhitelist', false, 'boolean');
|
const enableForwardedWhitelist = getConfigValue('enableForwardedWhitelist', false, 'boolean');
|
||||||
|
/** @type {string[]} */
|
||||||
let whitelist = getConfigValue('whitelist', []);
|
let whitelist = getConfigValue('whitelist', []);
|
||||||
|
|
||||||
if (fs.existsSync(whitelistPath)) {
|
if (fs.existsSync(whitelistPath)) {
|
||||||
@@ -83,24 +84,33 @@ function isIpFormat(entry) {
|
|||||||
* This function will modify the whitelist array in place.
|
* This function will modify the whitelist array in place.
|
||||||
*/
|
*/
|
||||||
async function resolveHostnames() {
|
async function resolveHostnames() {
|
||||||
for (let i = 0; i < whitelist.length; i++) {
|
const resolvedWhitelist = [];
|
||||||
try {
|
|
||||||
const entry = whitelist[i];
|
|
||||||
|
|
||||||
// Skip if entry appears to be an IP address, CIDR notation, or IP wildcard
|
const promises = whitelist.map(async (entry) => {
|
||||||
if (isIpFormat(entry)) {
|
if (!entry || typeof entry !== 'string') {
|
||||||
continue;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isValidHostname(entry)) {
|
// Skip if entry appears to be an IP address, CIDR notation, or IP wildcard
|
||||||
|
if (isIpFormat(entry)) {
|
||||||
|
resolvedWhitelist.push(entry);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isValidHostname(entry)) {
|
||||||
|
try {
|
||||||
const result = await dns.promises.lookup(entry);
|
const result = await dns.promises.lookup(entry);
|
||||||
console.info(`Resolved whitelist hostname ${color.green(entry)} to IPv${result.family} address ${color.green(result.address)}`);
|
console.info(`Resolved whitelist hostname ${color.green(entry)} to IPv${result.family} address ${color.green(result.address)}`);
|
||||||
whitelist[i] = result.address;
|
resolvedWhitelist.push(result.address);
|
||||||
|
} catch (e) {
|
||||||
|
console.warn(`Failed to resolve whitelist hostname ${color.red(entry)}: ${e.message}`);
|
||||||
}
|
}
|
||||||
} catch {
|
} else {
|
||||||
// Ignore errors when resolving hostnames
|
resolvedWhitelist.push(entry);
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
|
||||||
|
await Promise.allSettled(promises);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user