This commit is contained in:
Jun Kurihara 2022-07-28 19:46:03 +09:00
commit 4d590f328f
No known key found for this signature in database
GPG key ID: 48ADFD173ED22B03
8 changed files with 39 additions and 33 deletions

View file

@ -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<u8>; // lowercase ascii bytes
pub type PathNameExp = Vec<u8>; // lowercase ascii bytes
pub type ServerNameBytesExp = Vec<u8>; // lowercase ascii bytes
pub type PathNameBytesExp = Vec<u8>; // 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<ServerNameExp, Backend>, // hyper::uriで抜いたhostで引っ掛ける
pub default_server_name_bytes: Option<ServerNameExp>, // for plaintext http
pub apps: HashMap<ServerNameBytesExp, Backend>, // hyper::uriで抜いたhostで引っ掛ける
pub default_server_name_bytes: Option<ServerNameBytesExp>, // for plaintext http
}
impl Backends {

View file

@ -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<PathNameExp, UpstreamGroup>, // TODO: HashMapでいいのかは疑問。max_by_keyでlongest prefix matchしてるのも無駄っぽいが。。。
pub upstream: HashMap<PathNameBytesExp, UpstreamGroup>, // 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<Upstream>,
pub path: PathNameExp,
pub replace_path: Option<PathNameExp>,
pub path: PathNameBytesExp,
pub replace_path: Option<PathNameBytesExp>,
pub lb: LoadBalance,
pub cnt: UpstreamCount, // counter for load balancing
pub opts: HashSet<UpstreamOption>,

View file

@ -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<ReverseProxy, anyhow::Error> {
let mut upstream: HashMap<PathNameExp, UpstreamGroup> = HashMap::default();
let mut upstream: HashMap<PathNameBytesExp, UpstreamGroup> = HashMap::default();
rp_settings.iter().for_each(|rpo| {
let path = match &rpo.path {
Some(p) => p.as_bytes().to_ascii_lowercase(),

View file

@ -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<ServerNameExp>,
tls_server_name: Option<ServerNameBytesExp>,
) -> Result<Response<Body>> {
////////
let mut log_data = MessageLog::from(&req);

View file

@ -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::<ServerNameExp, Backend>::default(),
apps: HashMap::<ServerNameBytesExp, Backend>::default(),
},
sni_consistency: true,

View file

@ -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<T> Proxy<T>
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<S, Bytes>,
client_addr: SocketAddr,
tls_server_name: ServerNameExp,
tls_server_name: ServerNameBytesExp,
) -> Result<()>
where
S: BidiStream<Bytes> + Send + 'static,

View file

@ -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<LocalExecutor>,
peer_addr: SocketAddr,
tls_server_name: Option<ServerNameExp>,
tls_server_name: Option<ServerNameBytesExp>,
) where
I: AsyncRead + AsyncWrite + Send + Unpin + 'static,
{

View file

@ -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<T: Into<String>> BytesName for T {
type Output = Vec<u8>;
type Output = Bytes;
fn to_byte_name(self) -> Self::Output {
self.into().bytes().collect::<Vec<u8>>()
// Bytes::from(b)
fn to_bytes(self) -> Self::Output {
let b = self.into().bytes().collect::<Vec<u8>>();
Bytes::from(b)
}
fn to_ascii_lowercase_byte_name(self) -> Self::Output {
self.into().bytes().collect::<Vec<u8>>().to_ascii_lowercase()
// Bytes::from(b)
fn to_ascii_lowercase_bytes(self) -> Self::Output {
let b = self.into().bytes().collect::<Vec<u8>>().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);
}
}