Skip to content

Commit 62b6b6b

Browse files
fix(server): T3 Connect says whether an unresolved address exists in public DNS
The reachability check reported every local lookup failure the same way, so a stale cached "not found" (the first probe runs before the relay publishes the record) and a record missing from public DNS both told users to check their DNS. A getaddrinfo EBUSY was reported as a timeout. Any getaddrinfo failure now counts as a DNS failure. On one, the check asks Cloudflare's DNS over HTTPS (by IP, so local DNS and port 53 interception play no part) whether the name exists, and says either that this network has an old cached answer that clears within about 30 minutes, or that the record is missing on the relay side. If public DNS cannot be asked, the message is unchanged. AI-assisted; verified with the tests and tools listed above.
1 parent c931fd4 commit 62b6b6b

3 files changed

Lines changed: 128 additions & 38 deletions

File tree

‎apps/server/src/cloud/ConnectReachability.test.ts‎

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,10 @@ const cloudflareErrorPage = (request: HttpClientRequest.HttpClientRequest, code:
115115
? JSON.stringify({ status: 530, error_code: code, error_name: "tunnel_error" })
116116
: `<!doctype html><h1><span>Error</span> <span>${code}</span></h1>`;
117117

118+
type Served = Effect.Effect<ServedResponse, HttpClientError.HttpClientError>;
119+
118120
const reachabilityLayer = (
119-
respond: Effect.Effect<ServedResponse, HttpClientError.HttpClientError>,
121+
respond: Served | ((request: HttpClientRequest.HttpClientRequest) => Served),
120122
restartConnector: (pid: number, stillDead: Effect.Effect<boolean>) => Effect.Effect<boolean>,
121123
) =>
122124
ConnectReachability.layer.pipe(
@@ -133,7 +135,7 @@ const reachabilityLayer = (
133135
Layer.succeed(
134136
HttpClient.HttpClient,
135137
HttpClient.make((request) =>
136-
respond.pipe(
138+
(Effect.isEffect(respond) ? respond : respond(request)).pipe(
137139
Effect.map(({ status, body }) =>
138140
HttpClientResponse.fromWeb(
139141
request,
@@ -381,32 +383,59 @@ describe("ConnectReachability stale connector recovery", () => {
381383
describe("ConnectReachability DNS failures", () => {
382384
// undici wraps getaddrinfo's error in `TypeError: fetch failed`. glibc behind
383385
// systemd-resolved can report an NXDOMAIN as EBUSY rather than ENOTFOUND.
386+
const lookupFailed = (code: string) =>
387+
Effect.fail(
388+
new HttpClientError.HttpClientError({
389+
reason: new HttpClientError.TransportError({
390+
request: HttpClientRequest.get("https://env.example.test"),
391+
cause: new TypeError("fetch failed", {
392+
cause: Object.assign(new Error(`getaddrinfo ${code} env.example.test`), {
393+
code,
394+
syscall: "getaddrinfo",
395+
hostname: "env.example.test",
396+
}),
397+
}),
398+
}),
399+
}),
400+
);
401+
384402
it.effect("reports a getaddrinfo EBUSY as a DNS failure", () =>
385403
Effect.gen(function* () {
386404
using _connect = mockEdgeConnect();
387-
const layer = reachabilityLayer(
388-
Effect.fail(
389-
new HttpClientError.HttpClientError({
390-
reason: new HttpClientError.TransportError({
391-
request: HttpClientRequest.get("https://env.example.test"),
392-
cause: new TypeError("fetch failed", {
393-
cause: Object.assign(new Error("getaddrinfo EBUSY env.example.test"), {
394-
code: "EBUSY",
395-
syscall: "getaddrinfo",
396-
hostname: "env.example.test",
397-
}),
398-
}),
399-
}),
400-
}),
401-
),
402-
() => Effect.succeed(false),
403-
);
405+
const layer = reachabilityLayer(lookupFailed("EBUSY"), () => Effect.succeed(false));
404406
const result = yield* ConnectReachability.ConnectReachability.use(
405407
(reachability) => reachability.check,
406408
).pipe(Effect.provide(layer));
407409
expect(result).toMatchObject({ status: "unreachable", cause: "dns_failed" });
408410
}),
409411
);
412+
413+
// A local "not found" cached before the relay published the record looks like a missing
414+
// record. Public DNS tells them apart.
415+
it.effect("says whether the name exists in public DNS", () =>
416+
Effect.gen(function* () {
417+
using _connect = mockEdgeConnect();
418+
for (const [dnsStatus, reason] of [
419+
[0, "old cached answer"],
420+
[3, "missing from public DNS"],
421+
] as const) {
422+
const layer = reachabilityLayer(
423+
(request) =>
424+
request.url.startsWith("https://1.1.1.1/dns-query")
425+
? Effect.succeed({ status: 200, body: JSON.stringify({ Status: dnsStatus }) })
426+
: lookupFailed("ENOTFOUND"),
427+
() => Effect.succeed(false),
428+
);
429+
const result = yield* ConnectReachability.ConnectReachability.use(
430+
(reachability) => reachability.check,
431+
).pipe(Effect.provide(layer));
432+
expect(result).toMatchObject({
433+
cause: "dns_failed",
434+
reason: expect.stringContaining(reason),
435+
});
436+
}
437+
}),
438+
);
410439
});
411440

412441
describe("T3 Connect troubleshooting links", () => {

‎apps/server/src/cloud/ConnectReachability.ts‎

Lines changed: 78 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ import * as Ref from "effect/Ref";
1717
import * as Scope from "effect/Scope";
1818
import * as Stream from "effect/Stream";
1919
import * 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";
2122
import { ChildProcessSpawner } from "effect/process";
2223

2324
import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
@@ -31,6 +32,10 @@ const TUNNEL_EDGE_PORT = 7844;
3132
const EDGE_CONNECT_TIMEOUT = Duration.seconds(5);
3233
const ENDPOINT_REQUEST_TIMEOUT = Duration.seconds(10);
3334
const 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.
3540
const 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

4449
export 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. */
4959
export 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

189221
const DNS_ERROR_CODES = new Set(["ENOTFOUND", "EAI_AGAIN", "EAI_NONAME"]);
190222

223+
const DnsJsonAnswer = Schema.Struct({ Status: Schema.Number });
224+
191225
const 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* () {

‎docs/user/t3-connect-troubleshooting.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ Fix:
7878
1. Make sure the host is awake and online.
7979
2. On the host, run `t3 connect status`.
8080
3. If the check fails only on the host, check the host's DNS, VPN, or proxy.
81+
If it says the host's network has an old cached answer, wait: it usually
82+
clears within about 30 minutes.
8183

8284
## This device can't reach T3 Connect
8385

0 commit comments

Comments
 (0)