split proxy into stream handler and http message handler
This commit is contained in:
parent
0c6ffa5e77
commit
954a1993a9
14 changed files with 92 additions and 71 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
use super::UpstreamOption;
|
use crate::backend_opt::UpstreamOption;
|
||||||
use crate::log::*;
|
use crate::log::*;
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet};
|
use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet};
|
||||||
|
|
@ -1,10 +1,11 @@
|
||||||
use super::toml::{ConfigToml, ReverseProxyOption};
|
use super::toml::{ConfigToml, ReverseProxyOption};
|
||||||
use crate::{
|
use crate::{
|
||||||
|
backend::{Backend, ReverseProxy, Upstream},
|
||||||
|
backend_opt::UpstreamOption,
|
||||||
constants::*,
|
constants::*,
|
||||||
error::*,
|
error::*,
|
||||||
globals::*,
|
globals::*,
|
||||||
log::*,
|
log::*,
|
||||||
proxy::{Backend, Backends, ReverseProxy, Upstream, UpstreamOption},
|
|
||||||
};
|
};
|
||||||
use clap::Arg;
|
use clap::Arg;
|
||||||
use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet};
|
use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet};
|
||||||
|
|
@ -13,7 +14,7 @@ use std::net::SocketAddr;
|
||||||
// #[cfg(feature = "tls")]
|
// #[cfg(feature = "tls")]
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
pub fn parse_opts(globals: &mut Globals, backends: &mut Backends) -> Result<()> {
|
pub fn parse_opts(globals: &mut Globals) -> Result<()> {
|
||||||
let _ = include_str!("../../Cargo.toml");
|
let _ = include_str!("../../Cargo.toml");
|
||||||
let options = clap::command!().arg(
|
let options = clap::command!().arg(
|
||||||
Arg::new("config_file")
|
Arg::new("config_file")
|
||||||
|
|
@ -121,7 +122,7 @@ pub fn parse_opts(globals: &mut Globals, backends: &mut Backends) -> Result<()>
|
||||||
ensure!(app.reverse_proxy.is_some(), "Missing reverse_proxy");
|
ensure!(app.reverse_proxy.is_some(), "Missing reverse_proxy");
|
||||||
let reverse_proxy = get_reverse_proxy(app.reverse_proxy.as_ref().unwrap())?;
|
let reverse_proxy = get_reverse_proxy(app.reverse_proxy.as_ref().unwrap())?;
|
||||||
|
|
||||||
backends.apps.insert(
|
globals.backends.apps.insert(
|
||||||
server_name.as_bytes().to_vec(),
|
server_name.as_bytes().to_vec(),
|
||||||
Backend {
|
Backend {
|
||||||
app_name: app_name.to_owned(),
|
app_name: app_name.to_owned(),
|
||||||
|
|
@ -138,7 +139,8 @@ pub fn parse_opts(globals: &mut Globals, backends: &mut Backends) -> Result<()>
|
||||||
|
|
||||||
// default backend application for plaintext http requests
|
// default backend application for plaintext http requests
|
||||||
if let Some(d) = config.default_app {
|
if let Some(d) = config.default_app {
|
||||||
let d_sn: Vec<&str> = backends
|
let d_sn: Vec<&str> = globals
|
||||||
|
.backends
|
||||||
.apps
|
.apps
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|(_k, v)| v.app_name == d)
|
.filter(|(_k, v)| v.app_name == d)
|
||||||
|
|
@ -149,7 +151,7 @@ pub fn parse_opts(globals: &mut Globals, backends: &mut Backends) -> Result<()>
|
||||||
"Serving plaintext http for requests to unconfigured server_name by app {} (server_name: {}).",
|
"Serving plaintext http for requests to unconfigured server_name by app {} (server_name: {}).",
|
||||||
d, d_sn[0]
|
d, d_sn[0]
|
||||||
);
|
);
|
||||||
backends.default_server_name = Some(d_sn[0].as_bytes().to_vec());
|
globals.backends.default_server_name = Some(d_sn[0].as_bytes().to_vec());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
use crate::backend::Backends;
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use std::sync::{
|
use std::sync::{
|
||||||
atomic::{AtomicUsize, Ordering},
|
atomic::{AtomicUsize, Ordering},
|
||||||
|
|
@ -5,7 +6,6 @@ use std::sync::{
|
||||||
};
|
};
|
||||||
use tokio::time::Duration;
|
use tokio::time::Duration;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct Globals {
|
pub struct Globals {
|
||||||
pub listen_sockets: Vec<SocketAddr>,
|
pub listen_sockets: Vec<SocketAddr>,
|
||||||
pub http_port: Option<u16>,
|
pub http_port: Option<u16>,
|
||||||
|
|
@ -19,6 +19,8 @@ pub struct Globals {
|
||||||
pub http3: bool,
|
pub http3: bool,
|
||||||
|
|
||||||
pub runtime_handle: tokio::runtime::Handle,
|
pub runtime_handle: tokio::runtime::Handle,
|
||||||
|
|
||||||
|
pub backends: Backends,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Default)]
|
#[derive(Debug, Clone, Default)]
|
||||||
|
|
|
||||||
27
src/main.rs
27
src/main.rs
|
|
@ -2,24 +2,29 @@
|
||||||
#[global_allocator]
|
#[global_allocator]
|
||||||
static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
|
static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
|
||||||
|
|
||||||
|
mod backend;
|
||||||
|
mod backend_opt;
|
||||||
mod config;
|
mod config;
|
||||||
mod constants;
|
mod constants;
|
||||||
mod error;
|
mod error;
|
||||||
mod globals;
|
mod globals;
|
||||||
mod log;
|
mod log;
|
||||||
|
mod msg_handler;
|
||||||
mod proxy;
|
mod proxy;
|
||||||
mod utils;
|
mod utils;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
backend::{Backend, Backends, ServerNameLC},
|
||||||
config::parse_opts,
|
config::parse_opts,
|
||||||
constants::*,
|
constants::*,
|
||||||
error::*,
|
error::*,
|
||||||
globals::*,
|
globals::*,
|
||||||
log::*,
|
log::*,
|
||||||
proxy::{Backend, Backends, Proxy, ServerNameLC},
|
proxy::Proxy,
|
||||||
};
|
};
|
||||||
use futures::future::select_all;
|
use futures::future::select_all;
|
||||||
use hyper::Client;
|
use hyper::Client;
|
||||||
|
use msg_handler::HttpMessageHandler;
|
||||||
// use hyper_trust_dns::TrustDnsResolver;
|
// use hyper_trust_dns::TrustDnsResolver;
|
||||||
use rustc_hash::FxHashMap as HashMap;
|
use rustc_hash::FxHashMap as HashMap;
|
||||||
use std::{io::Write, sync::Arc};
|
use std::{io::Write, sync::Arc};
|
||||||
|
|
@ -61,24 +66,22 @@ fn main() {
|
||||||
max_concurrent_streams: MAX_CONCURRENT_STREAMS,
|
max_concurrent_streams: MAX_CONCURRENT_STREAMS,
|
||||||
keepalive: true,
|
keepalive: true,
|
||||||
runtime_handle: runtime.handle().clone(),
|
runtime_handle: runtime.handle().clone(),
|
||||||
};
|
|
||||||
|
|
||||||
let mut backends = Backends {
|
backends: Backends {
|
||||||
default_server_name: None,
|
default_server_name: None,
|
||||||
apps: HashMap::<ServerNameLC, Backend>::default(),
|
apps: HashMap::<ServerNameLC, Backend>::default(),
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
parse_opts(&mut globals, &mut backends).expect("Invalid configuration");
|
parse_opts(&mut globals).expect("Invalid configuration");
|
||||||
|
|
||||||
entrypoint(Arc::new(globals), Arc::new(backends))
|
entrypoint(Arc::new(globals)).await.unwrap()
|
||||||
.await
|
|
||||||
.unwrap()
|
|
||||||
});
|
});
|
||||||
warn!("Exit the program");
|
warn!("Exit the program");
|
||||||
}
|
}
|
||||||
|
|
||||||
// entrypoint creates and spawns tasks of proxy services
|
// entrypoint creates and spawns tasks of proxy services
|
||||||
async fn entrypoint(globals: Arc<Globals>, backends: Arc<Backends>) -> Result<()> {
|
async fn entrypoint(globals: Arc<Globals>) -> Result<()> {
|
||||||
// let connector = TrustDnsResolver::default().into_rustls_webpki_https_connector();
|
// let connector = TrustDnsResolver::default().into_rustls_webpki_https_connector();
|
||||||
let connector = hyper_rustls::HttpsConnectorBuilder::new()
|
let connector = hyper_rustls::HttpsConnectorBuilder::new()
|
||||||
.with_webpki_roots()
|
.with_webpki_roots()
|
||||||
|
|
@ -86,7 +89,10 @@ async fn entrypoint(globals: Arc<Globals>, backends: Arc<Backends>) -> Result<()
|
||||||
.enable_http1()
|
.enable_http1()
|
||||||
.enable_http2()
|
.enable_http2()
|
||||||
.build();
|
.build();
|
||||||
let forwarder = Arc::new(Client::builder().build::<_, hyper::Body>(connector));
|
let msg_handler = HttpMessageHandler {
|
||||||
|
forwarder: Arc::new(Client::builder().build::<_, hyper::Body>(connector)),
|
||||||
|
globals: globals.clone(),
|
||||||
|
};
|
||||||
|
|
||||||
let addresses = globals.listen_sockets.clone();
|
let addresses = globals.listen_sockets.clone();
|
||||||
let futures = select_all(addresses.into_iter().map(|addr| {
|
let futures = select_all(addresses.into_iter().map(|addr| {
|
||||||
|
|
@ -99,8 +105,7 @@ async fn entrypoint(globals: Arc<Globals>, backends: Arc<Backends>) -> Result<()
|
||||||
globals: globals.clone(),
|
globals: globals.clone(),
|
||||||
listening_on: addr,
|
listening_on: addr,
|
||||||
tls_enabled,
|
tls_enabled,
|
||||||
backends: backends.clone(),
|
msg_handler: msg_handler.clone(),
|
||||||
forwarder: forwarder.clone(),
|
|
||||||
};
|
};
|
||||||
globals.runtime_handle.spawn(proxy.start())
|
globals.runtime_handle.spawn(proxy.start())
|
||||||
}));
|
}));
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,25 @@
|
||||||
// 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::*, Proxy, Upstream};
|
use super::{utils_headers::*, utils_request::*, utils_synth_response::*};
|
||||||
use crate::{constants::*, error::*, log::*};
|
use crate::{backend::Upstream, constants::*, error::*, globals::Globals, log::*};
|
||||||
use hyper::{
|
use hyper::{
|
||||||
client::connect::Connect,
|
client::connect::Connect,
|
||||||
header::{self, HeaderValue},
|
header::{self, HeaderValue},
|
||||||
http::uri::Scheme,
|
http::uri::Scheme,
|
||||||
Body, Request, Response, StatusCode, Uri, Version,
|
Body, Client, Request, Response, StatusCode, Uri, Version,
|
||||||
};
|
};
|
||||||
use std::net::SocketAddr;
|
use std::{net::SocketAddr, sync::Arc};
|
||||||
use tokio::io::copy_bidirectional;
|
use tokio::io::copy_bidirectional;
|
||||||
|
|
||||||
impl<T> Proxy<T>
|
#[derive(Clone)]
|
||||||
|
pub struct HttpMessageHandler<T>
|
||||||
|
where
|
||||||
|
T: Connect + Clone + Sync + Send + 'static,
|
||||||
|
{
|
||||||
|
pub forwarder: Arc<Client<T>>,
|
||||||
|
pub globals: Arc<Globals>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> HttpMessageHandler<T>
|
||||||
where
|
where
|
||||||
T: Connect + Clone + Sync + Send + 'static,
|
T: Connect + Clone + Sync + Send + 'static,
|
||||||
{
|
{
|
||||||
|
|
@ -18,6 +27,8 @@ where
|
||||||
self,
|
self,
|
||||||
mut req: Request<Body>,
|
mut req: Request<Body>,
|
||||||
client_addr: SocketAddr, // アクセス制御用
|
client_addr: SocketAddr, // アクセス制御用
|
||||||
|
listen_addr: SocketAddr,
|
||||||
|
tls_enabled: bool,
|
||||||
) -> Result<Response<Body>> {
|
) -> Result<Response<Body>> {
|
||||||
req.log(&client_addr, Some("(Incoming)"));
|
req.log(&client_addr, Some("(Incoming)"));
|
||||||
|
|
||||||
|
|
@ -26,18 +37,18 @@ where
|
||||||
// let (server_name, _port) = parse_host_port(&req)?;
|
// let (server_name, _port) = parse_host_port(&req)?;
|
||||||
let server_name_bytes = req.parse_host()?.to_ascii_lowercase();
|
let server_name_bytes = req.parse_host()?.to_ascii_lowercase();
|
||||||
|
|
||||||
let backend = if let Some(be) = self.backends.apps.get(&server_name_bytes) {
|
let backend = if let Some(be) = self.globals.backends.apps.get(&server_name_bytes) {
|
||||||
be
|
be
|
||||||
} else if let Some(default_server_name) = &self.backends.default_server_name {
|
} else if let Some(default_server_name) = &self.globals.backends.default_server_name {
|
||||||
debug!("Serving by default app");
|
debug!("Serving by default app");
|
||||||
self.backends.apps.get(default_server_name).unwrap()
|
self.globals.backends.apps.get(default_server_name).unwrap()
|
||||||
} else {
|
} else {
|
||||||
// info!("{} => {}", request_log, StatusCode::SERVICE_UNAVAILABLE);
|
// info!("{} => {}", request_log, StatusCode::SERVICE_UNAVAILABLE);
|
||||||
return http_error(StatusCode::SERVICE_UNAVAILABLE);
|
return http_error(StatusCode::SERVICE_UNAVAILABLE);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Redirect to https if !tls_enabled and redirect_to_https is true
|
// Redirect to https if !tls_enabled and redirect_to_https is true
|
||||||
if !self.tls_enabled && backend.https_redirection.unwrap_or(false) {
|
if !tls_enabled && backend.https_redirection.unwrap_or(false) {
|
||||||
debug!("Redirect to secure connection: {}", &backend.server_name);
|
debug!("Redirect to secure connection: {}", &backend.server_name);
|
||||||
// info!("{} => {}", request_log, StatusCode::PERMANENT_REDIRECT);
|
// info!("{} => {}", request_log, StatusCode::PERMANENT_REDIRECT);
|
||||||
return secure_redirection(&backend.server_name, self.globals.https_port, &req);
|
return secure_redirection(&backend.server_name, self.globals.https_port, &req);
|
||||||
|
|
@ -68,10 +79,12 @@ where
|
||||||
// Build request from destination information
|
// Build request from destination information
|
||||||
let req_forwarded = if let Ok(req) = self.generate_request_forwarded(
|
let req_forwarded = if let Ok(req) = self.generate_request_forwarded(
|
||||||
&client_addr,
|
&client_addr,
|
||||||
|
&listen_addr,
|
||||||
req,
|
req,
|
||||||
upstream_scheme_host,
|
upstream_scheme_host,
|
||||||
&upgrade_in_request,
|
&upgrade_in_request,
|
||||||
upstream,
|
upstream,
|
||||||
|
tls_enabled,
|
||||||
) {
|
) {
|
||||||
req
|
req
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -110,7 +123,7 @@ where
|
||||||
.await?;
|
.await?;
|
||||||
// TODO: H3で死ぬことがある
|
// TODO: H3で死ぬことがある
|
||||||
// thread 'rpxy' panicked at 'Failed to upgrade request: hyper::Error(User(ManualUpgrade))', src/proxy/proxy_handler.rs:124:63
|
// thread 'rpxy' panicked at 'Failed to upgrade request: hyper::Error(User(ManualUpgrade))', src/proxy/proxy_handler.rs:124:63
|
||||||
tokio::spawn(async move {
|
self.globals.runtime_handle.spawn(async move {
|
||||||
let mut request_upgraded = request_upgraded.await.map_err(|e| {
|
let mut request_upgraded = request_upgraded.await.map_err(|e| {
|
||||||
error!("Failed to upgrade request: {}", e);
|
error!("Failed to upgrade request: {}", e);
|
||||||
anyhow!("Failed to upgrade request: {}", e)
|
anyhow!("Failed to upgrade request: {}", e)
|
||||||
|
|
@ -181,13 +194,16 @@ where
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
fn generate_request_forwarded<B: core::fmt::Debug>(
|
fn generate_request_forwarded<B: core::fmt::Debug>(
|
||||||
&self,
|
&self,
|
||||||
client_addr: &SocketAddr,
|
client_addr: &SocketAddr,
|
||||||
|
listen_addr: &SocketAddr,
|
||||||
mut req: Request<B>,
|
mut req: Request<B>,
|
||||||
upstream_scheme_host: &Uri,
|
upstream_scheme_host: &Uri,
|
||||||
upgrade: &Option<String>,
|
upgrade: &Option<String>,
|
||||||
upstream: &Upstream,
|
upstream: &Upstream,
|
||||||
|
tls_enabled: bool,
|
||||||
) -> Result<Request<B>> {
|
) -> Result<Request<B>> {
|
||||||
debug!("Generate request to be forwarded");
|
debug!("Generate request to be forwarded");
|
||||||
|
|
||||||
|
|
@ -208,7 +224,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, &self.globals)?;
|
add_forwarding_header(headers, client_addr, listen_addr, tls_enabled)?;
|
||||||
|
|
||||||
// Add te: trailer if te_trailer
|
// Add te: trailer if te_trailer
|
||||||
if te_trailers {
|
if te_trailers {
|
||||||
6
src/msg_handler/mod.rs
Normal file
6
src/msg_handler/mod.rs
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
mod handler;
|
||||||
|
mod utils_headers;
|
||||||
|
mod utils_request;
|
||||||
|
mod utils_synth_response;
|
||||||
|
|
||||||
|
pub use handler::HttpMessageHandler;
|
||||||
|
|
@ -1,11 +1,10 @@
|
||||||
use super::{Upstream, UpstreamOption};
|
use crate::{backend::Upstream, backend_opt::UpstreamOption, error::*, log::*, utils::*};
|
||||||
use crate::{error::*, globals::Globals, log::*, utils::*};
|
|
||||||
use bytes::BufMut;
|
use bytes::BufMut;
|
||||||
use hyper::{
|
use hyper::{
|
||||||
header::{self, HeaderMap, HeaderName, HeaderValue},
|
header::{self, HeaderMap, HeaderName, HeaderValue},
|
||||||
Uri,
|
Uri,
|
||||||
};
|
};
|
||||||
use std::{net::SocketAddr, sync::Arc};
|
use std::net::SocketAddr;
|
||||||
|
|
||||||
////////////////////////////////////////////////////
|
////////////////////////////////////////////////////
|
||||||
// Functions to manipulate headers
|
// Functions to manipulate headers
|
||||||
|
|
@ -71,8 +70,8 @@ pub(super) fn add_header_entry_if_not_exist(
|
||||||
pub(super) fn add_forwarding_header(
|
pub(super) fn add_forwarding_header(
|
||||||
headers: &mut HeaderMap,
|
headers: &mut HeaderMap,
|
||||||
client_addr: &SocketAddr,
|
client_addr: &SocketAddr,
|
||||||
|
listen_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
|
||||||
|
|
@ -92,15 +91,7 @@ pub(super) fn add_forwarding_header(
|
||||||
)?;
|
)?;
|
||||||
// If we receive X-Forwarded-Port, pass it through; otherwise, pass along the
|
// If we receive X-Forwarded-Port, pass it through; otherwise, pass along the
|
||||||
// server port the client connected to
|
// server port the client connected to
|
||||||
add_header_entry_if_not_exist(
|
add_header_entry_if_not_exist(headers, "x-forwarded-port", listen_addr.port().to_string())?;
|
||||||
headers,
|
|
||||||
"x-forwarded-port",
|
|
||||||
if tls {
|
|
||||||
globals.https_port.unwrap().to_string()
|
|
||||||
} else {
|
|
||||||
globals.http_port.unwrap().to_string()
|
|
||||||
},
|
|
||||||
)?;
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
@ -1,14 +1,6 @@
|
||||||
mod backend;
|
|
||||||
mod backend_opt;
|
|
||||||
#[cfg(feature = "h3")]
|
#[cfg(feature = "h3")]
|
||||||
mod proxy_h3;
|
mod proxy_h3;
|
||||||
mod proxy_handler;
|
|
||||||
mod proxy_main;
|
mod proxy_main;
|
||||||
mod proxy_tls;
|
mod proxy_tls;
|
||||||
mod utils_headers;
|
|
||||||
mod utils_request;
|
|
||||||
mod utils_synth_response;
|
|
||||||
|
|
||||||
pub use backend::*;
|
|
||||||
pub use backend_opt::UpstreamOption;
|
|
||||||
pub use proxy_main::Proxy;
|
pub use proxy_main::Proxy;
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ impl<T> Proxy<T>
|
||||||
where
|
where
|
||||||
T: Connect + Clone + Sync + Send + 'static,
|
T: Connect + Clone + Sync + Send + 'static,
|
||||||
{
|
{
|
||||||
pub async fn client_serve_h3(self, conn: quinn::Connecting) {
|
pub async fn client_serve_h3(&self, conn: quinn::Connecting) {
|
||||||
let clients_count = self.globals.clients_count.clone();
|
let clients_count = self.globals.clients_count.clone();
|
||||||
if clients_count.increment() > self.globals.max_clients {
|
if clients_count.increment() > self.globals.max_clients {
|
||||||
clients_count.decrement();
|
clients_count.decrement();
|
||||||
|
|
@ -79,7 +79,7 @@ where
|
||||||
|
|
||||||
let self_inner = self.clone();
|
let self_inner = self.clone();
|
||||||
self.globals.runtime_handle.spawn(async move {
|
self.globals.runtime_handle.spawn(async move {
|
||||||
if let Err(e) = self_inner.handle_request_h3(req, stream, client_addr).await {
|
if let Err(e) = self_inner.handle_stream_h3(req, stream, client_addr).await {
|
||||||
error!("HTTP/3 request failed: {}", e);
|
error!("HTTP/3 request failed: {}", e);
|
||||||
}
|
}
|
||||||
// // TODO: Work around for timeout
|
// // TODO: Work around for timeout
|
||||||
|
|
@ -98,7 +98,7 @@ where
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_request_h3<S>(
|
async fn handle_stream_h3<S>(
|
||||||
self,
|
self,
|
||||||
req: Request<()>,
|
req: Request<()>,
|
||||||
mut stream: RequestStream<S, Bytes>,
|
mut stream: RequestStream<S, Bytes>,
|
||||||
|
|
@ -129,7 +129,11 @@ where
|
||||||
};
|
};
|
||||||
|
|
||||||
let new_req: Request<Body> = Request::from_parts(req_parts, body);
|
let new_req: Request<Body> = Request::from_parts(req_parts, body);
|
||||||
let res = self.handle_request(new_req, client_addr).await?;
|
let res = self
|
||||||
|
.msg_handler
|
||||||
|
.clone()
|
||||||
|
.handle_request(new_req, client_addr, self.listening_on, self.tls_enabled)
|
||||||
|
.await?;
|
||||||
|
|
||||||
let (new_res_parts, new_body) = res.into_parts();
|
let (new_res_parts, new_body) = res.into_parts();
|
||||||
let new_res = Response::from_parts(new_res_parts, ());
|
let new_res = Response::from_parts(new_res_parts, ());
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,6 @@
|
||||||
// use super::proxy_handler::handle_request;
|
// use super::proxy_handler::handle_request;
|
||||||
use super::Backends;
|
use crate::{error::*, globals::Globals, log::*, msg_handler::HttpMessageHandler};
|
||||||
use crate::{error::*, globals::Globals, log::*};
|
use hyper::{client::connect::Connect, server::conn::Http, service::service_fn, Body, Request};
|
||||||
use hyper::{
|
|
||||||
client::connect::Connect, server::conn::Http, service::service_fn, Body, Client, Request,
|
|
||||||
};
|
|
||||||
use std::{net::SocketAddr, sync::Arc};
|
use std::{net::SocketAddr, sync::Arc};
|
||||||
use tokio::{
|
use tokio::{
|
||||||
io::{AsyncRead, AsyncWrite},
|
io::{AsyncRead, AsyncWrite},
|
||||||
|
|
@ -40,8 +37,7 @@ where
|
||||||
{
|
{
|
||||||
pub listening_on: SocketAddr,
|
pub listening_on: SocketAddr,
|
||||||
pub tls_enabled: bool, // TCP待受がTLSかどうか
|
pub tls_enabled: bool, // TCP待受がTLSかどうか
|
||||||
pub backends: Arc<Backends>,
|
pub msg_handler: HttpMessageHandler<T>,
|
||||||
pub forwarder: Arc<Client<T>>,
|
|
||||||
pub globals: Arc<Globals>,
|
pub globals: Arc<Globals>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -59,13 +55,21 @@ where
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// let handler_inner = self.msg_handler.clone();
|
||||||
self.globals.runtime_handle.clone().spawn(async move {
|
self.globals.runtime_handle.clone().spawn(async move {
|
||||||
tokio::time::timeout(
|
tokio::time::timeout(
|
||||||
self.globals.timeout + Duration::from_secs(1),
|
self.globals.timeout + Duration::from_secs(1),
|
||||||
server
|
server
|
||||||
.serve_connection(
|
.serve_connection(
|
||||||
stream,
|
stream,
|
||||||
service_fn(move |req: Request<Body>| self.clone().handle_request(req, peer_addr)),
|
service_fn(move |req: Request<Body>| {
|
||||||
|
self.msg_handler.clone().handle_request(
|
||||||
|
req,
|
||||||
|
peer_addr,
|
||||||
|
self.listening_on,
|
||||||
|
self.tls_enabled,
|
||||||
|
)
|
||||||
|
}),
|
||||||
)
|
)
|
||||||
.with_upgrades(),
|
.with_upgrades(),
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,5 @@
|
||||||
use super::{
|
use super::proxy_main::{LocalExecutor, Proxy};
|
||||||
proxy_main::{LocalExecutor, Proxy},
|
use crate::{backend::ServerNameLC, constants::*, error::*, log::*};
|
||||||
ServerNameLC,
|
|
||||||
};
|
|
||||||
use crate::{constants::*, error::*, log::*};
|
|
||||||
#[cfg(feature = "h3")]
|
#[cfg(feature = "h3")]
|
||||||
use futures::StreamExt;
|
use futures::StreamExt;
|
||||||
use futures::{future::FutureExt, select};
|
use futures::{future::FutureExt, select};
|
||||||
|
|
@ -24,7 +21,7 @@ where
|
||||||
info!("Start cert watch service");
|
info!("Start cert watch service");
|
||||||
loop {
|
loop {
|
||||||
let mut hm_server_config = HashMap::<ServerNameLC, Arc<ServerConfig>>::default();
|
let mut hm_server_config = HashMap::<ServerNameLC, Arc<ServerConfig>>::default();
|
||||||
for (server_name_bytes, backend) in self.backends.apps.iter() {
|
for (server_name_bytes, backend) in self.globals.backends.apps.iter() {
|
||||||
if backend.tls_cert_key_path.is_some() && backend.tls_cert_path.is_some() {
|
if backend.tls_cert_key_path.is_some() && backend.tls_cert_path.is_some() {
|
||||||
match backend.update_server_config().await {
|
match backend.update_server_config().await {
|
||||||
Err(_e) => {
|
Err(_e) => {
|
||||||
|
|
@ -137,6 +134,7 @@ where
|
||||||
// TODO: Work around to initially serve incoming connection
|
// TODO: Work around to initially serve incoming connection
|
||||||
// かなり適当。エラーが出たり出なかったり。原因がわからない…
|
// かなり適当。エラーが出たり出なかったり。原因がわからない…
|
||||||
let next = self
|
let next = self
|
||||||
|
.globals
|
||||||
.backends
|
.backends
|
||||||
.apps
|
.apps
|
||||||
.iter()
|
.iter()
|
||||||
|
|
@ -152,6 +150,7 @@ where
|
||||||
std::str::from_utf8(initial_app_name)
|
std::str::from_utf8(initial_app_name)
|
||||||
);
|
);
|
||||||
let backend_serve = self
|
let backend_serve = self
|
||||||
|
.globals
|
||||||
.backends
|
.backends
|
||||||
.apps
|
.apps
|
||||||
.get(initial_app_name)
|
.get(initial_app_name)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue