Reorganize a bit

This commit is contained in:
Frank Denis 2021-02-16 00:00:02 +01:00
parent 39124df9fc
commit 518341df37
2 changed files with 35 additions and 27 deletions

View file

@ -9,10 +9,6 @@ use crate::constants::*;
pub use crate::errors::*;
pub use crate::globals::*;
#[cfg(feature = "tls")]
use crate::tls::*;
use futures::join;
use futures::prelude::*;
use futures::task::{Context, Poll};
use hyper::http;
@ -24,7 +20,6 @@ use std::time::Duration;
use tokio::io::{AsyncRead, AsyncWrite};
use tokio::net::{TcpListener, UdpSocket};
use tokio::runtime;
use tokio::sync::mpsc;
pub mod reexports {
pub use tokio;
@ -299,25 +294,8 @@ impl DoH {
#[cfg(feature = "tls")]
{
if tls_enabled {
let certs_path = self.globals.tls_cert_path.as_ref().unwrap().clone();
let certs_keys_path = self.globals.tls_cert_key_path.as_ref().unwrap().clone();
let (tls_acceptor_sender, tls_acceptor_receiver) = mpsc::channel(1);
let http_service = self.start_with_tls(tls_acceptor_receiver, listener, server);
let cert_service = async {
loop {
match create_tls_acceptor(&certs_path, &certs_keys_path) {
Ok(tls_acceptor) => {
if tls_acceptor_sender.send(tls_acceptor).await.is_err() {
break;
}
}
Err(e) => eprintln!("TLS certificates error: {}", e),
}
tokio::time::sleep(Duration::from_secs(5)).await;
}
Ok::<_, DoHError>(())
};
return join!(http_service, cert_service).0;
self.start_with_tls(listener, server).await?;
return Ok(());
}
}
self.start_without_tls(listener, server).await?;

View file

@ -1,13 +1,17 @@
use crate::errors::*;
use crate::{DoH, LocalExecutor};
use futures::{future::FutureExt, select};
use futures::{future::FutureExt, join, select};
use hyper::server::conn::Http;
use std::fs::File;
use std::io::{self, BufReader, Cursor, Read};
use std::path::Path;
use std::sync::Arc;
use tokio::{net::TcpListener, sync::mpsc::Receiver};
use std::time::Duration;
use tokio::{
net::TcpListener,
sync::mpsc::{self, Receiver},
};
use tokio_rustls::{
rustls::{internal::pemfile, NoClientAuth, ServerConfig},
TlsAcceptor,
@ -96,7 +100,7 @@ where
}
impl DoH {
pub async fn start_with_tls(
async fn start_https_service(
self,
mut tls_acceptor_receiver: Receiver<TlsAcceptor>,
listener: TcpListener,
@ -129,4 +133,30 @@ impl DoH {
listener_service.await?;
Ok(())
}
pub async fn start_with_tls(
self,
listener: TcpListener,
server: Http<LocalExecutor>,
) -> Result<(), DoHError> {
let certs_path = self.globals.tls_cert_path.as_ref().unwrap().clone();
let certs_keys_path = self.globals.tls_cert_key_path.as_ref().unwrap().clone();
let (tls_acceptor_sender, tls_acceptor_receiver) = mpsc::channel(1);
let https_service = self.start_https_service(tls_acceptor_receiver, listener, server);
let cert_service = async {
loop {
match create_tls_acceptor(&certs_path, &certs_keys_path) {
Ok(tls_acceptor) => {
if tls_acceptor_sender.send(tls_acceptor).await.is_err() {
break;
}
}
Err(e) => eprintln!("TLS certificates error: {}", e),
}
tokio::time::sleep(Duration::from_secs(5)).await;
}
Ok::<_, DoHError>(())
};
return join!(https_service, cert_service).0;
}
}