@@ -17,7 +17,8 @@ import * as Ref from "effect/Ref";
1717import * as Scope from "effect/Scope" ;
1818import * as Stream from "effect/Stream" ;
1919import * as SynchronizedRef from "effect/SynchronizedRef" ;
20- import { HttpClient } from "effect/http" ;
20+ import * as Schema from "effect/Schema" ;
21+ import { HttpClient , HttpClientResponse } from "effect/http" ;
2122import { ChildProcessSpawner } from "effect/process" ;
2223
2324import * as ServerSecretStore from "../auth/ServerSecretStore.ts" ;
@@ -31,6 +32,10 @@ const TUNNEL_EDGE_PORT = 7844;
3132const EDGE_CONNECT_TIMEOUT = Duration . seconds ( 5 ) ;
3233const ENDPOINT_REQUEST_TIMEOUT = Duration . seconds ( 10 ) ;
3334const ERROR_BODY_READ_TIMEOUT = Duration . seconds ( 2 ) ;
35+ // An IP literal, so the answer does not come from this machine's DNS, over HTTPS because
36+ // some networks intercept port 53.
37+ const PUBLIC_DNS_OVER_HTTPS = "https://1.1.1.1/dns-query" ;
38+ const PUBLIC_DNS_TIMEOUT = Duration . seconds ( 5 ) ;
3439// cloudflared registers several connections within a few seconds; they are one change.
3540const READINESS_CHANGE_DEBOUNCE = Duration . seconds ( 3 ) ;
3641// Readiness changes miss failures and recoveries past cloudflared (DNS, origin, edge errors).
@@ -43,7 +48,12 @@ const DEAD_CONNECTOR_RESTART_AFTER_MAX = Duration.hours(1);
4348
4449export type EndpointProbe =
4550 | { readonly kind : "status" ; readonly status : number ; readonly cloudflareError ?: string }
46- | { readonly kind : "dns_failed" | "timeout" | "network_error" } ;
51+ | {
52+ readonly kind : "dns_failed" ;
53+ /** Whether public DNS has the name; absent when it could not be asked. */
54+ readonly existsPublicly ?: boolean ;
55+ }
56+ | { readonly kind : "timeout" | "network_error" } ;
4757
4858/** Everything one check learned. `null` means that step did not run. */
4959export interface ReachabilityFacts {
@@ -149,19 +159,37 @@ export function classifyReachability(
149159 restart ,
150160 ) ;
151161 }
152- return endpoint . kind === "dns_failed"
153- ? fail (
162+ if ( endpoint . kind === "dns_failed" ) {
163+ // A lookup made before the relay published the record leaves a cached "not found"
164+ // for the zone's negative TTL (30 minutes).
165+ if ( endpoint . existsPublicly === true ) {
166+ return fail (
154167 "dns_failed" ,
155- "The T3 Connect address does not resolve from this machine." ,
156- "Check the DNS settings on this machine or its VPN." ,
157- exitNodeHint ( exitNode ) ,
158- )
159- : fail (
160- "timeout" ,
161- "The T3 Connect address did not answer from this machine." ,
162- "Check the network, VPN, or proxy on this machine." ,
168+ "The T3 Connect address resolves in public DNS, but this network's DNS still has an old cached answer that it does not exist." ,
169+ "Wait. The cached answer usually clears within about 30 minutes." ,
163170 exitNodeHint ( exitNode ) ,
164171 ) ;
172+ }
173+ if ( endpoint . existsPublicly === false ) {
174+ return fail (
175+ "dns_failed" ,
176+ "The T3 Connect address is missing from public DNS. The relay did not publish it." ,
177+ restart ,
178+ ) ;
179+ }
180+ return fail (
181+ "dns_failed" ,
182+ "The T3 Connect address does not resolve from this machine." ,
183+ "Check the DNS settings on this machine or its VPN." ,
184+ exitNodeHint ( exitNode ) ,
185+ ) ;
186+ }
187+ return fail (
188+ "timeout" ,
189+ "The T3 Connect address did not answer from this machine." ,
190+ "Check the network, VPN, or proxy on this machine." ,
191+ exitNodeHint ( exitNode ) ,
192+ ) ;
165193}
166194
167195/** The loud diagnosis the CLI prints and the server logs. */
@@ -175,10 +203,14 @@ export function formatReachabilityFailure(result: Unreachable): string {
175203 ] . join ( "\n" ) ;
176204}
177205
178- function hasErrorCode ( error : unknown , codes : ReadonlySet < string > ) : boolean {
206+ /** Any failed name lookup. getaddrinfo reports more than the DNS codes, such as EBUSY. */
207+ function isDnsLookupError ( error : unknown ) : boolean {
179208 let current = error ;
180209 for ( let depth = 0 ; depth < 6 && typeof current === "object" && current !== null ; depth ++ ) {
181- if ( "code" in current && typeof current . code === "string" && codes . has ( current . code ) ) {
210+ if (
211+ ( "syscall" in current && current . syscall === "getaddrinfo" ) ||
212+ ( "code" in current && typeof current . code === "string" && DNS_ERROR_CODES . has ( current . code ) )
213+ ) {
182214 return true ;
183215 }
184216 current = "cause" in current ? current . cause : undefined ;
@@ -188,6 +220,8 @@ function hasErrorCode(error: unknown, codes: ReadonlySet<string>): boolean {
188220
189221const DNS_ERROR_CODES = new Set ( [ "ENOTFOUND" , "EAI_AGAIN" , "EAI_NONAME" ] ) ;
190222
223+ const DnsJsonAnswer = Schema . Struct ( { Status : Schema . Number } ) ;
224+
191225const canOpenTcp = ( host : string , port : number ) =>
192226 Effect . callback < boolean > ( ( resume ) => {
193227 const socket = NodeNet . connect ( { host, port } ) ;
@@ -242,6 +276,22 @@ const make = Effect.gen(function* () {
242276 Effect . orElseSucceed ( ( ) => null ) ,
243277 ) ;
244278
279+ // DNS JSON status 0 is NOERROR (the name exists, even with no A record) and 3 is NXDOMAIN.
280+ const existsInPublicDns = Effect . fn ( "ConnectReachability.existsInPublicDns" ) (
281+ function * ( hostname : string ) {
282+ const response = yield * httpClient . get ( PUBLIC_DNS_OVER_HTTPS , {
283+ urlParams : { name : hostname , type : "A" } ,
284+ headers : { accept : "application/dns-json" } ,
285+ } ) ;
286+ const { Status } = yield * HttpClientResponse . schemaBodyJson ( DnsJsonAnswer ) ( response ) ;
287+ return Status === 0 ? true : Status === 3 ? false : null ;
288+ } ,
289+ Effect . scoped ,
290+ Effect . timeoutOption ( PUBLIC_DNS_TIMEOUT ) ,
291+ Effect . map ( Option . getOrNull ) ,
292+ Effect . orElseSucceed ( ( ) => null ) ,
293+ ) ;
294+
245295 const probeEndpoint = Effect . fn ( "ConnectReachability.probeEndpoint" ) (
246296 function * ( hostname : string ) {
247297 const response = yield * httpClient
@@ -264,11 +314,20 @@ const make = Effect.gen(function* () {
264314 ) satisfies EndpointProbe ;
265315 } ,
266316 Effect . scoped ,
267- Effect . catch ( ( error ) =>
268- Effect . succeed < EndpointProbe > ( {
269- kind : hasErrorCode ( error , DNS_ERROR_CODES ) ? "dns_failed" : "network_error" ,
270- } ) ,
271- ) ,
317+ ( effect , hostname ) =>
318+ effect . pipe (
319+ Effect . catch ( ( error ) =>
320+ isDnsLookupError ( error )
321+ ? existsInPublicDns ( hostname ) . pipe (
322+ Effect . map ( ( existsPublicly ) : EndpointProbe =>
323+ existsPublicly === null
324+ ? { kind : "dns_failed" }
325+ : { kind : "dns_failed" , existsPublicly } ,
326+ ) ,
327+ )
328+ : Effect . succeed < EndpointProbe > ( { kind : "network_error" } ) ,
329+ ) ,
330+ ) ,
272331 ) ;
273332
274333 const gatherFacts = Effect . gen ( function * ( ) {
0 commit comments