Skip to content

Commit

Permalink
wip: implemented backend
Browse files Browse the repository at this point in the history
  • Loading branch information
junkurihara committed Nov 24, 2023
1 parent de91c7a commit 240a6eb
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 23 deletions.
26 changes: 13 additions & 13 deletions rpxy-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ publish = false
default = ["http3-s2n", "sticky-cookie", "cache"]
http3-quinn = ["socket2"] #"quinn", "h3", "h3-quinn", ]
http3-s2n = [] #"h3", "s2n-quic", "s2n-quic-rustls", "s2n-quic-h3"]
sticky-cookie = [] #"base64", "sha2", "chrono"]
sticky-cookie = ["base64", "sha2", "chrono"]
cache = [] #"http-cache-semantics", "lru"]
native-roots = [] #"hyper-rustls/native-tokio"]

[dependencies]
# rand = "0.8.5"
# rustc-hash = "1.1.0"
rand = "0.8.5"
rustc-hash = "1.1.0"
# bytes = "1.5.0"
# derive_builder = "0.12.0"
derive_builder = "0.12.0"
futures = { version = "0.3.29", features = ["alloc", "async-await"] }
tokio = { version = "1.34.0", default-features = false, features = [
"net",
Expand All @@ -34,13 +34,13 @@ tokio = { version = "1.34.0", default-features = false, features = [
"fs",
] }
async-trait = "0.1.74"
# hot_reload = "0.1.4" # reloading certs

# Error handling
anyhow = "1.0.75"
thiserror = "1.0.50"

# http and tls
hot_reload = "0.1.4" # reloading certs
http = "1.0.0"
# http-body-util = "0.1.0"
hyper = { version = "1.0.1", default-features = false }
Expand Down Expand Up @@ -75,14 +75,14 @@ socket2 = { version = "0.5.5", features = ["all"], optional = true }
# http-cache-semantics = { path = "../submodules/rusty-http-cache-semantics/", optional = true }
# lru = { version = "0.12.0", optional = true }

# # cookie handling for sticky cookie
# chrono = { version = "0.4.31", default-features = false, features = [
# "unstable-locales",
# "alloc",
# "clock",
# ], optional = true }
# base64 = { version = "0.21.5", optional = true }
# sha2 = { version = "0.10.8", default-features = false, optional = true }
# cookie handling for sticky cookie
chrono = { version = "0.4.31", default-features = false, features = [
"unstable-locales",
"alloc",
"clock",
], optional = true }
base64 = { version = "0.21.5", optional = true }
sha2 = { version = "0.10.8", default-features = false, optional = true }


# [dev-dependencies]
Expand Down
11 changes: 11 additions & 0 deletions rpxy-lib/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,15 @@ pub type RpxyResult<T> = std::result::Result<T, RpxyError>;
pub enum RpxyError {
#[error("IO error: {0}")]
Io(#[from] std::io::Error),

// backend errors
#[error("Invalid reverse proxy setting")]
InvalidReverseProxyConfig,
#[error("Invalid upstream option setting")]
InvalidUpstreamOptionSetting,
#[error("Failed to build backend app")]
FailedToBuildBackendApp(#[from] crate::backend::BackendAppBuilderError),

#[error("Unsupported upstream option")]
UnsupportedUpstreamOption,
}
6 changes: 4 additions & 2 deletions rpxy-lib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
mod backend;
mod certs;
mod constants;
mod count;
mod error;
mod globals;
mod hyper_executor;
mod log;
mod name_exp;
mod proxy;

use crate::{error::*, globals::Globals, log::*, proxy::Proxy};
Expand Down Expand Up @@ -70,8 +72,8 @@ where
term_notify: term_notify.clone(),
});

// TODO: 1. build backends, and make it contained in Arc
// app_config_list: app_config_list.clone(),
// 1. build backends, and make it contained in Arc
let app_manager = Arc::new(backend::BackendAppManager::try_from(app_config_list)?);

// TODO: 2. build message handler with Arc-ed http_client and backends, and make it contained in Arc as well
// // build message handler including a request forwarder
Expand Down
1 change: 1 addition & 0 deletions rpxy-lib/src/proxy/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod proxy_main;
mod socket;
mod proxy_tls;

use crate::{globals::Globals, hyper_executor::LocalExecutor};
use hyper_util::server::{self, conn::auto::Builder as ConnectionBuilder};
Expand Down
29 changes: 21 additions & 8 deletions rpxy-lib/src/proxy/proxy_main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use super::socket::bind_tcp_socket;
use crate::{error::RpxyResult, globals::Globals, hyper_executor::LocalExecutor, log::*};
use crate::{error::RpxyResult, globals::Globals, log::*};
use hyper_util::server::conn::auto::Builder as ConnectionBuilder;
use std::{net::SocketAddr, sync::Arc};
use hot_reload::{ReloaderReceiver, ReloaderService};

/// Proxy main object responsible to serve requests received from clients at the given socket address.
pub(crate) struct Proxy<E = LocalExecutor> {
pub(crate) struct Proxy<E> {
/// global context shared among async tasks
pub globals: Arc<Globals>,
/// listen socket address
Expand All @@ -15,7 +16,7 @@ pub(crate) struct Proxy<E = LocalExecutor> {
pub connection_builder: Arc<ConnectionBuilder<E>>,
}

impl Proxy {
impl<E> Proxy<E> {
/// Start without TLS (HTTP cleartext)
async fn start_without_tls(&self) -> RpxyResult<()> {
let listener_service = async {
Expand All @@ -31,14 +32,26 @@ impl Proxy {
Ok(())
}

/// Start with TLS (HTTPS)
pub(super) async fn start_with_tls(&self) -> RpxyResult<()> {
// let (cert_reloader_service, cert_reloader_rx) = ReloaderService::<CryptoReloader<U>, ServerCryptoBase>::new(
// &self.globals.clone(),
// CERTS_WATCH_DELAY_SECS,
// !LOAD_CERTS_ONLY_WHEN_UPDATED,
// )
// .await
// .map_err(|e| anyhow::anyhow!(e))?;
Ok(())
}

/// Entrypoint for HTTP/1.1, 2 and 3 servers
pub async fn start(&self) -> RpxyResult<()> {
let proxy_service = async {
// if self.tls_enabled {
// self.start_with_tls().await
// } else {
self.start_without_tls().await
// }
if self.tls_enabled {
self.start_with_tls().await
} else {
self.start_without_tls().await
}
};

match &self.globals.term_notify {
Expand Down

0 comments on commit 240a6eb

Please sign in to comment.