mirror of
https://github.com/pithings/unkernel.git
synced 2026-12-06 03:30:22 +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.
191 lines
6.9 KiB
TypeScript
191 lines
6.9 KiB
TypeScript
// SPDX-License-Identifier: MIT
|
|
// Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>
|
|
// Tailscale disco messages (disco/disco.go) as they arrive over DERP:
|
|
// "TS💬" + sender disco key (32) + nonce (24) + naclbox([type][version]
|
|
// body). Parses Ping, Pong and CallMeMaybe and builds Pings and Pongs, so
|
|
// a relay-only client can answer the pings peers send through DERP.
|
|
|
|
import { naclBoxBeforeNm, naclBoxOpenAfterNm, naclBoxSealAfterNm, randomBytes, x25519Public } from "./crypto.ts";
|
|
|
|
export const DISCO_MAGIC = new Uint8Array([0x54, 0x53, 0xf0, 0x9f, 0x92, 0xac]);
|
|
export const DISCO_TYPE_PING = 0x01;
|
|
export const DISCO_TYPE_PONG = 0x02;
|
|
export const DISCO_TYPE_CALL_ME_MAYBE = 0x03;
|
|
|
|
const KEY_LEN = 32;
|
|
const NONCE_LEN = 24;
|
|
const HEADER_LEN = DISCO_MAGIC.length + KEY_LEN + NONCE_LEN;
|
|
const EP_LEN = 18;
|
|
|
|
// An IP (16 bytes, IPv4 as v4-mapped IPv6) and port.
|
|
export interface DiscoAddrPort {
|
|
ip: Uint8Array;
|
|
port: number;
|
|
}
|
|
|
|
export type DiscoMessage =
|
|
| { type: "ping"; txid: Uint8Array; nodeKey: Uint8Array | null; padding: number }
|
|
| { type: "pong"; txid: Uint8Array; src: DiscoAddrPort }
|
|
| { type: "callMeMaybe"; endpoints: DiscoAddrPort[] }
|
|
| { type: "other"; msgType: number; version: number; body: Uint8Array };
|
|
|
|
export function looksLikeDisco(p: Uint8Array): boolean {
|
|
if (p.length < HEADER_LEN) return false;
|
|
for (let i = 0; i < DISCO_MAGIC.length; i++) if (p[i] !== DISCO_MAGIC[i]) return false;
|
|
return true;
|
|
}
|
|
|
|
// The sender's disco public key, or null if p is not a disco packet.
|
|
export function discoSender(p: Uint8Array): Uint8Array | null {
|
|
return looksLikeDisco(p) ? p.subarray(DISCO_MAGIC.length, DISCO_MAGIC.length + KEY_LEN) : null;
|
|
}
|
|
|
|
// Where magicsock says a DERP-relayed packet came from: 127.3.3.40:<region>.
|
|
export function derpMagicAddr(regionId: number): DiscoAddrPort {
|
|
return { ip: v4Mapped(127, 3, 3, 40), port: regionId & 0xffff };
|
|
}
|
|
|
|
export function v4Mapped(a: number, b: number, c: number, d: number): Uint8Array {
|
|
return new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, a, b, c, d]);
|
|
}
|
|
|
|
// Our disco key pair, with the per-peer box keys cached (least recently
|
|
// used out past 1024).
|
|
export class DiscoKey {
|
|
readonly publicKey: Uint8Array;
|
|
/** X25519 computations made for box keys (each costs about a millisecond). */
|
|
readonly stats = { sharedKeys: 0 };
|
|
#priv: Uint8Array;
|
|
#shared = new Map<string, Uint8Array>();
|
|
|
|
constructor(privateKey: Uint8Array = randomBytes(32)) {
|
|
this.#priv = privateKey.slice();
|
|
this.publicKey = x25519Public(this.#priv);
|
|
}
|
|
|
|
#key(peer: Uint8Array): Uint8Array {
|
|
const id = hex(peer);
|
|
let k = this.#shared.get(id);
|
|
if (k) {
|
|
this.#shared.delete(id);
|
|
} else {
|
|
if (this.#shared.size >= 1024) this.#shared.delete(this.#shared.keys().next().value!);
|
|
k = naclBoxBeforeNm(peer, this.#priv);
|
|
this.stats.sharedKeys++;
|
|
}
|
|
this.#shared.set(id, k);
|
|
return k;
|
|
}
|
|
|
|
// Opens a disco packet: its sender's disco key and the message, or
|
|
// null if it is not one or does not authenticate. With `from`, a
|
|
// packet naming another sender is refused before any key is computed.
|
|
open(p: Uint8Array, from?: Uint8Array): { sender: Uint8Array; message: DiscoMessage } | null {
|
|
const sender = discoSender(p);
|
|
if (!sender || (from && !sameBytes(sender, from))) return null;
|
|
const nonce = p.subarray(DISCO_MAGIC.length + KEY_LEN, HEADER_LEN);
|
|
const payload = naclBoxOpenAfterNm(this.#key(sender), nonce, p.subarray(HEADER_LEN));
|
|
if (!payload) return null;
|
|
const message = parseDiscoMessage(payload);
|
|
return message && { sender: sender.slice(), message };
|
|
}
|
|
|
|
// Wraps an encoded message (encodePing/encodePong/...) for peer.
|
|
seal(peer: Uint8Array, payload: Uint8Array): Uint8Array {
|
|
const nonce = randomBytes(NONCE_LEN);
|
|
const box = naclBoxSealAfterNm(this.#key(peer), nonce, payload);
|
|
const out = new Uint8Array(HEADER_LEN + box.length);
|
|
out.set(DISCO_MAGIC);
|
|
out.set(this.publicKey, DISCO_MAGIC.length);
|
|
out.set(nonce, DISCO_MAGIC.length + KEY_LEN);
|
|
out.set(box, HEADER_LEN);
|
|
return out;
|
|
}
|
|
|
|
// Answer to a disco ping: the sealed Pong, or null if p is not a ping
|
|
// for us. src is where the ping came from (derpMagicAddr for DERP);
|
|
// from, when given, the only sender disco key to accept.
|
|
answerPing(p: Uint8Array, src: DiscoAddrPort, from?: Uint8Array): { sender: Uint8Array; ping: DiscoMessage & { type: "ping" }; pong: Uint8Array } | null {
|
|
const m = this.open(p, from);
|
|
if (!m || m.message.type !== "ping") return null;
|
|
return { sender: m.sender, ping: m.message, pong: this.seal(m.sender, encodePong(m.message.txid, src)) };
|
|
}
|
|
}
|
|
|
|
export function parseDiscoMessage(p: Uint8Array): DiscoMessage | null {
|
|
if (p.length < 2) return null;
|
|
const type = p[0]!;
|
|
const version = p[1]!;
|
|
const body = p.subarray(2);
|
|
if (type === DISCO_TYPE_PING) {
|
|
if (body.length < 12) return null;
|
|
let nodeKey: Uint8Array | null = null;
|
|
let padding = body.length - 12;
|
|
if (body.length >= 12 + KEY_LEN) {
|
|
const k = body.subarray(12, 12 + KEY_LEN);
|
|
if (k.some((b) => b !== 0)) {
|
|
nodeKey = k.slice();
|
|
padding -= KEY_LEN;
|
|
}
|
|
}
|
|
return { type: "ping", txid: body.slice(0, 12), nodeKey, padding };
|
|
}
|
|
if (type === DISCO_TYPE_PONG) {
|
|
if (body.length < 12 + EP_LEN) return null;
|
|
return { type: "pong", txid: body.slice(0, 12), src: readEndpoint(body, 12) };
|
|
}
|
|
if (type === DISCO_TYPE_CALL_ME_MAYBE) {
|
|
const endpoints: DiscoAddrPort[] = [];
|
|
if (version === 0 && body.length % EP_LEN === 0) {
|
|
for (let o = 0; o < body.length; o += EP_LEN) endpoints.push(readEndpoint(body, o));
|
|
}
|
|
return { type: "callMeMaybe", endpoints };
|
|
}
|
|
return { type: "other", msgType: type, version, body: body.slice() };
|
|
}
|
|
|
|
export function encodePing(txid: Uint8Array, nodeKey: Uint8Array | null = null, padding = 0): Uint8Array {
|
|
const out = new Uint8Array(2 + 12 + (nodeKey ? KEY_LEN : 0) + padding);
|
|
out[0] = DISCO_TYPE_PING;
|
|
out.set(txid.subarray(0, 12), 2);
|
|
if (nodeKey) out.set(nodeKey, 14);
|
|
return out;
|
|
}
|
|
|
|
export function encodePong(txid: Uint8Array, src: DiscoAddrPort): Uint8Array {
|
|
const out = new Uint8Array(2 + 12 + EP_LEN);
|
|
out[0] = DISCO_TYPE_PONG;
|
|
out.set(txid.subarray(0, 12), 2);
|
|
out.set(src.ip, 14);
|
|
out[30] = src.port >> 8;
|
|
out[31] = src.port & 0xff;
|
|
return out;
|
|
}
|
|
|
|
export function encodeCallMeMaybe(endpoints: DiscoAddrPort[]): Uint8Array {
|
|
const out = new Uint8Array(2 + EP_LEN * endpoints.length);
|
|
out[0] = DISCO_TYPE_CALL_ME_MAYBE;
|
|
endpoints.forEach((e, i) => {
|
|
out.set(e.ip, 2 + i * EP_LEN);
|
|
out[2 + i * EP_LEN + 16] = e.port >> 8;
|
|
out[2 + i * EP_LEN + 17] = e.port & 0xff;
|
|
});
|
|
return out;
|
|
}
|
|
|
|
function readEndpoint(b: Uint8Array, o: number): DiscoAddrPort {
|
|
return { ip: b.slice(o, o + 16), port: (b[o + 16]! << 8) | b[o + 17]! };
|
|
}
|
|
|
|
function hex(b: Uint8Array): string {
|
|
let s = "";
|
|
for (let i = 0; i < b.length; i++) s += b[i]!.toString(16).padStart(2, "0");
|
|
return s;
|
|
}
|
|
|
|
function sameBytes(a: Uint8Array, b: Uint8Array): boolean {
|
|
if (a.length !== b.length) return false;
|
|
for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false;
|
|
return true;
|
|
}
|