Skip to content

Commit a41b882

Browse files
fix(server): advertise the tailnet name when sshd listens only on the tailnet address (#34745)
1 parent b75f117 commit a41b882

2 files changed

Lines changed: 41 additions & 10 deletions

File tree

‎apps/server/src/environment/RemoteOpenTargets.test.ts‎

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,28 @@ const layerSpawner = (input: { readonly status: CommandResult; readonly prefs: C
5353
}),
5454
);
5555

56-
const layerNet = (input: { readonly ipv4: boolean; readonly ipv6: boolean }) =>
56+
const layerNet = (input: {
57+
readonly ipv4: boolean;
58+
readonly ipv6: boolean;
59+
readonly tailnet?: boolean;
60+
}) =>
5761
Layer.succeed(NetService.NetService, {
5862
canListenOnHost: () => Effect.succeed(true),
5963
isPortAvailableOnLoopback: () => Effect.succeed(true),
60-
hasListenerOnHost: (_port, host) => Effect.succeed(host === "::1" ? input.ipv6 : input.ipv4),
64+
hasListenerOnHost: (_port, host) =>
65+
Effect.succeed(
66+
host === "127.0.0.1"
67+
? input.ipv4
68+
: host === "::1"
69+
? input.ipv6
70+
: (host === "100.64.1.2" || host === "bb-1.tail1234.ts.net") && input.tailnet === true,
71+
),
6172
reserveLoopbackPort: () => Effect.succeed(40_000),
6273
findAvailablePort: (preferred) => Effect.succeed(preferred),
6374
});
6475

6576
const resolveTargets = (input: {
66-
readonly sshd: { readonly ipv4: boolean; readonly ipv6: boolean };
77+
readonly sshd: { readonly ipv4: boolean; readonly ipv6: boolean; readonly tailnet?: boolean };
6778
readonly tailscale: { readonly status: CommandResult; readonly prefs: CommandResult };
6879
readonly hostname: string;
6980
readonly username?: string | null;
@@ -125,6 +136,20 @@ describe("RemoteOpenTargets", () => {
125136
}),
126137
);
127138

139+
it.effect("advertises the tailnet name when sshd listens only on the tailnet address", () =>
140+
Effect.gen(function* () {
141+
const targets = yield* resolveTargets({
142+
sshd: { ipv4: false, ipv6: false, tailnet: true },
143+
tailscale: TAILSCALE_UP,
144+
hostname: "bb-1",
145+
username: "t3-user",
146+
});
147+
expect(targets).toEqual([
148+
{ kind: "tailscale", host: "bb-1.tail1234.ts.net", username: "t3-user" },
149+
]);
150+
}),
151+
);
152+
128153
it.effect("ignores a stored RunSSH after `tailscale down`", () =>
129154
Effect.gen(function* () {
130155
const targets = yield* resolveTargets({

‎apps/server/src/environment/RemoteOpenTargets.ts‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,29 @@ export const make = Effect.gen(function* () {
3333
);
3434
const username = yield* HostProcess.Username;
3535

36-
const magicDnsName = yield* readTailscaleStatus.pipe(
37-
Effect.map((status) => status.magicDnsName),
36+
const tailscaleStatus = yield* readTailscaleStatus.pipe(
3837
Effect.orElseSucceed(() => null),
3938
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
4039
);
40+
const magicDnsName = tailscaleStatus?.magicDnsName ?? null;
4141

4242
const targets: Array<RemoteOpenTarget> = [];
4343

4444
if (!sshdListening) {
45-
// Tailscale SSH needs no local sshd; tailscaled alone does not prove an SSH route.
46-
if (
47-
magicDnsName !== null &&
45+
// sshd may listen only on the tailnet address (ListenAddress), and Tailscale SSH
46+
// needs no local sshd; tailscaled alone does not prove an SSH route.
47+
if (magicDnsName === null) return targets;
48+
const tailnetSshdListening = yield* Effect.forEach(
49+
tailscaleStatus?.tailnetIpv4Addresses ?? [],
50+
(address) => net.hasListenerOnHost(SSH_PORT, address),
51+
).pipe(Effect.map((listening) => listening.includes(true)));
52+
const sshRoute =
53+
tailnetSshdListening ||
4854
(yield* readTailscaleSshEnabled.pipe(
4955
Effect.orElseSucceed(() => false),
5056
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
51-
))
52-
) {
57+
));
58+
if (sshRoute) {
5359
targets.push({
5460
kind: "tailscale",
5561
host: magicDnsName,

0 commit comments

Comments
 (0)