-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.rs
More file actions
261 lines (229 loc) · 6.52 KB
/
config.rs
File metadata and controls
261 lines (229 loc) · 6.52 KB
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
use std::net::SocketAddr;
use std::path::Path;
use serde::Deserialize;
use crate::cdn_whitelist::CdnProvider;
#[derive(Debug, Deserialize)]
pub struct Config {
pub listen: ListenConfig,
pub upstream: UpstreamConfig,
#[serde(default)]
pub blocklist: Option<BlocklistConfig>,
#[serde(default)]
pub feeds: FeedsConfig,
#[serde(default)]
pub logging: LoggingConfig,
#[serde(default)]
pub tunneling_detection: TunnelingDetectionConfig,
#[serde(default)]
pub metrics: MetricsConfig,
}
#[derive(Debug, Clone, Deserialize)]
pub struct MetricsConfig {
#[serde(default = "default_true")]
pub enabled: bool,
#[serde(default = "default_metrics_bind_addr")]
pub bind_addr: String,
}
fn default_metrics_bind_addr() -> String {
"127.0.0.1:9090".to_string()
}
impl Default for MetricsConfig {
fn default() -> Self {
Self {
enabled: true,
bind_addr: default_metrics_bind_addr(),
}
}
}
#[derive(Debug, Deserialize)]
pub struct TunnelingDetectionConfig {
#[serde(default)]
pub enabled: bool,
#[serde(default = "default_entropy_threshold")]
pub entropy_threshold: f64,
#[serde(default = "default_min_subdomain_length")]
pub min_subdomain_length: usize,
#[serde(default)]
pub cdn_whitelist: CdnWhitelistConfig,
}
fn default_entropy_threshold() -> f64 {
3.5
}
fn default_min_subdomain_length() -> usize {
20
}
impl Default for TunnelingDetectionConfig {
fn default() -> Self {
Self {
enabled: false,
entropy_threshold: default_entropy_threshold(),
min_subdomain_length: default_min_subdomain_length(),
cdn_whitelist: CdnWhitelistConfig::default(),
}
}
}
#[derive(Debug, Clone, Deserialize)]
pub struct CdnWhitelistConfig {
#[serde(default = "default_true")]
pub enabled: bool,
#[serde(default)]
pub providers: Vec<CdnProvider>,
}
impl Default for CdnWhitelistConfig {
fn default() -> Self {
Self {
enabled: true,
providers: Vec::new(),
}
}
}
#[derive(Debug, Deserialize, Default, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum LogFormat {
#[default]
Text,
Json,
}
#[derive(Debug, Deserialize, Default)]
pub struct LoggingConfig {
#[serde(default)]
pub format: LogFormat,
pub file: Option<String>,
}
#[derive(Debug, Deserialize, Default)]
pub struct FeedsConfig {
#[serde(default = "default_true")]
pub urlhaus: bool,
#[serde(default = "default_true")]
pub openphish: bool,
pub phishtank_api_key: Option<String>,
/// oisd.nl big list (~32K ad/tracker domains, AdBlock syntax).
/// Opt-in — expands the blocklist beyond the security-only feeds.
#[serde(default)]
pub oisd: bool,
/// Interval in seconds to re-fetch feeds (used by hot-reload)
#[serde(default = "default_refresh_secs")]
pub refresh_secs: u64,
}
fn default_true() -> bool {
true
}
fn default_refresh_secs() -> u64 {
3600
}
#[derive(Debug, Deserialize)]
pub struct BlocklistConfig {
pub path: String,
}
#[derive(Debug, Deserialize)]
pub struct ListenConfig {
pub address: String,
pub port: u16,
// Optional TCP-specific bind address. When None, TCP binds to the
// same address as UDP. Needed on fly.io where UDP must bind to
// `fly-global-services` (for correct reply source-IP) but TCP
// must bind to a wildcard so fly-proxy's external route-in lands.
#[serde(default)]
pub tcp_address: Option<String>,
}
#[derive(Debug, Deserialize, Default, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum UpstreamProtocol {
#[default]
Udp,
Doh,
}
#[derive(Debug, Deserialize)]
pub struct UpstreamConfig {
pub address: String,
pub port: u16,
pub timeout_ms: u64,
#[serde(default)]
pub protocol: UpstreamProtocol,
pub doh_url: Option<String>,
}
const DEFAULT_DOH_URL: &str = "https://1.1.1.1/dns-query";
impl Config {
pub fn load(path: &Path) -> anyhow::Result<Self> {
let contents = std::fs::read_to_string(path)?;
let config: Config = toml::from_str(&contents)?;
Ok(config)
}
pub fn upstream_addr(&self) -> anyhow::Result<SocketAddr> {
Ok(format!("{}:{}", self.upstream.address, self.upstream.port).parse()?)
}
pub fn doh_url(&self) -> &str {
self.upstream.doh_url.as_deref().unwrap_or(DEFAULT_DOH_URL)
}
}
impl Default for Config {
fn default() -> Self {
Self {
listen: ListenConfig {
address: "127.0.0.1".to_string(),
port: 5353,
tcp_address: None,
},
upstream: UpstreamConfig {
address: "8.8.8.8".to_string(),
port: 53,
timeout_ms: 5000,
protocol: UpstreamProtocol::default(),
doh_url: None,
},
blocklist: None,
feeds: FeedsConfig {
urlhaus: true,
openphish: true,
phishtank_api_key: None,
oisd: false,
refresh_secs: 3600,
},
logging: LoggingConfig::default(),
tunneling_detection: TunnelingDetectionConfig::default(),
metrics: MetricsConfig::default(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_config() {
let config = Config::default();
assert_eq!(config.listen.address, "127.0.0.1");
assert_eq!(config.listen.port, 5353);
assert_eq!(config.upstream.address, "8.8.8.8");
assert_eq!(config.upstream.port, 53);
assert_eq!(config.upstream.timeout_ms, 5000);
}
#[test]
fn test_config_from_toml() {
let toml_str = r#"
[listen]
address = "0.0.0.0"
port = 1053
[upstream]
address = "1.1.1.1"
port = 53
timeout_ms = 3000
"#;
let config: Config = toml::from_str(toml_str).unwrap();
assert_eq!(config.listen.address, "0.0.0.0");
assert_eq!(config.listen.port, 1053);
assert_eq!(config.upstream.address, "1.1.1.1");
assert_eq!(config.upstream.port, 53);
assert_eq!(config.upstream.timeout_ms, 3000);
}
#[test]
fn test_upstream_addr() {
let config = Config::default();
let addr = config.upstream_addr().unwrap();
assert_eq!(addr, "8.8.8.8:53".parse::<SocketAddr>().unwrap());
}
#[test]
fn test_config_load_missing_file() {
let result = Config::load(Path::new("/nonexistent/config.toml"));
assert!(result.is_err());
}
}