diff --git a/Cargo.lock b/Cargo.lock index 38bb4e2..46bd766 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -466,6 +466,7 @@ dependencies = [ "hex", "http", "hyper", + "hyper-reverse-proxy", "log", "mime_guess", "mockall", @@ -1538,6 +1539,17 @@ dependencies = [ "want", ] +[[package]] +name = "hyper-reverse-proxy" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc1af9b1b483fb9f33bd1cda26b35eacf902f0d116fcf0d56075ea5e5923b935" +dependencies = [ + "hyper", + "lazy_static", + "unicase", +] + [[package]] name = "hyper-rustls" version = "0.24.1" diff --git a/Cargo.toml b/Cargo.toml index 1d28e79..c43a8c7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -44,3 +44,4 @@ env_logger = "0.10.0" serde_yaml = "0.9.22" rand = "0.8.5" reqwest = "0.11.18" +hyper-reverse-proxy = "0.5.1" diff --git a/src/origin.rs b/src/origin.rs index 8ae2ba8..c156f52 100644 --- a/src/origin.rs +++ b/src/origin.rs @@ -2,6 +2,7 @@ mod config; mod descr; pub mod git; pub mod mux; +pub mod proxy; pub use config::*; pub use descr::Descr; diff --git a/src/origin/proxy.rs b/src/origin/proxy.rs new file mode 100644 index 0000000..242b942 --- /dev/null +++ b/src/origin/proxy.rs @@ -0,0 +1,21 @@ +use crate::error::unexpected; +use std::net; + +// proxy is a special case because it is so tied to the underlying protocol that a request is +// being served on, it can't be abstracted out into a simple "get_file" operation like other +// origins. + +pub async fn serve_http_request( + client_ip: net::IpAddr, + proxy_url: &str, + req: hyper::Request, +) -> unexpected::Result> { + match hyper_reverse_proxy::call(client_ip, proxy_url, req).await { + Ok(res) => Ok(res), + // ProxyError doesn't actually implement Error :facepalm: so we have to format the error + // manually + Err(e) => Err(unexpected::Error::from( + format!("error while proxying: {e:?}").as_str(), + )), + } +}