Skip to main content

Mist/
Zone.rs

1#![allow(non_snake_case, non_camel_case_types, non_upper_case_globals)]
2//! # DNS Zone
3//!
4//! Provides DNS zone configuration for the CodeEditorLand private network.
5//! Creates an authoritative zone for `editor.land` that resolves to loopback
6//! addresses.
7
8use anyhow::Result;
9use hickory_proto::rr::{
10	Name,
11	RData,
12	Record,
13	rdata::{A, NS, SOA},
14};
15// hickory-server 0.26: see the block comment in `Server.rs` for the full
16// rename table (`authority::*` → `zone_handler::*`, `InMemoryAuthority` →
17// `InMemoryZoneHandler`, AXFR bool → `AxfrPolicy`). The handler became
18// generic over `P: RuntimeProvider`; we pin it to `TokioRuntimeProvider` so
19// return types are concrete and callers don't have to thread the parameter.
20use hickory_server::{
21	net::runtime::TokioRuntimeProvider,
22	store::in_memory::InMemoryZoneHandler,
23	zone_handler::{AxfrPolicy, ZoneType},
24};
25
26/// Creates the `editor.land` authoritative zone records.
27///
28/// All `*.editor.land` domains resolve to `127.x.x.x` (loopback).
29pub fn EditorLandZone() -> Result<Vec<Record>> {
30	let mut Records = Vec::new();
31	let Origin = Name::from_ascii("editor.land.").unwrap();
32	let TTL = 300u32;
33
34	let Serial = 2025010100u32;
35	let Refresh:i32 = 86400;
36	let Retry:i32 = 7200;
37	let Expire:i32 = 604800;
38	let Minimum:u32 = 3600;
39
40	let SOARecord = SOA::new(
41		Name::from_ascii("ns1.editor.land.").unwrap(),
42		Name::from_ascii("hostmaster.editor.land.").unwrap(),
43		Serial,
44		Refresh,
45		Retry,
46		Expire,
47		Minimum,
48	);
49	Records.push(Record::from_rdata(Origin.clone(), TTL, RData::SOA(SOARecord)));
50
51	let NSName = Name::from_ascii("ns1.editor.land.").unwrap();
52	Records.push(Record::from_rdata(Origin.clone(), TTL, RData::NS(NS(NSName))));
53
54	Records.push(Record::from_rdata(
55		Name::from_ascii("ns1.editor.land.").unwrap(),
56		TTL,
57		RData::A(A::new(127, 0, 0, 1)),
58	));
59
60	let Subdomains = vec!["editor", "www", "localhost", "sidecar", "cocoon"];
61	for (Index, Subdomain) in Subdomains.iter().enumerate() {
62		let SubdomainName = Name::from_ascii(format!("{}.editor.land.", Subdomain)).unwrap();
63		let IP = A::new(127, 0, (Index / 255) as u8, (Index % 255) as u8);
64		Records.push(Record::from_rdata(SubdomainName, TTL, RData::A(IP)));
65	}
66
67	Records.push(Record::from_rdata(Origin, TTL, RData::A(A::new(127, 0, 0, 1))));
68
69	Ok(Records)
70}
71
72/// Creates an `InMemoryZoneHandler` for the `editor.land` zone.
73pub fn EditorLandAuthority() -> Result<InMemoryZoneHandler<TokioRuntimeProvider>> {
74	let Origin = Name::from_ascii("editor.land.").unwrap();
75	let Authority =
76		InMemoryZoneHandler::<TokioRuntimeProvider>::empty(Origin, ZoneType::Primary, AxfrPolicy::Deny, None);
77	let _Records = EditorLandZone()?;
78	Ok(Authority)
79}
80
81/// Creates an `InMemoryZoneHandler` for a custom origin with specified records.
82pub fn CustomAuthority(Origin:&Name, _Records:Vec<Record>) -> Result<InMemoryZoneHandler<TokioRuntimeProvider>> {
83	let Authority =
84		InMemoryZoneHandler::<TokioRuntimeProvider>::empty(Origin.clone(), ZoneType::Primary, AxfrPolicy::Deny, None);
85	Ok(Authority)
86}
87
88#[cfg(test)]
89mod tests {
90	use hickory_server::zone_handler::ZoneHandler;
91
92	use super::*;
93
94	#[test]
95	fn TestZoneCreation() {
96		let Zone = EditorLandZone().expect("Failed to create zone");
97		assert!(!Zone.is_empty());
98	}
99
100	#[test]
101	fn TestZoneHasLoopbackRecords() {
102		let Zone = EditorLandZone().expect("Failed to create zone");
103		for Record in &Zone {
104			if let RData::A(IP) = Record.data() {
105				assert_eq!(IP.octets()[0], 127, "A record must resolve to 127.x.x.x");
106			}
107		}
108	}
109}