Skip to content
This repository was archived by the owner on Dec 10, 2022. It is now read-only.

Commit 4ef39c9

Browse files
committed
feat(config): add new parser for config file
1 parent fe0fa56 commit 4ef39c9

File tree

2 files changed

+398
-26
lines changed

2 files changed

+398
-26
lines changed

src/main.rs

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,15 @@ extern crate regex;
1313
extern crate syslog;
1414
extern crate tokio;
1515
extern crate tokio_codec;
16+
extern crate config;
17+
#[macro_use]
18+
extern crate serde_derive;
19+
extern crate serde;
20+
extern crate serde_ignored;
21+
extern crate serde_yaml;
1622
extern crate tox;
1723

18-
mod cli_config;
24+
mod node_config;
1925
mod motd;
2026

2127
use std::fs::{File, OpenOptions};
@@ -43,7 +49,7 @@ use tox::toxcore::stats::Stats;
4349
#[cfg(unix)]
4450
use syslog::Facility;
4551

46-
use cli_config::*;
52+
use node_config::*;
4753
use motd::{Motd, Counters};
4854

4955
/// Channel size for onion messages between UDP and TCP relay.
@@ -118,18 +124,18 @@ fn load_or_gen_keys(keys_file: &str) -> (PublicKey, SecretKey) {
118124
}
119125

120126
/// Run a future with the runtime specified by config.
121-
fn run<F>(future: F, threads_config: ThreadsConfig)
127+
fn run<F>(future: F, threads: Threads)
122128
where F: Future<Item = (), Error = Error> + Send + 'static
123129
{
124-
if threads_config == ThreadsConfig::N(1) {
130+
if threads == Threads::N(1) {
125131
let mut runtime = runtime::current_thread::Runtime::new().expect("Failed to create runtime");
126132
runtime.block_on(future).expect("Execution was terminated with error");
127133
} else {
128134
let mut builder = runtime::Builder::new();
129135
builder.name_prefix("tox-node-");
130-
match threads_config {
131-
ThreadsConfig::N(n) => { builder.core_threads(n as usize); },
132-
ThreadsConfig::Auto => { }, // builder will detect number of cores automatically
136+
match threads {
137+
Threads::N(n) => { builder.core_threads(n as usize); },
138+
Threads::Auto => { }, // builder will detect number of cores automatically
133139
}
134140
let mut runtime = builder
135141
.build()
@@ -169,8 +175,8 @@ fn create_onion_streams() -> (TcpOnion, UdpOnion) {
169175
(tcp_onion, udp_onion)
170176
}
171177

172-
fn run_tcp(cli_config: &CliConfig, dht_sk: SecretKey, tcp_onion: TcpOnion, stats: Stats) -> impl Future<Item = (), Error = Error> {
173-
if cli_config.tcp_addrs.is_empty() {
178+
fn run_tcp(config: &NodeConfig, dht_sk: SecretKey, tcp_onion: TcpOnion, stats: Stats) -> impl Future<Item = (), Error = Error> {
179+
if config.tcp_addrs.is_empty() {
174180
// If TCP address is not specified don't start TCP server and only drop
175181
// all onion packets from DHT server
176182
let tcp_onion_future = tcp_onion.rx
@@ -183,7 +189,7 @@ fn run_tcp(cli_config: &CliConfig, dht_sk: SecretKey, tcp_onion: TcpOnion, stats
183189
tcp_server.set_udp_onion_sink(tcp_onion.tx);
184190

185191
let tcp_server_c = tcp_server.clone();
186-
let tcp_server_futures = cli_config.tcp_addrs.iter().map(move |&addr| {
192+
let tcp_server_futures = config.tcp_addrs.iter().map(move |&addr| {
187193
let tcp_server_c = tcp_server_c.clone();
188194
let dht_sk = dht_sk.clone();
189195
let listener = TcpListener::bind(&addr).expect("Failed to bind TCP listener");
@@ -204,15 +210,15 @@ fn run_tcp(cli_config: &CliConfig, dht_sk: SecretKey, tcp_onion: TcpOnion, stats
204210
})
205211
);
206212

207-
info!("Running TCP relay on {}", cli_config.tcp_addrs.iter().format(","));
213+
info!("Running TCP relay on {}", config.tcp_addrs.iter().format(","));
208214

209215
Either::B(tcp_server_future
210216
.join(tcp_onion_future)
211217
.map(|_| ()))
212218
}
213219

214-
fn run_udp(cli_config: &CliConfig, dht_pk: PublicKey, dht_sk: &SecretKey, udp_onion: UdpOnion, tcp_stats: Stats) -> impl Future<Item = (), Error = Error> {
215-
let udp_addr = if let Some(udp_addr) = cli_config.udp_addr {
220+
fn run_udp(config: &NodeConfig, dht_pk: PublicKey, dht_sk: &SecretKey, udp_onion: UdpOnion, tcp_stats: Stats) -> impl Future<Item = (), Error = Error> {
221+
let udp_addr = if let Some(udp_addr) = config.udp_addr {
216222
udp_addr
217223
} else {
218224
// If UDP address is not specified don't start DHT server and only drop
@@ -229,7 +235,7 @@ fn run_udp(cli_config: &CliConfig, dht_pk: PublicKey, dht_sk: &SecretKey, udp_on
229235
// Create a channel for server to communicate with network
230236
let (tx, rx) = mpsc::channel(DHT_CHANNEL_SIZE);
231237

232-
let lan_discovery_future = if cli_config.lan_discovery_enabled {
238+
let lan_discovery_future = if config.lan_discovery_enabled {
233239
Either::A(LanDiscoverySender::new(tx.clone(), dht_pk, udp_addr.is_ipv6())
234240
.run()
235241
.map_err(Error::from))
@@ -239,9 +245,9 @@ fn run_udp(cli_config: &CliConfig, dht_pk: PublicKey, dht_sk: &SecretKey, udp_on
239245

240246
let mut udp_server = UdpServer::new(tx, dht_pk, dht_sk.clone());
241247
let counters = Counters::new(tcp_stats, udp_stats.clone());
242-
let motd = Motd::new(cli_config.motd.clone(), counters);
248+
let motd = Motd::new(config.motd.clone(), counters);
243249
udp_server.set_bootstrap_info(version(), Box::new(move |_| motd.format().as_bytes().to_owned()));
244-
udp_server.enable_lan_discovery(cli_config.lan_discovery_enabled);
250+
udp_server.enable_lan_discovery(config.lan_discovery_enabled);
245251
udp_server.set_tcp_onion_sink(udp_onion.tx);
246252
udp_server.enable_ipv6_mode(udp_addr.is_ipv6());
247253

@@ -255,11 +261,11 @@ fn run_udp(cli_config: &CliConfig, dht_pk: PublicKey, dht_sk: &SecretKey, udp_on
255261
})
256262
);
257263

258-
if cli_config.bootstrap_nodes.is_empty() {
264+
if config.bootstrap_nodes.is_empty() {
259265
warn!("No bootstrap nodes!");
260266
}
261267

262-
for node in cli_config.bootstrap_nodes.iter().flat_map(|node| node.resolve()) {
268+
for node in config.bootstrap_nodes.iter().flat_map(|node| node.resolve()) {
263269
udp_server.add_initial_bootstrap(node);
264270
}
265271

@@ -275,9 +281,9 @@ fn main() {
275281
panic!("Crypto initialization failed.");
276282
}
277283

278-
let cli_config = cli_parse();
284+
let config = cli_parse();
279285

280-
match cli_config.log_type {
286+
match config.log_type {
281287
LogType::Stderr => {
282288
let env = env_logger::Env::default()
283289
.filter_or("RUST_LOG", "info");
@@ -299,14 +305,23 @@ fn main() {
299305
LogType::None => { },
300306
}
301307

302-
let (dht_pk, dht_sk) = if let Some(ref sk) = cli_config.sk {
308+
for arg_unused in config.unused.clone() {
309+
warn!("Unused configuration key: {:?}", arg_unused);
310+
}
311+
312+
let (dht_pk, dht_sk) = if let Some(ref sk) = config.sk {
303313
(sk.public_key(), sk.clone())
304-
} else if let Some(ref keys_file) = cli_config.keys_file {
314+
} else if let Some(ref keys_file) = config.keys_file {
305315
load_or_gen_keys(keys_file)
306316
} else {
307317
panic!("Neither secret key nor keys file is specified")
308318
};
309-
if cli_config.sk_passed_as_arg {
319+
320+
if config.tcp_addrs.is_empty() && config.udp_addr.is_none() {
321+
panic!("Both TCP addresses and UDP address are not defined.")
322+
}
323+
324+
if config.sk_passed_as_arg {
310325
warn!("You should not pass the secret key via arguments due to \
311326
security reasons. Use the environment variable instead");
312327
}
@@ -316,10 +331,10 @@ fn main() {
316331
let (tcp_onion, udp_onion) = create_onion_streams();
317332

318333
let tcp_stats = Stats::new();
319-
let udp_server_future = run_udp(&cli_config, dht_pk, &dht_sk, udp_onion, tcp_stats.clone());
320-
let tcp_server_future = run_tcp(&cli_config, dht_sk, tcp_onion, tcp_stats);
334+
let udp_server_future = run_udp(&config, dht_pk, &dht_sk, udp_onion, tcp_stats.clone());
335+
let tcp_server_future = run_tcp(&config, dht_sk, tcp_onion, tcp_stats);
321336

322337
let future = udp_server_future.select(tcp_server_future).map(|_| ()).map_err(|(e, _)| e);
323338

324-
run(future, cli_config.threads_config);
339+
run(future, config.threads);
325340
}

0 commit comments

Comments
 (0)