diff --git a/src/backend/mod.rs b/src/backend/mod.rs index 0cc037e..08577a0 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -19,8 +19,8 @@ pub use upstream_opts::UpstreamOption; // Server name (hostname or ip address) and path name representation in backends // For searching hashmap or key list by exact or longest-prefix matching -pub type ServerNameExp = Vec; // lowercase ascii bytes -pub type PathNameExp = Vec; // lowercase ascii bytes +pub type ServerNameBytesExp = Vec; // lowercase ascii bytes +pub type PathNameBytesExp = Vec; // lowercase ascii bytes /// Struct serving information to route incoming connections, like server name to be handled and tls certs/keys settings. pub struct Backend { @@ -111,8 +111,8 @@ impl Backend { /// HashMap and some meta information for multiple Backend structs. pub struct Backends { - pub apps: HashMap, // hyper::uriで抜いたhostで引っ掛ける - pub default_server_name_bytes: Option, // for plaintext http + pub apps: HashMap, // hyper::uriで抜いたhostで引っ掛ける + pub default_server_name_bytes: Option, // for plaintext http } impl Backends { diff --git a/src/backend/upstream.rs b/src/backend/upstream.rs index da4a97a..03a3648 100644 --- a/src/backend/upstream.rs +++ b/src/backend/upstream.rs @@ -1,4 +1,4 @@ -use super::{PathNameExp, UpstreamOption}; +use super::{PathNameBytesExp, UpstreamOption}; use crate::log::*; use rand::Rng; use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet}; @@ -12,7 +12,7 @@ use std::{ #[derive(Debug, Clone)] pub struct ReverseProxy { - pub upstream: HashMap, // TODO: HashMapでいいのかは疑問。max_by_keyでlongest prefix matchしてるのも無駄っぽいが。。。 + pub upstream: HashMap, // TODO: HashMapでいいのかは疑問。max_by_keyでlongest prefix matchしてるのも無駄っぽいが。。。 } impl ReverseProxy { @@ -70,8 +70,8 @@ pub struct Upstream { #[derive(Debug, Clone)] pub struct UpstreamGroup { pub upstream: Vec, - pub path: PathNameExp, - pub replace_path: Option, + pub path: PathNameBytesExp, + pub replace_path: Option, pub lb: LoadBalance, pub cnt: UpstreamCount, // counter for load balancing pub opts: HashSet, diff --git a/src/config/parse.rs b/src/config/parse.rs index 7be29ee..2475336 100644 --- a/src/config/parse.rs +++ b/src/config/parse.rs @@ -1,6 +1,6 @@ use super::toml::{ConfigToml, ReverseProxyOption}; use crate::{ - backend::{Backend, PathNameExp, ReverseProxy, UpstreamGroup, UpstreamOption}, + backend::{Backend, PathNameBytesExp, ReverseProxy, UpstreamGroup, UpstreamOption}, constants::*, error::*, globals::*, @@ -191,7 +191,7 @@ pub fn parse_opts(globals: &mut Globals) -> std::result::Result<(), anyhow::Erro } fn get_reverse_proxy(rp_settings: &[ReverseProxyOption]) -> std::result::Result { - let mut upstream: HashMap = HashMap::default(); + let mut upstream: HashMap = HashMap::default(); rp_settings.iter().for_each(|rpo| { let path = match &rpo.path { Some(p) => p.as_bytes().to_ascii_lowercase(), diff --git a/src/handler/handler_main.rs b/src/handler/handler_main.rs index f371cf8..d2c1e30 100644 --- a/src/handler/handler_main.rs +++ b/src/handler/handler_main.rs @@ -1,7 +1,7 @@ // Highly motivated by https://github.com/felipenoris/hyper-reverse-proxy use super::{utils_headers::*, utils_request::*, utils_synth_response::*}; use crate::{ - backend::{ServerNameExp, UpstreamGroup}, + backend::{ServerNameBytesExp, UpstreamGroup}, error::*, globals::Globals, log::*, @@ -39,7 +39,7 @@ where client_addr: SocketAddr, // アクセス制御用 listen_addr: SocketAddr, tls_enabled: bool, - tls_server_name: Option, + tls_server_name: Option, ) -> Result> { //////// let mut log_data = MessageLog::from(&req); diff --git a/src/main.rs b/src/main.rs index 32703a2..92e2ba1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,7 +16,7 @@ mod proxy; mod utils; use crate::{ - backend::{Backend, Backends, ServerNameExp}, + backend::{Backend, Backends, ServerNameBytesExp}, config::parse_opts, constants::*, error::*, @@ -72,7 +72,7 @@ fn main() { runtime_handle: runtime.handle().clone(), backends: Backends { default_server_name_bytes: None, - apps: HashMap::::default(), + apps: HashMap::::default(), }, sni_consistency: true, diff --git a/src/proxy/proxy_h3.rs b/src/proxy/proxy_h3.rs index 0599a61..9efce98 100644 --- a/src/proxy/proxy_h3.rs +++ b/src/proxy/proxy_h3.rs @@ -1,5 +1,5 @@ use super::Proxy; -use crate::{backend::ServerNameExp, error::*, log::*}; +use crate::{backend::ServerNameBytesExp, error::*, log::*}; use bytes::{Buf, Bytes}; use h3::{quic::BidiStream, server::RequestStream}; use hyper::{client::connect::Connect, Body, Request, Response}; @@ -10,7 +10,11 @@ impl Proxy where T: Connect + Clone + Sync + Send + 'static, { - pub(super) async fn connection_serve_h3(self, conn: quinn::Connecting, tls_server_name: ServerNameExp) -> Result<()> { + pub(super) async fn connection_serve_h3( + self, + conn: quinn::Connecting, + tls_server_name: ServerNameBytesExp, + ) -> Result<()> { let client_addr = conn.remote_address(); match conn.await { @@ -68,7 +72,7 @@ where req: Request<()>, stream: RequestStream, client_addr: SocketAddr, - tls_server_name: ServerNameExp, + tls_server_name: ServerNameBytesExp, ) -> Result<()> where S: BidiStream + Send + 'static, diff --git a/src/proxy/proxy_main.rs b/src/proxy/proxy_main.rs index f3cf3b7..4b823ab 100644 --- a/src/proxy/proxy_main.rs +++ b/src/proxy/proxy_main.rs @@ -1,5 +1,5 @@ // use super::proxy_handler::handle_request; -use crate::{backend::ServerNameExp, error::*, globals::Globals, handler::HttpMessageHandler, log::*}; +use crate::{backend::ServerNameBytesExp, error::*, globals::Globals, handler::HttpMessageHandler, log::*}; use hyper::{client::connect::Connect, server::conn::Http, service::service_fn, Body, Request}; use std::{net::SocketAddr, sync::Arc}; use tokio::{ @@ -50,7 +50,7 @@ where stream: I, server: Http, peer_addr: SocketAddr, - tls_server_name: Option, + tls_server_name: Option, ) where I: AsyncRead + AsyncWrite + Send + Unpin + 'static, { diff --git a/src/utils/bytes_name.rs b/src/utils/bytes_name.rs index 4dedf56..e8af08c 100644 --- a/src/utils/bytes_name.rs +++ b/src/utils/bytes_name.rs @@ -1,20 +1,22 @@ +use bytes::{Buf, Bytes}; + pub trait BytesName { - type Output; - fn to_byte_name(self) -> Self::Output; - fn to_ascii_lowercase_byte_name(self) -> Self::Output; + type Output: Buf; + fn to_bytes(self) -> Self::Output; + fn to_ascii_lowercase_bytes(self) -> Self::Output; } impl> BytesName for T { - type Output = Vec; + type Output = Bytes; - fn to_byte_name(self) -> Self::Output { - self.into().bytes().collect::>() - // Bytes::from(b) + fn to_bytes(self) -> Self::Output { + let b = self.into().bytes().collect::>(); + Bytes::from(b) } - fn to_ascii_lowercase_byte_name(self) -> Self::Output { - self.into().bytes().collect::>().to_ascii_lowercase() - // Bytes::from(b) + fn to_ascii_lowercase_bytes(self) -> Self::Output { + let b = self.into().bytes().collect::>().to_ascii_lowercase(); + Bytes::from(b) } } @@ -24,10 +26,10 @@ mod tests { #[test] fn bytes_name_str_works() { let s = "OK_string"; - let bn = s.to_byte_name(); - let bn_lc = s.to_ascii_lowercase_byte_name(); + let bn = s.to_bytes(); + let bn_lc = s.to_ascii_lowercase_bytes(); - assert_eq!(Vec::from(s.as_bytes()), bn); - assert_eq!(Vec::from("ok_string"), bn_lc); + assert_eq!(Bytes::from(s.as_bytes()), bn); + assert_eq!(Bytes::from("ok_string"), bn_lc); } }