refactor
This commit is contained in:
parent
8bebdc82ab
commit
ad6dc84c77
1 changed files with 156 additions and 140 deletions
|
|
@ -2,7 +2,7 @@ use super::proxy_main::{LocalExecutor, Proxy};
|
||||||
use crate::{constants::*, error::*, log::*};
|
use crate::{constants::*, error::*, log::*};
|
||||||
#[cfg(feature = "h3")]
|
#[cfg(feature = "h3")]
|
||||||
use futures::StreamExt;
|
use futures::StreamExt;
|
||||||
use futures::{future::FutureExt, join, select};
|
use futures::{future::FutureExt, select};
|
||||||
use hyper::{client::connect::Connect, server::conn::Http};
|
use hyper::{client::connect::Connect, server::conn::Http};
|
||||||
use rustls::ServerConfig;
|
use rustls::ServerConfig;
|
||||||
use std::{sync::Arc, time::Duration};
|
use std::{sync::Arc, time::Duration};
|
||||||
|
|
@ -11,8 +11,7 @@ impl<T> Proxy<T>
|
||||||
where
|
where
|
||||||
T: Connect + Clone + Sync + Send + 'static,
|
T: Connect + Clone + Sync + Send + 'static,
|
||||||
{
|
{
|
||||||
pub async fn start_with_tls(self, server: Http<LocalExecutor>) -> Result<()> {
|
pub async fn cert_service(&self) {
|
||||||
let cert_service = async {
|
|
||||||
info!("Start cert watch service for {}", self.listening_on);
|
info!("Start cert watch service for {}", self.listening_on);
|
||||||
loop {
|
loop {
|
||||||
for (server_name, backend) in self.backends.apps.iter() {
|
for (server_name, backend) in self.backends.apps.iter() {
|
||||||
|
|
@ -24,10 +23,10 @@ where
|
||||||
}
|
}
|
||||||
tokio::time::sleep(Duration::from_secs(CERTS_WATCH_DELAY_SECS.into())).await;
|
tokio::time::sleep(Duration::from_secs(CERTS_WATCH_DELAY_SECS.into())).await;
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
// TCP Listener Service, i.e., http/2 and http/1.1
|
// TCP Listener Service, i.e., http/2 and http/1.1
|
||||||
let listener_service = async {
|
pub async fn listener_service(&self, server: Http<LocalExecutor>) -> Result<()> {
|
||||||
// let tcp_listener = TcpListener::bind(&self.listening_on).await?;
|
// let tcp_listener = TcpListener::bind(&self.listening_on).await?;
|
||||||
let tcp_listener = self.try_bind_tcp_listener().await?;
|
let tcp_listener = self.try_bind_tcp_listener().await?;
|
||||||
info!(
|
info!(
|
||||||
|
|
@ -73,11 +72,10 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(()) as Result<()>
|
Ok(()) as Result<()>
|
||||||
};
|
}
|
||||||
|
|
||||||
///////////////////////
|
|
||||||
#[cfg(feature = "h3")]
|
#[cfg(feature = "h3")]
|
||||||
let listener_service_h3 = async {
|
pub async fn listener_service_h3(&self) -> Result<()> {
|
||||||
// TODO: Work around to initially serve incoming connection
|
// TODO: Work around to initially serve incoming connection
|
||||||
// かなり適当。エラーが出たり出なかったり。原因がわからない…
|
// かなり適当。エラーが出たり出なかったり。原因がわからない…
|
||||||
let tls_app_names: Vec<String> = self
|
let tls_app_names: Vec<String> = self
|
||||||
|
|
@ -155,30 +153,48 @@ where
|
||||||
}
|
}
|
||||||
endpoint.wait_idle().await;
|
endpoint.wait_idle().await;
|
||||||
Ok(()) as Result<()>
|
Ok(()) as Result<()>
|
||||||
};
|
}
|
||||||
|
|
||||||
|
pub async fn start_with_tls(self, server: Http<LocalExecutor>) -> Result<()> {
|
||||||
#[cfg(not(feature = "h3"))]
|
#[cfg(not(feature = "h3"))]
|
||||||
{
|
{
|
||||||
join!(listener_service, cert_service).0
|
select! {
|
||||||
}
|
|
||||||
#[cfg(feature = "h3")]
|
|
||||||
{
|
|
||||||
if self.globals.http3 {
|
|
||||||
tokio::select! {
|
|
||||||
_= cert_service => {
|
_= cert_service => {
|
||||||
error!("Cert service for TLS exited");
|
error!("Cert service for TLS exited");
|
||||||
},
|
},
|
||||||
_ = listener_service => {
|
_ = listener_service => {
|
||||||
error!("TCP proxy service for TLS exited");
|
error!("TCP proxy service for TLS exited");
|
||||||
},
|
},
|
||||||
_= listener_service_h3 => {
|
|
||||||
error!("UDP proxy service for TLS exited");
|
};
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
#[cfg(feature = "h3")]
|
||||||
|
{
|
||||||
|
if self.globals.http3 {
|
||||||
|
tokio::select! {
|
||||||
|
_= self.cert_service() => {
|
||||||
|
error!("Cert service for TLS exited");
|
||||||
|
},
|
||||||
|
_ = self.listener_service(server) => {
|
||||||
|
error!("TCP proxy service for TLS exited");
|
||||||
|
},
|
||||||
|
_= self.listener_service_h3() => {
|
||||||
|
error!("UDP proxy service for QUIC exited");
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
// join!(listener_service, cert_service, listener_service_h3).0
|
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
join!(listener_service, cert_service).0
|
tokio::select! {
|
||||||
|
_= self.cert_service() => {
|
||||||
|
error!("Cert service for TLS exited");
|
||||||
|
},
|
||||||
|
_ = self.listener_service(server) => {
|
||||||
|
error!("TCP proxy service for TLS exited");
|
||||||
|
},
|
||||||
|
|
||||||
|
};
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue