works almost. todo: config.toml

This commit is contained in:
Jun Kurihara 2022-06-25 09:31:13 -04:00
commit 744b65b738
No known key found for this signature in database
GPG key ID: 48ADFD173ED22B03
6 changed files with 74 additions and 23 deletions

3
src/config/mod.rs Normal file
View file

@ -0,0 +1,3 @@
mod parse;
pub use parse::parse_opts;

View file

@ -32,10 +32,11 @@ pub fn parse_opts(globals: &mut Globals, backends: &mut HashMap<String, Backend>
app_name: "Localhost to Google except for maps".to_string(),
hostname: "localhost".to_string(),
reverse_proxy: ReverseProxy {
default_destination_uri: "https://www.google.com".parse::<Uri>().unwrap(),
// default_destination_uri: "http://www.google.com".parse::<Uri>().unwrap(),
default_destination_uri: "http://abehiroshi.la.coocan.jp/".parse::<Uri>().unwrap(), // httpのみの場合の好例
destination_uris: map_example,
},
https_redirection: Some(true), // TODO: ここはtlsが存在する時はSomeにすべき。Noneはtlsがないときのみのはず
https_redirection: Some(false), // TODO: ここはtlsが存在する時はSomeにすべき。Noneはtlsがないときのみのはず
tls_cert_path: Some(PathBuf::from(r"localhost1.pem")),
tls_cert_key_path: Some(PathBuf::from(r"localhost1.pem")),

View file

@ -4,7 +4,8 @@ use crate::{error::*, log::*};
use hyper::{
client::connect::Connect,
header::{HeaderMap, HeaderValue},
Body, Request, Response, StatusCode, Uri,
http::uri::Scheme,
Body, Request, Response, StatusCode, Uri, Version,
};
use std::net::SocketAddr;
use tokio::io::copy_bidirectional;
@ -144,16 +145,6 @@ fn generate_request_forwarded<B: core::fmt::Debug>(
) -> Result<Request<B>> {
debug!("Generate request to be forwarded");
// update "host" key in request header
if req.headers().contains_key("host") {
// HTTP/1.1
req.headers_mut().insert(
"host",
HeaderValue::from_str(destination_scheme_host.host().unwrap())
.map_err(|_| anyhow!("Failed to insert destination host into forwarded request"))?,
);
}
// Add te: trailer if contained in original request
let te_trailer = {
if let Some(te) = req.headers().get("te") {
@ -178,6 +169,9 @@ fn generate_request_forwarded<B: core::fmt::Debug>(
headers.insert("te", "trailer".parse().unwrap());
}
// Drop "host" key in request header to specify uri in absolute form
req.headers_mut().remove("host");
// update uri in request
*req.uri_mut() = Uri::builder()
.scheme(destination_scheme_host.scheme().unwrap().as_str())
@ -193,6 +187,11 @@ fn generate_request_forwarded<B: core::fmt::Debug>(
.insert("connection", HeaderValue::from_str("upgrade")?);
}
// Change version to http/1.1 when destination scheme is http
if req.version() != Version::HTTP_11 && destination_scheme_host.scheme() == Some(&Scheme::HTTP) {
*req.version_mut() = Version::HTTP_11;
}
Ok(req)
}

View file

@ -61,7 +61,6 @@ where
self.globals.runtime_handle.clone().spawn(async move {
tokio::time::timeout(
self.globals.timeout + Duration::from_secs(1),
// server.serve_connection(stream, self),
server.serve_connection(
stream,
service_fn(move |req: Request<Body>| self.clone().handle_request(req, peer_addr)),
@ -71,6 +70,7 @@ where
.ok();
clients_count.decrement();
debug!("Client #: {}", clients_count.current());
});
}