reformat
This commit is contained in:
parent
36c8ebcb54
commit
45895ca7bc
12 changed files with 36 additions and 151 deletions
|
|
@ -135,28 +135,18 @@ impl Backend {
|
|||
if let (Some(c), Some(k)) = (self.tls_cert_path.as_ref(), self.tls_cert_key_path.as_ref()) {
|
||||
(c, k)
|
||||
} else {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
"Invalid certs and keys paths",
|
||||
));
|
||||
return Err(io::Error::new(io::ErrorKind::Other, "Invalid certs and keys paths"));
|
||||
};
|
||||
let certs: Vec<_> = {
|
||||
let certs_path_str = certs_path.display().to_string();
|
||||
let mut reader = BufReader::new(File::open(certs_path).map_err(|e| {
|
||||
io::Error::new(
|
||||
e.kind(),
|
||||
format!(
|
||||
"Unable to load the certificates [{}]: {}",
|
||||
certs_path_str, e
|
||||
),
|
||||
format!("Unable to load the certificates [{}]: {}", certs_path_str, e),
|
||||
)
|
||||
})?);
|
||||
rustls_pemfile::certs(&mut reader).map_err(|_| {
|
||||
io::Error::new(
|
||||
io::ErrorKind::InvalidInput,
|
||||
"Unable to parse the certificates",
|
||||
)
|
||||
})?
|
||||
rustls_pemfile::certs(&mut reader)
|
||||
.map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "Unable to parse the certificates"))?
|
||||
}
|
||||
.drain(..)
|
||||
.map(Certificate)
|
||||
|
|
@ -169,10 +159,7 @@ impl Backend {
|
|||
.map_err(|e| {
|
||||
io::Error::new(
|
||||
e.kind(),
|
||||
format!(
|
||||
"Unable to load the certificate keys [{}]: {}",
|
||||
certs_keys_path_str, e
|
||||
),
|
||||
format!("Unable to load the certificate keys [{}]: {}", certs_keys_path_str, e),
|
||||
)
|
||||
})?
|
||||
.read_to_end(&mut encoded_keys)?;
|
||||
|
|
@ -217,9 +204,7 @@ impl Backend {
|
|||
}
|
||||
|
||||
impl Backends {
|
||||
pub async fn generate_server_crypto_with_cert_resolver(
|
||||
&self,
|
||||
) -> Result<ServerConfig, anyhow::Error> {
|
||||
pub async fn generate_server_crypto_with_cert_resolver(&self) -> Result<ServerConfig, anyhow::Error> {
|
||||
let mut resolver = ResolvesServerCertUsingSni::new();
|
||||
|
||||
let mut cnt = 0;
|
||||
|
|
@ -234,19 +219,12 @@ impl Backends {
|
|||
e
|
||||
)
|
||||
} else {
|
||||
debug!(
|
||||
"Add certificate for server_name: {}",
|
||||
backend.server_name.as_str()
|
||||
);
|
||||
debug!("Add certificate for server_name: {}", backend.server_name.as_str());
|
||||
cnt += 1;
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
warn!(
|
||||
"Failed to add certificate for {}: {}",
|
||||
backend.server_name.as_str(),
|
||||
e
|
||||
);
|
||||
warn!("Failed to add certificate for {}: {}", backend.server_name.as_str(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -207,10 +207,7 @@ fn get_reverse_proxy(rp_settings: &[ReverseProxyOption]) -> Result<ReverseProxy>
|
|||
};
|
||||
|
||||
if rpo.path.is_some() {
|
||||
upstream.insert(
|
||||
rpo.path.as_ref().unwrap().as_bytes().to_ascii_lowercase(),
|
||||
elem,
|
||||
);
|
||||
upstream.insert(rpo.path.as_ref().unwrap().as_bytes().to_ascii_lowercase(), elem);
|
||||
} else {
|
||||
upstream.insert("/".as_bytes().to_ascii_lowercase(), elem);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -62,9 +62,7 @@ impl MessageLog {
|
|||
self
|
||||
}
|
||||
pub fn xff(&mut self, xff: &Option<&hyper::header::HeaderValue>) -> &mut Self {
|
||||
self.xff = xff
|
||||
.map_or_else(|| "", |v| v.to_str().unwrap_or(""))
|
||||
.to_string();
|
||||
self.xff = xff.map_or_else(|| "", |v| v.to_str().unwrap_or("")).to_string();
|
||||
self
|
||||
}
|
||||
pub fn upstream(&mut self, upstream: &str) -> &mut Self {
|
||||
|
|
|
|||
|
|
@ -28,11 +28,7 @@ impl<T> HttpMessageHandler<T>
|
|||
where
|
||||
T: Connect + Clone + Sync + Send + 'static,
|
||||
{
|
||||
fn return_with_error_log(
|
||||
&self,
|
||||
status_code: StatusCode,
|
||||
log_data: &mut MessageLog,
|
||||
) -> Result<Response<Body>> {
|
||||
fn return_with_error_log(&self, status_code: StatusCode, log_data: &mut MessageLog) -> Result<Response<Body>> {
|
||||
log_data.status_code(&status_code).output();
|
||||
http_error(status_code)
|
||||
}
|
||||
|
|
@ -77,9 +73,7 @@ where
|
|||
// Redirect to https if !tls_enabled and redirect_to_https is true
|
||||
if !tls_enabled && backend.https_redirection.unwrap_or(false) {
|
||||
debug!("Redirect to secure connection: {}", &backend.server_name);
|
||||
log_data
|
||||
.status_code(&StatusCode::PERMANENT_REDIRECT)
|
||||
.output();
|
||||
log_data.status_code(&StatusCode::PERMANENT_REDIRECT).output();
|
||||
return secure_redirection(&backend.server_name, self.globals.https_port, &req);
|
||||
}
|
||||
|
||||
|
|
@ -124,12 +118,7 @@ where
|
|||
|
||||
// Forward request to
|
||||
let mut res_backend = {
|
||||
match timeout(
|
||||
self.globals.upstream_timeout,
|
||||
self.forwarder.request(req_forwarded),
|
||||
)
|
||||
.await
|
||||
{
|
||||
match timeout(self.globals.upstream_timeout, self.forwarder.request(req_forwarded)).await {
|
||||
Err(_) => {
|
||||
return self.return_with_error_log(StatusCode::GATEWAY_TIMEOUT, &mut log_data);
|
||||
}
|
||||
|
|
@ -160,18 +149,13 @@ where
|
|||
|
||||
// Handle StatusCode::SWITCHING_PROTOCOLS in response
|
||||
let upgrade_in_response = extract_upgrade(res_backend.headers());
|
||||
if if let (Some(u_req), Some(u_res)) =
|
||||
(upgrade_in_request.as_ref(), upgrade_in_response.as_ref())
|
||||
{
|
||||
if if let (Some(u_req), Some(u_res)) = (upgrade_in_request.as_ref(), upgrade_in_response.as_ref()) {
|
||||
u_req.to_ascii_lowercase() == u_res.to_ascii_lowercase()
|
||||
} else {
|
||||
false
|
||||
} {
|
||||
if let Some(request_upgraded) = request_upgraded {
|
||||
let onupgrade = if let Some(onupgrade) = res_backend
|
||||
.extensions_mut()
|
||||
.remove::<hyper::upgrade::OnUpgrade>()
|
||||
{
|
||||
let onupgrade = if let Some(onupgrade) = res_backend.extensions_mut().remove::<hyper::upgrade::OnUpgrade>() {
|
||||
onupgrade
|
||||
} else {
|
||||
error!("Response does not have an upgrade extension");
|
||||
|
|
@ -210,10 +194,7 @@ where
|
|||
////////////////////////////////////////////////////
|
||||
// Functions to generate messages
|
||||
|
||||
fn generate_response_forwarded<B: core::fmt::Debug>(
|
||||
&self,
|
||||
response: &mut Response<B>,
|
||||
) -> Result<()> {
|
||||
fn generate_response_forwarded<B: core::fmt::Debug>(&self, response: &mut Response<B>) -> Result<()> {
|
||||
let headers = response.headers_mut();
|
||||
remove_connection_header(headers);
|
||||
remove_hop_header(headers);
|
||||
|
|
@ -272,19 +253,12 @@ where
|
|||
|
||||
// Add te: trailer if te_trailer
|
||||
if contains_te_trailers {
|
||||
headers.insert(
|
||||
header::TE,
|
||||
HeaderValue::from_bytes("trailers".as_bytes()).unwrap(),
|
||||
);
|
||||
headers.insert(header::TE, HeaderValue::from_bytes("trailers".as_bytes()).unwrap());
|
||||
}
|
||||
|
||||
// add "host" header of original server_name if not exist (default)
|
||||
if req.headers().get(header::HOST).is_none() {
|
||||
let org_host = req
|
||||
.uri()
|
||||
.host()
|
||||
.ok_or_else(|| anyhow!("Invalid request"))?
|
||||
.to_owned();
|
||||
let org_host = req.uri().host().ok_or_else(|| anyhow!("Invalid request"))?.to_owned();
|
||||
req
|
||||
.headers_mut()
|
||||
.insert(header::HOST, HeaderValue::from_str(&org_host)?);
|
||||
|
|
|
|||
|
|
@ -37,11 +37,7 @@ pub(super) fn apply_upstream_options_to_header(
|
|||
}
|
||||
|
||||
// https://datatracker.ietf.org/doc/html/rfc9110
|
||||
pub(super) fn append_header_entry_with_comma(
|
||||
headers: &mut HeaderMap,
|
||||
key: &str,
|
||||
value: &str,
|
||||
) -> Result<()> {
|
||||
pub(super) fn append_header_entry_with_comma(headers: &mut HeaderMap, key: &str, value: &str) -> Result<()> {
|
||||
match headers.entry(HeaderName::from_bytes(key.as_bytes())?) {
|
||||
header::Entry::Vacant(entry) => {
|
||||
entry.insert(value.parse::<HeaderValue>()?);
|
||||
|
|
@ -99,20 +95,12 @@ pub(super) fn add_forwarding_header(
|
|||
) -> Result<()> {
|
||||
// default process
|
||||
// optional process defined by upstream_option is applied in fn apply_upstream_options
|
||||
append_header_entry_with_comma(
|
||||
headers,
|
||||
"x-forwarded-for",
|
||||
&client_addr.to_canonical().ip().to_string(),
|
||||
)?;
|
||||
append_header_entry_with_comma(headers, "x-forwarded-for", &client_addr.to_canonical().ip().to_string())?;
|
||||
|
||||
/////////// As Nginx
|
||||
// If we receive X-Forwarded-Proto, pass it through; otherwise, pass along the
|
||||
// scheme used to connect to this server
|
||||
add_header_entry_if_not_exist(
|
||||
headers,
|
||||
"x-forwarded-proto",
|
||||
if tls { "https" } else { "http" },
|
||||
)?;
|
||||
add_header_entry_if_not_exist(headers, "x-forwarded-proto", if tls { "https" } else { "http" })?;
|
||||
// If we receive X-Forwarded-Port, pass it through; otherwise, pass along the
|
||||
// server port the client connected to
|
||||
add_header_entry_if_not_exist(headers, "x-forwarded-port", listen_addr.port().to_string())?;
|
||||
|
|
|
|||
|
|
@ -28,10 +28,7 @@ impl<B> ReqLog for &Request<B> {
|
|||
.scheme_str()
|
||||
.map_or_else(|| "".to_string(), |v| format!("{}://", v));
|
||||
let uri_host = self.uri().host().unwrap_or("");
|
||||
let uri_pq = self
|
||||
.uri()
|
||||
.path_and_query()
|
||||
.map_or_else(|| "", |v| v.as_str());
|
||||
let uri_pq = self.uri().path_and_query().map_or_else(|| "", |v| v.as_str());
|
||||
let ua = self
|
||||
.headers()
|
||||
.get(header::USER_AGENT)
|
||||
|
|
|
|||
|
|
@ -5,18 +5,8 @@ use std::fmt::Display;
|
|||
////////////////////////////////////////////////////
|
||||
// Functions of utils for request messages
|
||||
pub trait ResLog {
|
||||
fn log<T1: Display, T2: Display + ToCanonical>(
|
||||
self,
|
||||
server_name: &T1,
|
||||
client_addr: &T2,
|
||||
extra: Option<&str>,
|
||||
);
|
||||
fn log_debug<T1: Display, T2: Display + ToCanonical>(
|
||||
self,
|
||||
server_name: &T1,
|
||||
client_addr: &T2,
|
||||
extra: Option<&str>,
|
||||
);
|
||||
fn log<T1: Display, T2: Display + ToCanonical>(self, server_name: &T1, client_addr: &T2, extra: Option<&str>);
|
||||
fn log_debug<T1: Display, T2: Display + ToCanonical>(self, server_name: &T1, client_addr: &T2, extra: Option<&str>);
|
||||
fn build_message<T1: Display, T2: Display + ToCanonical>(
|
||||
self,
|
||||
server_name: &T1,
|
||||
|
|
@ -25,20 +15,10 @@ pub trait ResLog {
|
|||
) -> String;
|
||||
}
|
||||
impl<B> ResLog for &Response<B> {
|
||||
fn log<T1: Display, T2: Display + ToCanonical>(
|
||||
self,
|
||||
server_name: &T1,
|
||||
client_addr: &T2,
|
||||
extra: Option<&str>,
|
||||
) {
|
||||
fn log<T1: Display, T2: Display + ToCanonical>(self, server_name: &T1, client_addr: &T2, extra: Option<&str>) {
|
||||
info!("{}", &self.build_message(server_name, client_addr, extra));
|
||||
}
|
||||
fn log_debug<T1: Display, T2: Display + ToCanonical>(
|
||||
self,
|
||||
server_name: &T1,
|
||||
client_addr: &T2,
|
||||
extra: Option<&str>,
|
||||
) {
|
||||
fn log_debug<T1: Display, T2: Display + ToCanonical>(self, server_name: &T1, client_addr: &T2, extra: Option<&str>) {
|
||||
debug!("{}", &self.build_message(server_name, client_addr, extra));
|
||||
}
|
||||
fn build_message<T1: Display, T2: Display + ToCanonical>(
|
||||
|
|
|
|||
|
|
@ -6,9 +6,7 @@ use hyper::{Body, Request, Response, StatusCode, Uri};
|
|||
// Functions to create response (error or redirect)
|
||||
|
||||
pub(super) fn http_error(status_code: StatusCode) -> Result<Response<Body>> {
|
||||
let response = Response::builder()
|
||||
.status(status_code)
|
||||
.body(Body::empty())?;
|
||||
let response = Response::builder().status(status_code).body(Body::empty())?;
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -11,9 +11,7 @@ where
|
|||
T: Connect + Clone + Sync + Send + 'static,
|
||||
{
|
||||
pub(super) fn connection_serve_h3(&self, conn: quinn::Connecting, tls_server_name: &[u8]) {
|
||||
let fut = self
|
||||
.clone()
|
||||
.handle_connection_h3(conn, tls_server_name.to_vec());
|
||||
let fut = self.clone().handle_connection_h3(conn, tls_server_name.to_vec());
|
||||
self.globals.runtime_handle.spawn(async move {
|
||||
// Timeout is based on underlying quic
|
||||
if let Err(e) = fut.await {
|
||||
|
|
@ -22,18 +20,12 @@ where
|
|||
});
|
||||
}
|
||||
|
||||
async fn handle_connection_h3(
|
||||
self,
|
||||
conn: quinn::Connecting,
|
||||
tls_server_name: ServerNameLC,
|
||||
) -> Result<()> {
|
||||
async fn handle_connection_h3(self, conn: quinn::Connecting, tls_server_name: ServerNameLC) -> Result<()> {
|
||||
let client_addr = conn.remote_address();
|
||||
|
||||
match conn.await {
|
||||
Ok(new_conn) => {
|
||||
let mut h3_conn =
|
||||
h3::server::Connection::<_, bytes::Bytes>::new(h3_quinn::Connection::new(new_conn))
|
||||
.await?;
|
||||
let mut h3_conn = h3::server::Connection::<_, bytes::Bytes>::new(h3_quinn::Connection::new(new_conn)).await?;
|
||||
info!(
|
||||
"QUIC/HTTP3 connection established from {:?} {:?}",
|
||||
client_addr, tls_server_name
|
||||
|
|
@ -113,9 +105,7 @@ where
|
|||
return Err(anyhow!("Exceeds max request body size for HTTP/3"));
|
||||
}
|
||||
// create stream body to save memory, shallow copy (increment of ref-count) to Bytes using copy_to_bytes
|
||||
sender
|
||||
.send_data(body.copy_to_bytes(body.remaining()))
|
||||
.await?;
|
||||
sender.send_data(body.copy_to_bytes(body.remaining())).await?;
|
||||
}
|
||||
|
||||
// trailers: use inner for work around. (directly get trailer)
|
||||
|
|
|
|||
|
|
@ -1,7 +1,5 @@
|
|||
// use super::proxy_handler::handle_request;
|
||||
use crate::{
|
||||
backend::ServerNameLC, error::*, globals::Globals, log::*, msg_handler::HttpMessageHandler,
|
||||
};
|
||||
use crate::{backend::ServerNameLC, error::*, globals::Globals, log::*, msg_handler::HttpMessageHandler};
|
||||
use hyper::{client::connect::Connect, server::conn::Http, service::service_fn, Body, Request};
|
||||
use std::{net::SocketAddr, sync::Arc};
|
||||
use tokio::{
|
||||
|
|
@ -95,9 +93,7 @@ where
|
|||
let tcp_listener = TcpListener::bind(&self.listening_on).await?;
|
||||
info!("Start TCP proxy serving with HTTP request for configured host names");
|
||||
while let Ok((stream, _client_addr)) = tcp_listener.accept().await {
|
||||
self
|
||||
.clone()
|
||||
.client_serve(stream, server.clone(), _client_addr, None);
|
||||
self.clone().client_serve(stream, server.clone(), _client_addr, None);
|
||||
}
|
||||
Ok(()) as Result<()>
|
||||
};
|
||||
|
|
|
|||
|
|
@ -20,12 +20,7 @@ where
|
|||
async fn cert_service(&self, server_crypto_tx: watch::Sender<Option<Arc<ServerConfig>>>) {
|
||||
info!("Start cert watch service");
|
||||
loop {
|
||||
if let Ok(server_crypto) = self
|
||||
.globals
|
||||
.backends
|
||||
.generate_server_crypto_with_cert_resolver()
|
||||
.await
|
||||
{
|
||||
if let Ok(server_crypto) = self.globals.backends.generate_server_crypto_with_cert_resolver().await {
|
||||
if let Err(_e) = server_crypto_tx.send(Some(Arc::new(server_crypto))) {
|
||||
error!("Failed to populate server crypto");
|
||||
break;
|
||||
|
|
@ -82,10 +77,7 @@ where
|
|||
}
|
||||
|
||||
#[cfg(feature = "h3")]
|
||||
async fn listener_service_h3(
|
||||
&self,
|
||||
mut server_crypto_rx: watch::Receiver<Option<Arc<ServerConfig>>>,
|
||||
) -> Result<()> {
|
||||
async fn listener_service_h3(&self, mut server_crypto_rx: watch::Receiver<Option<Arc<ServerConfig>>>) -> Result<()> {
|
||||
let mut transport_config_quic = quinn::TransportConfig::default();
|
||||
transport_config_quic
|
||||
.max_concurrent_bidi_streams(self.globals.h3_max_concurrent_bidistream)
|
||||
|
|
|
|||
|
|
@ -51,10 +51,7 @@ mod tests {
|
|||
}
|
||||
#[test]
|
||||
fn ipv4_mapped_to_ipv6_to_canonical() {
|
||||
let socket = SocketAddr::new(
|
||||
IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff)),
|
||||
8080,
|
||||
);
|
||||
let socket = SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff)), 8080);
|
||||
assert_eq!(
|
||||
socket.to_canonical(),
|
||||
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(192, 10, 2, 255)), 8080)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue