Open History to see the last commit
1import type * as NodeOS from "node:os";
2
3import { describe, expect, it } from "@effect/vitest";
4
5import {
6 acceptsPrivateNetworkConnection,
7 resolveHeadlessConnectionString,
8} from "./startupAccess.ts";
9
10const ipv4 = (address: string, internal = false): NodeOS.NetworkInterfaceInfo => ({
11 address,
12 netmask: "255.255.255.0",
13 family: "IPv4",
14 mac: "00:00:00:00:00:00",
15 internal,
16 cidr: `${address}/24`,
17});
18
19it("resolves wildcard hosts to a concrete external interface when one is available", () => {
20 const connectionString = resolveHeadlessConnectionString("0.0.0.0", 3773, {
21 interfaces: { en0: [ipv4("192.168.1.42")], lo0: [ipv4("127.0.0.1", true)] },
22 });
23
24 expect(connectionString).toBe("http://192.168.1.42:3773");
25});
26
27it("skips public and container addresses for a private-network listener", () => {
28 const interfaces = {
29 eth0: [ipv4("203.0.113.7")],
30 docker0: [ipv4("172.17.0.1")],
31 bridge200: [ipv4("192.168.64.1")],
32 eth1: [ipv4("10.0.0.5")],
33 };
34
35 expect(resolveHeadlessConnectionString("0.0.0.0", 3773, { interfaces })).toBe(
36 "http://203.0.113.7:3773",
37 );
38 expect(
39 resolveHeadlessConnectionString("0.0.0.0", 3773, { interfaces, privateNetworkOnly: true }),
40 ).toBe("http://10.0.0.5:3773");
41 expect(
42 resolveHeadlessConnectionString("0.0.0.0", 3773, {
43 interfaces: { eth0: [ipv4("203.0.113.7")] },
44 privateNetworkOnly: true,
45 }),
46 ).toBe("http://localhost:3773");
47});
48
49it("prefers a LAN address over a tailnet one for a private-network pairing URL", () => {
50 expect(
51 resolveHeadlessConnectionString("0.0.0.0", 3773, {
52 interfaces: { tailscale0: [ipv4("100.101.102.103")], en0: [ipv4("192.168.1.42")] },
53 privateNetworkOnly: true,
54 }),
55 ).toBe("http://192.168.1.42:3773");
56});
57
58describe("acceptsPrivateNetworkConnection", () => {
59 const accepts = (localAddress: string | undefined, remoteAddress: string | undefined) =>
60 acceptsPrivateNetworkConnection({ localAddress, remoteAddress });
61
62 it("serves loopback, LAN, and tailnet clients", () => {
63 expect(accepts("127.0.0.1", "127.0.0.1")).toBe(true);
64 expect(accepts("::ffff:192.168.1.20", "::ffff:192.168.1.31")).toBe(true);
65 expect(accepts("100.101.102.103", "100.90.1.2")).toBe(true);
66 });
67
68 it("closes connections from or to a public address", () => {
69 // A cloud VM's public address is NATed to a private interface address.
70 expect(accepts("172.31.4.10", "203.0.113.7")).toBe(false);
71 expect(accepts("203.0.113.7", "192.168.1.31")).toBe(false);
72 expect(accepts("169.254.10.1", "169.254.10.2")).toBe(false);
73 expect(accepts(undefined, "192.168.1.31")).toBe(false);
74 });
75});