Make Technitium zones from the DNS page
A New zone form on a Technitium DNS provider makes a Primary zone (a domain name, or an address or network for a reverse zone), a Secondary zone (from the servers that hold it) or a Forwarder zone (where to pass questions, the protocol, DNSSEC). Names are written lower case, ASCII, without the trailing dot and refused before Technitium is asked if they are not zone names; Technitium's own refusal (a zone that exists) is shown as it was said. Administrators only, audited, and only for providers that can (the adapter gets an optional createZone). The new zone opens when it is made. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f73704e6ca
commit
0244f5a8ab
12 files changed
+533
-10
No files matched your search
@@ -4,8 +4,11 @@
|
||||
*
|
||||
* API docs: https://github.com/TechnitiumSoftware/DnsServer/blob/master/APIDOCS.md
|
||||
* Record ID format: "type||name||content||priority"
|
||||
*
|
||||
* Making a zone is `/api/zones/create` (permission "Zones: Modify"): the name and the type, and for a Secondary zone its primary servers, for a Forwarder zone the address to pass questions to, the
|
||||
* protocol to use and whether to validate DNSSEC. It answers with the name of the zone it made (`response.domain` — for a reverse zone the …arpa name). Not checked against a live server.
|
||||
*/
|
||||
import type { DnsAdapter, DnsRecord, DnsRecordInput, DnsZone } from "../types.js";
|
||||
import type { DnsAdapter, DnsRecord, DnsRecordInput, DnsZone, NewDnsZone } from "../types.js";
|
||||
import { withDiagLogging } from "../../services/diagLog.js";
|
||||
|
||||
export interface TechnitiumConfig {
|
||||
@@ -96,6 +99,20 @@ export function createTechnitiumAdapter(config: TechnitiumConfig): DnsAdapter {
|
||||
return { type, name, content, priority: priorityStr ? parseInt(priorityStr, 10) : undefined };
|
||||
}
|
||||
|
||||
async function createZone(zone: NewDnsZone): Promise<DnsZone> {
|
||||
const params: Record<string, string> = { zone: zone.name, type: zone.type };
|
||||
if (zone.type === "Secondary") params.primaryNameServerAddresses = zone.primaryNameServers.join(",");
|
||||
if (zone.type === "Forwarder") {
|
||||
params.initializeForwarder = "true";
|
||||
params.forwarder = zone.forwarder;
|
||||
params.protocol = zone.protocol;
|
||||
params.dnssecValidation = String(zone.dnssecValidation);
|
||||
}
|
||||
const resp = await apiPost("/api/zones/create", params);
|
||||
const made = typeof resp?.domain === "string" && resp.domain.trim() !== "" ? resp.domain.trim() : zone.name;
|
||||
return { id: made, name: made };
|
||||
}
|
||||
|
||||
async function listZones(): Promise<DnsZone[]> {
|
||||
const resp = await apiGet("/api/zones/list");
|
||||
const zones = resp?.zones ?? [];
|
||||
@@ -149,5 +166,5 @@ export function createTechnitiumAdapter(config: TechnitiumConfig): DnsAdapter {
|
||||
await apiPost("/api/zones/records/delete", { domain: name, zone, type, ...typeParams });
|
||||
}
|
||||
|
||||
return withDiagLogging("technitium", { listZones, listRecords, addRecord, updateRecord, deleteRecord });
|
||||
return withDiagLogging("technitium", { listZones, createZone, listRecords, addRecord, updateRecord, deleteRecord });
|
||||
}
|
||||
@@ -7,6 +7,9 @@ import { createAzureAdapter } from "./adapters/azure.js";
|
||||
import { createCpanelAdapter } from "./adapters/cpanel.js";
|
||||
import { createTechnitiumAdapter } from "./adapters/technitium.js";
|
||||
|
||||
/** The kinds of provider whose adapter can make a zone: the page offers "New zone" for these, and the route refuses it for the others. */
|
||||
export const ZONE_CREATING_PROVIDERS: ReadonlySet<DnsProviderType> = new Set<DnsProviderType>(["technitium"]);
|
||||
|
||||
export function createDnsAdapter(providerType: DnsProviderType, config: DnsProviderConfig): DnsAdapter {
|
||||
switch (providerType) {
|
||||
case "cloudflare":
|
||||
|
||||
@@ -22,8 +22,16 @@ export interface DnsRecordInput {
|
||||
proxied?: boolean;
|
||||
}
|
||||
|
||||
/** A zone to be made. Primary: this server holds it. Secondary: a copy of one a primary server holds. Forwarder: questions about the name are passed on to another DNS server. */
|
||||
export type NewDnsZone =
|
||||
| { type: "Primary"; name: string }
|
||||
| { type: "Secondary"; name: string; primaryNameServers: string[] }
|
||||
| { type: "Forwarder"; name: string; forwarder: string; protocol: "Udp" | "Tcp" | "Tls" | "Https" | "Quic"; dnssecValidation: boolean };
|
||||
|
||||
export interface DnsAdapter {
|
||||
listZones(): Promise<DnsZone[]>;
|
||||
/** Only for a provider that can make a zone (see ZONE_CREATING_PROVIDERS in registry.ts); the zone it made, by the name the provider gave it. */
|
||||
createZone?(zone: NewDnsZone): Promise<DnsZone>;
|
||||
listRecords(zoneId: string): Promise<DnsRecord[]>;
|
||||
addRecord(zoneId: string, record: DnsRecordInput): Promise<DnsRecord>;
|
||||
updateRecord(zoneId: string, recordId: string, record: DnsRecordInput): Promise<DnsRecord>;
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
/**
|
||||
* The name a new DNS zone is made with. A zone is a domain name (`home.lan`, a single label such as `lan` is one too), or — for a reverse zone — an address or a network in CIDR form
|
||||
* (`192.168.1.0/24`, `fd00::/8`), which Technitium turns into the `…in-addr.arpa` or `…ip6.arpa` zone. Names are written the way DNS compares them: lower case, no trailing dot, and only
|
||||
* ASCII — a name with other letters is given in its punycode form (`xn--…`). Anything else is refused here, before a provider is asked.
|
||||
*/
|
||||
import * as net from "node:net";
|
||||
|
||||
const LABEL = /^[a-z0-9_]([a-z0-9_-]{0,61}[a-z0-9_])?$/;
|
||||
|
||||
/** The name as it is to be sent, or null when it is not a zone name. */
|
||||
export function normaliseZoneName(input: string): string | null {
|
||||
const s = input.trim().toLowerCase().replace(/\.$/, "");
|
||||
if (s === "" || s.length > 253) return null;
|
||||
const network = /^([0-9a-f.:]+)\/(\d{1,3})$/.exec(s);
|
||||
if (network) {
|
||||
const bits = Number(network[2]);
|
||||
if (net.isIPv4(network[1])) return bits <= 32 ? s : null;
|
||||
if (net.isIPv6(network[1])) return bits <= 128 ? s : null;
|
||||
return null;
|
||||
}
|
||||
if (net.isIP(s) !== 0) return s;
|
||||
return s.split(".").every((label) => LABEL.test(label)) ? s : null;
|
||||
}
|
||||
@@ -11,7 +11,8 @@ import {
|
||||
splitProviderConfig,
|
||||
validateProviderConfig,
|
||||
} from "../dns/providerSchemas.js";
|
||||
import { createDnsAdapter } from "../dns/registry.js";
|
||||
import { createDnsAdapter, ZONE_CREATING_PROVIDERS } from "../dns/registry.js";
|
||||
import { normaliseZoneName } from "../dns/zoneNames.js";
|
||||
import { loadDnsProviderConfig, getDnsAdapterForProvider } from "../dns/loadProvider.js";
|
||||
import { getDnsStats } from "../dns/stats.js";
|
||||
import { asyncHandler } from "../utils/asyncHandler.js";
|
||||
@@ -55,7 +56,7 @@ dnsRouter.get("/providers", asyncHandler(async (_req, res) => {
|
||||
createdAt: dnsProviders.createdAt,
|
||||
})
|
||||
.from(dnsProviders);
|
||||
res.json({ providers: rows });
|
||||
res.json({ providers: rows.map((r) => ({ ...r, canCreateZones: ZONE_CREATING_PROVIDERS.has(r.providerType) })) });
|
||||
}));
|
||||
|
||||
const configValueSchema = z.union([z.string(), z.boolean()]);
|
||||
@@ -277,6 +278,59 @@ dnsRouter.get("/providers/:id/zones", asyncHandler(async (req, res) => {
|
||||
}
|
||||
}));
|
||||
|
||||
const zoneName = z.string().max(300).transform((s, ctx) => {
|
||||
const name = normaliseZoneName(s);
|
||||
if (name === null) {
|
||||
ctx.addIssue({ code: "custom", message: "That is not a zone name: use a domain name such as home.lan (letters, digits, - and _, ASCII), or an address or network such as 192.168.1.0/24 for a reverse zone." });
|
||||
return z.NEVER;
|
||||
}
|
||||
return name;
|
||||
});
|
||||
const serverAddress = z.string().trim().min(1).max(255).regex(/^[A-Za-z0-9._:\[\]-]+$/, "A server is given by its address or host name.");
|
||||
|
||||
const newZoneSchema = z.discriminatedUnion("type", [
|
||||
z.object({ type: z.literal("Primary"), name: zoneName }),
|
||||
z.object({ type: z.literal("Secondary"), name: zoneName, primaryNameServers: z.array(serverAddress).min(1, "Give the server that holds the zone.").max(10) }),
|
||||
z.object({
|
||||
type: z.literal("Forwarder"),
|
||||
name: zoneName,
|
||||
forwarder: z.string().trim().min(1, "Give the DNS server to pass questions to.").max(255).regex(/^[^\s,;"'<>]+$/, "The server is an address, a host name, a URL or this-server."),
|
||||
protocol: z.enum(["Udp", "Tcp", "Tls", "Https", "Quic"]).default("Udp"),
|
||||
dnssecValidation: z.boolean().default(false),
|
||||
}),
|
||||
]);
|
||||
|
||||
/**
|
||||
* Makes a zone on a provider that can (Technitium): a Primary zone, a Secondary zone from the servers that hold it, or a Forwarder zone that passes questions on. An administrator's alone — it makes
|
||||
* the server an authority for a name. Nothing is made if the name is not one; what the provider says when it refuses (a zone that exists, a name it will not take) is passed on.
|
||||
*/
|
||||
dnsRouter.post("/providers/:id/zones", requireRole("admin"), asyncHandler(async (req, res) => {
|
||||
const providerId = /^\d+$/.test(req.params.id) ? Number(req.params.id) : Number.NaN;
|
||||
if (!Number.isInteger(providerId)) return res.status(400).json({ error: "invalid_id" });
|
||||
const parsed = newZoneSchema.safeParse(req.body);
|
||||
if (!parsed.success) return res.status(400).json({ error: "invalid_body", message: parsed.error.issues[0]?.message ?? "That is not a zone.", details: parsed.error.flatten() });
|
||||
const found = await getDnsAdapterForProvider(providerId);
|
||||
if (!found) return res.status(404).json({ error: "not_found" });
|
||||
if (!found.provider.enabled) return res.status(400).json({ error: "provider_disabled", message: "This provider is switched off." });
|
||||
if (!ZONE_CREATING_PROVIDERS.has(found.provider.providerType) || !found.adapter.createZone) {
|
||||
return res.status(400).json({ error: "not_supported", message: "Zones can't be made from here on this kind of provider." });
|
||||
}
|
||||
try {
|
||||
const zone = await found.adapter.createZone(parsed.data);
|
||||
await recordAudit({
|
||||
actor: req.currentUser!,
|
||||
category: "dns",
|
||||
action: "create_zone",
|
||||
targetType: "dns_zone",
|
||||
targetId: zone.id,
|
||||
detail: { providerId, name: zone.name, type: parsed.data.type, ...(parsed.data.type === "Forwarder" ? { forwarder: parsed.data.forwarder } : {}) },
|
||||
});
|
||||
res.status(201).json({ zone });
|
||||
} catch (err) {
|
||||
res.status(502).json({ error: "dns_provider_error", message: err instanceof Error ? err.message : String(err) });
|
||||
}
|
||||
}));
|
||||
|
||||
// ─── Records (cached) ────────────────────────────────────────────────────────
|
||||
|
||||
dnsRouter.get("/providers/:id/zones/:zoneId/records", asyncHandler(async (req, res) => {
|
||||
|
||||
Reference in new issue
Block a user