add version setting of request forwarded

This commit is contained in:
Jun Kurihara 2022-08-02 22:22:32 +09:00
commit c5c7847b92
No known key found for this signature in database
GPG key ID: 48ADFD173ED22B03
6 changed files with 43 additions and 9 deletions

View file

@ -51,7 +51,7 @@ upstream = [
{ location = 'www.yahoo.com', tls = true }, { location = 'www.yahoo.com', tls = true },
{ location = 'www.yahoo.co.jp', tls = true }, { location = 'www.yahoo.co.jp', tls = true },
] ]
upstream_options = ["override_host"] upstream_options = ["override_host", "convert_to_http2"]
# 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]]
@ -66,7 +66,11 @@ upstream = [
{ location = 'www.bing.com', tls = true }, { location = 'www.bing.com', tls = true },
{ location = 'www.bing.co.jp', tls = true }, { location = 'www.bing.co.jp', tls = true },
] ]
upstream_options = ["override_host", "upgrade_insecure_requests"] upstream_options = [
"override_host",
"upgrade_insecure_requests",
"convert_to_http11",
]
###################################################################### ######################################################################
###################################################################### ######################################################################

View file

@ -4,6 +4,8 @@ use crate::error::*;
pub enum UpstreamOption { pub enum UpstreamOption {
OverrideHost, OverrideHost,
UpgradeInsecureRequests, UpgradeInsecureRequests,
ConvertToHttp11,
ConvertToHttp2,
// TODO: Adds more options for heder override // TODO: Adds more options for heder override
} }
impl TryFrom<&str> for UpstreamOption { impl TryFrom<&str> for UpstreamOption {
@ -12,6 +14,8 @@ impl TryFrom<&str> for UpstreamOption {
match val { match val {
"override_host" => Ok(Self::OverrideHost), "override_host" => Ok(Self::OverrideHost),
"upgrade_insecure_requests" => Ok(Self::UpgradeInsecureRequests), "upgrade_insecure_requests" => Ok(Self::UpgradeInsecureRequests),
"convert_to_http11" => Ok(Self::ConvertToHttp11),
"convert_to_http2" => Ok(Self::ConvertToHttp2),
_ => Err(RpxyError::Other(anyhow!("Unsupported header option"))), _ => Err(RpxyError::Other(anyhow!("Unsupported header option"))),
} }
} }

View file

@ -224,5 +224,13 @@ fn get_reverse_proxy(rp_settings: &[ReverseProxyOption]) -> std::result::Result<
rp_settings.iter().filter(|rpo| rpo.path.is_none()).count() < 2, rp_settings.iter().filter(|rpo| rpo.path.is_none()).count() < 2,
"Multiple default reverse proxy setting" "Multiple default reverse proxy setting"
); );
ensure!(
upstream
.iter()
.all(|(_, elem)| !(elem.opts.contains(&UpstreamOption::ConvertToHttp11)
&& elem.opts.contains(&UpstreamOption::ConvertToHttp2))),
"either one of force_http11 or force_http2 can be enabled"
);
Ok(ReverseProxy { upstream }) Ok(ReverseProxy { upstream })
} }

View file

@ -4,7 +4,6 @@ use crate::{backend::UpstreamGroup, error::*, globals::Globals, log::*, utils::S
use hyper::{ use hyper::{
client::connect::Connect, client::connect::Connect,
header::{self, HeaderValue}, header::{self, HeaderValue},
http::uri::Scheme,
Body, Client, Request, Response, StatusCode, Uri, Version, Body, Client, Request, Response, StatusCode, Uri, Version,
}; };
use std::{env, net::SocketAddr, sync::Arc}; use std::{env, net::SocketAddr, sync::Arc};
@ -95,7 +94,7 @@ where
error!("Failed to generate destination uri for reverse proxy: {}", e); error!("Failed to generate destination uri for reverse proxy: {}", e);
return self.return_with_error_log(StatusCode::SERVICE_UNAVAILABLE, &mut log_data); return self.return_with_error_log(StatusCode::SERVICE_UNAVAILABLE, &mut log_data);
}; };
// debug!("Request to be forwarded: {:?}", req_forwarded); debug!("Request to be forwarded: {:?}", req);
log_data.xff(&req.headers().get("x-forwarded-for")); log_data.xff(&req.headers().get("x-forwarded-for"));
log_data.upstream(req.uri()); log_data.upstream(req.uri());
////// //////
@ -290,10 +289,10 @@ where
.insert(header::CONNECTION, HeaderValue::from_str("upgrade")?); .insert(header::CONNECTION, HeaderValue::from_str("upgrade")?);
} }
// Change version to http/1.1 when destination scheme is http apply_upstream_options_to_request_line(req, upstream_group)?;
if req.version() != Version::HTTP_11 && upstream_chosen.uri.scheme() == Some(&Scheme::HTTP) {
*req.version_mut() = Version::HTTP_11; // if not specified (force_httpXX_upstream), version is preserved except for http/3
} else if req.version() == Version::HTTP_3 { if req.version() == Version::HTTP_3 {
debug!("HTTP/3 is currently unsupported for request to upstream. Use HTTP/2."); debug!("HTTP/3 is currently unsupported for request to upstream. Use HTTP/2.");
*req.version_mut() = Version::HTTP_2; *req.version_mut() = Version::HTTP_2;
} }

View file

@ -37,6 +37,7 @@ pub(super) fn apply_upstream_options_to_header(
.entry(header::UPGRADE_INSECURE_REQUESTS) .entry(header::UPGRADE_INSECURE_REQUESTS)
.or_insert(HeaderValue::from_bytes(&[b'1']).unwrap()); .or_insert(HeaderValue::from_bytes(&[b'1']).unwrap());
} }
_ => (),
} }
} }

View file

@ -1,6 +1,24 @@
use crate::error::*; use crate::{
backend::{UpstreamGroup, UpstreamOption},
error::*,
};
use hyper::{header, Request}; use hyper::{header, Request};
////////////////////////////////////////////////////
// Functions to manipulate request line
pub(super) fn apply_upstream_options_to_request_line<B>(req: &mut Request<B>, upstream: &UpstreamGroup) -> Result<()> {
for opt in upstream.opts.iter() {
match opt {
UpstreamOption::ConvertToHttp11 => *req.version_mut() = hyper::Version::HTTP_11,
UpstreamOption::ConvertToHttp2 => *req.version_mut() = hyper::Version::HTTP_2,
_ => (),
}
}
Ok(())
}
pub trait ParseHost { pub trait ParseHost {
fn parse_host(&self) -> Result<&[u8]>; fn parse_host(&self) -> Result<&[u8]>;
} }