x-forwarded-* as rfc and nginx
This commit is contained in:
parent
9335fc702e
commit
0c6ffa5e77
4 changed files with 56 additions and 39 deletions
|
|
@ -157,7 +157,7 @@ where
|
||||||
let headers = response.headers_mut();
|
let headers = response.headers_mut();
|
||||||
remove_hop_header(headers);
|
remove_hop_header(headers);
|
||||||
remove_connection_header(headers);
|
remove_connection_header(headers);
|
||||||
append_header_entry(
|
append_header_entry_with_comma(
|
||||||
headers,
|
headers,
|
||||||
"server",
|
"server",
|
||||||
&format!("{}/{}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION")),
|
&format!("{}/{}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION")),
|
||||||
|
|
@ -166,7 +166,7 @@ where
|
||||||
{
|
{
|
||||||
if self.globals.http3 {
|
if self.globals.http3 {
|
||||||
if let Some(port) = self.globals.https_port {
|
if let Some(port) = self.globals.https_port {
|
||||||
append_header_entry(
|
append_header_entry_with_comma(
|
||||||
headers,
|
headers,
|
||||||
header::ALT_SVC.as_str(),
|
header::ALT_SVC.as_str(),
|
||||||
&format!(
|
&format!(
|
||||||
|
|
@ -208,7 +208,7 @@ where
|
||||||
// delete hop headers including header.connection
|
// delete hop headers including header.connection
|
||||||
remove_hop_header(headers);
|
remove_hop_header(headers);
|
||||||
// X-Forwarded-For
|
// X-Forwarded-For
|
||||||
add_forwarding_header(headers, client_addr, self.tls_enabled)?;
|
add_forwarding_header(headers, client_addr, self.tls_enabled, &self.globals)?;
|
||||||
|
|
||||||
// Add te: trailer if te_trailer
|
// Add te: trailer if te_trailer
|
||||||
if te_trailers {
|
if te_trailers {
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,8 @@ use futures::{future::FutureExt, select};
|
||||||
use hyper::{client::connect::Connect, server::conn::Http};
|
use hyper::{client::connect::Connect, server::conn::Http};
|
||||||
use rustc_hash::FxHashMap as HashMap;
|
use rustc_hash::FxHashMap as HashMap;
|
||||||
use rustls::ServerConfig;
|
use rustls::ServerConfig;
|
||||||
|
#[cfg(feature = "h3")]
|
||||||
|
use std::pin::Pin;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use tokio::{net::TcpListener, sync::watch, time::Duration};
|
use tokio::{net::TcpListener, sync::watch, time::Duration};
|
||||||
|
|
||||||
|
|
@ -147,7 +149,7 @@ where
|
||||||
let initial_app_name = next.ok_or_else(|| anyhow!(""))?;
|
let initial_app_name = next.ok_or_else(|| anyhow!(""))?;
|
||||||
debug!(
|
debug!(
|
||||||
"HTTP/3 SNI multiplexer initial app_name: {:?}",
|
"HTTP/3 SNI multiplexer initial app_name: {:?}",
|
||||||
String::from_utf8(initial_app_name.to_vec())
|
std::str::from_utf8(initial_app_name)
|
||||||
);
|
);
|
||||||
let backend_serve = self
|
let backend_serve = self
|
||||||
.backends
|
.backends
|
||||||
|
|
@ -167,7 +169,7 @@ where
|
||||||
select! {
|
select! {
|
||||||
// TODO: Not sure if this properly works to handle multiple "server_name"s to host multiple hosts.
|
// TODO: Not sure if this properly works to handle multiple "server_name"s to host multiple hosts.
|
||||||
// peek() should work for that.
|
// peek() should work for that.
|
||||||
peeked_conn = std::pin::Pin::new(&mut p).peek_mut().fuse() => {
|
peeked_conn = Pin::new(&mut p).peek_mut().fuse() => {
|
||||||
if server_crypto_map.is_none() || peeked_conn.is_none() {
|
if server_crypto_map.is_none() || peeked_conn.is_none() {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
@ -181,7 +183,7 @@ where
|
||||||
false
|
false
|
||||||
};
|
};
|
||||||
// Then acquire actual connection
|
// Then acquire actual connection
|
||||||
let peekable_incoming = std::pin::Pin::new(&mut p);
|
let peekable_incoming = Pin::new(&mut p);
|
||||||
if let Some(conn) = peekable_incoming.get_mut().next().await {
|
if let Some(conn) = peekable_incoming.get_mut().next().await {
|
||||||
if is_acceptable {
|
if is_acceptable {
|
||||||
self.clone().client_serve_h3(conn).await;
|
self.clone().client_serve_h3(conn).await;
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,11 @@
|
||||||
use super::{Upstream, UpstreamOption};
|
use super::{Upstream, UpstreamOption};
|
||||||
use crate::{error::*, log::*, utils::*};
|
use crate::{error::*, globals::Globals, log::*, utils::*};
|
||||||
|
use bytes::BufMut;
|
||||||
use hyper::{
|
use hyper::{
|
||||||
header::{self, HeaderMap, HeaderName, HeaderValue},
|
header::{self, HeaderMap, HeaderName, HeaderValue},
|
||||||
Uri,
|
Uri,
|
||||||
};
|
};
|
||||||
use std::net::SocketAddr;
|
use std::{net::SocketAddr, sync::Arc};
|
||||||
|
|
||||||
////////////////////////////////////////////////////
|
////////////////////////////////////////////////////
|
||||||
// Functions to manipulate headers
|
// Functions to manipulate headers
|
||||||
|
|
@ -29,36 +30,77 @@ pub(super) fn apply_upstream_options_to_header(
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn append_header_entry(headers: &mut HeaderMap, key: &str, value: &str) -> Result<()> {
|
// https://datatracker.ietf.org/doc/html/rfc9110
|
||||||
|
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())?) {
|
match headers.entry(HeaderName::from_bytes(key.as_bytes())?) {
|
||||||
header::Entry::Vacant(entry) => {
|
header::Entry::Vacant(entry) => {
|
||||||
entry.insert(value.parse::<HeaderValue>()?);
|
entry.insert(value.parse::<HeaderValue>()?);
|
||||||
}
|
}
|
||||||
header::Entry::Occupied(mut entry) => {
|
header::Entry::Occupied(mut entry) => {
|
||||||
entry.append(value.parse::<HeaderValue>()?);
|
// entry.append(value.parse::<HeaderValue>()?);
|
||||||
|
let mut new_value = Vec::<u8>::with_capacity(entry.get().as_bytes().len() + 2 + value.len());
|
||||||
|
new_value.put_slice(entry.get().as_bytes());
|
||||||
|
new_value.put_slice(&[b',', b' ']);
|
||||||
|
new_value.put_slice(value.as_bytes());
|
||||||
|
entry.insert(HeaderValue::from_bytes(&new_value)?);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(super) fn add_header_entry_if_not_exist(
|
||||||
|
headers: &mut HeaderMap,
|
||||||
|
key: impl Into<std::borrow::Cow<'static, str>>,
|
||||||
|
value: impl Into<std::borrow::Cow<'static, str>>,
|
||||||
|
) -> Result<()> {
|
||||||
|
match headers.entry(HeaderName::from_bytes(key.into().as_bytes())?) {
|
||||||
|
header::Entry::Vacant(entry) => {
|
||||||
|
entry.insert(value.into().parse::<HeaderValue>()?);
|
||||||
|
}
|
||||||
|
header::Entry::Occupied(_) => (),
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
pub(super) fn add_forwarding_header(
|
pub(super) fn add_forwarding_header(
|
||||||
headers: &mut HeaderMap,
|
headers: &mut HeaderMap,
|
||||||
client_addr: &SocketAddr,
|
client_addr: &SocketAddr,
|
||||||
tls: bool,
|
tls: bool,
|
||||||
|
globals: &Arc<Globals>, // TODO: Fix
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
// default process
|
// default process
|
||||||
// optional process defined by upstream_option is applied in fn apply_upstream_options
|
// optional process defined by upstream_option is applied in fn apply_upstream_options
|
||||||
append_header_entry(
|
append_header_entry_with_comma(
|
||||||
headers,
|
headers,
|
||||||
"x-forwarded-for",
|
"x-forwarded-for",
|
||||||
&client_addr.to_canonical().ip().to_string(),
|
&client_addr.to_canonical().ip().to_string(),
|
||||||
)?;
|
)?;
|
||||||
append_header_entry(
|
|
||||||
|
/////////// 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,
|
headers,
|
||||||
"x-forwarded-proto",
|
"x-forwarded-proto",
|
||||||
if tls { "https" } else { "http" },
|
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",
|
||||||
|
if tls {
|
||||||
|
globals.https_port.unwrap().to_string()
|
||||||
|
} else {
|
||||||
|
globals.http_port.unwrap().to_string()
|
||||||
|
},
|
||||||
|
)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -76,30 +76,3 @@ impl<B> ParseHost for Request<B> {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// pub(super) fn parse_host_port<B: core::fmt::Debug>(
|
|
||||||
// req: &Request<B>,
|
|
||||||
// ) -> Result<(String, Option<u16>)> {
|
|
||||||
// let headers_host = req.headers().get("host");
|
|
||||||
// let uri_host = req.uri().host();
|
|
||||||
// let uri_port = req.uri().port_u16();
|
|
||||||
|
|
||||||
// ensure!(
|
|
||||||
// !(headers_host.is_none() && uri_host.is_none()),
|
|
||||||
// "No host in request header"
|
|
||||||
// );
|
|
||||||
|
|
||||||
// // prioritize server_name in uri
|
|
||||||
// if let Some(v) = uri_host {
|
|
||||||
// Ok((v.to_string(), uri_port))
|
|
||||||
// } else {
|
|
||||||
// let uri_from_host = headers_host.unwrap().to_str()?.parse::<Uri>()?;
|
|
||||||
// Ok((
|
|
||||||
// uri_from_host
|
|
||||||
// .host()
|
|
||||||
// .ok_or_else(|| anyhow!("Failed to parse host"))?
|
|
||||||
// .to_string(),
|
|
||||||
// uri_from_host.port_u16(),
|
|
||||||
// ))
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue