temp
This commit is contained in:
parent
985b1d3810
commit
7757ef32f4
5 changed files with 796 additions and 30 deletions
|
@ -1,16 +1,21 @@
|
|||
use std::{any::type_name_of_val, error::Error};
|
||||
use std::{any::type_name_of_val, collections::HashMap, error::Error, sync::Arc};
|
||||
|
||||
use http_body_util::{Either, Full};
|
||||
use hyper::{
|
||||
Request, Response, StatusCode,
|
||||
body::{Body, Bytes, Incoming},
|
||||
rt::{Read, Write},
|
||||
server::conn::http1,
|
||||
service::{HttpService, Service},
|
||||
};
|
||||
use hyper_util::rt::TokioIo;
|
||||
use json::JsonValue;
|
||||
use log::{error, info};
|
||||
use rustls::server::Acceptor;
|
||||
use tokio::net::{TcpListener, TcpStream};
|
||||
use tokio_rustls::{LazyConfigAcceptor, StartHandshake};
|
||||
|
||||
use crate::tls::TlsOption;
|
||||
|
||||
pub type GeneralResponse = Response<GeneralBody>;
|
||||
pub type GeneralBody = Either<Incoming, Full<Bytes>>;
|
||||
|
@ -23,6 +28,7 @@ pub fn to_general_response(res: Response<Incoming>) -> GeneralResponse {
|
|||
pub struct Server<S> {
|
||||
listener: TcpListener,
|
||||
service: S,
|
||||
tls: TlsOption,
|
||||
}
|
||||
|
||||
pub trait TcpIntercept {
|
||||
|
@ -59,7 +65,7 @@ pub async fn json_to_vec(v: JsonValue) -> Option<Vec<String>> {
|
|||
|
||||
impl<S> Server<S>
|
||||
where
|
||||
S: TcpIntercept,
|
||||
S: TcpIntercept + Sync,
|
||||
S: Service<Request<Incoming>> + Clone + Send + 'static,
|
||||
S: HttpService<Incoming> + Clone + Send,
|
||||
<S::ResBody as Body>::Error: Into<Box<dyn Error + Send + Sync>>,
|
||||
|
@ -75,31 +81,63 @@ where
|
|||
);
|
||||
|
||||
loop {
|
||||
let (stream, _) = self.listener.accept().await.unwrap();
|
||||
let (tcp_stream, _) = self.listener.accept().await.unwrap();
|
||||
|
||||
let mut svc_clone = self.service.clone();
|
||||
svc_clone.stream(&stream);
|
||||
|
||||
let io = TokioIo::new(stream);
|
||||
|
||||
let tls = self.tls.clone();
|
||||
tokio::task::spawn(async move {
|
||||
if let Err(err) = http1::Builder::new()
|
||||
.writev(false)
|
||||
.serve_connection(io, svc_clone)
|
||||
.await
|
||||
{
|
||||
error!("Error while trying to serve connection: {err}")
|
||||
svc_clone.stream(&tcp_stream);
|
||||
|
||||
match tls {
|
||||
TlsOption::NoTls => {
|
||||
if let Err(err) = http1::Builder::new()
|
||||
.writev(false)
|
||||
.serve_connection(TokioIo::new(tcp_stream), svc_clone)
|
||||
.await
|
||||
{
|
||||
error!("Error while trying to serve connection: {err}")
|
||||
};
|
||||
}
|
||||
TlsOption::Tls(x) => {
|
||||
let acceptor = LazyConfigAcceptor::new(Acceptor::default(), tcp_stream);
|
||||
|
||||
match acceptor.await {
|
||||
Ok(y) => {
|
||||
let hello = y.client_hello();
|
||||
let hostname = hello.server_name().clone().unwrap();
|
||||
let config = Arc::new(x.matcher(hostname).unwrap());
|
||||
let stream = y
|
||||
.into_stream(config)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
if let Err(err) = http1::Builder::new()
|
||||
.writev(false)
|
||||
.serve_connection(TokioIo::new(stream), svc_clone)
|
||||
.await
|
||||
{
|
||||
error!("Error while trying to serve connection: {err}")
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Error while initiating handshake: {e}");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn new(service: S, a: (String, u16)) -> Result<Self, Box<dyn Error>> {
|
||||
pub async fn new(service: S, a: (String, u16), tls: TlsOption) -> Result<Self, Box<dyn Error>> {
|
||||
Ok(Self {
|
||||
listener: TcpListener::bind(&a).await?,
|
||||
service,
|
||||
tls,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
*/
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue