Initial implementation of proxy module

This commit is contained in:
Brian Picciano 2023-07-16 16:09:20 +02:00
parent a86020eedf
commit 4a2ac7460f
4 changed files with 35 additions and 0 deletions

12
Cargo.lock generated
View File

@ -466,6 +466,7 @@ dependencies = [
"hex", "hex",
"http", "http",
"hyper", "hyper",
"hyper-reverse-proxy",
"log", "log",
"mime_guess", "mime_guess",
"mockall", "mockall",
@ -1538,6 +1539,17 @@ dependencies = [
"want", "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]] [[package]]
name = "hyper-rustls" name = "hyper-rustls"
version = "0.24.1" version = "0.24.1"

View File

@ -44,3 +44,4 @@ env_logger = "0.10.0"
serde_yaml = "0.9.22" serde_yaml = "0.9.22"
rand = "0.8.5" rand = "0.8.5"
reqwest = "0.11.18" reqwest = "0.11.18"
hyper-reverse-proxy = "0.5.1"

View File

@ -2,6 +2,7 @@ mod config;
mod descr; mod descr;
pub mod git; pub mod git;
pub mod mux; pub mod mux;
pub mod proxy;
pub use config::*; pub use config::*;
pub use descr::Descr; pub use descr::Descr;

21
src/origin/proxy.rs Normal file
View File

@ -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<hyper::Body>,
) -> unexpected::Result<hyper::Response<hyper::Body>> {
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(),
)),
}
}