split files

This commit is contained in:
Jun Kurihara 2022-06-23 14:35:36 -04:00
commit 730ae1f08d
No known key found for this signature in database
GPG key ID: 48ADFD173ED22B03
6 changed files with 66 additions and 51 deletions

View file

@ -29,8 +29,8 @@ pub fn parse_opts(globals: &mut Globals, backends: &mut HashMap<String, Backend>
backends.insert(
"localhost".to_string(),
Backend {
app_name: "Google except for maps".to_string(),
hostname: "google.com".to_string(),
app_name: "Localhost to Google except for maps".to_string(),
hostname: "localhost".to_string(),
reverse_proxy: ReverseProxy {
default_destination_uri: "https://google.com/".parse::<Uri>().unwrap(),
destination_uris: Some(map_example),

View file

@ -8,7 +8,6 @@ mod error;
mod globals;
mod log;
mod proxy;
mod proxy_tls;
use crate::{
backend::Backend, config::parse_opts, constants::*, error::*, globals::*, log::*, proxy::Proxy,

5
src/proxy/mod.rs Normal file
View file

@ -0,0 +1,5 @@
mod proxy_handler;
mod proxy_main;
mod proxy_tls;
pub use proxy_main::Proxy;

View file

@ -0,0 +1,48 @@
use crate::{backend::Backend, error::*, globals::Globals, log::*};
use futures::{
select,
task::{Context, Poll},
Future, FutureExt,
};
use hyper::{
client::connect::Connect,
http,
server::conn::Http,
service::{service_fn, Service},
Body, Client, HeaderMap, Method, Request, Response, StatusCode,
};
use std::{collections::HashMap, net::SocketAddr, pin::Pin, sync::Arc};
use tokio::{
io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt},
net::TcpListener,
runtime::Handle,
time::Duration,
};
// TODO: ここでbackendの名前単位でリクエストを分岐させる
pub async fn handle_request(
req: Request<Body>,
client_ip: SocketAddr,
globals: Arc<Globals>,
) -> Result<Response<Body>, http::Error> {
// http_error(StatusCode::NOT_FOUND)
debug!("{:?}", req);
// if req.version() == hyper::Version::HTTP_11 {
// Ok(Response::new(Body::from("Hello World")))
// } else {
// Note: it's usually better to return a Response
// with an appropriate StatusCode instead of an Err.
// Err("not HTTP/1.1, abort connection")
http_error(StatusCode::NOT_FOUND)
// }
// });
}
#[allow(clippy::unnecessary_wraps)]
fn http_error(status_code: StatusCode) -> Result<Response<Body>, http::Error> {
let response = Response::builder()
.status(status_code)
.body(Body::empty())
.unwrap();
Ok(response)
}

View file

@ -1,33 +1,16 @@
use super::proxy_handler::handle_request;
use crate::{backend::Backend, error::*, globals::Globals, log::*};
use futures::{
select,
task::{Context, Poll},
Future, FutureExt,
};
use hyper::{
client::connect::Connect,
http,
server::conn::Http,
service::{service_fn, Service},
Body, Client, HeaderMap, Method, Request, Response, StatusCode,
client::connect::Connect, server::conn::Http, service::service_fn, Body, Client, Request,
};
use std::{collections::HashMap, net::SocketAddr, pin::Pin, sync::Arc};
use std::{collections::HashMap, net::SocketAddr, sync::Arc};
use tokio::{
io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt},
io::{AsyncRead, AsyncWrite},
net::TcpListener,
runtime::Handle,
time::Duration,
};
#[allow(clippy::unnecessary_wraps)]
fn http_error(status_code: StatusCode) -> Result<Response<Body>, http::Error> {
let response = Response::builder()
.status(status_code)
.body(Body::empty())
.unwrap();
Ok(response)
}
#[derive(Clone, Debug)]
pub struct LocalExecutor {
runtime_handle: Handle,
@ -61,25 +44,6 @@ where
pub globals: Arc<Globals>,
}
// TODO: ここでbackendの名前単位でリクエストを分岐させる
async fn handle_request(
req: Request<Body>,
client_ip: SocketAddr,
globals: Arc<Globals>,
) -> Result<Response<Body>, http::Error> {
// http_error(StatusCode::NOT_FOUND)
debug!("{:?}", req);
// if req.version() == hyper::Version::HTTP_11 {
// Ok(Response::new(Body::from("Hello World")))
// } else {
// Note: it's usually better to return a Response
// with an appropriate StatusCode instead of an Err.
// Err("not HTTP/1.1, abort connection")
http_error(StatusCode::NOT_FOUND)
// }
// });
}
impl<T> Proxy<T>
where
T: Connect + Clone + Sync + Send + 'static,

View file

@ -1,9 +1,5 @@
use crate::{
constants::CERTS_WATCH_DELAY_SECS,
error::*,
log::*,
proxy::{LocalExecutor, Proxy},
};
use super::proxy_main::{LocalExecutor, Proxy};
use crate::{constants::CERTS_WATCH_DELAY_SECS, error::*, log::*};
use futures::{future::FutureExt, join, select};
use hyper::{client::connect::Connect, server::conn::Http};
use std::{sync::Arc, time::Duration};
@ -43,8 +39,11 @@ where
// First check SNI
let rustls_acceptor = rustls::server::Acceptor::new().unwrap();
let acceptor = tokio_rustls::LazyConfigAcceptor::new(rustls_acceptor, raw_stream);
let start = acceptor.await.unwrap();
let acceptor = tokio_rustls::LazyConfigAcceptor::new(rustls_acceptor, raw_stream).await;
if acceptor.is_err() {
continue;
}
let start = acceptor.unwrap();
let client_hello = start.client_hello();
debug!("SNI in ClientHello: {:?}", client_hello.server_name());
// Find server config for given SNI