@@ -13,9 +13,15 @@ extern crate regex;
13
13
extern crate syslog;
14
14
extern crate tokio;
15
15
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;
16
22
extern crate tox;
17
23
18
- mod cli_config ;
24
+ mod node_config ;
19
25
mod motd;
20
26
21
27
use std:: fs:: { File , OpenOptions } ;
@@ -43,7 +49,7 @@ use tox::toxcore::stats::Stats;
43
49
#[ cfg( unix) ]
44
50
use syslog:: Facility ;
45
51
46
- use cli_config :: * ;
52
+ use node_config :: * ;
47
53
use motd:: { Motd , Counters } ;
48
54
49
55
/// Channel size for onion messages between UDP and TCP relay.
@@ -118,18 +124,18 @@ fn load_or_gen_keys(keys_file: &str) -> (PublicKey, SecretKey) {
118
124
}
119
125
120
126
/// 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 )
122
128
where F : Future < Item = ( ) , Error = Error > + Send + ' static
123
129
{
124
- if threads_config == ThreadsConfig :: N ( 1 ) {
130
+ if threads == Threads :: N ( 1 ) {
125
131
let mut runtime = runtime:: current_thread:: Runtime :: new ( ) . expect ( "Failed to create runtime" ) ;
126
132
runtime. block_on ( future) . expect ( "Execution was terminated with error" ) ;
127
133
} else {
128
134
let mut builder = runtime:: Builder :: new ( ) ;
129
135
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
133
139
}
134
140
let mut runtime = builder
135
141
. build ( )
@@ -169,8 +175,8 @@ fn create_onion_streams() -> (TcpOnion, UdpOnion) {
169
175
( tcp_onion, udp_onion)
170
176
}
171
177
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 ( ) {
174
180
// If TCP address is not specified don't start TCP server and only drop
175
181
// all onion packets from DHT server
176
182
let tcp_onion_future = tcp_onion. rx
@@ -183,7 +189,7 @@ fn run_tcp(cli_config: &CliConfig, dht_sk: SecretKey, tcp_onion: TcpOnion, stats
183
189
tcp_server. set_udp_onion_sink ( tcp_onion. tx ) ;
184
190
185
191
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| {
187
193
let tcp_server_c = tcp_server_c. clone ( ) ;
188
194
let dht_sk = dht_sk. clone ( ) ;
189
195
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
204
210
} )
205
211
) ;
206
212
207
- info ! ( "Running TCP relay on {}" , cli_config . tcp_addrs. iter( ) . format( "," ) ) ;
213
+ info ! ( "Running TCP relay on {}" , config . tcp_addrs. iter( ) . format( "," ) ) ;
208
214
209
215
Either :: B ( tcp_server_future
210
216
. join ( tcp_onion_future)
211
217
. map ( |_| ( ) ) )
212
218
}
213
219
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 {
216
222
udp_addr
217
223
} else {
218
224
// 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
229
235
// Create a channel for server to communicate with network
230
236
let ( tx, rx) = mpsc:: channel ( DHT_CHANNEL_SIZE ) ;
231
237
232
- let lan_discovery_future = if cli_config . lan_discovery_enabled {
238
+ let lan_discovery_future = if config . lan_discovery_enabled {
233
239
Either :: A ( LanDiscoverySender :: new ( tx. clone ( ) , dht_pk, udp_addr. is_ipv6 ( ) )
234
240
. run ( )
235
241
. map_err ( Error :: from) )
@@ -239,9 +245,9 @@ fn run_udp(cli_config: &CliConfig, dht_pk: PublicKey, dht_sk: &SecretKey, udp_on
239
245
240
246
let mut udp_server = UdpServer :: new ( tx, dht_pk, dht_sk. clone ( ) ) ;
241
247
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) ;
243
249
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 ) ;
245
251
udp_server. set_tcp_onion_sink ( udp_onion. tx ) ;
246
252
udp_server. enable_ipv6_mode ( udp_addr. is_ipv6 ( ) ) ;
247
253
@@ -255,11 +261,11 @@ fn run_udp(cli_config: &CliConfig, dht_pk: PublicKey, dht_sk: &SecretKey, udp_on
255
261
} )
256
262
) ;
257
263
258
- if cli_config . bootstrap_nodes . is_empty ( ) {
264
+ if config . bootstrap_nodes . is_empty ( ) {
259
265
warn ! ( "No bootstrap nodes!" ) ;
260
266
}
261
267
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 ( ) ) {
263
269
udp_server. add_initial_bootstrap ( node) ;
264
270
}
265
271
@@ -275,9 +281,9 @@ fn main() {
275
281
panic ! ( "Crypto initialization failed." ) ;
276
282
}
277
283
278
- let cli_config = cli_parse ( ) ;
284
+ let config = cli_parse ( ) ;
279
285
280
- match cli_config . log_type {
286
+ match config . log_type {
281
287
LogType :: Stderr => {
282
288
let env = env_logger:: Env :: default ( )
283
289
. filter_or ( "RUST_LOG" , "info" ) ;
@@ -299,14 +305,23 @@ fn main() {
299
305
LogType :: None => { } ,
300
306
}
301
307
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 {
303
313
( 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 {
305
315
load_or_gen_keys ( keys_file)
306
316
} else {
307
317
panic ! ( "Neither secret key nor keys file is specified" )
308
318
} ;
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 {
310
325
warn ! ( "You should not pass the secret key via arguments due to \
311
326
security reasons. Use the environment variable instead") ;
312
327
}
@@ -316,10 +331,10 @@ fn main() {
316
331
let ( tcp_onion, udp_onion) = create_onion_streams ( ) ;
317
332
318
333
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) ;
321
336
322
337
let future = udp_server_future. select ( tcp_server_future) . map ( |_| ( ) ) . map_err ( |( e, _) | e) ;
323
338
324
- run ( future, cli_config . threads_config ) ;
339
+ run ( future, config . threads ) ;
325
340
}
0 commit comments