diff --git a/src/config.rs b/src/config.rs index 800d645..d8ff086 100644 --- a/src/config.rs +++ b/src/config.rs @@ -29,8 +29,8 @@ pub fn parse_opts(globals: &mut Globals, backends: &mut HashMap 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::().unwrap(), destination_uris: Some(map_example), diff --git a/src/main.rs b/src/main.rs index 0047535..3acad8f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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, diff --git a/src/proxy/mod.rs b/src/proxy/mod.rs new file mode 100644 index 0000000..a094bdc --- /dev/null +++ b/src/proxy/mod.rs @@ -0,0 +1,5 @@ +mod proxy_handler; +mod proxy_main; +mod proxy_tls; + +pub use proxy_main::Proxy; diff --git a/src/proxy/proxy_handler.rs b/src/proxy/proxy_handler.rs new file mode 100644 index 0000000..de8ac9a --- /dev/null +++ b/src/proxy/proxy_handler.rs @@ -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, + client_ip: SocketAddr, + globals: Arc, +) -> Result, 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, http::Error> { + let response = Response::builder() + .status(status_code) + .body(Body::empty()) + .unwrap(); + Ok(response) +} diff --git a/src/proxy.rs b/src/proxy/proxy_main.rs similarity index 72% rename from src/proxy.rs rename to src/proxy/proxy_main.rs index 2f8a325..6833b79 100644 --- a/src/proxy.rs +++ b/src/proxy/proxy_main.rs @@ -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, 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, } -// TODO: ここでbackendの名前単位でリクエストを分岐させる -async fn handle_request( - req: Request, - client_ip: SocketAddr, - globals: Arc, -) -> Result, 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 Proxy where T: Connect + Clone + Sync + Send + 'static, diff --git a/src/proxy_tls.rs b/src/proxy/proxy_tls.rs similarity index 90% rename from src/proxy_tls.rs rename to src/proxy/proxy_tls.rs index e3cc95e..1af0068 100644 --- a/src/proxy_tls.rs +++ b/src/proxy/proxy_tls.rs @@ -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