Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions conformance/function/relay-selection.json
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,76 @@
"function": "relay-selection.selectRelay",
"id": "breaks-a-tie-by-lowest-device-id",
"kind": "function"
},
{
"args": [
{
"localNetworks": [
"10.34.16.48/23",
"100.88.132.111/32",
"fe80::1/64"
],
"offers": [
{
"addresses": [
"ws://192.168.1.39:36937/",
"ws://172.18.0.1:36937/",
"ws://100.123.240.67:36937/"
],
"deviceHex": "1111111111111111111111111111111111111111111111111111111111111111",
"machineLocal": false
}
],
"own": "0000000000000000000000000000000000000000000000000000000000000000",
"publicHub": "wss://mesh.example.com/",
"trusted": [
"1111111111111111111111111111111111111111111111111111111111111111"
]
}
],
"expect": {
"deviceHex": "1111111111111111111111111111111111111111111111111111111111111111",
"tier": "trusted",
"url": "ws://100.123.240.67:36937/"
},
"from": "relay-selection.unit.test.ts: selectRelay > skips private addresses on networks this host is not on",
"function": "relay-selection.selectRelay",
"id": "skips-private-addresses-off-local-networks",
"kind": "function"
},
{
"args": [
{
"localNetworks": [
"192.168.1.20/24"
],
"offers": [
{
"addresses": [
"ws://192.168.1.39:36937/",
"ws://172.18.0.1:36937/",
"ws://100.123.240.67:36937/"
],
"deviceHex": "1111111111111111111111111111111111111111111111111111111111111111",
"machineLocal": false
}
],
"own": "0000000000000000000000000000000000000000000000000000000000000000",
"publicHub": "wss://mesh.example.com/",
"trusted": [
"1111111111111111111111111111111111111111111111111111111111111111"
]
}
],
"expect": {
"deviceHex": "1111111111111111111111111111111111111111111111111111111111111111",
"tier": "lan",
"url": "ws://192.168.1.39:36937/"
},
"from": "relay-selection.unit.test.ts: selectRelay > still prefers a private address on a network this host is on",
"function": "relay-selection.selectRelay",
"id": "prefers-private-address-on-local-network",
"kind": "function"
}
]
}
2 changes: 1 addition & 1 deletion conformance/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"skip": []
},
"function/relay-selection.json": {
"cases": 10,
"cases": 12,
"skip": []
},
"function/request-timeouts.json": {
Expand Down
7 changes: 6 additions & 1 deletion src/core/relay-role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ import type { ElectionSessions } from "./election-sessions.js";
import type { GatewayTrustReader } from "./gateway-trust.js";
import type { TransportEvents } from "./transport.js";
import { RelayServer } from "./relay-server.js";
import { selectRelay, type RelayOffer } from "./relay-selection.js";
import {
localNetworkCidrs,
selectRelay,
type RelayOffer,
} from "./relay-selection.js";

export interface RelayRoleDeps {
/** The interface a served relay listens on (RelayServerOptions.host). */
Expand Down Expand Up @@ -106,6 +110,7 @@ export class RelayRole {
isTrusted: (deviceHex) => this.deps.gatewayTrust.isReachable(deviceHex),
ownDeviceHex: this.deps.ownDeviceHex,
publicHubUrl,
localNetworks: localNetworkCidrs(),
}).url;
}

Expand Down
53 changes: 52 additions & 1 deletion src/core/relay-selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import { BlockList, isIPv4, isIPv6 } from "node:net";
import * as os from "node:os";
import { compareCodeUnits } from "./ordering.js";

/** A relay offer this store has heard, with what it knows about the device making it. */
Expand Down Expand Up @@ -36,6 +37,53 @@ export interface RelaySelectionInput {
ownDeviceHex: string;
/** The configured public hub, the fallback when no offer qualifies. */
publicHubUrl: string;
/** The CIDRs of this host's own network interfaces (localNetworkCidrs). When given, a private or link-local address outside every one of them is skipped in every tier: it belongs to a network this host is not on, so dialling it can only time out (a device offering its home-LAN address to a peer on another network over a tailnet). When absent, every private address counts as local, as before. */
localNetworks?: readonly string[] | undefined;
}

/** The CIDRs of this host's non-internal interfaces, as os.networkInterfaces reports them: the input selectRelay's localNetworks expects. */
export function localNetworkCidrs(
interfaces: NodeJS.Dict<os.NetworkInterfaceInfo[]> = os.networkInterfaces(),
): string[] {
const result: string[] = [];
for (const addresses of Object.values(interfaces)) {
for (const address of addresses ?? []) {
if (!address.internal && address.cidr !== null) result.push(address.cidr);
}
}
return result;
}

/** A BlockList of the given CIDRs, or undefined when none were given (selection then treats every private address as local). Malformed entries are ignored. */
function localNetworkList(
cidrs: readonly string[] | undefined,
): BlockList | undefined {
if (cidrs === undefined) return undefined;
const list = new BlockList();
for (const cidr of cidrs) {
const [address, prefix] = cidr.split("/");
if (address === undefined || prefix === undefined) continue;
const family = isIPv4(address)
? "ipv4"
: isIPv6(address)
? "ipv6"
: undefined;
if (family === undefined) continue;
try {
list.addSubnet(address, Number(prefix), family);
} catch {
// An out-of-range prefix: not a network this host can be judged to be on.
}
}
return list;
}

/** Whether a private host sits on a network this host is on, judged against localNetworks; true when localNetworks was not given. */
function onLocalNetwork(host: string, local: BlockList | undefined): boolean {
if (local === undefined) return true;
if (isIPv4(host)) return local.check(host, "ipv4");
if (isIPv6(host)) return local.check(host, "ipv6");
return true;
}

/** The private (RFC 1918), IPv4 link-local (RFC 3927), IPv6 unique-local (RFC 4193) and IPv6 link-local ranges: addresses reachable on the local network and nowhere else. */
Expand Down Expand Up @@ -82,10 +130,12 @@ function relayHost(address: string): string | undefined {
function addressFor(
offer: Readonly<RelayOffer>,
tier: RelayTier,
local: BlockList | undefined,
): string | undefined {
for (const address of offer.addresses) {
const host = relayHost(address);
if (host === undefined) continue;
if (isPrivateHost(host) && !onLocalNetwork(host, local)) continue;
if (tier === "machine" && isLoopbackHost(host)) return address;
if (tier === "lan" && isPrivateHost(host)) return address;
if (tier === "trusted" && !isLoopbackHost(host)) return address;
Expand All @@ -106,14 +156,15 @@ export function selectRelay(
const eligible = input.offers
.filter((offer) => offer.deviceHex !== input.ownDeviceHex)
.toSorted((a, b) => compareCodeUnits(a.deviceHex, b.deviceHex));
const local = localNetworkList(input.localNetworks);
for (const tier of OFFER_TIERS) {
for (const offer of eligible) {
const admitted =
tier === "machine"
? offer.machineLocal
: input.isTrusted(offer.deviceHex);
if (!admitted) continue;
const url = addressFor(offer, tier);
const url = addressFor(offer, tier, local);
if (url !== undefined) return { url, tier, deviceHex: offer.deviceHex };
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/test/conformance-functions.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,10 @@ function relaySelection(value: unknown): unknown {
isTrusted: (hex) => trusted.includes(hex),
ownDeviceHex: str(input.own),
publicHubUrl: str(input.publicHub),
localNetworks:
input.localNetworks === undefined
? undefined
: strings(input.localNetworks),
});
}

Expand Down