refactor
This commit is contained in:
parent
d750f87331
commit
4d590f328f
8 changed files with 39 additions and 33 deletions
|
|
@ -19,8 +19,8 @@ pub use upstream_opts::UpstreamOption;
|
||||||
|
|
||||||
// Server name (hostname or ip address) and path name representation in backends
|
// Server name (hostname or ip address) and path name representation in backends
|
||||||
// For searching hashmap or key list by exact or longest-prefix matching
|
// For searching hashmap or key list by exact or longest-prefix matching
|
||||||
pub type ServerNameExp = Vec<u8>; // lowercase ascii bytes
|
pub type ServerNameBytesExp = Vec<u8>; // lowercase ascii bytes
|
||||||
pub type PathNameExp = 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.
|
/// Struct serving information to route incoming connections, like server name to be handled and tls certs/keys settings.
|
||||||
pub struct Backend {
|
pub struct Backend {
|
||||||
|
|
@ -111,8 +111,8 @@ impl Backend {
|
||||||
|
|
||||||
/// HashMap and some meta information for multiple Backend structs.
|
/// HashMap and some meta information for multiple Backend structs.
|
||||||
pub struct Backends {
|
pub struct Backends {
|
||||||
pub apps: HashMap<ServerNameExp, Backend>, // hyper::uriで抜いたhostで引っ掛ける
|
pub apps: HashMap<ServerNameBytesExp, Backend>, // hyper::uriで抜いたhostで引っ掛ける
|
||||||
pub default_server_name_bytes: Option<ServerNameExp>, // for plaintext http
|
pub default_server_name_bytes: Option<ServerNameBytesExp>, // for plaintext http
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Backends {
|
impl Backends {
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
use super::{PathNameExp, UpstreamOption};
|
use super::{PathNameBytesExp, 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};
|
||||||
|
|
@ -12,7 +12,7 @@ use std::{
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct ReverseProxy {
|
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 {
|
impl ReverseProxy {
|
||||||
|
|
@ -70,8 +70,8 @@ pub struct Upstream {
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct UpstreamGroup {
|
pub struct UpstreamGroup {
|
||||||
pub upstream: Vec<Upstream>,
|
pub upstream: Vec<Upstream>,
|
||||||
pub path: PathNameExp,
|
pub path: PathNameBytesExp,
|
||||||
pub replace_path: Option<PathNameExp>,
|
pub replace_path: Option<PathNameBytesExp>,
|
||||||
pub lb: LoadBalance,
|
pub lb: LoadBalance,
|
||||||
pub cnt: UpstreamCount, // counter for load balancing
|
pub cnt: UpstreamCount, // counter for load balancing
|
||||||
pub opts: HashSet<UpstreamOption>,
|
pub opts: HashSet<UpstreamOption>,
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
use super::toml::{ConfigToml, ReverseProxyOption};
|
use super::toml::{ConfigToml, ReverseProxyOption};
|
||||||
use crate::{
|
use crate::{
|
||||||
backend::{Backend, PathNameExp, ReverseProxy, UpstreamGroup, UpstreamOption},
|
backend::{Backend, PathNameBytesExp, ReverseProxy, UpstreamGroup, UpstreamOption},
|
||||||
constants::*,
|
constants::*,
|
||||||
error::*,
|
error::*,
|
||||||
globals::*,
|
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> {
|
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| {
|
rp_settings.iter().for_each(|rpo| {
|
||||||
let path = match &rpo.path {
|
let path = match &rpo.path {
|
||||||
Some(p) => p.as_bytes().to_ascii_lowercase(),
|
Some(p) => p.as_bytes().to_ascii_lowercase(),
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
// 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::*};
|
use super::{utils_headers::*, utils_request::*, utils_synth_response::*};
|
||||||
use crate::{
|
use crate::{
|
||||||
backend::{ServerNameExp, UpstreamGroup},
|
backend::{ServerNameBytesExp, UpstreamGroup},
|
||||||
error::*,
|
error::*,
|
||||||
globals::Globals,
|
globals::Globals,
|
||||||
log::*,
|
log::*,
|
||||||
|
|
@ -39,7 +39,7 @@ where
|
||||||
client_addr: SocketAddr, // アクセス制御用
|
client_addr: SocketAddr, // アクセス制御用
|
||||||
listen_addr: SocketAddr,
|
listen_addr: SocketAddr,
|
||||||
tls_enabled: bool,
|
tls_enabled: bool,
|
||||||
tls_server_name: Option<ServerNameExp>,
|
tls_server_name: Option<ServerNameBytesExp>,
|
||||||
) -> Result<Response<Body>> {
|
) -> Result<Response<Body>> {
|
||||||
////////
|
////////
|
||||||
let mut log_data = MessageLog::from(&req);
|
let mut log_data = MessageLog::from(&req);
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ mod proxy;
|
||||||
mod utils;
|
mod utils;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
backend::{Backend, Backends, ServerNameExp},
|
backend::{Backend, Backends, ServerNameBytesExp},
|
||||||
config::parse_opts,
|
config::parse_opts,
|
||||||
constants::*,
|
constants::*,
|
||||||
error::*,
|
error::*,
|
||||||
|
|
@ -72,7 +72,7 @@ fn main() {
|
||||||
runtime_handle: runtime.handle().clone(),
|
runtime_handle: runtime.handle().clone(),
|
||||||
backends: Backends {
|
backends: Backends {
|
||||||
default_server_name_bytes: None,
|
default_server_name_bytes: None,
|
||||||
apps: HashMap::<ServerNameExp, Backend>::default(),
|
apps: HashMap::<ServerNameBytesExp, Backend>::default(),
|
||||||
},
|
},
|
||||||
|
|
||||||
sni_consistency: true,
|
sni_consistency: true,
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
use super::Proxy;
|
use super::Proxy;
|
||||||
use crate::{backend::ServerNameExp, error::*, log::*};
|
use crate::{backend::ServerNameBytesExp, error::*, log::*};
|
||||||
use bytes::{Buf, Bytes};
|
use bytes::{Buf, Bytes};
|
||||||
use h3::{quic::BidiStream, server::RequestStream};
|
use h3::{quic::BidiStream, server::RequestStream};
|
||||||
use hyper::{client::connect::Connect, Body, Request, Response};
|
use hyper::{client::connect::Connect, Body, Request, Response};
|
||||||
|
|
@ -10,7 +10,11 @@ impl<T> Proxy<T>
|
||||||
where
|
where
|
||||||
T: Connect + Clone + Sync + Send + 'static,
|
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();
|
let client_addr = conn.remote_address();
|
||||||
|
|
||||||
match conn.await {
|
match conn.await {
|
||||||
|
|
@ -68,7 +72,7 @@ where
|
||||||
req: Request<()>,
|
req: Request<()>,
|
||||||
stream: RequestStream<S, Bytes>,
|
stream: RequestStream<S, Bytes>,
|
||||||
client_addr: SocketAddr,
|
client_addr: SocketAddr,
|
||||||
tls_server_name: ServerNameExp,
|
tls_server_name: ServerNameBytesExp,
|
||||||
) -> Result<()>
|
) -> Result<()>
|
||||||
where
|
where
|
||||||
S: BidiStream<Bytes> + Send + 'static,
|
S: BidiStream<Bytes> + Send + 'static,
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
// use super::proxy_handler::handle_request;
|
// 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 hyper::{client::connect::Connect, server::conn::Http, service::service_fn, Body, Request};
|
||||||
use std::{net::SocketAddr, sync::Arc};
|
use std::{net::SocketAddr, sync::Arc};
|
||||||
use tokio::{
|
use tokio::{
|
||||||
|
|
@ -50,7 +50,7 @@ where
|
||||||
stream: I,
|
stream: I,
|
||||||
server: Http<LocalExecutor>,
|
server: Http<LocalExecutor>,
|
||||||
peer_addr: SocketAddr,
|
peer_addr: SocketAddr,
|
||||||
tls_server_name: Option<ServerNameExp>,
|
tls_server_name: Option<ServerNameBytesExp>,
|
||||||
) where
|
) where
|
||||||
I: AsyncRead + AsyncWrite + Send + Unpin + 'static,
|
I: AsyncRead + AsyncWrite + Send + Unpin + 'static,
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -1,20 +1,22 @@
|
||||||
|
use bytes::{Buf, Bytes};
|
||||||
|
|
||||||
pub trait BytesName {
|
pub trait BytesName {
|
||||||
type Output;
|
type Output: Buf;
|
||||||
fn to_byte_name(self) -> Self::Output;
|
fn to_bytes(self) -> Self::Output;
|
||||||
fn to_ascii_lowercase_byte_name(self) -> Self::Output;
|
fn to_ascii_lowercase_bytes(self) -> Self::Output;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Into<String>> BytesName for T {
|
impl<T: Into<String>> BytesName for T {
|
||||||
type Output = Vec<u8>;
|
type Output = Bytes;
|
||||||
|
|
||||||
fn to_byte_name(self) -> Self::Output {
|
fn to_bytes(self) -> Self::Output {
|
||||||
self.into().bytes().collect::<Vec<u8>>()
|
let b = self.into().bytes().collect::<Vec<u8>>();
|
||||||
// Bytes::from(b)
|
Bytes::from(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_ascii_lowercase_byte_name(self) -> Self::Output {
|
fn to_ascii_lowercase_bytes(self) -> Self::Output {
|
||||||
self.into().bytes().collect::<Vec<u8>>().to_ascii_lowercase()
|
let b = self.into().bytes().collect::<Vec<u8>>().to_ascii_lowercase();
|
||||||
// Bytes::from(b)
|
Bytes::from(b)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -24,10 +26,10 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn bytes_name_str_works() {
|
fn bytes_name_str_works() {
|
||||||
let s = "OK_string";
|
let s = "OK_string";
|
||||||
let bn = s.to_byte_name();
|
let bn = s.to_bytes();
|
||||||
let bn_lc = s.to_ascii_lowercase_byte_name();
|
let bn_lc = s.to_ascii_lowercase_bytes();
|
||||||
|
|
||||||
assert_eq!(Vec::from(s.as_bytes()), bn);
|
assert_eq!(Bytes::from(s.as_bytes()), bn);
|
||||||
assert_eq!(Vec::from("ok_string"), bn_lc);
|
assert_eq!(Bytes::from("ok_string"), bn_lc);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue