refactor: separeted forwarder definition for more flexibility
This commit is contained in:
parent
efda83b214
commit
43b004cf6e
5 changed files with 84 additions and 15 deletions
|
|
@ -56,7 +56,10 @@ upstream = [
|
||||||
{ location = 'www.yahoo.co.jp', tls = true },
|
{ location = 'www.yahoo.co.jp', tls = true },
|
||||||
]
|
]
|
||||||
load_balance = "round_robin" # or "random" or "sticky" (sticky session) or "none" (fix to the first one, default)
|
load_balance = "round_robin" # or "random" or "sticky" (sticky session) or "none" (fix to the first one, default)
|
||||||
upstream_options = ["override_host", "convert_https_to_2"]
|
upstream_options = [
|
||||||
|
"override_host",
|
||||||
|
"force_http2_upstream", # mutually exclusive with "force_http11_upstream"
|
||||||
|
]
|
||||||
|
|
||||||
# Non-default destination in "localhost" app, which is routed by "path"
|
# Non-default destination in "localhost" app, which is routed by "path"
|
||||||
[[apps.localhost.reverse_proxy]]
|
[[apps.localhost.reverse_proxy]]
|
||||||
|
|
|
||||||
58
rpxy-lib/src/handler/forwarder.rs
Normal file
58
rpxy-lib/src/handler/forwarder.rs
Normal file
|
|
@ -0,0 +1,58 @@
|
||||||
|
use crate::error::RpxyError;
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use derive_builder::Builder;
|
||||||
|
use hyper::{
|
||||||
|
body::{Body, HttpBody},
|
||||||
|
client::{connect::Connect, HttpConnector},
|
||||||
|
Client, Request, Response,
|
||||||
|
};
|
||||||
|
use hyper_rustls::HttpsConnector;
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
/// Definition of the forwarder that simply forward requests from downstream client to upstream app servers.
|
||||||
|
pub trait ForwardRequest<B> {
|
||||||
|
type Error;
|
||||||
|
async fn request(&self, req: Request<B>) -> Result<Response<Body>, Self::Error>;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Builder, Clone)]
|
||||||
|
/// Forwarder struct
|
||||||
|
pub struct Forwarder<C, B = Body>
|
||||||
|
where
|
||||||
|
C: Connect + Clone + Sync + Send + 'static,
|
||||||
|
{
|
||||||
|
// TODO: need `h2c` or http/2-only client separately
|
||||||
|
inner: Client<C, B>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl<C, B> ForwardRequest<B> for Forwarder<C, B>
|
||||||
|
where
|
||||||
|
B: HttpBody + Send + 'static,
|
||||||
|
B::Data: Send,
|
||||||
|
B::Error: Into<Box<dyn std::error::Error + Send + Sync>>,
|
||||||
|
C: Connect + Clone + Sync + Send + 'static,
|
||||||
|
{
|
||||||
|
type Error = RpxyError;
|
||||||
|
async fn request(&self, req: Request<B>) -> Result<Response<Body>, Self::Error> {
|
||||||
|
// TODO:
|
||||||
|
// TODO: Implement here a client that handles `h2c` requests
|
||||||
|
// TODO:
|
||||||
|
self.inner.request(req).await.map_err(RpxyError::Hyper)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Forwarder<HttpsConnector<HttpConnector>, Body> {
|
||||||
|
pub async fn new() -> Self {
|
||||||
|
// let connector = TrustDnsResolver::default().into_rustls_webpki_https_connector();
|
||||||
|
let connector = hyper_rustls::HttpsConnectorBuilder::new()
|
||||||
|
.with_webpki_roots()
|
||||||
|
.https_or_http()
|
||||||
|
.enable_http1()
|
||||||
|
.enable_http2()
|
||||||
|
.build();
|
||||||
|
|
||||||
|
let inner = Client::builder().build::<_, Body>(connector);
|
||||||
|
Self { inner }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,5 +1,11 @@
|
||||||
// Highly motivated by https://github.com/felipenoris/hyper-reverse-proxy
|
// Highly motivated by https://github.com/felipenoris/hyper-reverse-proxy
|
||||||
use super::{utils_headers::*, utils_request::*, utils_synth_response::*, HandlerContext};
|
use super::{
|
||||||
|
forwarder::{ForwardRequest, Forwarder},
|
||||||
|
utils_headers::*,
|
||||||
|
utils_request::*,
|
||||||
|
utils_synth_response::*,
|
||||||
|
HandlerContext,
|
||||||
|
};
|
||||||
use crate::{
|
use crate::{
|
||||||
backend::{Backend, UpstreamGroup},
|
backend::{Backend, UpstreamGroup},
|
||||||
certs::CryptoSource,
|
certs::CryptoSource,
|
||||||
|
|
@ -14,7 +20,7 @@ use hyper::{
|
||||||
client::connect::Connect,
|
client::connect::Connect,
|
||||||
header::{self, HeaderValue},
|
header::{self, HeaderValue},
|
||||||
http::uri::Scheme,
|
http::uri::Scheme,
|
||||||
Body, Client, Request, Response, StatusCode, Uri, Version,
|
Body, Request, Response, StatusCode, Uri, Version,
|
||||||
};
|
};
|
||||||
use std::{net::SocketAddr, sync::Arc};
|
use std::{net::SocketAddr, sync::Arc};
|
||||||
use tokio::{io::copy_bidirectional, time::timeout};
|
use tokio::{io::copy_bidirectional, time::timeout};
|
||||||
|
|
@ -27,7 +33,7 @@ where
|
||||||
T: Connect + Clone + Sync + Send + 'static,
|
T: Connect + Clone + Sync + Send + 'static,
|
||||||
U: CryptoSource + Clone,
|
U: CryptoSource + Clone,
|
||||||
{
|
{
|
||||||
forwarder: Arc<Client<T>>,
|
forwarder: Arc<Forwarder<T>>,
|
||||||
globals: Arc<Globals<U>>,
|
globals: Arc<Globals<U>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
mod forwarder;
|
||||||
mod handler_main;
|
mod handler_main;
|
||||||
mod utils_headers;
|
mod utils_headers;
|
||||||
mod utils_request;
|
mod utils_request;
|
||||||
|
|
@ -5,7 +6,10 @@ mod utils_synth_response;
|
||||||
|
|
||||||
#[cfg(feature = "sticky-cookie")]
|
#[cfg(feature = "sticky-cookie")]
|
||||||
use crate::backend::LbContext;
|
use crate::backend::LbContext;
|
||||||
pub use handler_main::{HttpMessageHandler, HttpMessageHandlerBuilder, HttpMessageHandlerBuilderError};
|
pub use {
|
||||||
|
forwarder::Forwarder,
|
||||||
|
handler_main::{HttpMessageHandler, HttpMessageHandlerBuilder, HttpMessageHandlerBuilderError},
|
||||||
|
};
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
|
|
||||||
|
|
@ -8,9 +8,14 @@ mod log;
|
||||||
mod proxy;
|
mod proxy;
|
||||||
mod utils;
|
mod utils;
|
||||||
|
|
||||||
use crate::{error::*, globals::Globals, handler::HttpMessageHandlerBuilder, log::*, proxy::ProxyBuilder};
|
use crate::{
|
||||||
|
error::*,
|
||||||
|
globals::Globals,
|
||||||
|
handler::{Forwarder, HttpMessageHandlerBuilder},
|
||||||
|
log::*,
|
||||||
|
proxy::ProxyBuilder,
|
||||||
|
};
|
||||||
use futures::future::select_all;
|
use futures::future::select_all;
|
||||||
use hyper::Client;
|
|
||||||
// use hyper_trust_dns::TrustDnsResolver;
|
// use hyper_trust_dns::TrustDnsResolver;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
|
@ -62,17 +67,10 @@ where
|
||||||
request_count: Default::default(),
|
request_count: Default::default(),
|
||||||
runtime_handle: runtime_handle.clone(),
|
runtime_handle: runtime_handle.clone(),
|
||||||
});
|
});
|
||||||
// let connector = TrustDnsResolver::default().into_rustls_webpki_https_connector();
|
|
||||||
let connector = hyper_rustls::HttpsConnectorBuilder::new()
|
|
||||||
.with_webpki_roots()
|
|
||||||
.https_or_http()
|
|
||||||
.enable_http1()
|
|
||||||
.enable_http2()
|
|
||||||
.build();
|
|
||||||
|
|
||||||
// TODO: HTTP2 only client is needed for http2 cleartext case
|
// TODO: HTTP2 only client is needed for http2 cleartext case
|
||||||
let msg_handler = HttpMessageHandlerBuilder::default()
|
let msg_handler = HttpMessageHandlerBuilder::default()
|
||||||
.forwarder(Arc::new(Client::builder().build::<_, hyper::Body>(connector)))
|
.forwarder(Arc::new(Forwarder::new().await))
|
||||||
.globals(globals.clone())
|
.globals(globals.clone())
|
||||||
.build()?;
|
.build()?;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue