Add timeout to DNS lookup

This commit is contained in:
trevor-signal 2024-03-13 15:57:08 -04:00 committed by GitHub
parent 44a8049e63
commit 05c09ef769
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -9,11 +9,14 @@ import type {
} from 'dns';
import { ipcRenderer, net } from 'electron';
import type { ResolvedHost, ResolvedEndpoint } from 'electron';
import pTimeout from 'p-timeout';
import { strictAssert } from './assert';
import { drop } from './drop';
import type { DNSFallbackType } from '../types/DNSFallback';
import { SECOND } from './durations';
const LOOKUP_TIMEOUT_MS = 1 * SECOND;
const fallbackAddrs = new Map<string, ReadonlyArray<ResolvedEndpoint>>();
export function setFallback(dnsFallback: DNSFallbackType): void {
@ -49,15 +52,19 @@ function lookupAll(
try {
if (net) {
// Main process
result = await net.resolveHost(hostname, {
queryType,
});
result = await pTimeout(
net.resolveHost(hostname, {
queryType,
}),
LOOKUP_TIMEOUT_MS,
'lookupAll: Electron lookup timed out'
);
} else {
// Renderer
result = await ipcRenderer.invoke(
'net.resolveHost',
hostname,
queryType
result = await pTimeout(
ipcRenderer.invoke('net.resolveHost', hostname, queryType),
LOOKUP_TIMEOUT_MS,
'lookupAll: Electron lookup timed out'
);
}
} catch (error) {