2021-03-05 04:03:40 +00:00
|
|
|
use std::{
|
|
|
|
net::SocketAddr,
|
|
|
|
sync::Arc,
|
|
|
|
};
|
|
|
|
|
2021-03-06 22:58:23 +00:00
|
|
|
use async_trait::async_trait;
|
2021-03-06 21:15:41 +00:00
|
|
|
use futures_util::StreamExt;
|
2021-03-05 04:03:40 +00:00
|
|
|
use hyper::{
|
|
|
|
Body,
|
|
|
|
Request,
|
|
|
|
Response,
|
|
|
|
Server,
|
2021-03-06 22:58:23 +00:00
|
|
|
body::Bytes,
|
2021-03-05 04:03:40 +00:00
|
|
|
service::{
|
|
|
|
make_service_fn,
|
|
|
|
service_fn,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
use reqwest::Client;
|
|
|
|
use tokio::{
|
|
|
|
spawn,
|
2021-03-06 18:55:05 +00:00
|
|
|
sync::{
|
|
|
|
mpsc,
|
|
|
|
oneshot,
|
|
|
|
},
|
2021-03-05 04:03:40 +00:00
|
|
|
};
|
2021-03-06 21:15:41 +00:00
|
|
|
use tokio_stream::wrappers::ReceiverStream;
|
2021-03-05 04:03:40 +00:00
|
|
|
|
2021-03-06 22:58:23 +00:00
|
|
|
#[async_trait]
|
|
|
|
trait ProxyFilter {
|
|
|
|
async fn request_body (&self, mut body: Body, tx: mpsc::Sender <Result <Bytes, hyper::Error>>) -> anyhow::Result <()>;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct PassthroughFilter {}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl ProxyFilter for PassthroughFilter {
|
|
|
|
async fn request_body (&self, mut body: Body, tx: mpsc::Sender <Result <Bytes, hyper::Error>>) -> anyhow::Result <()> {
|
|
|
|
let mut bytes_transferred = 0;
|
|
|
|
|
|
|
|
loop {
|
|
|
|
let item = body.next ().await;
|
|
|
|
|
|
|
|
if let Some (item) = item {
|
|
|
|
if let Ok (item) = &item {
|
|
|
|
bytes_transferred += item.len ();
|
|
|
|
}
|
|
|
|
tx.send (item).await?;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Finished
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-03 15:23:03 +00:00
|
|
|
let _bytes_transferred = bytes_transferred;
|
|
|
|
|
2021-03-06 22:58:23 +00:00
|
|
|
Ok (())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct RequestBodyDropFilter {}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl ProxyFilter for RequestBodyDropFilter {
|
2021-04-03 15:23:03 +00:00
|
|
|
async fn request_body (&self, mut body: Body, _tx: mpsc::Sender <Result <Bytes, hyper::Error>>) -> anyhow::Result <()> {
|
2021-03-06 22:58:23 +00:00
|
|
|
let mut bytes_transferred = 0;
|
|
|
|
|
|
|
|
loop {
|
|
|
|
let item = body.next ().await;
|
|
|
|
|
|
|
|
if let Some (item) = item {
|
|
|
|
if let Ok (item) = &item {
|
|
|
|
bytes_transferred += item.len ();
|
|
|
|
}
|
|
|
|
// tx.send (item).await?;
|
|
|
|
tracing::debug! ("RequestBodyDropFilter dropping chunk");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Finished
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-03 15:23:03 +00:00
|
|
|
let _bytes_transferred = bytes_transferred;
|
|
|
|
|
2021-03-06 22:58:23 +00:00
|
|
|
Ok (())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct State <PF> {
|
2021-03-05 04:03:40 +00:00
|
|
|
client: Client,
|
|
|
|
upstream_authority: String,
|
2021-03-06 22:58:23 +00:00
|
|
|
proxy_filter: Arc <PF>,
|
2021-03-05 04:03:40 +00:00
|
|
|
}
|
|
|
|
|
2021-03-06 22:58:23 +00:00
|
|
|
async fn handle_all <PF: 'static + ProxyFilter + Sync + Send> (req: Request <Body>, state: Arc <State <PF>>)
|
2021-03-05 04:03:40 +00:00
|
|
|
-> anyhow::Result <Response <Body>>
|
|
|
|
{
|
2021-03-21 03:09:44 +00:00
|
|
|
let req_id = rusty_ulid::generate_ulid_string ();
|
2021-03-21 03:34:47 +00:00
|
|
|
let (head, body) = req.into_parts ();
|
2021-03-05 04:03:40 +00:00
|
|
|
|
2021-03-06 22:58:23 +00:00
|
|
|
tracing::debug! ("{} Got URI {}", req_id, head.uri);
|
2021-03-05 04:03:40 +00:00
|
|
|
|
|
|
|
let upstream_authority = state.upstream_authority.clone ();
|
|
|
|
|
|
|
|
let mut new_uri = head.uri.clone ().into_parts ();
|
|
|
|
new_uri.scheme = Some (http::uri::Scheme::HTTP);
|
|
|
|
new_uri.authority = Some (http::uri::Authority::from_maybe_shared (upstream_authority)?);
|
|
|
|
let new_uri = http::Uri::from_parts (new_uri)?;
|
|
|
|
|
|
|
|
tracing::trace! ("{} Rebuilt URI as {}", req_id, new_uri);
|
|
|
|
|
|
|
|
let mut upstream_req = state.client.request (head.method, &new_uri.to_string ());
|
|
|
|
for (k, v) in &head.headers {
|
|
|
|
upstream_req = upstream_req.header (k, v);
|
|
|
|
}
|
|
|
|
|
2021-03-06 21:15:41 +00:00
|
|
|
let (tx, rx) = mpsc::channel (1);
|
2021-03-05 04:03:40 +00:00
|
|
|
spawn ({
|
2021-03-21 03:34:47 +00:00
|
|
|
let _req_id = req_id.clone ();
|
2021-03-06 22:58:23 +00:00
|
|
|
let proxy_filter = state.proxy_filter.clone ();
|
|
|
|
|
2021-03-05 04:03:40 +00:00
|
|
|
async move {
|
2021-03-06 22:58:23 +00:00
|
|
|
proxy_filter.request_body (body, tx).await
|
2021-03-05 04:03:40 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-03-06 21:15:41 +00:00
|
|
|
let upstream_resp = upstream_req.body (reqwest::Body::wrap_stream (ReceiverStream::new (rx))).send ().await?;
|
2021-03-05 04:03:40 +00:00
|
|
|
|
|
|
|
let mut resp = Response::builder ()
|
|
|
|
.status (upstream_resp.status ());
|
|
|
|
|
|
|
|
for (k, v) in upstream_resp.headers () {
|
|
|
|
resp = resp.header (k, v);
|
|
|
|
}
|
|
|
|
|
2021-03-06 21:15:41 +00:00
|
|
|
let (tx, rx) = mpsc::channel (1);
|
2021-03-05 04:03:40 +00:00
|
|
|
spawn (async move {
|
|
|
|
let mut body = upstream_resp.bytes_stream ();
|
|
|
|
let mut bytes_transferred = 0;
|
|
|
|
|
|
|
|
loop {
|
|
|
|
let item = body.next ().await;
|
|
|
|
|
|
|
|
if let Some (item) = item {
|
|
|
|
if let Ok (item) = &item {
|
|
|
|
bytes_transferred += item.len ();
|
|
|
|
}
|
|
|
|
tx.send (item).await?;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Finished
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tracing::trace! ("{} Response body bytes: {}", req_id, bytes_transferred);
|
|
|
|
|
|
|
|
Ok::<_, anyhow::Error> (())
|
|
|
|
});
|
|
|
|
|
2021-03-06 21:15:41 +00:00
|
|
|
Ok (resp.body (Body::wrap_stream (ReceiverStream::new (rx)))?)
|
2021-03-05 04:03:40 +00:00
|
|
|
}
|
|
|
|
|
2021-03-06 18:55:05 +00:00
|
|
|
pub async fn run_proxy (
|
|
|
|
addr: SocketAddr,
|
|
|
|
upstream_authority: String,
|
|
|
|
shutdown_oneshot: oneshot::Receiver <()>,
|
|
|
|
) -> anyhow::Result <()>
|
|
|
|
{
|
2021-03-06 22:58:23 +00:00
|
|
|
let filter = PassthroughFilter {};
|
|
|
|
// let filter = RequestBodyDropFilter {};
|
|
|
|
|
2021-03-05 04:03:40 +00:00
|
|
|
let state = Arc::new (State {
|
|
|
|
client: Client::builder ().build ()?,
|
|
|
|
upstream_authority,
|
2021-03-06 22:58:23 +00:00
|
|
|
proxy_filter: Arc::new (filter),
|
2021-03-05 04:03:40 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let make_svc = make_service_fn (|_conn| {
|
|
|
|
let state = state.clone ();
|
|
|
|
|
|
|
|
async {
|
|
|
|
Ok::<_, String> (service_fn (move |req| {
|
|
|
|
let state = state.clone ();
|
|
|
|
|
|
|
|
handle_all (req, state)
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-03-06 18:55:05 +00:00
|
|
|
let server = Server::bind (&addr)
|
|
|
|
.serve (make_svc);
|
|
|
|
|
|
|
|
tracing::info! ("Proxy binding to {}", addr);
|
|
|
|
server.with_graceful_shutdown (async {
|
|
|
|
shutdown_oneshot.await.ok ();
|
|
|
|
}).await?;
|
|
|
|
|
|
|
|
Ok (())
|
2021-03-05 04:03:40 +00:00
|
|
|
}
|