1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//! Configuration file support for Seer.
//!
//! Loads settings from `~/.seer/config.toml` with environment variable overrides.
use std::path::PathBuf;
use std::time::Duration;
use serde::{Deserialize, Serialize};
use tracing::debug;
/// Seer configuration loaded from `~/.seer/config.toml`.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct SeerConfig {
/// Default output format ("human", "json", "yaml")
pub output_format: String,
/// Default DNS nameserver spec. Accepts a bare IP/hostname with an
/// optional port (UDP, e.g. `"8.8.8.8"`, `"dns.google"`,
/// `"[2001:4860:4860::8888]:53"`), `tls://host[:port]` for DNS over TLS
/// (default port 853, e.g. `"tls://1.1.1.1"`), or
/// `https://host[:port][/path]` for DNS over HTTPS (default port 443,
/// default path `/dns-query`, e.g. `"https://cloudflare-dns.com/dns-query"`).
/// Parsed by [`crate::dns::NameserverSpec`]; invalid specs surface as an
/// error on first use rather than at load time.
pub nameserver: Option<String>,
/// Timeout settings
pub timeouts: TimeoutConfig,
/// Bulk operation settings
pub bulk: BulkConfig,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct TimeoutConfig {
/// WHOIS query timeout in seconds
pub whois_secs: u64,
/// RDAP query timeout in seconds
pub rdap_secs: u64,
/// DNS query timeout in seconds
pub dns_secs: u64,
/// HTTP/SSL check timeout in seconds
pub http_secs: u64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct BulkConfig {
/// Default concurrency for bulk operations
pub concurrency: usize,
/// Rate limit delay in milliseconds between operations
pub rate_limit_ms: u64,
}
impl Default for SeerConfig {
fn default() -> Self {
Self {
output_format: "human".to_string(),
nameserver: None,
timeouts: TimeoutConfig::default(),
bulk: BulkConfig::default(),
}
}
}
impl Default for TimeoutConfig {
fn default() -> Self {
Self {
whois_secs: 15,
// Matches the RDAP client's own DEFAULT_TIMEOUT (15s) and the
// documented value; the client uses 15s, so the default must too.
rdap_secs: 15,
dns_secs: 5,
http_secs: 10,
}
}
}
impl Default for BulkConfig {
fn default() -> Self {
Self {
concurrency: 10,
rate_limit_ms: 100,
}
}
}
impl SeerConfig {
/// Returns the path to the config file (`~/.seer/config.toml`).
pub fn config_path() -> Option<PathBuf> {
dirs::home_dir().map(|home| home.join(".seer").join("config.toml"))
}
/// Loads config from `~/.seer/config.toml`, falling back to defaults if not found.
pub fn load() -> Self {
let Some(path) = Self::config_path() else {
return Self::default();
};
if !path.exists() {
return Self::default();
}
match std::fs::read_to_string(&path) {
Ok(content) => match toml::from_str::<SeerConfig>(&content) {
Ok(config) => {
debug!(?path, "Loaded config");
config.clamped()
}
Err(e) => {
tracing::warn!(?path, error = %e, "Failed to parse config, using defaults");
Self::default()
}
},
Err(e) => {
debug!(?path, error = %e, "Could not read config, using defaults");
Self::default()
}
}
}
/// Clamp loaded values into sane ranges. Without this, a user
/// (accidentally or maliciously) setting `bulk.concurrency = 0` would
/// hand `Semaphore::new(0)` to every bulk operation and block forever;
/// `bulk.concurrency = 10000` would spawn thousands of concurrent
/// connections. Timeouts of `0` would error every network call
/// immediately. The bounds are per-protocol (a config file can't bypass
/// them): concurrency 1–50; whois/rdap timeouts 1–300s; dns 1–60s;
/// http 1–120s.
fn clamped(mut self) -> Self {
self.bulk.concurrency = self.bulk.concurrency.clamp(1, 50);
// A multi-day per-operation delay would stall bulk runs indefinitely;
// cap at 60s (0 is valid — no inter-operation delay).
self.bulk.rate_limit_ms = self.bulk.rate_limit_ms.min(60_000);
self.timeouts.whois_secs = self.timeouts.whois_secs.clamp(1, 300);
self.timeouts.rdap_secs = self.timeouts.rdap_secs.clamp(1, 300);
self.timeouts.dns_secs = self.timeouts.dns_secs.clamp(1, 60);
self.timeouts.http_secs = self.timeouts.http_secs.clamp(1, 120);
self
}
/// Returns the WHOIS timeout as a Duration.
pub fn whois_timeout(&self) -> Duration {
Duration::from_secs(self.timeouts.whois_secs)
}
/// Returns the RDAP timeout as a Duration.
pub fn rdap_timeout(&self) -> Duration {
Duration::from_secs(self.timeouts.rdap_secs)
}
/// Returns the DNS timeout as a Duration.
pub fn dns_timeout(&self) -> Duration {
Duration::from_secs(self.timeouts.dns_secs)
}
/// Returns the HTTP timeout as a Duration.
pub fn http_timeout(&self) -> Duration {
Duration::from_secs(self.timeouts.http_secs)
}
/// Returns the inter-operation bulk rate-limit delay as a Duration.
pub fn bulk_rate_limit(&self) -> Duration {
Duration::from_millis(self.bulk.rate_limit_ms)
}
/// Generates a default config file content as TOML.
pub fn default_toml() -> String {
toml::to_string_pretty(&Self::default()).unwrap_or_else(|_| String::new())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_config() {
let config = SeerConfig::default();
assert_eq!(config.output_format, "human");
assert_eq!(config.timeouts.whois_secs, 15);
assert_eq!(config.timeouts.rdap_secs, 15);
assert_eq!(config.timeouts.dns_secs, 5);
assert_eq!(config.bulk.concurrency, 10);
}
#[test]
fn test_parse_config_toml() {
let toml_str = r#"
output_format = "json"
nameserver = "1.1.1.1"
[timeouts]
whois_secs = 20
rdap_secs = 45
[bulk]
concurrency = 20
"#;
let config: SeerConfig = toml::from_str(toml_str).unwrap();
assert_eq!(config.output_format, "json");
assert_eq!(config.nameserver, Some("1.1.1.1".to_string()));
assert_eq!(config.timeouts.whois_secs, 20);
assert_eq!(config.timeouts.rdap_secs, 45);
assert_eq!(config.timeouts.dns_secs, 5); // default
assert_eq!(config.bulk.concurrency, 20);
}
#[test]
fn nameserver_accepts_dot_doh_specs_opaquely() {
// The nameserver key is an opaque spec string: DoT/DoH forms load
// unchanged and are validated by NameserverSpec::parse on first use,
// not at config-load time.
let config: SeerConfig = toml::from_str(r#"nameserver = "tls://1.1.1.1""#).unwrap();
assert_eq!(config.nameserver.as_deref(), Some("tls://1.1.1.1"));
}
#[test]
fn test_default_toml_roundtrip() {
let toml_str = SeerConfig::default_toml();
let config: SeerConfig = toml::from_str(&toml_str).unwrap();
assert_eq!(config.output_format, "human");
}
#[test]
fn test_timeout_durations() {
let config = SeerConfig::default();
assert_eq!(config.whois_timeout(), Duration::from_secs(15));
assert_eq!(config.rdap_timeout(), Duration::from_secs(15));
assert_eq!(config.dns_timeout(), Duration::from_secs(5));
assert_eq!(config.http_timeout(), Duration::from_secs(10));
}
#[test]
fn clamp_bounds_rate_limit_ms() {
// An unbounded rate-limit delay (once wired into the bulk executor)
// would stall every operation for the configured duration; clamp it.
let mut config = SeerConfig::default();
config.bulk.rate_limit_ms = 10_000_000;
let config = config.clamped();
assert!(config.bulk.rate_limit_ms <= 60_000);
}
}