This commit is contained in:
Mateusz Gruszczyński committed 2026-11-23 09:31:05 +01:00
1 parent 6e57b9e1b8
commit 7b8c7e93fb
48 files changed
+1010 -116

No files matched your search

+130
View File
@@ -0,0 +1,130 @@
//! Hosts files (`hosts:`): dnsmasq-style answers before local zones and forwarding.
#![allow(clippy::unwrap_used, clippy::expect_used)]
mod common;
use std::time::Duration;
use common::*;
use dns_core::proto::op::ResponseCode;
use dns_core::proto::rr::RecordType;
/// Upstream: an authoritative server for `example.org`.
async fn upstream(dir: &std::path::Path, port: u16) -> dns_server::ServerHandle {
let d = dir.join("upstream");
std::fs::create_dir_all(&d).unwrap();
write(
&d,
"example.zone",
"$TTL 300\n@ SOA ns hm 1 3600 600 86400 300\n@ NS ns\nns A 127.0.0.3\nwww A 203.0.113.80\noverride A 203.0.113.99\n",
);
start(
&d,
&format!(
"server: {{ listen: [\"127.0.0.3:{port}\"], data_dir: data }}\napi: {{ enabled: false }}\nrate_limit: {{ enabled: false }}\n\
zones:\n - {{ name: example.org, type: primary, file: ../example.zone }}\n"
),
)
.await
}
fn forwarder_yaml(port: u16, upstream_port: u16, extra: &str) -> String {
format!(
"server: {{ mode: hybrid, listen: [\"127.0.0.2:{port}\"], data_dir: data }}\napi: {{ enabled: false }}\n\
rate_limit: {{ enabled: false }}\nupstreams: {{ servers: [\"127.0.0.3:{upstream_port}\"] }}\n\
resolution: {{ default: forward }}\n{extra}"
)
}
#[tokio::test(flavor = "multi_thread")]
async fn hosts_answer_first_then_forward() {
let dir = tmpdir("hosts");
let (port, up_port) = (free_port(), free_port());
let _up = upstream(&dir, up_port).await;
write(
&dir,
"hosts",
"127.0.0.1 localhost\n192.168.1.10 nas nas.home\nfd00::10 nas\n192.168.1.50 override.example.org\n10.9.9.9 shadow.local.example.net\n",
);
write(
&dir,
"local.zone",
"$TTL 300\n@ SOA ns hm 1 3600 600 86400 300\n@ NS ns\nns A 127.0.0.2\nshadow A 10.0.0.1\nzoneonly A 10.0.0.2\n",
);
let yaml = forwarder_yaml(
port,
up_port,
"recursion: { allowed_clients: [\"127.0.0.0/8\"] }\n\
hosts: { files: [hosts, missing.hosts], ttl: 30, domain: lan, reload_interval: 100ms }\n\
zones:\n - { name: local.example.net, type: primary, file: ../local.zone }\n",
);
let s = start(&dir, &yaml).await;
let addr = s.bound.dns[0];
// names from the hosts file, with the domain appended to bare names
let m = q(addr, "nas.", RecordType::A).await;
assert_eq!(answers(&m), vec!["A 192.168.1.10"]);
assert!(m.metadata.authoritative);
assert_eq!(m.answers[0].ttl, 30);
assert_eq!(answers(&q(addr, "nas.lan.", RecordType::A).await), vec!["A 192.168.1.10"]);
assert_eq!(answers(&q(addr, "nas.home.", RecordType::AAAA).await), Vec::<String>::new());
assert_eq!(answers(&q(addr, "nas.", RecordType::AAAA).await), vec!["AAAA fd00::10"]);
// known name, no data of that type: NODATA, not forwarded
let m = q(addr, "nas.", RecordType::MX).await;
assert_eq!(m.metadata.response_code, ResponseCode::NoError);
assert!(m.answers.is_empty());
// reverse lookup from the same file (private range would otherwise be NXDOMAIN locally)
let m = q(addr, "10.1.168.192.in-addr.arpa.", RecordType::PTR).await;
assert_eq!(answers(&m), vec!["PTR nas.lan."]);
// hosts override both the upstream and local zones
assert_eq!(answers(&q(addr, "override.example.org.", RecordType::A).await), vec!["A 192.168.1.50"]);
assert_eq!(answers(&q(addr, "shadow.local.example.net.", RecordType::A).await), vec!["A 10.9.9.9"]);
assert_eq!(answers(&q(addr, "zoneonly.local.example.net.", RecordType::A).await), vec!["A 10.0.0.2"]);
// everything else goes on to the upstream
assert_eq!(answers(&q(addr, "www.example.org.", RecordType::A).await), vec!["A 203.0.113.80"]);
// edits are picked up without a reload
write(&dir, "hosts", "192.168.1.11 nas\n192.168.1.60 tv\n");
eventually("hosts file change", || async { answers(&q(addr, "tv.", RecordType::A).await) == vec!["A 192.168.1.60"] }).await;
assert_eq!(answers(&q(addr, "nas.", RecordType::A).await), vec!["A 192.168.1.11"]);
// removed name falls through to the upstream again
assert_eq!(answers(&q(addr, "override.example.org.", RecordType::A).await), vec!["A 203.0.113.99"]);
// a file that appears later is read too
write(&dir, "missing.hosts", "192.168.1.70 printer\n");
eventually("new hosts file", || async { answers(&q(addr, "printer.", RecordType::A).await) == vec!["A 192.168.1.70"] }).await;
s.shutdown().await;
}
#[tokio::test(flavor = "multi_thread")]
async fn hosts_not_served_to_clients_without_resolution() {
let dir = tmpdir("hosts-acl");
let (port, up_port) = (free_port(), free_port());
write(&dir, "hosts", "192.168.1.10 nas\n");
let yaml =
forwarder_yaml(port, up_port, "recursion: { allowed_clients: [\"10.0.0.0/8\"] }\nhosts: { files: [hosts], reload_interval: 0s }\n");
let s = start(&dir, &yaml).await;
let m = q(s.bound.dns[0], "nas.", RecordType::A).await;
assert_eq!(m.metadata.response_code, ResponseCode::Refused);
tokio::time::sleep(Duration::from_millis(10)).await;
s.shutdown().await;
}
#[tokio::test(flavor = "multi_thread")]
async fn hosts_follow_reload() {
let dir = tmpdir("hosts-reload");
let (port, up_port) = (free_port(), free_port());
write(&dir, "a.hosts", "192.168.1.10 nas\n");
write(&dir, "b.hosts", "192.168.1.20 nas\n");
let base = "recursion: { allowed_clients: [\"127.0.0.0/8\"] }\n";
let s = start(&dir, &forwarder_yaml(port, up_port, &format!("{base}hosts: {{ files: [a.hosts], reload_interval: 0s }}\n"))).await;
let addr = s.bound.dns[0];
assert_eq!(answers(&q(addr, "nas.", RecordType::A).await), vec!["A 192.168.1.10"]);
write(&dir, "config.yaml", &forwarder_yaml(port, up_port, &format!("{base}hosts: {{ files: [\"*.hosts\"], reload_interval: 0s }}\n")));
s.reload().unwrap();
assert_eq!(answers(&q(addr, "nas.", RecordType::A).await), vec!["A 192.168.1.10", "A 192.168.1.20"]);
s.shutdown().await;
}