cli_helpers: use anyhow in app fn; aquatic_ws: reorganize error handling

This commit is contained in:
Joakim Frostegård 2020-05-23 14:05:50 +02:00
parent 1efe6f96c5
commit 526faa9aab
13 changed files with 134 additions and 93 deletions

View file

@ -1,4 +1,7 @@
use std::time::Duration;
use std::fs::File;
use std::io::Read;
use native_tls::{Identity, TlsAcceptor};
pub mod common;
pub mod config;
@ -11,7 +14,9 @@ use common::*;
use config::Config;
pub fn run(config: Config){
pub fn run(config: Config) -> anyhow::Result<()> {
let opt_tls_acceptor = create_tls_acceptor(&config)?;
let state = State::default();
let (in_message_sender, in_message_receiver) = ::flume::unbounded();
@ -21,6 +26,7 @@ pub fn run(config: Config){
for i in 0..config.socket_workers {
let config = config.clone();
let in_message_sender = in_message_sender.clone();
let opt_tls_acceptor = opt_tls_acceptor.clone();
let (out_message_sender, out_message_receiver) = ::flume::unbounded();
@ -32,6 +38,7 @@ pub fn run(config: Config){
i,
in_message_sender,
out_message_receiver,
opt_tls_acceptor
);
});
}
@ -57,4 +64,27 @@ pub fn run(config: Config){
tasks::clean_torrents(&state);
}
}
pub fn create_tls_acceptor(
config: &Config,
) -> anyhow::Result<Option<TlsAcceptor>> {
if config.network.use_tls {
let mut identity_bytes = Vec::new();
let mut file = File::open(&config.network.tls_pkcs12_path)?;
file.read_to_end(&mut identity_bytes)?;
let identity = Identity::from_pkcs12(
&mut identity_bytes,
&config.network.tls_pkcs12_password
)?;
let acceptor = TlsAcceptor::new(identity)?;
Ok(Some(acceptor))
} else {
Ok(None)
}
}