mirror of
https://github.com/pithings/unkernel.git
synced 2026-12-02 01:20:23 +08:00
The guest joins a tailnet from Node or the page: ts2021 control over WebSocket, DERP relays, WireGuard and the packet filter in JS, loaded as a separate chunk only when the option is used. SDK net option, CLI --tailscale* flags and a page panel. Relay only; exit nodes, IPv6 and real peer traffic are not covered yet.
99 lines
3.0 KiB
TypeScript
99 lines
3.0 KiB
TypeScript
// SPDX-License-Identifier: MIT
|
|
// Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>
|
|
// A FIFO of byte chunks for length-prefixed framing (control's map
|
|
// stream, DERP frames): a message that spans many chunks is copied once,
|
|
// when it is complete, instead of the whole buffer on every chunk. Also
|
|
// widenConnectAttempts() (Node's connect timeout per address).
|
|
|
|
const EMPTY = new Uint8Array(0);
|
|
|
|
export class ChunkQueue {
|
|
#chunks: Uint8Array[] = [];
|
|
/** Offset into chunks[0]. */
|
|
#head = 0;
|
|
length = 0;
|
|
|
|
push(chunk: Uint8Array): void {
|
|
if (!chunk.length) return;
|
|
this.#chunks.push(chunk);
|
|
this.length += chunk.length;
|
|
}
|
|
|
|
/** The first n bytes (n <= length), without consuming them; a view when they sit in one chunk. */
|
|
peek(n: number): Uint8Array {
|
|
const c = this.#chunks[0];
|
|
if (!c || n === 0) return EMPTY;
|
|
if (c.length - this.#head >= n) return c.subarray(this.#head, this.#head + n);
|
|
return this.#copy(n, false);
|
|
}
|
|
|
|
/** Removes and returns the first n bytes (n <= length); a view when they sit in one chunk. */
|
|
take(n: number): Uint8Array {
|
|
const c = this.#chunks[0];
|
|
if (!c || n === 0) return EMPTY;
|
|
if (c.length - this.#head >= n) {
|
|
const out = c.subarray(this.#head, this.#head + n);
|
|
this.#drop(n);
|
|
return out;
|
|
}
|
|
return this.#copy(n, true);
|
|
}
|
|
|
|
clear(): void {
|
|
this.#chunks = [];
|
|
this.#head = 0;
|
|
this.length = 0;
|
|
}
|
|
|
|
#copy(n: number, consume: boolean): Uint8Array {
|
|
const out = new Uint8Array(n);
|
|
let o = 0, i = 0, pos = this.#head;
|
|
while (o < n) {
|
|
const c = this.#chunks[i++]!;
|
|
const k = Math.min(n - o, c.length - pos);
|
|
out.set(c.subarray(pos, pos + k), o);
|
|
o += k;
|
|
pos = 0;
|
|
}
|
|
if (consume) this.#drop(n);
|
|
return out;
|
|
}
|
|
|
|
#drop(n: number): void {
|
|
this.length -= n;
|
|
while (n > 0) {
|
|
const c = this.#chunks[0]!;
|
|
const avail = c.length - this.#head;
|
|
if (n < avail) {
|
|
this.#head += n;
|
|
return;
|
|
}
|
|
n -= avail;
|
|
this.#chunks.shift();
|
|
this.#head = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
/** The per-address connect timeout the Tailscale client needs at least on Node (ms). */
|
|
export const CONNECT_ATTEMPT_MS = 2500;
|
|
|
|
/**
|
|
* On Node: raises the process-wide Happy Eyeballs attempt timeout to
|
|
* CONNECT_ATTEMPT_MS (never lowers it). Node gives each address of a host
|
|
* 250 ms and then abandons it for the next (a timeout, not a head start),
|
|
* so a server farther than that (a DERP region on another continent:
|
|
* Sydney from Europe is 320 ms) never connects where its IPv6 address is
|
|
* unreachable, and the client dials every region's server. Elsewhere it
|
|
* does nothing.
|
|
*/
|
|
export function widenConnectAttempts(): void {
|
|
const net = (globalThis as { process?: { getBuiltinModule?(id: string): unknown } }).process?.getBuiltinModule?.("node:net") as
|
|
{ getDefaultAutoSelectFamilyAttemptTimeout?(): number; setDefaultAutoSelectFamilyAttemptTimeout?(ms: number): void } | undefined;
|
|
try {
|
|
if (net?.getDefaultAutoSelectFamilyAttemptTimeout && net.getDefaultAutoSelectFamilyAttemptTimeout() < CONNECT_ATTEMPT_MS) net.setDefaultAutoSelectFamilyAttemptTimeout?.(CONNECT_ATTEMPT_MS);
|
|
} catch {
|
|
// not Node's net
|
|
}
|
|
}
|