1- import { execFile } from "node:child_process" ;
1+ import { execFile , type ChildProcess } from "node:child_process" ;
22import { promises as dns } from "node:dns" ;
3+ import { readFile } from "node:fs/promises" ;
34import { isIP } from "node:net" ;
45import { request as httpRequest , type IncomingMessage , type ClientRequest } from "node:http" ;
56import { request as httpsRequest } from "node:https" ;
@@ -54,17 +55,60 @@ export async function resolvePublicAddress(hostname: string): Promise<string> {
5455 ) {
5556 throw new Error ( "Capture requires a public hostname" ) ;
5657 }
57- const deadline = Date . now ( ) + DNS_TIMEOUT_MS ;
58- const answers = await resolveWithCares ( host ) . catch ( ( ) =>
59- resolveWithSystemInKillableChild ( host , deadline - Date . now ( ) ) ,
60- ) ;
58+ const answers = await resolveIPv4 ( host ) ;
6159 const first = answers [ 0 ] ;
6260 if ( ! first || ! answers . every ( isPublicIPv4 ) ) {
6361 throw new Error ( "Capture DNS returned a non-public address" ) ;
6462 }
6563 return first ;
6664}
6765
66+ async function resolveIPv4 ( host : string ) : Promise < string [ ] > {
67+ const fromHostsFile = ( await hostsFileAnswers ( ) ) . get ( host ) ;
68+ if ( fromHostsFile ) return fromHostsFile ;
69+ const deadline = Date . now ( ) + DNS_TIMEOUT_MS ;
70+ const fromSystem = systemAnswers . get ( host ) ;
71+ if ( fromSystem ) return beforeDeadline ( fromSystem , deadline ) ;
72+ return resolveWithCares ( host ) . catch ( ( ) =>
73+ beforeDeadline ( systemLookupInKillableChild ( host , deadline - Date . now ( ) ) , deadline ) ,
74+ ) ;
75+ }
76+
77+ let hostsFile : Promise < Map < string , string [ ] > > | undefined ;
78+
79+ function hostsFileAnswers ( ) : Promise < Map < string , string [ ] > > {
80+ const path =
81+ process . platform === "win32"
82+ ? `${ process . env . SystemRoot ?? "C:\\Windows" } \\System32\\drivers\\etc\\hosts`
83+ : "/etc/hosts" ;
84+ hostsFile ??= readFile ( path , "utf8" ) . then ( parseHostsFile , ( ) => new Map ( ) ) ;
85+ return hostsFile ;
86+ }
87+
88+ function parseHostsFile ( text : string ) : Map < string , string [ ] > {
89+ const answers = new Map < string , string [ ] > ( ) ;
90+ for ( const line of text . split ( / \r ? \n / ) ) {
91+ const [ address , ...names ] = line . replace ( / # .* / , "" ) . trim ( ) . split ( / \s + / ) ;
92+ if ( ! address || isIP ( address ) !== 4 ) continue ;
93+ for ( const name of names ) {
94+ const key = name . toLowerCase ( ) . replace ( / \. $ / , "" ) ;
95+ if ( ! answers . has ( key ) ) answers . set ( key , [ address ] ) ;
96+ }
97+ }
98+ return answers ;
99+ }
100+
101+ function beforeDeadline < T > ( work : Promise < T > , deadline : number ) : Promise < T > {
102+ let timer : ReturnType < typeof setTimeout > | undefined ;
103+ const late = new Promise < never > ( ( _resolve , reject ) => {
104+ timer = setTimeout (
105+ ( ) => reject ( new Error ( "Capture DNS timed out" ) ) ,
106+ Math . max ( 0 , deadline - Date . now ( ) ) ,
107+ ) ;
108+ } ) ;
109+ return Promise . race ( [ work , late ] ) . finally ( ( ) => clearTimeout ( timer ) ) ;
110+ }
111+
68112async function resolveWithCares ( host : string ) : Promise < string [ ] > {
69113 const cancellableResolver = new dns . Resolver ( ) ;
70114 const timer = setTimeout ( ( ) => cancellableResolver . cancel ( ) , CARES_TIMEOUT_MS ) ;
@@ -79,11 +123,12 @@ const systemAnswers = new Map<string, Promise<string[]>>();
79123const SYSTEM_LOOKUP = `require("node:dns").lookup(process.argv[1], { all: true, family: 4 }, (error, answers) =>
80124 error ? process.stderr.write(String(error.code)) : process.stdout.write(JSON.stringify(answers.map((a) => a.address))))` ;
81125
82- function resolveWithSystemInKillableChild ( host : string , timeoutMs : number ) : Promise < string [ ] > {
83- const cached = systemAnswers . get ( host ) ;
84- if ( cached ) return cached ;
126+ const liveLookups = new Set < ChildProcess > ( ) ;
127+
128+ export function systemLookupInKillableChild ( host : string , timeoutMs : number ) : Promise < string [ ] > {
129+ if ( ! process . listeners ( "exit" ) . includes ( killLiveLookups ) ) process . on ( "exit" , killLiveLookups ) ;
85130 const answers = new Promise < string [ ] > ( ( resolve , reject ) => {
86- execFile (
131+ const child = execFile (
87132 process . execPath ,
88133 [ "-e" , SYSTEM_LOOKUP , host ] ,
89134 {
@@ -104,12 +149,18 @@ function resolveWithSystemInKillableChild(host: string, timeoutMs: number): Prom
104149 }
105150 } ,
106151 ) ;
152+ liveLookups . add ( child ) ;
153+ child . once ( "exit" , ( ) => liveLookups . delete ( child ) ) ;
107154 } ) ;
108155 systemAnswers . set ( host , answers ) ;
109156 answers . catch ( ( ) => systemAnswers . delete ( host ) ) ;
110157 return answers ;
111158}
112159
160+ function killLiveLookups ( ) : void {
161+ for ( const child of liveLookups ) child . kill ( "SIGKILL" ) ;
162+ }
163+
113164/** Pinned asset transport; redirects are returned for caller validation. */
114165export async function publicRequest ( url : string | URL , init : RequestInit = { } ) : Promise < Response > {
115166 const target = new URL ( url ) ;
0 commit comments