From ede71d8acddb3c2cc05be59373305dc61153276b Mon Sep 17 00:00:00 2001 From: Solly Ross Date: Thu, 5 Feb 2026 16:57:32 -0800 Subject: [PATCH] basic binding from the config both uds and tcp support, but no support for advanced tcp options --- Cargo.lock | 13 ++++---- Cargo.toml | 1 + src/main.rs | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 100 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 33f06d0..444979f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -203,6 +203,7 @@ dependencies = [ "prost", "prost-reflect", "prost-reflect-build", + "rustls", "tokio", ] @@ -1148,9 +1149,9 @@ dependencies = [ [[package]] name = "memchr" -version = "2.7.6" +version = "2.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273" +checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79" [[package]] name = "memoffset" @@ -2101,9 +2102,9 @@ checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" [[package]] name = "ryu" -version = "1.0.22" +version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a50f4cf475b65d88e057964e0e9bb1f0aa9bbb2036dc65c64596b42932536984" +checksum = "9774ba4a74de5f7b1c1451ed6cd5285a32eddb5cccb8cc655a4e50009e06477f" [[package]] name = "schannel" @@ -2558,9 +2559,9 @@ checksum = "dbc4bc3a9f746d862c45cb89d705aa10f187bb96c76001afab07a0d35ce60142" [[package]] name = "unicode-ident" -version = "1.0.22" +version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5" +checksum = "537dd038a89878be9b64dd4bd1b260315c1bb94f4d784956b81e27a088d9a09e" [[package]] name = "unsafe-libyaml" diff --git a/Cargo.toml b/Cargo.toml index 4f6762c..d5ab116 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ color-eyre = "0.6.5" pingora = { version = "0.7.0", features = ["rustls", "proxy"] } prost = "0.14.3" prost-reflect = { version = "0.16.3", features = ["derive", "text-format"] } +rustls = { version = "0.23.36", features = ["aws-lc-rs"] } tokio = { version = "1.49.0", features = ["fs"] } [build-dependencies] diff --git a/src/main.rs b/src/main.rs index e7a11a9..324dff1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,93 @@ -fn main() { - println!("Hello, world!"); +use async_trait::async_trait; +use pingora::modules::http::HttpModules; +use pingora::modules::http::compression::ResponseCompressionBuilder; +use pingora::prelude::*; + +mod config; + +pub struct AuthGateway; + +#[async_trait] +impl ProxyHttp for AuthGateway { + type CTX = (); + fn new_ctx(&self) -> Self::CTX {} + + fn init_downstream_modules(&self, modules: &mut HttpModules) { + // TODO: make this configurable? + modules.add_module(ResponseCompressionBuilder::enable(1)); + } + + async fn request_filter(&self, session: &mut Session, _ctx: &mut Self::CTX) -> Result { + Ok(false) + } + + async fn upstream_peer(&self, _session: &mut Session, _ctx: &mut ()) -> Result> { + todo!() + } + + // TODO: upstream_request_filter to insert the right headers + + async fn response_filter( + &self, + _session: &mut Session, + _upstream_response: &mut ResponseHeader, + _ctx: &mut Self::CTX, + ) -> Result<()> + where + Self::CTX: Send + Sync, + { + Ok(()) + } + + // TODO: logging +} + +fn main() -> color_eyre::Result<()> { + use color_eyre::eyre::Context as _; + use color_eyre::eyre::eyre; + + use std::os::unix::fs::PermissionsExt as _; + + color_eyre::install()?; + rustls::crypto::aws_lc_rs::default_provider() + .install_default() + .expect("unable to install crypto provider"); + + let opts = Opt::parse_args(); + + let config = config::load(opts.conf.as_ref().ok_or_else(|| { + eyre!("no config file specified, refusing to do anything (try `-c FILE`?)") + })?)?; + + let pingora_config = match config.pingora.as_ref().map(Into::into) { + Some(conf) => conf, + None => pingora::server::configuration::ServerConf::new_with_opt_override(&opts) + .ok_or_else(|| { + eyre!("could not create a base pingora config, and none was specified") + })?, + }; + let mut server = Server::new_with_opt_and_conf(opts, pingora_config); + server.bootstrap(); + + let mut gateway = http_proxy_service(&server.configuration, AuthGateway); + for binding in config.bind_to_tcp { + match binding.tls { + Some(tls) => gateway + .add_tls(&binding.addr, &tls.cert_path, &tls.key_path) + .context("setting up tls")?, + None => gateway.add_tcp(&binding.addr), + } + } + for binding in config.bind_to_uds { + gateway.add_uds( + &binding.path, + binding + .permissions + .map(|p| std::fs::Permissions::from_mode(p.mode)), + ); + } + + server.add_service(gateway); + + server.run_forever(); } -- knotmirror 0.0.1