Skip to content
Open
109 changes: 107 additions & 2 deletions apps/server/src/peer/PeerLinks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import * as NodeFS from "node:fs";
// @effect-diagnostics-next-line nodeBuiltinImport:off - A peer with a certificate this machine does not trust.
import * as NodeHttps from "node:https";
import { NodeHttpServer } from "@effect/platform-node";
import { NodeHttpClient, NodeHttpServer } from "@effect/platform-node";
import * as Undici from "@effect/platform-node/Undici";
import { type ExecutionEnvironmentDescriptor, OrchestratorMcpFailure } from "@t3tools/contracts";
import { expect, it } from "@effect/vitest";
import * as Context from "effect/Context";
Expand All @@ -17,7 +18,10 @@ import * as Queue from "effect/Queue";
import * as Ref from "effect/Ref";
import * as Schema from "effect/Schema";
import * as HttpRouter from "effect/http/HttpRouter";
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
import * as Sink from "effect/Sink";
import * as SqlClient from "effect/sql/SqlClient";
import * as Stream from "effect/Stream";
import * as TestClock from "effect/testing/TestClock";
import { Tool, Toolkit } from "effect/ai";
import {
Expand Down Expand Up @@ -89,8 +93,9 @@ const serveB = (descriptor: ExecutionEnvironmentDescriptor) =>
const makeA = (
descriptor: ExecutionEnvironmentDescriptor,
secrets?: typeof ServerSecretStore.layer,
machine?: Layer.Layer<never>,
) =>
layerLinkingEnvironment(descriptor, secrets).pipe(
layerLinkingEnvironment(descriptor, secrets, undefined, machine).pipe(
Layer.build,
Effect.map((context) => ({
links: Context.get(context, PeerLinks.PeerLinks),
Expand Down Expand Up @@ -346,6 +351,106 @@ const serveRaw = <E, R>(
),
);

/** `tailscale status --json` with `peerIps` as another machine on its tailnet. */
const tailscaleStatus = (backendState: string, peerIps: ReadonlyArray<string>, tun = true) =>
JSON.stringify({
BackendState: backendState,
TUN: tun,
Self: { TailscaleIPs: ["100.101.0.1"] },
Peer: { "nodekey:box": { TailscaleIPs: peerIps } },
});

/** A's `tailscale`, whose `status --json` prints what `status` holds at each run. */
const layerTailscale = (status: Ref.Ref<string>) =>
Layer.succeed(
ChildProcessSpawner.ChildProcessSpawner,
ChildProcessSpawner.make(() =>
Ref.get(status).pipe(
Effect.map((json) =>
ChildProcessSpawner.makeHandle({
pid: ChildProcessSpawner.ProcessId(1),
exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(0)),
isRunning: Effect.succeed(false),
kill: () => Effect.void,
unref: Effect.succeed(Effect.void),
stdin: Sink.drain,
stdout: Stream.make(new TextEncoder().encode(json)),
stderr: Stream.empty,
all: Stream.empty,
getInputFd: () => Sink.drain,
getOutputFd: () => Stream.empty,
}),
),
),
),
);

/** Connections to `host` reach 127.0.0.1 instead, as a tailnet route would reach the peer. */
const layerRouteHostToLoopback = (host: string) => {
const connect = Undici.buildConnector({});
return NodeHttpClient.layerUndiciOptions({
connect: (options, callback) =>
connect(
{ ...options, hostname: options.hostname === host ? "127.0.0.1" : options.hostname },
callback,
),
});
};

it.effect(
"links over plain http to a 100.64.0.0/10 address only while Tailscale routes to it as a peer",
() =>
Effect.scoped(
Effect.gen(function* () {
const b = yield* serveB(box);
const port = new URL(b.url).port;
const status = yield* Ref.make(tailscaleStatus("Stopped", ["100.100.1.2"]));
const a = yield* makeA(
laptop,
undefined,
Layer.mergeAll(layerTailscale(status), layerRouteHostToLoopback("100.100.1.2")),
);
const linkTo = (url: string) =>
b.auth
.issuePairingCredential()
.pipe(
Effect.flatMap(({ credential }) =>
a.links.link({ url, pairingCode: credential, access: "auto" }),
),
);

// Stopped or in userspace networking, Tailscale still lists its peers,
// but the OS would not send traffic to them through it.
for (const refused of [
tailscaleStatus("Stopped", ["100.100.1.2"]),
tailscaleStatus("Running", ["100.100.1.2"], false),
// WARP and carrier-grade NAT share the range: an address Tailscale does not list.
tailscaleStatus("Running", ["100.100.9.9"]),
]) {
yield* Ref.set(status, refused);
const error = yield* linkTo(`http://100.100.1.2:${port}`).pipe(Effect.flip);
expect(error.message).toContain("plain http");
}
expect(yield* linkedSessions(b)).toEqual([]);

yield* Ref.set(status, tailscaleStatus("Running", ["100.100.1.2"]));
expect(yield* linkTo(`http://100.100.1.2:${port}/`)).toMatchObject({
environmentId: box.environmentId,
urls: [`http://100.100.1.2:${port}`],
});
yield* probe(a);
expect((yield* Ref.get(b.bearers)).length).toBeGreaterThan(0);

// Once Tailscale stops, the link's token stays here.
yield* Ref.set(status, tailscaleStatus("NeedsLogin", ["100.100.1.2"]));
yield* Ref.set(b.bearers, []);
yield* probe(a).pipe(Effect.flip);
expect(yield* Ref.get(b.bearers)).toEqual([]);
expect((yield* a.links.list).map((link) => link.status)).toEqual(["unreachable"]);
}),
).pipe(Effect.provide(NodeHttpServer.layerTest)),
);

it.effect("never follows a peer's redirect with the pairing code", () =>
Effect.scoped(
Effect.gen(function* () {
Expand Down
5 changes: 4 additions & 1 deletion apps/server/src/peer/PeerLinks.testkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ export type ServedPeer = Effect.Success<ReturnType<typeof servePeer>>;

/**
* The environment that links: its own database and secret store, reaching
* peers over plain HTTP. Provide it to anything built on its links.
* peers over plain HTTP. Provide it to anything built on its links. `machine`
* replaces what this machine provides, such as its `tailscale` or its network.
*/
export const layerLinkingEnvironment = <E = never>(
descriptor: ExecutionEnvironmentDescriptor,
Expand All @@ -205,9 +206,11 @@ export const layerLinkingEnvironment = <E = never>(
SqlClient.SqlClient,
E | Layer.Error<typeof Sqlite.layerMemory>
> = Sqlite.layerMemory,
machine: Layer.Layer<never> = Layer.empty,
) =>
PeerMcpClient.layer.pipe(
Layer.provideMerge(PeerLinks.layer),
Layer.provide(machine),
Layer.provideMerge(ServerSettings.layerTest({ linkedEnvironmentsEnabled: true })),
Layer.provideMerge(database),
Layer.provide(Layer.mergeAll(secrets, FetchHttpClient.layer)),
Expand Down
92 changes: 57 additions & 35 deletions apps/server/src/peer/PeerLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ import * as HttpClient from "effect/http/HttpClient";
import * as HttpClientError from "effect/http/HttpClientError";
import * as HttpApi from "effect/http-api/HttpApi";
import * as HttpApiClient from "effect/http-api/HttpApiClient";
import { isLocalLoopbackHost, isTailnetHost } from "@t3tools/shared/hostClassification";
import {
isLocalLoopbackHost,
isSharedAddressSpaceHost,
isTailnetHost,
} from "@t3tools/shared/hostClassification";
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
import { readTailnetAddresses } from "@t3tools/tailscale";

import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
import * as ServerEnvironment from "../environment/ServerEnvironment.ts";
Expand Down Expand Up @@ -169,37 +175,6 @@ const failStorage = (cause: unknown) =>
),
);

/**
* The base URL a peer is reached at, normalised to its origin. The pairing
* code and then the link's token travel to it, so plain http is accepted only
* where the network already encrypts: this machine, or a Tailscale address.
*/
const peerOrigin = (url: string) => {
const parsed = URL.parse(url);
if (parsed === null || (parsed.protocol !== "https:" && parsed.protocol !== "http:")) {
return Effect.fail(
new PeerLinkError({
reason: "unreachable",
// Never the address as given: it may hold a username, password or query.
message: "That address is not an http or https address.",
}),
);
}
if (
parsed.protocol === "http:" &&
!isLocalLoopbackHost(parsed.hostname) &&
!isTailnetHost(parsed.hostname)
) {
return Effect.fail(
new PeerLinkError({
reason: "unreachable",
message: `${parsed.origin} is plain http, which would send the link's credentials unencrypted. Use its https address, or its Tailscale address.`,
}),
);
}
return Effect.succeed(parsed.origin);
};

/** The system error under a failed connection, such as ECONNREFUSED or ENOTFOUND. */
const connectionCode = (cause: unknown) =>
[cause, Predicate.hasProperty(cause, "cause") ? cause.cause : undefined]
Expand Down Expand Up @@ -251,11 +226,47 @@ const make = Effect.gen(function* () {
const environment = yield* ServerEnvironment.ServerEnvironment;
const serverSettings = yield* ServerSettings.ServerSettingsService;
const isOwnLoopback = yield* LoopbackHost;
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
const enabled = serverSettings.getSettings.pipe(
Effect.map((settings) => settings.linkedEnvironmentsEnabled),
Effect.orElseSucceed(() => false),
);

const isTailnetAddress = (host: string) =>
readTailnetAddresses.pipe(
Effect.map((addresses) => addresses.has(host)),
Effect.orElseSucceed(() => false),
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
);

/**
* A peer's base URL as its origin. Its pairing code and token travel there, so
* plain http only where the network encrypts: this machine, or Tailscale. WARP
* and CGNAT share 100.64.0.0/10, so those count only where Tailscale lists them.
*/
const peerOrigin = Effect.fn("PeerLinks.peerOrigin")(function* (url: string) {
const parsed = URL.parse(url);
if (parsed === null || (parsed.protocol !== "https:" && parsed.protocol !== "http:")) {
return yield* new PeerLinkError({
reason: "unreachable",
// Never the address as given: it may hold a username, password or query.
message: "That address is not an http or https address.",
});
}
if (
parsed.protocol === "http:" &&
!isLocalLoopbackHost(parsed.hostname) &&
!isTailnetHost(parsed.hostname) &&
!(isSharedAddressSpaceHost(parsed.hostname) && (yield* isTailnetAddress(parsed.hostname)))
) {
return yield* new PeerLinkError({
reason: "unreachable",
message: `${parsed.origin} is plain http, which would send the link's credentials unencrypted. Use its https address, or its Tailscale address.`,
});
}
return parsed.origin;
});

/** A fresh name for one link's token; the row records it. */
const newSecretName = random.randomBytes(16).pipe(
Effect.map((bytes) => `peer-link-${Hex.encode(bytes)}`),
Expand Down Expand Up @@ -632,9 +643,18 @@ const make = Effect.gen(function* () {
if (link.urls.length === 0) return { ...link, status: "unreachable" };
const answered = yield* Effect.raceAll(
link.urls.map((url) =>
answersAs(url, link.environmentId).pipe(Effect.flatMap(Effect.fromOption)),
// Listed as reachable only where resolve would still use it. The probe
// budget starts after that check: `tailscale status` can take most of it.
peerOrigin(url).pipe(
Effect.flatMap((origin) =>
answersAs(origin, link.environmentId).pipe(
Effect.flatMap(Effect.fromOption),
Effect.timeout(LIST_PROBE_TIMEOUT),
),
),
),
),
).pipe(Effect.timeoutOption(LIST_PROBE_TIMEOUT), Effect.orElseSucceed(Option.none));
).pipe(Effect.option);
if (Option.isNone(answered)) return { ...link, status: "unreachable" };
return { ...(yield* withLabel(link, answered.value.label)), status: "reachable" };
});
Expand Down Expand Up @@ -759,7 +779,9 @@ const make = Effect.gen(function* () {
? Option.none()
: yield* Effect.raceAll(
link.urls.map((url) =>
answersAs(url, environmentId).pipe(
// A 100.x address is trusted only while Tailscale still lists it.
peerOrigin(url).pipe(
Effect.flatMap((origin) => answersAs(origin, environmentId)),
Effect.flatMap(Effect.fromOption),
Effect.map((peer) => ({ url, peer })),
),
Expand Down
58 changes: 46 additions & 12 deletions packages/tailscale/src/tailscale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,23 +308,30 @@ export const readTailscaleStatus = readTailscaleOutput("status", ["status", "--j
Effect.flatMap(parseTailscaleStatus),
);

const TailscaleExitNodeJson = Schema.Struct({
const TailscalePeersJson = Schema.Struct({
BackendState: Schema.optional(Schema.Unknown),
TUN: Schema.optional(Schema.Unknown),
// Go prints a nil Self or Peer as null: logged out, or no other machines.
Self: Schema.optional(
Schema.NullOr(Schema.Struct({ TailscaleIPs: Schema.optional(Schema.Unknown) })),
),
ExitNodeStatus: Schema.optional(Schema.Struct({ Online: Schema.optional(Schema.Unknown) })),
Peer: Schema.optional(
Schema.Record(
Schema.String,
Schema.Struct({
ExitNode: Schema.optional(Schema.Unknown),
HostName: Schema.optional(Schema.Unknown),
DNSName: Schema.optional(Schema.Unknown),
}),
Schema.NullOr(
Schema.Record(
Schema.String,
Schema.Struct({
ExitNode: Schema.optional(Schema.Unknown),
HostName: Schema.optional(Schema.Unknown),
DNSName: Schema.optional(Schema.Unknown),
TailscaleIPs: Schema.optional(Schema.Unknown),
}),
),
),
),
});

const decodeTailscaleExitNodeJson = Schema.decodeEffect(
Schema.fromJsonString(TailscaleExitNodeJson),
);
const decodeTailscalePeersJson = Schema.decodeEffect(Schema.fromJsonString(TailscalePeersJson));

export interface TailscaleExitNode {
readonly name: string | null;
Expand All @@ -338,7 +345,7 @@ export interface TailscaleExitNode {
export const parseTailscaleExitNode = (
rawStatusJson: string,
): Effect.Effect<TailscaleExitNode | null, TailscaleStatusParseError> =>
decodeTailscaleExitNodeJson(rawStatusJson).pipe(
decodeTailscalePeersJson(rawStatusJson).pipe(
Effect.mapError((cause) => new TailscaleStatusParseError({ cause })),
Effect.map((parsed) => {
if (parsed.ExitNodeStatus === undefined) return null;
Expand All @@ -359,6 +366,33 @@ export const readTailscaleExitNode = readTailscaleOutput("status", ["status", "-
Effect.flatMap(parseTailscaleExitNode),
);

/**
* The addresses of this machine and every other machine on its tailnet. Empty
* unless Tailscale runs with its TUN device: stopped or in userspace networking,
* it still lists them, but the OS would send traffic to them unencrypted.
*/
const parseTailnetAddresses = (
rawStatusJson: string,
): Effect.Effect<ReadonlySet<string>, TailscaleStatusParseError> =>
decodeTailscalePeersJson(rawStatusJson).pipe(
Effect.mapError((cause) => new TailscaleStatusParseError({ cause })),
Effect.map((parsed) => {
if (parsed.BackendState !== "Running" || parsed.TUN !== true) return new Set<string>();
const nodes = [...(parsed.Self ? [parsed.Self] : []), ...Object.values(parsed.Peer ?? {})];
return new Set(
nodes.flatMap((node) =>
Array.isArray(node.TailscaleIPs)
? node.TailscaleIPs.filter((address): address is string => typeof address === "string")
: [],
),
);
}),
);

export const readTailnetAddresses = readTailscaleOutput("status", ["status", "--json"]).pipe(
Effect.flatMap(parseTailnetAddresses),
);

const TailscalePrefsJson = Schema.Struct({
RunSSH: Schema.optional(Schema.Unknown),
WantRunning: Schema.optional(Schema.Unknown),
Expand Down
Loading