Tailscale

Join the guest to your tailnet, from Node or a browser.

let state = load(); // the node's keys from an earlier run, if any
const vm = await createKernel({
  net: {
    kind: "net",
    backend: { kind: "direct" }, // or { kind: "wisp", servers: "wss://wisp.example/" }
    tailscale: { authKey: "tskey-auth-...", state, hostname: "box", exitNode: "my-server" },
  },
  onTailscale(e) {
    if (e.type === "state") save(e.state); // keep it secret: it is the node's identity
    if (e.type === "authurl") console.log("log in at", e.url); // no auth key: interactive login
    if (e.type === "status") console.log(e.status.state, e.status.addresses);
  },
});
vm.tailscale.setExitNode(null); // back to the backend; a name picks another

With tailscale in the network spec, the guest joins your Tailscale network, or a headscale one (controlURL). The client is JavaScript inside the SDK and loads only when the option is used. It talks to the coordination server over a WebSocket and carries WireGuard over Tailscale's DERP relays, so it needs no UDP and no helper on the host, and every packet is relayed.

The guest gets its 100.x address on a tailscale0 interface and reaches the tailnet's devices by address or MagicDNS name. Inbound works too: tailnet devices can reach servers in the guest. Everything else keeps going through backend.

#Options

  • authKey: a pre-auth key. Without one, login is interactive, through an authurl event.
  • state: what the last state event gave. Pass it back to keep the same node instead of registering a new one.
  • hostname (default unkernel), ephemeral and tags, as with tailscale up.
  • exitNode: a device's name or 100.x address. It takes all the guest's other IPv4 traffic and its DNS instead of the backend. The device has to advertise itself as an exit node and be approved, and the tailnet's policy has to let this node use it. status.exitNodes lists the devices that qualify, and status.exitNode says whether the chosen one is usable. While it is not usable (offline, not found, not approved), that traffic is refused, never sent through the backend.
  • acceptRoutes: add the peers' subnet routes.
  • controlURL: a headscale server. A page must also pass controlPublicKey (mkey:...), because headscale serves its key without CORS.

vm.tailscale has status(), on("status", fn), setExitNode(choice) and logout().

Note

No IPv6, and the tunnel's MTU is 1280. From the terminal, npx unkernel --tailscale-authkey KEY --tailscale-state ts.json --tailscale-exit-node my-server does the same (--help lists the --tailscale-* options).