Skip to content

Commit 192b53b

Browse files
authored
Return defguard version (proxy, core) in http headers (#151)
* version headers * extract value * update version
1 parent 88d4a5a commit 192b53b

File tree

4 files changed

+41
-6
lines changed

4 files changed

+41
-6
lines changed

Cargo.lock

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ homepage = "https://github.com/DefGuard/proxy"
77
repository = "https://github.com/DefGuard/proxy"
88

99
[dependencies]
10-
defguard_version = { git = "https://github.com/DefGuard/defguard.git", rev = "db678a95398e38b72bbb4ecef36a27caa427e48c" }
10+
defguard_version = { git = "https://github.com/DefGuard/defguard.git", rev = "be3f96ced072ede3ebde72f2f6c6063d2e7f7403" }
1111
# base `axum` deps
1212
axum = { version = "0.8", features = ["macros", "tracing", "ws"] }
1313
axum-client-ip = "0.7"

src/grpc.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ use std::{
66
Arc, Mutex,
77
},
88
};
9+
10+
use defguard_version::{get_tracing_variables, parse_metadata, DefguardComponent, Version};
911
use tokio::sync::{mpsc, oneshot};
1012
use tokio_stream::wrappers::UnboundedReceiverStream;
1113
use tonic::{Request, Response, Status, Streaming};
1214
use tracing::Instrument;
1315

14-
use defguard_version::{get_tracing_variables, parse_metadata, DefguardComponent};
15-
1616
use crate::{
1717
error::ApiError,
1818
proto::{core_request, core_response, proxy_server, CoreRequest, CoreResponse, DeviceInfo},
@@ -27,6 +27,7 @@ pub(crate) struct ProxyServer {
2727
clients: Arc<Mutex<ClientMap>>,
2828
results: Arc<Mutex<HashMap<u64, oneshot::Sender<core_response::Payload>>>>,
2929
pub(crate) connected: Arc<AtomicBool>,
30+
pub(crate) core_version: Arc<Mutex<Option<Version>>>,
3031
}
3132

3233
impl ProxyServer {
@@ -38,6 +39,7 @@ impl ProxyServer {
3839
clients: Arc::new(Mutex::new(HashMap::new())),
3940
results: Arc::new(Mutex::new(HashMap::new())),
4041
connected: Arc::new(AtomicBool::new(false)),
42+
core_version: Arc::new(Mutex::new(None)),
4143
}
4244
}
4345

@@ -82,6 +84,7 @@ impl Clone for ProxyServer {
8284
clients: Arc::clone(&self.clients),
8385
results: Arc::clone(&self.results),
8486
connected: Arc::clone(&self.connected),
87+
core_version: Arc::clone(&self.core_version),
8588
}
8689
}
8790
}
@@ -102,6 +105,12 @@ impl proxy_server::Proxy for ProxyServer {
102105
};
103106
let maybe_info = parse_metadata(request.metadata());
104107
let (version, info) = get_tracing_variables(&maybe_info);
108+
109+
if let Ok(ver) = Version::parse(&version) {
110+
let mut core_version = self.core_version.lock().unwrap();
111+
*core_version = Some(ver);
112+
}
113+
105114
let span = tracing::info_span!("core_bidi_stream", component = %DefguardComponent::Core, version, info);
106115
let _guard = span.enter();
107116

src/http.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ use anyhow::Context;
1010
use axum::{
1111
body::Body,
1212
extract::{ConnectInfo, FromRef, State},
13-
http::{Request, StatusCode},
13+
http::{header::HeaderValue, Request, Response, StatusCode},
14+
middleware::{self, Next},
1415
routing::{get, post},
1516
serve, Json, Router,
1617
};
1718
use axum_extra::extract::cookie::Key;
1819
use clap::crate_version;
1920
use defguard_version::{
20-
server::{DefguardVersionInterceptor, DefguardVersionLayer},
21+
server::{grpc::DefguardVersionInterceptor, DefguardVersionLayer},
2122
DefguardComponent, Version,
2223
};
2324
use serde::Serialize;
@@ -44,6 +45,7 @@ use crate::{
4445

4546
pub(crate) static ENROLLMENT_COOKIE_NAME: &str = "defguard_proxy";
4647
pub(crate) static PASSWORD_RESET_COOKIE_NAME: &str = "defguard_proxy_password_reset";
48+
const DEFGUARD_CORE_VERSION_HEADER: &str = "defguard-core-version";
4749
const RATE_LIMITER_CLEANUP_PERIOD: Duration = Duration::from_secs(60);
4850

4951
#[derive(Clone)]
@@ -125,6 +127,24 @@ fn get_client_addr(request: &Request<Body>) -> String {
125127
)
126128
}
127129

130+
async fn core_version_middleware(
131+
State(app_state): State<AppState>,
132+
request: Request<Body>,
133+
next: Next,
134+
) -> Response<Body> {
135+
let mut response = next.run(request).await;
136+
137+
if let Some(core_version) = app_state.grpc_server.core_version.lock().unwrap().as_ref() {
138+
if let Ok(core_version_header) = HeaderValue::from_str(&core_version.to_string()) {
139+
response
140+
.headers_mut()
141+
.insert(DEFGUARD_CORE_VERSION_HEADER, core_version_header);
142+
}
143+
}
144+
145+
response
146+
}
147+
128148
pub async fn run_server(config: Config) -> anyhow::Result<()> {
129149
info!("Starting Defguard Proxy server");
130150
debug!("Using config: {config:?}");
@@ -246,6 +266,11 @@ pub async fn run_server(config: Config) -> anyhow::Result<()> {
246266
.route("/info", get(app_info)),
247267
)
248268
.fallback_service(get(handle_404))
269+
.layer(middleware::from_fn_with_state(
270+
shared_state.clone(),
271+
core_version_middleware,
272+
))
273+
.layer(DefguardVersionLayer::new(Version::parse(VERSION)?))
249274
.with_state(shared_state)
250275
.layer(
251276
TraceLayer::new_for_http()

0 commit comments

Comments
 (0)