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

View file

@ -18,21 +18,22 @@ app_name = 'localhost' # this should be option, if null then same as hostname
hostname = 'localhost' hostname = 'localhost'
https_redirection = true https_redirection = true
reverse_proxy = [ reverse_proxy = [
{ path = '*', destination = '192.168.10.0:3000', tls = true }, # default destination if path is not specified
{ path = '/path/to', destination = '192.168.10.1:4000', tls = true }, { destination = 'www.google.com', tls = true },
{ destination = 'www.bing.com', path = '/maps', tls = true },
] ]
## List of destinations to send data to. ## List of destinations to send data to.
## At this point, round-robin is used for load-balancing if multiple URLs are specified. ## At this point, round-robin is used for load-balancing if multiple URLs are specified.
allowhosts = ['127.0.0.1', '::1', '192.168.10.0/24'] # allowhosts = ['127.0.0.1', '::1', '192.168.10.0/24'] # TODO
denyhosts = ['*'] # denyhosts = ['*'] # TODO
tls_cert_path = 'localhost1.pem' tls_cert_path = 'localhost.pem'
tls_cert_key_path = 'localhost1.pem' tls_cert_key_path = 'localhost.pem'
[[application]] [[application]]
app_name = 'locahost_application' app_name = 'locahost_application'
hostname = 'localhost.localdomain' hostname = 'localhost.localdomain'
https_redirection = true https_redirection = true
reverse_proxy = [{ path = '/', destination = 'www.google.com', tls = true }] reverse_proxy = [{ destination = 'www.google.com', tls = true }]
tls_cert_path = 'localhost2.pem' tls_cert_path = 'localhost.pem'
tls_cert_key_path = 'localhost2.pem' tls_cert_key_path = 'localhost.pem'

47
lolalhost.pem Normal file
View file

@ -0,0 +1,47 @@
-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDb7g6EQhbfby97
k4oMbZTzdi2TWFBs7qK/QwgOu+L6EhNHPO1ZEU29v0APFBFJO5zyyAk9bZ9k9tPB
bCuVVI9jEUfLH3UCjEQPG6XI2w++uVh0yALvc/uurCvRHVlle/V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yLVJkWu
5/9/jJLiUCBhjQ2slwIDAQABMA0GCSqGSIb3DQEBCwUAA4IBAQA6Vz5HnGuy8jZz
5i8ipbcDMCZNdpYYnxgD53hEKOfoSv7LaF0ztD8Kmg3s5LHv9EHlkK3+G6FWRGiP
9f6IbtRITaiVQP3M13T78hpN5Qq5jgsqjR7ZcN7Etr6ZFd7G/0+mzqbyBuW/3szt
RdX/YLy1csvjbZoNNuXGWRohXjg0Mjko2tRLmARvxA/gZV5zWycv3BD2BPzyCdS9
MDMYSF0RPiL8+alfwLNqLcqMA5liHlmZa85uapQyoUI3ksKJkEgU53aD8cYhH9Yn
6mVpsrvrcRLBiHlbi24QBolhFkCSRK8bXes8XDIPuD8iYRwlrVBwOakMFQWMqNfI
IMOKJomU
-----END CERTIFICATE-----

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(), app_name: "Localhost to Google except for maps".to_string(),
hostname: "localhost".to_string(), hostname: "localhost".to_string(),
reverse_proxy: ReverseProxy { 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, 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_path: Some(PathBuf::from(r"localhost1.pem")),
tls_cert_key_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::{ use hyper::{
client::connect::Connect, client::connect::Connect,
header::{HeaderMap, HeaderValue}, header::{HeaderMap, HeaderValue},
Body, Request, Response, StatusCode, Uri, http::uri::Scheme,
Body, Request, Response, StatusCode, Uri, Version,
}; };
use std::net::SocketAddr; use std::net::SocketAddr;
use tokio::io::copy_bidirectional; use tokio::io::copy_bidirectional;
@ -144,16 +145,6 @@ fn generate_request_forwarded<B: core::fmt::Debug>(
) -> Result<Request<B>> { ) -> Result<Request<B>> {
debug!("Generate request to be forwarded"); 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 // Add te: trailer if contained in original request
let te_trailer = { let te_trailer = {
if let Some(te) = req.headers().get("te") { 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()); 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 // update uri in request
*req.uri_mut() = Uri::builder() *req.uri_mut() = Uri::builder()
.scheme(destination_scheme_host.scheme().unwrap().as_str()) .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")?); .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) Ok(req)
} }

View file

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