formatting changes
| @@ -9,7 +9,6 @@ import path from 'node:path'; | |||
| 9 | import util from 'node:util'; | 9 | import util from 'node:util'; |
| 10 | import net from 'node:net'; | 10 | import net from 'node:net'; |
| 11 | import dns from 'node:dns'; | 11 | import dns from 'node:dns'; |
| 12 | import { promises as dnsPromise } from 'node:dns'; | ||
| 13 | import process from 'node:process'; | 12 | import process from 'node:process'; |
| 14 | import { fileURLToPath } from 'node:url'; | 13 | import { fileURLToPath } from 'node:url'; |
| 15 | 14 | ||
| @@ -71,6 +70,7 @@ import { | |||
| 71 | getSeparator, | 70 | getSeparator, |
| 72 | stringToBool, | 71 | stringToBool, |
| 73 | urlHostnameToIPv6, | 72 | urlHostnameToIPv6, |
| 73 | canResolve, | ||
| 74 | } from './src/util.js'; | 74 | } from './src/util.js'; |
| 75 | import { UPLOADS_DIRECTORY } from './src/constants.js'; | 75 | import { UPLOADS_DIRECTORY } from './src/constants.js'; |
| 76 | import { ensureThumbnailCache } from './src/endpoints/thumbnails.js'; | 76 | import { ensureThumbnailCache } from './src/endpoints/thumbnails.js'; |
| @@ -383,37 +383,6 @@ function getSessionCookieAge() { | |||
| 383 | return undefined; | 383 | return undefined; |
| 384 | } | 384 | } |
| 385 | 385 | ||
| 386 | |||
| 387 | async function canResolve(name, useIPv6 = true, useIPv4 = true) { | ||
| 388 | try { | ||
| 389 | let v6Resolved = false; | ||
| 390 | let v4Resolved = false; | ||
| 391 | |||
| 392 | if (useIPv6) { | ||
| 393 | try { | ||
| 394 | await dnsPromise.resolve6(name); | ||
| 395 | v6Resolved = true; | ||
| 396 | } catch (error) { | ||
| 397 | v6Resolved = false; | ||
| 398 | } | ||
| 399 | } | ||
| 400 | |||
| 401 | if (useIPv4) { | ||
| 402 | try { | ||
| 403 | await dnsPromise.resolve(name); | ||
| 404 | v4Resolved = true; | ||
| 405 | } catch (error) { | ||
| 406 | v4Resolved = false; | ||
| 407 | } | ||
| 408 | } | ||
| 409 | |||
| 410 | return v6Resolved || v4Resolved; | ||
| 411 | |||
| 412 | } catch (error) { | ||
| 413 | return false; | ||
| 414 | } | ||
| 415 | } | ||
| 416 | |||
| 417 | async function getHasIP() { | 386 | async function getHasIP() { |
| 418 | let hasIPv6 = false; | 387 | let hasIPv6 = false; |
| 419 | let hasIPv6Local = false; | 388 | let hasIPv6Local = false; |
| @@ -794,6 +763,8 @@ async function getAutorunHostname(useIPv6, useIPv4) { | |||
| 794 | * Tasks that need to be run after the server starts listening. | 763 | * Tasks that need to be run after the server starts listening. |
| 795 | * @param {boolean} v6Failed If the server failed to start on IPv6 | 764 | * @param {boolean} v6Failed If the server failed to start on IPv6 |
| 796 | * @param {boolean} v4Failed If the server failed to start on IPv4 | 765 | * @param {boolean} v4Failed If the server failed to start on IPv4 |
| 766 | * @param {boolean} useIPv6 If the server is using IPv6 | ||
| 767 | * @param {boolean} useIPv4 If the server is using IPv4 | ||
| 797 | */ | 768 | */ |
| 798 | const postSetupTasks = async function (v6Failed, v4Failed, useIPv6, useIPv4) { | 769 | const postSetupTasks = async function (v6Failed, v4Failed, useIPv6, useIPv4) { |
| 799 | const autorunUrl = new URL( | 770 | const autorunUrl = new URL( |
| @@ -5,6 +5,7 @@ import process from 'node:process'; | |||
| 5 | import { Readable } from 'node:stream'; | 5 | import { Readable } from 'node:stream'; |
| 6 | import { createRequire } from 'node:module'; | 6 | import { createRequire } from 'node:module'; |
| 7 | import { Buffer } from 'node:buffer'; | 7 | import { Buffer } from 'node:buffer'; |
| 8 | import { promises as dnsPromise } from 'node:dns'; | ||
| 8 | 9 | ||
| 9 | import yaml from 'yaml'; | 10 | import yaml from 'yaml'; |
| 10 | import { sync as commandExistsSync } from 'command-exists'; | 11 | import { sync as commandExistsSync } from 'command-exists'; |
| @@ -692,6 +693,10 @@ export function isValidUrl(url) { | |||
| 692 | } | 693 | } |
| 693 | } | 694 | } |
| 694 | 695 | ||
| 696 | /** | ||
| 697 | * removes starting `[` or ending `]` from hostname. | ||
| 698 | * @param {string} hostname hostname to use | ||
| 699 | */ | ||
| 695 | export function urlHostnameToIPv6(hostname) { | 700 | export function urlHostnameToIPv6(hostname) { |
| 696 | if (hostname.startsWith('[')) { | 701 | if (hostname.startsWith('[')) { |
| 697 | hostname = hostname.slice(1); | 702 | hostname = hostname.slice(1); |
| @@ -702,6 +707,47 @@ export function urlHostnameToIPv6(hostname) { | |||
| 702 | return hostname; | 707 | return hostname; |
| 703 | } | 708 | } |
| 704 | 709 | ||
| 710 | /** | ||
| 711 | * Test if can resolve a dns name. | ||
| 712 | * @param {string} name Domain name to use | ||
| 713 | * @param {boolean} useIPv6 If use IPv6 | ||
| 714 | * @param {boolean} useIPv4 If use IPv4 | ||
| 715 | */ | ||
| 716 | export async function canResolve(name, useIPv6 = true, useIPv4 = true) { | ||
| 717 | try { | ||
| 718 | let v6Resolved = false; | ||
| 719 | let v4Resolved = false; | ||
| 720 | |||
| 721 | if (useIPv6) { | ||
| 722 | try { | ||
| 723 | await dnsPromise.resolve6(name); | ||
| 724 | v6Resolved = true; | ||
| 725 | } catch (error) { | ||
| 726 | v6Resolved = false; | ||
| 727 | } | ||
| 728 | } | ||
| 729 | |||
| 730 | if (useIPv4) { | ||
| 731 | try { | ||
| 732 | await dnsPromise.resolve(name); | ||
| 733 | v4Resolved = true; | ||
| 734 | } catch (error) { | ||
| 735 | v4Resolved = false; | ||
| 736 | } | ||
| 737 | } | ||
| 738 | |||
| 739 | return v6Resolved || v4Resolved; | ||
| 740 | |||
| 741 | } catch (error) { | ||
| 742 | return false; | ||
| 743 | } | ||
| 744 | } | ||
| 745 | |||
| 746 | |||
| 747 | /** | ||
| 748 | * converts string to boolean accepts 'true' or 'false' else it returns the string put in | ||
| 749 | * @param {string} str Input string | ||
| 750 | */ | ||
| 705 | export function stringToBool(str) { | 751 | export function stringToBool(str) { |
| 706 | if (str === 'true') return true; | 752 | if (str === 'true') return true; |
| 707 | if (str === 'false') return false; | 753 | if (str === 'false') return false; |