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

startupAccess.test.ts

106 lines3.6 KB

1import type * as NodeOS from "node:os";
2​
3import { describe, expect, it } from "@effect/vitest";
4import * as NetAddress from "effect/net/NetAddress";
5​
6import {
7 acceptsPrivateNetworkConnection,
8 loopbackUrlHostForAddress,
9 resolveHeadlessConnectionString,
10} from "./startupAccess.ts";
11​
12const ipv4 = (address: string, internal = false): NodeOS.NetworkInterfaceInfo => ({
13 address,
14 netmask: "255.255.255.0",
15 family: "IPv4",
16 mac: "00:00:00:00:00:00",
17 internal,
18 cidr: `${address}/24`,
19});
20​
21it("resolves wildcard hosts to a concrete external interface when one is available", () => {
22 const connectionString = resolveHeadlessConnectionString("0.0.0.0", 3773, {
23 interfaces: { en0: [ipv4("192.168.1.42")], lo0: [ipv4("127.0.0.1", true)] },
24 platform: "linux",
25 });
26​
27 expect(connectionString).toBe("http://192.168.1.42:3773");
28});
29​
30it("skips public and container addresses for a private-network listener", () => {
31 const interfaces = {
32 eth0: [ipv4("203.0.113.7")],
33 docker0: [ipv4("172.17.0.1")],
34 bridge200: [ipv4("192.168.64.1")],
35 eth1: [ipv4("10.0.0.5")],
36 };
37​
38 expect(resolveHeadlessConnectionString("0.0.0.0", 3773, { interfaces, platform: "linux" })).toBe(
39 "http://203.0.113.7:3773",
40 );
41 expect(
42 resolveHeadlessConnectionString("0.0.0.0", 3773, {
43 interfaces,
44 privateNetworkOnly: true,
45 platform: "linux",
46 }),
47 ).toBe("http://10.0.0.5:3773");
48 expect(
49 resolveHeadlessConnectionString("0.0.0.0", 3773, {
50 interfaces: { eth0: [ipv4("203.0.113.7")] },
51 privateNetworkOnly: true,
52 platform: "linux",
53 }),
54 ).toBe("http://localhost:3773");
55});
56​
57it("prefers a LAN address over a tailnet one for a private-network pairing URL", () => {
58 expect(
59 resolveHeadlessConnectionString("0.0.0.0", 3773, {
60 interfaces: { tailscale0: [ipv4("100.101.102.103")], en0: [ipv4("192.168.1.42")] },
61 privateNetworkOnly: true,
62 platform: "linux",
63 }),
64 ).toBe("http://192.168.1.42:3773");
65});
66​
67it("skips NixOS container and systemd-nspawn veths for a private-network pairing URL", () => {
68 expect(
69 resolveHeadlessConnectionString("0.0.0.0", 3773, {
70 interfaces: { "ve-jellyfin": [ipv4("192.168.53.193")], eth0: [ipv4("192.168.0.18")] },
71 privateNetworkOnly: true,
72 platform: "linux",
73 }),
74 ).toBe("http://192.168.0.18:3773");
75});
76​
77it("targets the bound interface when the listener binds a specific address", () => {
78 const hostFor = (ip: string) =>
79 loopbackUrlHostForAddress(NetAddress.inetAddressFromIpStringUnsafe(ip, 3773));
80​
81 expect(hostFor("0.0.0.0")).toBe("127.0.0.1");
82 expect(hostFor("::")).toBe("127.0.0.1");
83 expect(hostFor("::1")).toBe("[::1]");
84 expect(hostFor("127.0.0.1")).toBe("127.0.0.1");
85 expect(hostFor("127.0.0.2")).toBe("127.0.0.2");
86 expect(hostFor("100.101.102.103")).toBe("100.101.102.103");
87});
88​
89describe("acceptsPrivateNetworkConnection", () => {
90 const accepts = (localAddress: string | undefined, remoteAddress: string | undefined) =>
91 acceptsPrivateNetworkConnection({ localAddress, remoteAddress });
92​
93 it("serves loopback, LAN, and tailnet clients", () => {
94 expect(accepts("127.0.0.1", "127.0.0.1")).toBe(true);
95 expect(accepts("::ffff:192.168.1.20", "::ffff:192.168.1.31")).toBe(true);
96 expect(accepts("100.101.102.103", "100.90.1.2")).toBe(true);
97 });
98​
99 it("closes connections from or to a public address", () => {
100 // A cloud VM's public address is NATed to a private interface address.
101 expect(accepts("172.31.4.10", "203.0.113.7")).toBe(false);
102 expect(accepts("203.0.113.7", "192.168.1.31")).toBe(false);
103 expect(accepts("169.254.10.1", "169.254.10.2")).toBe(false);
104 expect(accepts(undefined, "192.168.1.31")).toBe(false);
105 });
106});