Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

startupAccess.test.ts

175 lines6.3 KB

1// @effect-diagnostics nodeBuiltinImport:off
2import * as NodeHttp from "node:http";
3import * as NodeNet from "node:net";
4import type * as NodeOS from "node:os";
5​
6import { describe, expect, it } from "@effect/vitest";
7import * as Effect from "effect/Effect";
8import * as Logger from "effect/Logger";
9import * as NetAddress from "effect/net/NetAddress";
10import * as TestClock from "effect/testing/TestClock";
11​
12import {
13 acceptsPrivateNetworkConnection,
14 closeNonPrivateNetworkConnections,
15 loopbackUrlHostForAddress,
16 resolveHeadlessConnectionString,
17} from "./startupAccess.ts";
18​
19const ipv4 = (address: string, internal = false): NodeOS.NetworkInterfaceInfo => ({
20 address,
21 netmask: "255.255.255.0",
22 family: "IPv4",
23 mac: "00:00:00:00:00:00",
24 internal,
25 cidr: `${address}/24`,
26});
27​
28it("resolves wildcard hosts to a concrete external interface when one is available", () => {
29 const connectionString = resolveHeadlessConnectionString("0.0.0.0", 3773, {
30 interfaces: { en0: [ipv4("192.168.1.42")], lo0: [ipv4("127.0.0.1", true)] },
31 platform: "linux",
32 });
33​
34 expect(connectionString).toBe("http://192.168.1.42:3773");
35});
36​
37it("skips public and container addresses for a private-network listener", () => {
38 const interfaces = {
39 eth0: [ipv4("203.0.113.7")],
40 docker0: [ipv4("172.17.0.1")],
41 bridge200: [ipv4("192.168.64.1")],
42 eth1: [ipv4("10.0.0.5")],
43 };
44​
45 expect(resolveHeadlessConnectionString("0.0.0.0", 3773, { interfaces, platform: "linux" })).toBe(
46 "http://203.0.113.7:3773",
47 );
48 expect(
49 resolveHeadlessConnectionString("0.0.0.0", 3773, {
50 interfaces,
51 privateNetworkOnly: true,
52 platform: "linux",
53 }),
54 ).toBe("http://10.0.0.5:3773");
55 expect(
56 resolveHeadlessConnectionString("0.0.0.0", 3773, {
57 interfaces: { eth0: [ipv4("203.0.113.7")] },
58 privateNetworkOnly: true,
59 platform: "linux",
60 }),
61 ).toBe("http://localhost:3773");
62});
63​
64it("prefers a LAN address over a tailnet one for a private-network pairing URL", () => {
65 expect(
66 resolveHeadlessConnectionString("0.0.0.0", 3773, {
67 interfaces: { tailscale0: [ipv4("100.101.102.103")], en0: [ipv4("192.168.1.42")] },
68 privateNetworkOnly: true,
69 platform: "linux",
70 }),
71 ).toBe("http://192.168.1.42:3773");
72});
73​
74it("skips Windows VirtualBox and VMware host-only adapters for a private-network listener", () => {
75 expect(
76 resolveHeadlessConnectionString("0.0.0.0", 3773, {
77 interfaces: {
78 "VirtualBox Host-Only Network": [{ ...ipv4("192.168.56.1"), mac: "0a:00:27:00:00:0a" }],
79 "VMware Network Adapter VMnet8": [{ ...ipv4("192.168.159.1"), mac: "00:50:56:c0:00:08" }],
80 "Wi-Fi": [{ ...ipv4("192.168.1.20"), mac: "3c:22:fb:12:34:56" }],
81 },
82 privateNetworkOnly: true,
83 platform: "win32",
84 }),
85 ).toBe("http://192.168.1.20:3773");
86});
87​
88it("skips NixOS container and systemd-nspawn veths for a private-network pairing URL", () => {
89 expect(
90 resolveHeadlessConnectionString("0.0.0.0", 3773, {
91 interfaces: { "ve-jellyfin": [ipv4("192.168.53.193")], eth0: [ipv4("192.168.0.18")] },
92 privateNetworkOnly: true,
93 platform: "linux",
94 }),
95 ).toBe("http://192.168.0.18:3773");
96});
97​
98it("targets the bound interface when the listener binds a specific address", () => {
99 const hostFor = (ip: string) =>
100 loopbackUrlHostForAddress(NetAddress.inetAddressFromIpStringUnsafe(ip, 3773));
101​
102 expect(hostFor("0.0.0.0")).toBe("127.0.0.1");
103 expect(hostFor("::")).toBe("127.0.0.1");
104 expect(hostFor("::1")).toBe("[::1]");
105 expect(hostFor("127.0.0.1")).toBe("127.0.0.1");
106 expect(hostFor("127.0.0.2")).toBe("127.0.0.2");
107 expect(hostFor("100.101.102.103")).toBe("100.101.102.103");
108});
109​
110describe("acceptsPrivateNetworkConnection", () => {
111 const accepts = (localAddress: string | undefined, remoteAddress: string | undefined) =>
112 acceptsPrivateNetworkConnection({ localAddress, remoteAddress });
113​
114 it("serves loopback, LAN, and tailnet clients", () => {
115 expect(accepts("127.0.0.1", "127.0.0.1")).toBe(true);
116 expect(accepts("::ffff:192.168.1.20", "::ffff:192.168.1.31")).toBe(true);
117 expect(accepts("100.101.102.103", "100.90.1.2")).toBe(true);
118 });
119​
120 it("closes connections from or to a public address", () => {
121 // A cloud VM's public address is NATed to a private interface address.
122 expect(accepts("172.31.4.10", "203.0.113.7")).toBe(false);
123 expect(accepts("203.0.113.7", "192.168.1.31")).toBe(false);
124 expect(accepts("169.254.10.1", "169.254.10.2")).toBe(false);
125 expect(accepts(undefined, "192.168.1.31")).toBe(false);
126 });
127});
128​
129describe("closeNonPrivateNetworkConnections", () => {
130 it.effect("logs the local and remote address of a connection it closes", () =>
131 Effect.gen(function* () {
132 const warnings: Array<Logger.Options<unknown>> = [];
133 const server = NodeHttp.createServer();
134 yield* closeNonPrivateNetworkConnections(server).pipe(
135 Effect.provide(
136 Logger.layer([Logger.make((entry) => warnings.push(entry))], {
137 mergeWithExisting: false,
138 }),
139 ),
140 );
141​
142 // An unconnected socket: Node reports neither address.
143 const socket = new NodeNet.Socket();
144 server.emit("connection", socket);
145​
146 expect(socket.destroyed).toBe(true);
147 expect(warnings.map((entry) => entry.logLevel)).toEqual(["Warn"]);
148 const line = Logger.formatLogFmt.log(warnings[0]!);
149 expect(line).toMatch(/localAddress\W+undefined/);
150 expect(line).toMatch(/remoteAddress\W+undefined/);
151 }),
152 );
153​
154 it.effect("logs a flood of closed connections at most once a minute, with a count", () =>
155 Effect.gen(function* () {
156 const warnings: Array<Logger.Options<unknown>> = [];
157 const server = NodeHttp.createServer();
158 yield* closeNonPrivateNetworkConnections(server).pipe(
159 Effect.provide(
160 Logger.layer([Logger.make((entry) => warnings.push(entry))], {
161 mergeWithExisting: false,
162 }),
163 ),
164 );
165 const sockets = Array.from({ length: 1000 }, () => new NodeNet.Socket());
166 for (const socket of sockets) server.emit("connection", socket);
167 yield* TestClock.adjust("1 minute");
168 server.emit("connection", new NodeNet.Socket());
169​
170 expect(sockets.every((socket) => socket.destroyed)).toBe(true);
171 expect(warnings).toHaveLength(2);
172 expect(Logger.formatLogFmt.log(warnings[1]!)).toMatch(/closedSinceLastLog\W+999/);
173 }),
174 );
175});