This repository has been archived by the owner on Mar 2, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
config.rs
271 lines (240 loc) · 7.38 KB
/
config.rs
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
262
263
264
265
266
267
268
269
270
271
// Pi-hole: A black hole for Internet advertisements
// (c) 2018 Pi-hole, LLC (https://pi-hole.net)
// Network-wide ad blocking via your own hardware.
//
// API
// Config File Structure
//
// This file is copyright under the latest version of the EUPL.
// Please see LICENSE file for your rights under this license.
use env::PiholeFile;
use failure::Fail;
use failure::ResultExt;
use rocket::config::LoggingLevel;
use std::fs::File;
use std::io::{self, prelude::*};
use std::net::Ipv4Addr;
use std::path::Path;
use std::str::FromStr;
use toml;
use util::{Error, ErrorKind};
/// The API config options
#[derive(Deserialize, Default)]
pub struct Config {
#[serde(default)]
general: General,
#[serde(default)]
file_locations: Files
}
impl Config {
/// Parse the config from the file located at `config_location`
pub fn parse(config_location: &str) -> Result<Config, Error> {
let mut buffer = String::new();
// Read the file to a string, but return the default config if the file doesn't
// exist
let mut file = match File::open(config_location) {
Ok(f) => f,
Err(e) => match e.kind() {
io::ErrorKind::NotFound => return Ok(Self::default()),
_ => {
return Err(e.context(ErrorKind::FileRead(config_location.to_owned())))
.map_err(Error::from)
}
}
};
file.read_to_string(&mut buffer)
.map_err(|e| e.context(ErrorKind::FileRead(config_location.to_owned())))?;
let config = toml::from_str::<Config>(&buffer).context(ErrorKind::ConfigParsingError)?;
if config.is_valid() {
Ok(config)
} else {
Err(Error::from(ErrorKind::ConfigParsingError))
}
}
/// Check if the config settings are valid
pub fn is_valid(&self) -> bool {
self.general.is_valid() && self.file_locations.is_valid()
}
/// Get the configured location of a file
pub fn file_location(&self, file: PiholeFile) -> &str {
match file {
PiholeFile::DnsmasqConfig => &self.file_locations.dnsmasq_config,
PiholeFile::Whitelist => &self.file_locations.whitelist,
PiholeFile::Blacklist => &self.file_locations.blacklist,
PiholeFile::Regexlist => &self.file_locations.regexlist,
PiholeFile::SetupVars => &self.file_locations.setup_vars,
PiholeFile::FtlConfig => &self.file_locations.ftl_config,
PiholeFile::LocalVersions => &self.file_locations.local_versions,
PiholeFile::LocalBranches => &self.file_locations.local_branches
}
}
pub fn address(&self) -> &str {
&self.general.address
}
pub fn port(&self) -> usize {
self.general.port
}
pub fn log_level(&self) -> LoggingLevel {
match self.general.log_level.as_str() {
"critical" => LoggingLevel::Critical,
"normal" => LoggingLevel::Normal,
"debug" => LoggingLevel::Debug,
// Should never happen, since config is validated on startup
_ => LoggingLevel::Critical
}
}
}
/// Defines the deserialization of the "file_locations" section of the config
/// file. The default functions are generated by `default!`.
#[derive(Deserialize)]
pub struct Files {
#[serde(default = "default_dnsmasq_config")]
dnsmasq_config: String,
#[serde(default = "default_whitelist")]
whitelist: String,
#[serde(default = "default_blacklist")]
blacklist: String,
#[serde(default = "default_regexlist")]
regexlist: String,
#[serde(default = "default_setup_vars")]
setup_vars: String,
#[serde(default = "default_ftl_config")]
ftl_config: String,
#[serde(default = "default_local_versions")]
local_versions: String,
#[serde(default = "default_local_branches")]
local_branches: String
}
impl Default for Files {
fn default() -> Self {
Files {
dnsmasq_config: default_dnsmasq_config(),
whitelist: default_whitelist(),
blacklist: default_blacklist(),
regexlist: default_regexlist(),
setup_vars: default_setup_vars(),
ftl_config: default_ftl_config(),
local_versions: default_local_versions(),
local_branches: default_local_branches()
}
}
}
impl Files {
fn is_valid(&self) -> bool {
[
&self.dnsmasq_config,
&self.whitelist,
&self.blacklist,
&self.regexlist,
&self.setup_vars,
&self.ftl_config,
&self.local_versions,
&self.local_branches
].into_iter()
.all(|file| Path::new(file).is_absolute())
}
}
/// Create an `fn() -> String` default function for deserialization
macro_rules! default {
($fn_name:ident, $variant:ident) => {
fn $fn_name() -> String {
PiholeFile::$variant.default_location().to_owned()
}
};
}
default!(default_dnsmasq_config, DnsmasqConfig);
default!(default_whitelist, Whitelist);
default!(default_blacklist, Blacklist);
default!(default_regexlist, Regexlist);
default!(default_setup_vars, SetupVars);
default!(default_ftl_config, FtlConfig);
default!(default_local_versions, LocalVersions);
default!(default_local_branches, LocalBranches);
/// General config settings
#[derive(Deserialize)]
struct General {
#[serde(default = "default_address")]
address: String,
#[serde(default = "default_port")]
port: usize,
#[serde(default = "default_log_level")]
log_level: String
}
impl Default for General {
fn default() -> Self {
General {
address: default_address(),
port: default_port(),
log_level: default_log_level()
}
}
}
impl General {
fn is_valid(&self) -> bool {
Ipv4Addr::from_str(&self.address).is_ok() && self.port <= 65535
&& match self.log_level.as_str() {
"debug" | "normal" | "critical" => true,
_ => false
}
}
}
fn default_address() -> String {
"0.0.0.0".to_owned()
}
fn default_port() -> usize {
80
}
fn default_log_level() -> String {
"critical".to_owned()
}
#[cfg(test)]
mod test {
use super::{Config, Files, General};
#[test]
fn valid_config() {
let config = Config::default();
assert!(config.is_valid());
}
#[test]
fn valid_files() {
let files = Files::default();
assert!(files.is_valid());
}
#[test]
fn valid_general() {
let general = General::default();
assert!(general.is_valid());
}
#[test]
fn invalid_file() {
let files = Files {
setup_vars: "!asd?f".to_owned(),
..Files::default()
};
assert!(!files.is_valid());
}
#[test]
fn invalid_general_address() {
let general = General {
address: "hello_world".to_owned(),
..General::default()
};
assert!(!general.is_valid());
}
#[test]
fn invalid_general_port() {
let general = General {
port: 65536,
..General::default()
};
assert!(!general.is_valid());
}
#[test]
fn invalid_general_log_level() {
let general = General {
log_level: "hello_world".to_owned(),
..General::default()
};
assert!(!general.is_valid());
}
}