From cb5e7a029ba11acff4eb36433c7c58b4bf9513ca Mon Sep 17 00:00:00 2001 From: Jon Seager Date: Thu, 1 Aug 2024 17:00:36 +0100 Subject: [PATCH] build: use `resolve4`/`resolve6` in `build:dns-fallback`. `dns.lookup` relies on making syscalls to the underlying system using `getaddrinfo`, which can sometimes fail in proxy situations. This is an equivalent implementation using the native NodeJS DNS functions. --- ts/scripts/generate-dns-fallback.ts | 58 ++++++++++++++--------------- 1 file changed, 27 insertions(+), 31 deletions(-) diff --git a/ts/scripts/generate-dns-fallback.ts b/ts/scripts/generate-dns-fallback.ts index 39f442f4b9e1..f204fe7843c3 100644 --- a/ts/scripts/generate-dns-fallback.ts +++ b/ts/scripts/generate-dns-fallback.ts @@ -1,15 +1,15 @@ // Copyright 2024 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only -import { join } from 'path'; -import { lookup as lookupCb } from 'dns'; +import { resolve4 as resolve4Cb, resolve6 as resolve6Cb } from 'dns'; import { writeFile } from 'fs/promises'; +import { join } from 'path'; import { promisify } from 'util'; -import type { ResolvedEndpoint } from 'electron'; import { isNotNil } from '../util/isNotNil'; -const lookup = promisify(lookupCb); +const resolve4 = promisify(resolve4Cb); +const resolve6 = promisify(resolve6Cb); const FALLBACK_DOMAINS = [ 'chat.signal.org', @@ -25,35 +25,31 @@ const FALLBACK_DOMAINS = [ async function main() { const config = await Promise.all( FALLBACK_DOMAINS.sort().map(async domain => { - const addresses = await lookup(domain, { all: true }); - const endpoints = addresses - .map(({ address, family }): ResolvedEndpoint | null => { - if (family === 4) { - return { family: 'ipv4', address }; - } - if (family === 6) { - return { family: 'ipv6', address }; - } - return null; - }) - .filter(isNotNil) - .sort((a, b) => { - if (a.family < b.family) { - return -1; - } - if (a.family > b.family) { - return 1; - } + const ipv4endpoints = (await resolve4(domain)) + .map(a => ({"family": "ipv4", "address": a})) - if (a.address < b.address) { - return -1; - } - if (a.address > b.address) { - return 1; - } - return 0; - }); + const ipv6endpoints = (await resolve6(domain)) + .map(a => ({"family": "ipv6", "address": a})) + + const endpoints = [...ipv4endpoints, ...ipv6endpoints] + .filter(isNotNil) + .sort((a, b) => { + if (a.family < b.family) { + return -1; + } + if (a.family > b.family) { + return 1; + } + + if (a.address < b.address) { + return -1; + } + if (a.address > b.address) { + return 1; + } + return 0; + }) return { domain, endpoints }; })