diff --git a/server.js b/server.js index 47d961c3b..4cda91ae8 100644 --- a/server.js +++ b/server.js @@ -9,7 +9,6 @@ import path from 'node:path'; import util from 'node:util'; import net from 'node:net'; import dns from 'node:dns'; -import { promises as dnsPromise } from 'node:dns'; import process from 'node:process'; import { fileURLToPath } from 'node:url'; @@ -71,6 +70,7 @@ import { getSeparator, stringToBool, urlHostnameToIPv6, + canResolve, } from './src/util.js'; import { UPLOADS_DIRECTORY } from './src/constants.js'; import { ensureThumbnailCache } from './src/endpoints/thumbnails.js'; @@ -383,37 +383,6 @@ function getSessionCookieAge() { return undefined; } - -async function canResolve(name, useIPv6 = true, useIPv4 = true) { - try { - let v6Resolved = false; - let v4Resolved = false; - - if (useIPv6) { - try { - await dnsPromise.resolve6(name); - v6Resolved = true; - } catch (error) { - v6Resolved = false; - } - } - - if (useIPv4) { - try { - await dnsPromise.resolve(name); - v4Resolved = true; - } catch (error) { - v4Resolved = false; - } - } - - return v6Resolved || v4Resolved; - - } catch (error) { - return false; - } -} - async function getHasIP() { let hasIPv6 = false; let hasIPv6Local = false; @@ -794,6 +763,8 @@ async function getAutorunHostname(useIPv6, useIPv4) { * Tasks that need to be run after the server starts listening. * @param {boolean} v6Failed If the server failed to start on IPv6 * @param {boolean} v4Failed If the server failed to start on IPv4 + * @param {boolean} useIPv6 If the server is using IPv6 + * @param {boolean} useIPv4 If the server is using IPv4 */ const postSetupTasks = async function (v6Failed, v4Failed, useIPv6, useIPv4) { const autorunUrl = new URL( diff --git a/src/util.js b/src/util.js index b5034d248..c11de84e8 100644 --- a/src/util.js +++ b/src/util.js @@ -5,6 +5,7 @@ import process from 'node:process'; import { Readable } from 'node:stream'; import { createRequire } from 'node:module'; import { Buffer } from 'node:buffer'; +import { promises as dnsPromise } from 'node:dns'; import yaml from 'yaml'; import { sync as commandExistsSync } from 'command-exists'; @@ -692,6 +693,10 @@ export function isValidUrl(url) { } } +/** + * removes starting `[` or ending `]` from hostname. + * @param {string} hostname hostname to use + */ export function urlHostnameToIPv6(hostname) { if (hostname.startsWith('[')) { hostname = hostname.slice(1); @@ -702,6 +707,47 @@ export function urlHostnameToIPv6(hostname) { return hostname; } +/** + * Test if can resolve a dns name. + * @param {string} name Domain name to use + * @param {boolean} useIPv6 If use IPv6 + * @param {boolean} useIPv4 If use IPv4 + */ +export async function canResolve(name, useIPv6 = true, useIPv4 = true) { + try { + let v6Resolved = false; + let v4Resolved = false; + + if (useIPv6) { + try { + await dnsPromise.resolve6(name); + v6Resolved = true; + } catch (error) { + v6Resolved = false; + } + } + + if (useIPv4) { + try { + await dnsPromise.resolve(name); + v4Resolved = true; + } catch (error) { + v4Resolved = false; + } + } + + return v6Resolved || v4Resolved; + + } catch (error) { + return false; + } +} + + +/** + * converts string to boolean accepts 'true' or 'false' else it returns the string put in + * @param {string} str Input string + */ export function stringToBool(str) { if (str === 'true') return true; if (str === 'false') return false;