← DNS server
DNS·Guide
All tools

The DNS Server

A small DNS server for pointing a phone (or an emulator) at your own hosts: set the device's DNS to the machine running the agent, and the names you list here are answered from your entries. The page does all the DNS work itself; the local agent only opens the UDP sockets and relays packets.

Contents

  1. The Dns library
  2. The Agent library

1The Dns library

lib/dns.js reads and writes the DNS wire format (RFC 1035, AAAA from RFC 3596, SRV from RFC 2782) and answers names from a list of entries. It is a JavaScript module with no dependencies and no user interface, so it also runs in a module Web Worker or in jsc. It exports one class, Dns, whose methods are all static:

import { Dns } from "../lib/dns.js";
MethodWhat it does
Dns.parse(bytes)A message: { id, qr, opcode, aa, tc, rd, ra, rcode, questions: [{ name, type, cls }], answers, authority, additional }; each record is { name, type, cls, ttl, value } with the data as text ("93.184.216.34", "10 mail.example.com", "\"some\" \"text\""), or "(N bytes)" for other types. Throws an Error saying what is wrong when the message is cut short or malformed.
Dns.response(query, answers, options)A Uint8Array answering a parsed query: its first question, then the answers [{ name, type, ttl, value }]. options: { rcode, aa }.
Dns.encodeValue(type, text)The record data bytes for a value written as text (A, AAAA, CNAME, NS, PTR, MX “priority host”, SRV “priority weight port target”, TXT); throws an Error with what is wrong.
Dns.readName(bytes, offset)[name, offset after it], following compression pointers.
Dns.TYPES, Dns.typeName(number), Dns.RCODESThe record type numbers by name, a type's name ("TYPE99" for unknown ones), and the response code names.
Dns.validateEntry(entry)For an entry { on, name, type, value, ttl }: null when it can be answered, else what is wrong.
Dns.entriesFor(entries, name)The enabled, valid entries for a name: the exact ones, or else those of the closest wildcard.
Dns.resolve(entries, name, type)The answers for a question, following CNAME entries, ready for Dns.response; null when no entry covers the name, [] when it has entries but none of that type.
Dns.upstreamAddress(text)A resolver address with its port: "1.1.1.1" → "1.1.1.1:53", "2606:4700::1111" → "[2606:4700::1111]:53".
import { Dns } from "../lib/dns.js";

const entries = [{ on: true, name: "api.example.com", type: "A", value: "192.168.1.10", ttl: 60 }];
const query = Dns.parse(packet);                       // bytes received on UDP port 53
const question = query.questions[0];
const answers = Dns.resolve(entries, question.name, question.type);
const reply = answers
	? Dns.response(query, answers, { aa: true })
	: Dns.response(query, [], { rcode: 3 });           // NXDOMAIN

2The Agent library

The page reaches the network through the local agent (agent/main.go), which gives pages what a browser can't do itself: UDP and TCP sockets, listeners, Unix sockets, TLS on any connection and USB transfers. lib/agent.js is its client over a WebSocket, a JavaScript module with no dependencies and no user interface. It exports Agent (a connection) and AgentSocket (the sockets an agent makes):

import { Agent } from "../lib/agent.js";
MethodWhat it does
Agent.connect(url, { timeout })A promise of a connected Agent, once the agent says hello (timeout in milliseconds, 5000 by default, 0 to wait forever). Rejects with a sentence saying what went wrong.
Agent.connectSaved(options)Connects to the address saved on the Settings page.
Agent.savedUrl(), Agent.saveUrl(url)The saved address (shared by every page, in localStorage under Agent.URL_STORAGE); saveUrl("") forgets it.
Agent.describe(url)The address without its token, for showing.
Agent.encodeBase64(bytes), Agent.decodeBase64(text)Base64 as the wire format uses it.
agent.url, agent.version, agent.oncloseThe address, the agent's version, and a callback (reason) for when the connection ends.
agent.request(operation, arguments)A request the agent answers ({ id, ok, …reply }), for operations without a helper such as "usb.list"; rejects with the agent's error.
agent.udpOpen(host, port)A UDP socket: socket.onmessage = (bytes, from) => …, socket.send(bytes, "1.2.3.4:53").
agent.tcpConnect(host, port, timeout), agent.unixConnect(path)A connection: socket.ondata = bytes => …, socket.send(bytes).
agent.tcpListen(host, port)A listener: socket.onaccept = connection => ….
agent.close()Closes the connection; every socket gets onclose("agent disconnected").
socket.id, kind, local, remote, closed, onclose, close()Every AgentSocket: kind is "udp", "tcp" or "listener"; onclose(error) is called when the agent closes it.

Pages show the saved address with common/agent.js (user-interface code): new AgentBar(host) adds an “Agent at …” line and a link to the Settings page, follows changes made on that page, and has refresh(); AgentBar.settingsHref(section) is the Settings page's address.

import { Agent } from "../lib/agent.js";
import { Dns } from "../lib/dns.js";

const agent = await Agent.connectSaved();
const server = await agent.udpOpen("127.0.0.1", 5353);
server.onmessage = (bytes, from) => {
	const query = Dns.parse(bytes);
	const question = query.questions[0];
	const answers = Dns.resolve(entries, question.name, question.type) || [];
	server.send(Dns.response(query, answers, { aa: true }), from);
};