From de3f6c9747cd550f2f04ca8b88713233a6f54f82 Mon Sep 17 00:00:00 2001 From: Aleksander <170264518+t-aleksander@users.noreply.github.com> Date: Mon, 1 Sep 2025 15:20:51 +0200 Subject: [PATCH 1/3] version headers --- Cargo.lock | 3 ++- Cargo.toml | 2 +- src/grpc.rs | 13 +++++++++++-- src/http.rs | 28 ++++++++++++++++++++++++++-- 4 files changed, 40 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2cf63480..ebf97f57 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -550,8 +550,9 @@ dependencies = [ [[package]] name = "defguard_version" version = "0.0.0" -source = "git+https://github.com/DefGuard/defguard.git?rev=db678a95398e38b72bbb4ecef36a27caa427e48c#db678a95398e38b72bbb4ecef36a27caa427e48c" +source = "git+https://github.com/DefGuard/defguard.git?rev=57f67948a431641bbf6b4faab9b0f9668324d53e#57f67948a431641bbf6b4faab9b0f9668324d53e" dependencies = [ + "axum", "http", "os_info", "semver", diff --git a/Cargo.toml b/Cargo.toml index 3bef7e64..42b24b6a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ homepage = "https://github.com/DefGuard/proxy" repository = "https://github.com/DefGuard/proxy" [dependencies] -defguard_version = { git = "https://github.com/DefGuard/defguard.git", rev = "db678a95398e38b72bbb4ecef36a27caa427e48c" } +defguard_version = { git = "https://github.com/DefGuard/defguard.git", rev = "57f67948a431641bbf6b4faab9b0f9668324d53e" } # base `axum` deps axum = { version = "0.8", features = ["macros", "tracing", "ws"] } axum-client-ip = "0.7" diff --git a/src/grpc.rs b/src/grpc.rs index f69a724a..dbd27d7a 100644 --- a/src/grpc.rs +++ b/src/grpc.rs @@ -6,13 +6,13 @@ use std::{ Arc, Mutex, }, }; + +use defguard_version::{get_tracing_variables, parse_metadata, DefguardComponent, Version}; use tokio::sync::{mpsc, oneshot}; use tokio_stream::wrappers::UnboundedReceiverStream; use tonic::{Request, Response, Status, Streaming}; use tracing::Instrument; -use defguard_version::{get_tracing_variables, parse_metadata, DefguardComponent}; - use crate::{ error::ApiError, proto::{core_request, core_response, proxy_server, CoreRequest, CoreResponse, DeviceInfo}, @@ -27,6 +27,7 @@ pub(crate) struct ProxyServer { clients: Arc>, results: Arc>>>, pub(crate) connected: Arc, + pub(crate) core_version: Arc>>, } impl ProxyServer { @@ -38,6 +39,7 @@ impl ProxyServer { clients: Arc::new(Mutex::new(HashMap::new())), results: Arc::new(Mutex::new(HashMap::new())), connected: Arc::new(AtomicBool::new(false)), + core_version: Arc::new(Mutex::new(None)), } } @@ -82,6 +84,7 @@ impl Clone for ProxyServer { clients: Arc::clone(&self.clients), results: Arc::clone(&self.results), connected: Arc::clone(&self.connected), + core_version: Arc::clone(&self.core_version), } } } @@ -102,6 +105,12 @@ impl proxy_server::Proxy for ProxyServer { }; let maybe_info = parse_metadata(request.metadata()); let (version, info) = get_tracing_variables(&maybe_info); + + if let Ok(ver) = Version::parse(&version) { + let mut core_version = self.core_version.lock().unwrap(); + *core_version = Some(ver); + } + let span = tracing::info_span!("core_bidi_stream", component = %DefguardComponent::Core, version, info); let _guard = span.enter(); diff --git a/src/http.rs b/src/http.rs index 382ba71b..95de1a79 100644 --- a/src/http.rs +++ b/src/http.rs @@ -10,14 +10,15 @@ use anyhow::Context; use axum::{ body::Body, extract::{ConnectInfo, FromRef, State}, - http::{Request, StatusCode}, + http::{header::HeaderValue, Request, Response, StatusCode}, + middleware::{self, Next}, routing::{get, post}, serve, Json, Router, }; use axum_extra::extract::cookie::Key; use clap::crate_version; use defguard_version::{ - server::{DefguardVersionInterceptor, DefguardVersionLayer}, + server::{grpc::DefguardVersionInterceptor, DefguardVersionLayer}, DefguardComponent, Version, }; use serde::Serialize; @@ -125,6 +126,24 @@ fn get_client_addr(request: &Request) -> String { ) } +async fn core_version_middleware( + State(app_state): State, + request: Request, + next: Next, +) -> Response { + let mut response = next.run(request).await; + + if let Some(core_version) = app_state.grpc_server.core_version.lock().unwrap().as_ref() { + if let Ok(core_version_header) = HeaderValue::from_str(&core_version.to_string()) { + response + .headers_mut() + .insert("defguard-core-version", core_version_header); + } + } + + response +} + pub async fn run_server(config: Config) -> anyhow::Result<()> { info!("Starting Defguard Proxy server"); debug!("Using config: {config:?}"); @@ -246,6 +265,11 @@ pub async fn run_server(config: Config) -> anyhow::Result<()> { .route("/info", get(app_info)), ) .fallback_service(get(handle_404)) + .layer(middleware::from_fn_with_state( + shared_state.clone(), + core_version_middleware, + )) + .layer(DefguardVersionLayer::new(Version::parse(VERSION)?)) .with_state(shared_state) .layer( TraceLayer::new_for_http() From 78fd7627a29df99e4ccbc8aea8474b34409edb92 Mon Sep 17 00:00:00 2001 From: Aleksander <170264518+t-aleksander@users.noreply.github.com> Date: Mon, 1 Sep 2025 16:10:05 +0200 Subject: [PATCH 2/3] extract value --- src/http.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/http.rs b/src/http.rs index 95de1a79..db0cd948 100644 --- a/src/http.rs +++ b/src/http.rs @@ -45,6 +45,7 @@ use crate::{ pub(crate) static ENROLLMENT_COOKIE_NAME: &str = "defguard_proxy"; pub(crate) static PASSWORD_RESET_COOKIE_NAME: &str = "defguard_proxy_password_reset"; +const DEFGUARD_CORE_VERSION_HEADER: &str = "defguard-core-version"; const RATE_LIMITER_CLEANUP_PERIOD: Duration = Duration::from_secs(60); #[derive(Clone)] @@ -137,7 +138,7 @@ async fn core_version_middleware( if let Ok(core_version_header) = HeaderValue::from_str(&core_version.to_string()) { response .headers_mut() - .insert("defguard-core-version", core_version_header); + .insert(DEFGUARD_CORE_VERSION_HEADER, core_version_header); } } From 24710ae0031a42f6874496ff1609359880c47c42 Mon Sep 17 00:00:00 2001 From: Aleksander <170264518+t-aleksander@users.noreply.github.com> Date: Mon, 1 Sep 2025 16:21:30 +0200 Subject: [PATCH 3/3] update version --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ebf97f57..30646972 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -550,7 +550,7 @@ dependencies = [ [[package]] name = "defguard_version" version = "0.0.0" -source = "git+https://github.com/DefGuard/defguard.git?rev=57f67948a431641bbf6b4faab9b0f9668324d53e#57f67948a431641bbf6b4faab9b0f9668324d53e" +source = "git+https://github.com/DefGuard/defguard.git?rev=be3f96ced072ede3ebde72f2f6c6063d2e7f7403#be3f96ced072ede3ebde72f2f6c6063d2e7f7403" dependencies = [ "axum", "http", diff --git a/Cargo.toml b/Cargo.toml index 42b24b6a..cf7b9611 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ homepage = "https://github.com/DefGuard/proxy" repository = "https://github.com/DefGuard/proxy" [dependencies] -defguard_version = { git = "https://github.com/DefGuard/defguard.git", rev = "57f67948a431641bbf6b4faab9b0f9668324d53e" } +defguard_version = { git = "https://github.com/DefGuard/defguard.git", rev = "be3f96ced072ede3ebde72f2f6c6063d2e7f7403" } # base `axum` deps axum = { version = "0.8", features = ["macros", "tracing", "ws"] } axum-client-ip = "0.7"