2024-03-05 19:47:44 +00:00
# This is a fork
This repo contains a fork of the[original hyper-reverse-proxy
codebase][upstream], adding to it a few improvements:
- Fix to a bug where the `Host` header was getting overwritten on the upstream
HTTP request.
- Upgraded hyper version to 1.x (and fixes related to that upgrade)
- Logging cleanup
Plus more as time goes on.
[upstream]: https://github.com/felipenoris/hyper-reverse-proxy
2019-02-16 17:46:52 +00:00
2017-07-15 06:23:12 +00:00
# hyper-reverse-proxy
2022-04-14 00:30:41 +00:00
[![License][license-img]](LICENSE)
[![CI][ci-img]][ci-url]
[![docs][docs-img]][docs-url]
[![version][version-img]][version-url]
[license-img]: https://img.shields.io/crates/l/hyper-reverse-proxy.svg
[ci-img]: https://github.com/felipenoris/hyper-reverse-proxy/workflows/CI/badge.svg
2022-04-14 00:37:23 +00:00
[ci-url]: https://github.com/felipenoris/hyper-reverse-proxy/actions/workflows/main.yml
2022-04-14 00:30:41 +00:00
[docs-img]: https://docs.rs/hyper-reverse-proxy/badge.svg
[docs-url]: https://docs.rs/hyper-reverse-proxy
[version-img]: https://img.shields.io/crates/v/hyper-reverse-proxy.svg
[version-url]: https://crates.io/crates/hyper-reverse-proxy
2017-07-15 06:23:12 +00:00
2019-02-16 17:46:52 +00:00
A simple reverse proxy, to be used with [Hyper].
The implementation ensures that [Hop-by-hop headers] are stripped correctly in both directions,
and adds the client's IP address to a comma-space-separated list of forwarding addresses in the
`X-Forwarded-For` header.
2017-07-15 06:23:12 +00:00
2019-02-16 17:46:52 +00:00
The implementation is based on Go's [`httputil.ReverseProxy`].
2017-07-15 06:23:12 +00:00
2017-07-15 08:41:28 +00:00
[Hyper]: http://hyper.rs/
2019-02-16 17:46:52 +00:00
[Hop-by-hop headers]: http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html
2017-07-15 06:23:12 +00:00
[`httputil.ReverseProxy`]: https://golang.org/pkg/net/http/httputil/#ReverseProxy
2019-02-16 17:46:52 +00:00
# Example
Add these dependencies to your `Cargo.toml` file.
2019-02-16 18:04:46 +00:00
```toml
2019-02-16 17:46:52 +00:00
[dependencies]
2022-05-01 18:36:09 +00:00
hyper-reverse-proxy = "?"
hyper = { version = "?", features = ["full"] }
tokio = { version = "?", features = ["full"] }
lazy_static = "?"
hyper-trust-dns = { version = "?", features = [
"rustls-http2",
"dnssec-ring",
"dns-over-https-rustls",
"rustls-webpki",
"https-only"
] }
2019-02-16 17:46:52 +00:00
```
The following example will set up a reverse proxy listening on `127.0.0.1:13900` ,
and will proxy these calls:
* `"/target/first"` will be proxied to `http://127.0.0.1:13901`
2022-03-12 14:58:18 +00:00
2019-02-16 17:46:52 +00:00
* `"/target/second"` will be proxied to `http://127.0.0.1:13902`
2022-03-12 14:58:18 +00:00
2019-02-16 17:46:52 +00:00
* All other URLs will be handled by `debug_request` function, that will display request information.
2022-05-01 18:36:09 +00:00
```rust
2019-02-16 17:46:52 +00:00
use hyper::server::conn::AddrStream;
2022-05-01 18:36:09 +00:00
use hyper::service::{make_service_fn, service_fn};
2022-03-12 14:58:18 +00:00
use hyper::{Body, Request, Response, Server, StatusCode};
2022-05-01 18:36:09 +00:00
use hyper_reverse_proxy::ReverseProxy;
use hyper_trust_dns::{RustlsHttpsConnector, TrustDnsResolver};
2022-03-12 14:58:18 +00:00
use std::net::IpAddr;
2022-05-01 18:36:09 +00:00
use std::{convert::Infallible, net::SocketAddr};
2019-02-16 17:46:52 +00:00
2022-05-01 18:36:09 +00:00
lazy_static::lazy_static! {
static ref PROXY_CLIENT: ReverseProxy< RustlsHttpsConnector > = {
ReverseProxy::new(
hyper::Client::builder().build::< _ , hyper::Body > (TrustDnsResolver::default().into_rustls_webpki_https_connector()),
)
};
}
fn debug_request(req: & Request< Body > ) -> Result< Response < Body > , Infallible> {
2019-02-16 17:46:52 +00:00
let body_str = format!("{:?}", req);
2022-03-12 14:58:18 +00:00
Ok(Response::new(Body::from(body_str)))
2019-02-16 17:46:52 +00:00
}
2022-03-12 14:58:18 +00:00
async fn handle(client_ip: IpAddr, req: Request< Body > ) -> Result< Response < Body > , Infallible> {
if req.uri().path().starts_with("/target/first") {
2022-05-01 18:36:09 +00:00
match PROXY_CLIENT.call(client_ip, "http://127.0.0.1:13901", req)
.await
{
Ok(response) => {
Ok(response)
},
Err(_error) => {
Ok(Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
.body(Body::empty())
.unwrap())},
2022-03-12 14:58:18 +00:00
}
} else if req.uri().path().starts_with("/target/second") {
2022-05-01 18:36:09 +00:00
match PROXY_CLIENT.call(client_ip, "http://127.0.0.1:13902", req)
.await
{
Ok(response) => Ok(response),
Err(_error) => Ok(Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
.body(Body::empty())
.unwrap()),
2022-03-12 14:58:18 +00:00
}
} else {
2022-05-01 18:36:09 +00:00
debug_request(& req)
2022-03-12 14:58:18 +00:00
}
}
2022-03-12 15:48:31 +00:00
2022-03-12 14:47:58 +00:00
#[tokio::main]
2022-03-12 14:58:18 +00:00
async fn main() {
let bind_addr = "127.0.0.1:8000";
2022-05-01 18:36:09 +00:00
let addr: SocketAddr = bind_addr.parse().expect("Could not parse ip:port.");
2022-03-12 15:48:31 +00:00
2022-03-12 14:58:18 +00:00
let make_svc = make_service_fn(|conn: & AddrStream| {
let remote_addr = conn.remote_addr().ip();
2022-05-01 18:36:09 +00:00
async move { Ok::< _ , Infallible > (service_fn(move |req| handle(remote_addr, req))) }
2019-02-16 17:46:52 +00:00
});
2022-03-12 15:48:31 +00:00
2022-03-12 14:47:58 +00:00
let server = Server::bind(&addr).serve(make_svc);
2022-03-12 15:48:31 +00:00
2019-02-16 17:46:52 +00:00
println!("Running server on {:?}", addr);
2022-03-12 15:48:31 +00:00
2022-03-12 14:47:58 +00:00
if let Err(e) = server.await {
eprintln!("server error: {}", e);
}
2019-02-16 17:46:52 +00:00
}
```
2022-04-13 14:53:37 +00:00
2022-05-01 18:36:09 +00:00
### A word about Security
2022-04-13 14:53:37 +00:00
2022-05-01 18:36:09 +00:00
Handling outgoing requests can be a security nightmare. This crate does not control the client for the outgoing requests, as it needs to be supplied to the proxy call. The following chapters may give you an overview on how you can secure your client using the `hyper-trust-dns` crate.
> You can see them being used in the example.
2022-04-13 14:53:37 +00:00
#### HTTPS
2022-05-01 18:36:09 +00:00
You should use a secure transport in order to know who you are talking to and so you can trust the connection. By default `hyper-trust-dns` enables the feature flag `https-only` which will panic if you supply a transport scheme which isn't `https` . It is a healthy default as it's not only you needing to trust the source but also everyone else seeing the content on unsecure connections.
> ATTENTION: if you are running on a host with added certificates in your cert store, make sure to audit them in a interval, so neither old certificates nor malicious certificates are considered as valid by your client.
2022-04-13 14:53:37 +00:00
#### TLS 1.2
2022-05-01 18:36:09 +00:00
By default `tls 1.2` is disabled in favor of `tls 1.3` , because many parts of `tls 1.2` can be considered as attach friendly. As not yet all services support it `tls 1.2` can be enabled via the `rustls-tls-12` feature.
> ATTENTION: make sure to audit the services you connect to on an interval
2022-04-13 14:53:37 +00:00
#### DNSSEC
2022-05-01 18:36:09 +00:00
As dns queries and entries aren't "trustworthy" by default from a security standpoint. `DNSSEC` adds a new cryptographic layer for verification. To enable it use the `dnssec-ring` feature.
2022-04-13 14:53:37 +00:00
#### HTTP/2
2022-05-01 18:36:09 +00:00
By default only rustlss `http1` feature is enabled for dns queries. While `http/3` might be just around the corner. `http/2` support can be enabled using the `rustls-http2` feature.
2022-04-13 14:53:37 +00:00
#### DoT & DoH
2022-05-01 18:36:09 +00:00
DoT and DoH provide you with a secure transport between you and your dns.
2022-04-13 14:53:37 +00:00
By default none of them are enabled. If you would like to enabled them, you can do so using the features `doh` and `dot` .
Recommendations:
2022-05-01 18:36:09 +00:00
- If you need to monitor network activities in relation to accessed ports, use dot with the `dns-over-rustls` feature flag
- If you are out in the wild and have no need to monitor based on ports, doh with the `dns-over-https-rustls` feature flag as it will blend in with other `https` traffic
2022-04-13 14:53:37 +00:00
It is highly recommended to use one of them.
2024-03-05 19:47:44 +00:00
> Currently only includes dns queries as `esni` or `ech` is still in draft by the `ietf`