forked from romanz/electrs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.rs
396 lines (348 loc) · 13 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
use bitcoin::p2p::Magic;
use bitcoin::Network;
use bitcoincore_rpc::Auth;
use dirs_next::home_dir;
use std::ffi::{OsStr, OsString};
use std::fmt;
use std::net::SocketAddr;
use std::net::ToSocketAddrs;
use std::path::PathBuf;
use std::str::FromStr;
use std::env::consts::{ARCH, OS};
use std::time::Duration;
pub const ELECTRS_VERSION: &str = env!("CARGO_PKG_VERSION");
const DEFAULT_SERVER_ADDRESS: [u8; 4] = [127, 0, 0, 1]; // by default, serve on IPv4 localhost
mod internal {
#![allow(clippy::enum_variant_names)]
#![allow(clippy::unnecessary_lazy_evaluations)]
#![allow(clippy::useless_conversion)]
include!(concat!(env!("OUT_DIR"), "/configure_me_config.rs"));
}
/// A simple error type representing invalid UTF-8 input.
pub struct InvalidUtf8(OsString);
impl fmt::Display for InvalidUtf8 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?} isn't a valid UTF-8 sequence", self.0)
}
}
/// An error that might happen when resolving an address
pub enum AddressError {
ResolvError { addr: String, err: std::io::Error },
NoAddrError(String),
}
impl fmt::Display for AddressError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
AddressError::ResolvError { addr, err } => {
write!(f, "Failed to resolve address {}: {}", addr, err)
}
AddressError::NoAddrError(addr) => write!(f, "No address found for {}", addr),
}
}
}
/// Newtype for an address that is parsed as `String`
///
/// The main point of this newtype is to provide better description than what `String` type
/// provides.
#[derive(Deserialize)]
pub struct ResolvAddr(String);
impl ::configure_me::parse_arg::ParseArg for ResolvAddr {
type Error = InvalidUtf8;
fn parse_arg(arg: &OsStr) -> std::result::Result<Self, Self::Error> {
Self::parse_owned_arg(arg.to_owned())
}
fn parse_owned_arg(arg: OsString) -> std::result::Result<Self, Self::Error> {
arg.into_string().map_err(InvalidUtf8).map(ResolvAddr)
}
fn describe_type<W: fmt::Write>(mut writer: W) -> fmt::Result {
write!(writer, "a network address (will be resolved if needed)")
}
}
impl ResolvAddr {
/// Resolves the address.
fn resolve(self) -> std::result::Result<SocketAddr, AddressError> {
match self.0.to_socket_addrs() {
Ok(mut iter) => iter.next().ok_or(AddressError::NoAddrError(self.0)),
Err(err) => Err(AddressError::ResolvError { addr: self.0, err }),
}
}
/// Resolves the address, but prints error and exits in case of failure.
fn resolve_or_exit(self) -> SocketAddr {
self.resolve().unwrap_or_else(|err| {
eprintln!("Error: {}", err);
std::process::exit(1)
})
}
}
/// This newtype implements `ParseArg` for `Network`.
#[derive(Deserialize)]
pub struct BitcoinNetwork(Network);
impl Default for BitcoinNetwork {
fn default() -> Self {
BitcoinNetwork(Network::Bitcoin)
}
}
impl FromStr for BitcoinNetwork {
type Err = <Network as FromStr>::Err;
fn from_str(string: &str) -> std::result::Result<Self, Self::Err> {
Network::from_str(string).map(BitcoinNetwork)
}
}
impl ::configure_me::parse_arg::ParseArgFromStr for BitcoinNetwork {
fn describe_type<W: fmt::Write>(mut writer: W) -> fmt::Result {
write!(writer, "either 'bitcoin', 'testnet', 'regtest' or 'signet'")
}
}
impl From<BitcoinNetwork> for Network {
fn from(network: BitcoinNetwork) -> Network {
network.0
}
}
/// Parsed and post-processed configuration
#[derive(Debug)]
pub struct Config {
// See below for the documentation of each field:
pub network: Network,
pub db_path: PathBuf,
pub db_log_dir: Option<PathBuf>,
pub daemon_dir: PathBuf,
pub daemon_auth: SensitiveAuth,
pub daemon_rpc_addr: SocketAddr,
pub daemon_p2p_addr: SocketAddr,
pub electrum_rpc_addr: SocketAddr,
pub monitoring_addr: SocketAddr,
pub wait_duration: Duration,
pub jsonrpc_timeout: Duration,
pub index_batch_size: usize,
pub index_lookup_limit: Option<usize>,
pub reindex_last_blocks: usize,
pub auto_reindex: bool,
pub ignore_mempool: bool,
pub sync_once: bool,
pub skip_block_download_wait: bool,
pub disable_electrum_rpc: bool,
pub server_banner: String,
pub signet_magic: Magic,
pub args: Vec<String>,
}
pub struct SensitiveAuth(pub Auth);
impl SensitiveAuth {
pub(crate) fn get_auth(&self) -> Auth {
self.0.clone()
}
}
impl fmt::Debug for SensitiveAuth {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.0 {
Auth::UserPass(ref user, _) => f
.debug_tuple("UserPass")
.field(&user)
.field(&"<sensitive>")
.finish(),
_ => write!(f, "{:?}", self.0),
}
}
}
/// Returns default daemon directory
fn default_daemon_dir() -> PathBuf {
let mut home = home_dir().unwrap_or_else(|| {
eprintln!("Error: unknown home directory");
std::process::exit(1)
});
home.push(".bitcoin");
home
}
fn default_config_files() -> Vec<OsString> {
let mut files = vec![OsString::from("electrs.toml")]; // cwd
if let Some(mut path) = home_dir() {
path.extend(&[".electrs", "config.toml"]);
files.push(OsString::from(path)) // home directory
}
files.push(OsString::from("/etc/electrs/config.toml")); // system-wide
files
}
impl Config {
/// Parses args, env vars, config files and post-processes them
pub fn from_args() -> Config {
use internal::ResultExt;
let (mut config, args) =
internal::Config::including_optional_config_files(default_config_files())
.unwrap_or_exit();
fn unsupported_network(network: Network) -> ! {
eprintln!("Error: unsupported network: {}", network);
std::process::exit(1);
}
let db_subdir = match config.network {
Network::Bitcoin => "bitcoin",
Network::Testnet => "testnet",
Network::Regtest => "regtest",
Network::Signet => "signet",
unsupported => unsupported_network(unsupported),
};
config.db_dir.push(db_subdir);
let default_daemon_rpc_port = match config.network {
Network::Bitcoin => 8332,
Network::Testnet => 18332,
Network::Regtest => 18443,
Network::Signet => 38332,
unsupported => unsupported_network(unsupported),
};
let default_daemon_p2p_port = match config.network {
Network::Bitcoin => 8333,
Network::Testnet => 18333,
Network::Regtest => 18444,
Network::Signet => 38333,
unsupported => unsupported_network(unsupported),
};
let default_electrum_port = match config.network {
Network::Bitcoin => 50001,
Network::Testnet => 60001,
Network::Regtest => 60401,
Network::Signet => 60601,
unsupported => unsupported_network(unsupported),
};
let default_monitoring_port = match config.network {
Network::Bitcoin => 4224,
Network::Testnet => 14224,
Network::Regtest => 24224,
Network::Signet => 34224,
unsupported => unsupported_network(unsupported),
};
let magic = match (config.network, config.signet_magic) {
(Network::Signet, Some(magic)) => magic.parse().unwrap_or_else(|error| {
eprintln!(
"Error: signet magic '{}' is not a valid hex string: {}",
magic, error
);
std::process::exit(1);
}),
(network, None) => network.magic(),
(_, Some(_)) => {
eprintln!("Error: signet magic only available on signet");
std::process::exit(1);
}
};
let daemon_rpc_addr: SocketAddr = config.daemon_rpc_addr.map_or(
(DEFAULT_SERVER_ADDRESS, default_daemon_rpc_port).into(),
ResolvAddr::resolve_or_exit,
);
let daemon_p2p_addr: SocketAddr = config.daemon_p2p_addr.map_or(
(DEFAULT_SERVER_ADDRESS, default_daemon_p2p_port).into(),
ResolvAddr::resolve_or_exit,
);
let electrum_rpc_addr: SocketAddr = config.electrum_rpc_addr.map_or(
(DEFAULT_SERVER_ADDRESS, default_electrum_port).into(),
ResolvAddr::resolve_or_exit,
);
#[cfg(not(feature = "metrics"))]
{
if config.monitoring_addr.is_some() {
eprintln!("Error: enable \"metrics\" feature to specify monitoring_addr");
std::process::exit(1);
}
}
let monitoring_addr: SocketAddr = config.monitoring_addr.map_or(
(DEFAULT_SERVER_ADDRESS, default_monitoring_port).into(),
ResolvAddr::resolve_or_exit,
);
match config.network {
Network::Bitcoin => (),
Network::Testnet => config.daemon_dir.push("testnet3"),
Network::Regtest => config.daemon_dir.push("regtest"),
Network::Signet => config.daemon_dir.push("signet"),
unsupported => unsupported_network(unsupported),
}
let daemon_dir = &config.daemon_dir;
let daemon_auth = SensitiveAuth(match (config.auth, config.cookie_file) {
(None, None) => Auth::CookieFile(daemon_dir.join(".cookie")),
(None, Some(cookie_file)) => Auth::CookieFile(cookie_file),
(Some(auth), None) => {
let parts: Vec<&str> = auth.splitn(2, ':').collect();
if parts.len() != 2 {
eprintln!("Error: auth cookie doesn't contain colon");
std::process::exit(1);
}
Auth::UserPass(parts[0].to_owned(), parts[1].to_owned())
}
(Some(_), Some(_)) => {
eprintln!("Error: ambiguous configuration - auth and cookie_file can't be specified at the same time");
std::process::exit(1);
}
});
if config.verbose > 0 {
eprintln!("Error: please use `log_filters` to set logging verbosity",);
std::process::exit(1);
}
let log_filters = config.log_filters;
let index_lookup_limit = match config.index_lookup_limit {
0 => None,
_ => Some(config.index_lookup_limit),
};
if config.jsonrpc_timeout_secs <= config.wait_duration_secs {
eprintln!(
"Error: jsonrpc_timeout_secs ({}) must be higher than wait_duration_secs ({})",
config.jsonrpc_timeout_secs, config.wait_duration_secs
);
std::process::exit(1);
}
if config.version {
println!("v{}", ELECTRS_VERSION);
std::process::exit(0);
}
let config = Config {
network: config.network,
db_path: config.db_dir,
db_log_dir: config.db_log_dir,
daemon_dir: config.daemon_dir,
daemon_auth,
daemon_rpc_addr,
daemon_p2p_addr,
electrum_rpc_addr,
monitoring_addr,
wait_duration: Duration::from_secs(config.wait_duration_secs),
jsonrpc_timeout: Duration::from_secs(config.jsonrpc_timeout_secs),
index_batch_size: config.index_batch_size,
index_lookup_limit,
reindex_last_blocks: config.reindex_last_blocks,
auto_reindex: config.auto_reindex,
ignore_mempool: config.ignore_mempool,
sync_once: config.sync_once,
skip_block_download_wait: config.skip_block_download_wait,
disable_electrum_rpc: config.disable_electrum_rpc,
server_banner: config.server_banner,
signet_magic: magic,
args: args.map(|a| a.into_string().unwrap()).collect(),
};
eprintln!(
"Starting electrs {} on {} {} with {:?}",
ELECTRS_VERSION, ARCH, OS, config
);
let mut builder = env_logger::Builder::from_default_env();
builder.default_format().format_timestamp_millis();
if let Some(log_filters) = &log_filters {
builder.parse_filters(log_filters);
}
builder.init();
config
}
}
#[cfg(test)]
mod tests {
use super::{Auth, SensitiveAuth};
use std::path::Path;
#[test]
fn test_auth_debug() {
let auth = Auth::None;
assert_eq!(format!("{:?}", SensitiveAuth(auth)), "None");
let auth = Auth::CookieFile(Path::new("/foo/bar/.cookie").to_path_buf());
assert_eq!(
format!("{:?}", SensitiveAuth(auth)),
"CookieFile(\"/foo/bar/.cookie\")"
);
let auth = Auth::UserPass("user".to_owned(), "pass".to_owned());
assert_eq!(
format!("{:?}", SensitiveAuth(auth)),
"UserPass(\"user\", \"<sensitive>\")"
);
}
}