5.8 KiB
icon
| icon |
|---|
| ph:globe-duotone |
Network
Give the guest's TCP/IP stack a way out.
import { createKernel, directBackend, wispBackend } from "unkernel";
await createKernel({ net: directBackend() }); // Node: host sockets
await createKernel({ net: wispBackend("wss://wisp.example/") }); // anywhere: your Wisp proxy
Networking is off unless you pass net. The guest runs Linux's own TCP/IP stack, with DHCP and DNS; the backend carries its TCP connections and UDP flows.
directBackend()(Node) opens real TCP and UDP sockets on the host. WithhostLoopbackin the network spec, the gateway (10.0.2.2) reaches the host's 127.0.0.1.wispBackend(servers)goes through a Wisp proxy that you run or choose (one URL, or several to race), with UDP where the server carries it. Browsers can't open raw TCP sockets, so this is the backend for the web. The proxy sees the host names and ports the guest connects to and plain-HTTP content; TLS from the guest is end to end. In Node, passheaders(e.g.{ Origin: "https://your.site" }) if your proxy checks the Origin.
Any object with connect and lookup methods can carry the guest's TCP and DNS, and with a udp method its UDP, so you can write your own backend. To join a tailnet, see Tailscale.
Wisp limits
Wisp carries streams and nothing else, and a guest program can tell:
- No half-close. When the guest shuts down its sending side (
shutdown(SHUT_WR), whichncdoes at the end of its input), the server is not told. A client that waits for the server to see end of file before it answers hangs, such asecho hi | nc HOST PORTagainst a server that reads to the end, or an HTTP/1.0 request with a body and no Content-Length. - No DNS. Names resolve to placeholder addresses in 198.18.0.0/15, and the server resolves the name when the guest connects. Every name resolves (a bad one fails at connect time, as a reset), the address the guest sees is not the host's, and each name has one address.
- No connect acknowledgement. A connect succeeds at once; a refused or unreachable host shows up as a reset.
For real addresses, pass a resolver of your own, such as DNS over HTTPS. It takes a name and returns IPv4 addresses, or rejects with { code: "ENOTFOUND" } for a name that does not exist:
await createKernel({ net: wispBackend(servers, { lookup: (name) => resolveOverHttps(name) }) });
For half-close, use a backend of your own over a transport that has it.
::note
No IPv6, no ICMP beyond the gateway, and no inbound connections (except from tailnet peers with Tailscale). To reach a server in the guest, use vm.connect() and vm.fetch(), which go over vsock.
::
Answer HTTP from the host
import { fetchRoutes, wispBackend } from "unkernel";
const net = fetchRoutes(wispBackend("wss://wisp.example/"), {
"deb.debian.org": "https://debian-mirror.example", // http://deb.debian.org/x → https://debian-mirror.example/x
"api.local": (req) => Response.json({ path: new URL(req.url).pathname }), // your own handler
});
await createKernel({ net });
fetchRoutes(inner, routes) answers the guest's plain-HTTP requests (port 80, GET and HEAD) to the named hosts with the host's fetch(), and passes everything else to inner. Pass undefined for inner to allow only the routes. A string route rewrites the URL's prefix and forwards only the range and cache headers; in a browser the target must allow them through CORS. A function gets a Request with the guest's headers. Wisp specs take string routes as routes. unkernel.dev uses this to fetch Debian packages from a CORS mirror instead of through its proxy.
Allow, deny or reroute connections
import { policyBackend, wispBackend } from "unkernel";
const allowed = new Set(["example.org", "registry.npmjs.org"]);
const net = policyBackend(
wispBackend("wss://wisp.example/"),
({ transport, name }) => transport === "tcp" && !!name && allowed.has(name), // only these names, no UDP
{ connectByName: true },
);
await createKernel({ net });
policyBackend(inner, policy, options) asks policy before each TCP connection and UDP flow of the guest reaches inner. The policy gets { transport, host, port, dnsName, protocol, name } and returns, or resolves to, true or nothing to allow, false to refuse, or { host, port } to connect inner there instead (({ name }) => name === "old.example" ? { host: "new.example" } : true). A policy that throws or rejects refuses.
hostis the address the guest connected to (with Wisp, a placeholder),dnsNamethe name the guest looked up for it, if any.- On ports 443 and 80 (
sniff: { 443: "tls", 80: "http" }, the default), the connection is accepted at once and the guest's first bytes are held until its TLS ClientHello or HTTP request head is in;nameis then its SNI orHost, andprotocolsays which. Bytes that are not that protocol, no name, more thanmaxBytes(32 KiB) or no complete hello withintimeoutMs(10 s) leavenameundefined. Since the guest has its connection already, a refusal there, or a server that cannot be reached, shows up as a reset. - Other ports, and UDP (including QUIC on 443), are decided at connect time without a name; a refused UDP flow gets ICMP port unreachable, so clients fall back to TCP.
- SNI and
Hostare what the guest wrote, not checked against the address it connects to. For a policy by name, setconnectByName: true(the connection goes to the sniffed name and the guest's port), or comparenamewithdnsName. A guest using Encrypted Client Hello shows only its outer name, and DNS over HTTPS hidesdnsName.
The policy is a function, so a Worker mode net spec cannot carry it.