2019-08-28 14:01:10 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2020-04-17 12:11:25 +00:00
|
|
|
use anyhow::{format_err, Error};
|
2019-06-29 12:57:54 +00:00
|
|
|
use futures::*;
|
2020-12-04 11:59:34 +00:00
|
|
|
use hyper::{Body, Request, Response};
|
|
|
|
use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
|
2019-08-28 14:01:10 +00:00
|
|
|
use tokio::net::{TcpListener, TcpStream};
|
2019-06-29 12:57:54 +00:00
|
|
|
|
2021-07-07 09:49:42 +00:00
|
|
|
use pbs_buildcfg::configdir;
|
2019-06-29 12:57:54 +00:00
|
|
|
|
2020-01-20 11:52:22 +00:00
|
|
|
fn main() -> Result<(), Error> {
|
2021-11-19 16:36:06 +00:00
|
|
|
proxmox_async::runtime::main(run())
|
2020-01-20 11:52:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn run() -> Result<(), Error> {
|
2019-07-02 11:33:58 +00:00
|
|
|
let key_path = configdir!("/proxy.key");
|
|
|
|
let cert_path = configdir!("/proxy.pem");
|
2019-06-29 12:57:54 +00:00
|
|
|
|
2019-07-02 11:33:58 +00:00
|
|
|
let mut acceptor = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
|
2020-12-04 11:59:34 +00:00
|
|
|
acceptor
|
|
|
|
.set_private_key_file(key_path, SslFiletype::PEM)
|
2019-07-02 11:33:58 +00:00
|
|
|
.map_err(|err| format_err!("unable to read proxy key {} - {}", key_path, err))?;
|
2020-12-04 11:59:34 +00:00
|
|
|
acceptor
|
|
|
|
.set_certificate_chain_file(cert_path)
|
2019-07-02 11:33:58 +00:00
|
|
|
.map_err(|err| format_err!("unable to read proxy cert {} - {}", cert_path, err))?;
|
|
|
|
acceptor.check_private_key().unwrap();
|
2019-06-29 12:57:54 +00:00
|
|
|
|
2019-07-02 11:33:58 +00:00
|
|
|
let acceptor = Arc::new(acceptor.build());
|
2019-06-29 12:57:54 +00:00
|
|
|
|
2020-12-04 11:59:34 +00:00
|
|
|
let listener = TcpListener::bind(std::net::SocketAddr::from(([127, 0, 0, 1], 8008))).await?;
|
2019-06-29 12:57:54 +00:00
|
|
|
|
|
|
|
println!("listening on {:?}", listener.local_addr());
|
|
|
|
|
2019-12-12 14:27:07 +00:00
|
|
|
loop {
|
|
|
|
let (socket, _addr) = listener.accept().await?;
|
2020-12-04 11:59:34 +00:00
|
|
|
tokio::spawn(handle_connection(socket, Arc::clone(&acceptor)).map(|res| {
|
|
|
|
if let Err(err) = res {
|
|
|
|
eprintln!("Error: {}", err);
|
|
|
|
}
|
|
|
|
}));
|
2019-08-28 14:01:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-04 11:59:34 +00:00
|
|
|
async fn handle_connection(socket: TcpStream, acceptor: Arc<SslAcceptor>) -> Result<(), Error> {
|
2019-08-28 14:01:10 +00:00
|
|
|
socket.set_nodelay(true).unwrap();
|
|
|
|
|
2020-12-04 11:59:34 +00:00
|
|
|
let ssl = openssl::ssl::Ssl::new(acceptor.context())?;
|
|
|
|
let stream = tokio_openssl::SslStream::new(ssl, socket)?;
|
|
|
|
let mut stream = Box::pin(stream);
|
|
|
|
|
|
|
|
stream.as_mut().accept().await?;
|
2019-08-28 14:01:10 +00:00
|
|
|
|
|
|
|
let mut http = hyper::server::conn::Http::new();
|
|
|
|
http.http2_only(true);
|
|
|
|
// increase window size: todo - find optiomal size
|
|
|
|
let max_window_size = (1 << 31) - 2;
|
|
|
|
http.http2_initial_stream_window_size(max_window_size);
|
|
|
|
http.http2_initial_connection_window_size(max_window_size);
|
|
|
|
|
|
|
|
let service = hyper::service::service_fn(|_req: Request<Body>| {
|
|
|
|
println!("Got request");
|
2020-12-04 11:59:34 +00:00
|
|
|
let buffer = vec![65u8; 4 * 1024 * 1024]; // nonsense [A,A,A,A...]
|
2019-08-28 14:01:10 +00:00
|
|
|
let body = Body::from(buffer);
|
|
|
|
|
|
|
|
let response = Response::builder()
|
|
|
|
.status(http::StatusCode::OK)
|
|
|
|
.header(http::header::CONTENT_TYPE, "application/octet-stream")
|
|
|
|
.body(body)
|
|
|
|
.unwrap();
|
|
|
|
future::ok::<_, Error>(response)
|
|
|
|
});
|
|
|
|
|
2020-12-04 11:59:34 +00:00
|
|
|
http.serve_connection(stream, service)
|
2019-08-28 14:01:10 +00:00
|
|
|
.map_err(Error::from)
|
|
|
|
.await?;
|
2019-06-29 12:57:54 +00:00
|
|
|
|
2019-08-28 14:01:10 +00:00
|
|
|
println!("H2 connection CLOSE !");
|
2019-06-29 12:57:54 +00:00
|
|
|
Ok(())
|
|
|
|
}
|