You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.5 KiB
49 lines
1.5 KiB
use crate::error::unexpected;
|
|
use std::net;
|
|
|
|
use hyper_reverse_proxy::ReverseProxy;
|
|
use hyper_trust_dns::{TrustDnsHttpConnector, TrustDnsResolver};
|
|
|
|
fn proxy_client() -> &'static ReverseProxy<TrustDnsHttpConnector> {
|
|
use std::sync::OnceLock;
|
|
static PROXY_CLIENT: OnceLock<ReverseProxy<TrustDnsHttpConnector>> = OnceLock::new();
|
|
PROXY_CLIENT.get_or_init(|| {
|
|
ReverseProxy::new(
|
|
hyper::Client::builder()
|
|
.build::<_, hyper::Body>(TrustDnsResolver::default().into_http_connector()),
|
|
)
|
|
})
|
|
}
|
|
|
|
pub async fn serve_http_request(
|
|
proxy_addr: &str,
|
|
headers: &http::header::HeaderMap,
|
|
client_ip: net::IpAddr,
|
|
mut req: hyper::Request<hyper::Body>,
|
|
req_is_https: bool,
|
|
) -> unexpected::Result<hyper::Response<hyper::Body>> {
|
|
for (name, value) in headers {
|
|
if value.is_empty() {
|
|
req.headers_mut().remove(name);
|
|
continue;
|
|
}
|
|
|
|
req.headers_mut().insert(name, value.clone());
|
|
}
|
|
|
|
if req_is_https {
|
|
req.headers_mut().insert(
|
|
"x-forwarded-proto",
|
|
http::header::HeaderValue::from_static("https"),
|
|
);
|
|
}
|
|
|
|
match proxy_client().call(client_ip, proxy_addr, 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 to {proxy_addr}: {e:?}").as_str(),
|
|
)),
|
|
}
|
|
}
|
|
|