v0.1.2
This commit is contained in:
1 parent
6e57b9e1b8
commit
7b8c7e93fb
48 files changed
+1010
-116
No files matched your search
@@ -42,6 +42,8 @@ pub struct Config {
|
||||
pub resolution: ResolutionConfig,
|
||||
pub cache: CacheConfig,
|
||||
pub policy: PolicyConfig,
|
||||
/// Names answered from hosts files (`/etc/hosts` format) before anything else, like dnsmasq.
|
||||
pub hosts: HostsConfig,
|
||||
pub rate_limit: RateLimitConfig,
|
||||
pub tls: TlsConfig,
|
||||
pub api: ApiConfig,
|
||||
@@ -277,8 +279,8 @@ pub struct ZoneConfig {
|
||||
pub name: String,
|
||||
#[serde(rename = "type")]
|
||||
pub zone_type: ZoneType,
|
||||
/// Zone file. A relative path is relative to `server.data_dir` (e.g. `zones/example.com.zone`
|
||||
/// = `<data_dir>/zones/example.com.zone`). Primary: required. Secondary: optional; defaults to
|
||||
/// Zone file. A relative path is relative to `server.data_dir` (e.g. `zones/primary/example.com.zone`
|
||||
/// = `<data_dir>/zones/primary/example.com.zone`). Primary: required. Secondary: optional; defaults to
|
||||
/// `<data_dir>/zones/secondary/<zone>.zone`.
|
||||
#[serde(default)]
|
||||
pub file: Option<PathBuf>,
|
||||
@@ -505,6 +507,43 @@ fn yes() -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
/// Hosts files answered locally (dnsmasq `addn-hosts` / reading `/etc/hosts`).
|
||||
///
|
||||
/// Every `address name [alias...]` line yields A/AAAA records for the names and a PTR
|
||||
/// record for the address (first name). Names found here are answered before local
|
||||
/// zones, forwarding and recursion, to clients allowed to resolve
|
||||
/// (`recursion.allowed_clients`). Off while `files` is empty.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
||||
#[serde(deny_unknown_fields, default)]
|
||||
pub struct HostsConfig {
|
||||
/// Files in hosts format, e.g. `["/etc/hosts", "/etc/hosts.d/*.hosts"]`. Relative paths
|
||||
/// are resolved against the configuration directory; `*`/`?` globs are allowed in the
|
||||
/// file name. A missing file is a warning, not an error (it is picked up once it appears).
|
||||
pub files: Vec<String>,
|
||||
/// TTL of the answers (dnsmasq `local-ttl`, default 0: clients do not cache them).
|
||||
pub ttl: u32,
|
||||
/// Domain appended to names without a dot (dnsmasq `expand-hosts` + `domain=`):
|
||||
/// with `domain: lan` the line `192.168.1.10 nas` answers both `nas` and `nas.lan`.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub domain: Option<String>,
|
||||
/// How often the files are checked for changes (modification time and size); changed
|
||||
/// files are re-read without a reload. `0s` turns it off (then only `easydns reload`).
|
||||
#[serde(with = "duration")]
|
||||
pub reload_interval: Duration,
|
||||
}
|
||||
|
||||
impl Default for HostsConfig {
|
||||
fn default() -> Self {
|
||||
Self { files: Vec::new(), ttl: 0, domain: None, reload_interval: Duration::from_secs(5) }
|
||||
}
|
||||
}
|
||||
|
||||
impl HostsConfig {
|
||||
pub fn enabled(&self) -> bool {
|
||||
!self.files.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields, default)]
|
||||
pub struct PolicyConfig {
|
||||
@@ -853,6 +892,27 @@ impl Config {
|
||||
}
|
||||
}
|
||||
|
||||
/// Expand `hosts.files` (paths relative to the config directory, `*`/`?` globs in the
|
||||
/// file name). Returns the existing files (sorted per pattern, without duplicates) and
|
||||
/// the literal paths that do not exist.
|
||||
pub fn hosts_file_paths(&self) -> (Vec<PathBuf>, Vec<PathBuf>) {
|
||||
let base = self.server.base_dir.clone().unwrap_or_default();
|
||||
let (mut found, mut missing) = (Vec::new(), Vec::new());
|
||||
for pat in &self.hosts.files {
|
||||
match include::expand(&base, pat) {
|
||||
Ok(v) => {
|
||||
for f in v {
|
||||
if !found.contains(&f) {
|
||||
found.push(f);
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(_) => missing.push(self.resolve_path(Path::new(pat))),
|
||||
}
|
||||
}
|
||||
(found, missing)
|
||||
}
|
||||
|
||||
/// Resolve a path relative to the config file directory.
|
||||
pub fn resolve_path(&self, p: &Path) -> PathBuf {
|
||||
if p.is_absolute() {
|
||||
@@ -875,7 +935,7 @@ impl Config {
|
||||
}
|
||||
|
||||
/// Resolve a zone `file:`. Relative paths are relative to the data directory
|
||||
/// (`zones/example.com.zone` → `<data_dir>/zones/example.com.zone`). For configurations
|
||||
/// (`zones/primary/example.com.zone` → `<data_dir>/zones/primary/example.com.zone`). For configurations
|
||||
/// written before this rule, a relative path that exists only relative to the config
|
||||
/// directory is still accepted.
|
||||
pub fn zone_file_path(&self, p: &Path) -> PathBuf {
|
||||
@@ -959,6 +1019,21 @@ impl Config {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn hosts_section_parses_and_validates() {
|
||||
let c: super::Config =
|
||||
serde_norway::from_str("hosts: { files: [/etc/hosts, /nonexistent/x.hosts], ttl: 60, domain: lan, reload_interval: 2s }\n")
|
||||
.unwrap();
|
||||
assert!(c.hosts.enabled());
|
||||
assert_eq!(c.hosts.ttl, 60);
|
||||
assert_eq!(c.hosts.reload_interval, std::time::Duration::from_secs(2));
|
||||
let issues = c.validate();
|
||||
assert!(issues.iter().any(|i| i.path == "hosts.files" && !i.is_error()), "{issues:?}");
|
||||
let bad: super::Config = serde_norway::from_str("hosts: { files: [/etc/hosts], domain: \"a..b\" }\n").unwrap();
|
||||
assert!(bad.validate().iter().any(|i| i.path == "hosts.domain" && i.is_error()));
|
||||
assert!(!super::Config::default().hosts.enabled());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn zone_path_with_parent_dir_before_data_dir_exists() {
|
||||
let d = std::env::temp_dir().join(format!("edns-zp-{}", std::process::id()));
|
||||
|
||||
Reference in new issue
Block a user