Skip to content

Commit d1b9a2e

Browse files
Version exchange and logging (#133)
Use defguard_version crate to log and exchange version and system information.
1 parent 67e2351 commit d1b9a2e

File tree

7 files changed

+180
-73
lines changed

7 files changed

+180
-73
lines changed

Cargo.lock

Lines changed: 74 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +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 = "f61ce40927a4d21095ea53a691219d5ae46e3e4e" }
1011
# base `axum` deps
1112
axum = { version = "0.7", features = ["macros", "tracing", "ws"] }
1213
axum-client-ip = "0.6"
@@ -48,6 +49,7 @@ tower_governor = "0.4"
4849
rust-embed = { version = "8.5", features = ["include-exclude"] }
4950
mime_guess = "2.0"
5051
base64 = "0.22.1"
52+
tower = "0.5.2"
5153
futures = "0.3.31"
5254
futures-util = "0.3.31"
5355

deny.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ confidence-threshold = 0.8
108108
# Allow 1 or more licenses on a per-crate basis, so that particular licenses
109109
# aren't accepted for every possible crate as with the normal allow list
110110
exceptions = [
111-
# Each entry is the crate and version constraint, and its specific allow
112-
# list
113-
#{ allow = ["Zlib"], crate = "adler32" },
111+
{ allow = [
112+
"AGPL-3.0-only",
113+
], crate = "defguard_version" },
114114
]
115115

116116
# Some crates don't have (easily) machine readable licensing information,

src/grpc.rs

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ use std::{
66
Arc, Mutex,
77
},
88
};
9-
109
use tokio::sync::{mpsc, oneshot};
1110
use tokio_stream::wrappers::UnboundedReceiverStream;
1211
use tonic::{Request, Response, Status, Streaming};
12+
use tracing::Instrument;
13+
14+
use defguard_version::{version_info_from_metadata, DefguardComponent};
1315

1416
use crate::{
1517
error::ApiError,
@@ -89,7 +91,7 @@ impl proxy_server::Proxy for ProxyServer {
8991
type BidiStream = UnboundedReceiverStream<Result<CoreRequest, Status>>;
9092

9193
/// Handle bidirectional communication with Defguard core.
92-
#[instrument(name = "bidirectional_communication", level = "debug", skip(self))]
94+
#[instrument(name = "bidirectional_communication", level = "info", skip(self))]
9395
async fn bidi(
9496
&self,
9597
request: Request<Streaming<CoreResponse>>,
@@ -98,6 +100,9 @@ impl proxy_server::Proxy for ProxyServer {
98100
error!("Failed to determine client address for request: {request:?}");
99101
return Err(Status::internal("Failed to determine client address"));
100102
};
103+
let (version, info) = version_info_from_metadata(request.metadata());
104+
let span = tracing::info_span!("core_bidi_stream", component = %DefguardComponent::Core, version, info);
105+
let _guard = span.enter();
101106
info!("Defguard Core gRPC client connected from: {address}");
102107

103108
let (tx, rx) = mpsc::unbounded_channel();
@@ -108,37 +113,40 @@ impl proxy_server::Proxy for ProxyServer {
108113
let results = Arc::clone(&self.results);
109114
let connected = Arc::clone(&self.connected);
110115
let mut stream = request.into_inner();
111-
tokio::spawn(async move {
112-
loop {
113-
match stream.message().await {
114-
Ok(Some(response)) => {
115-
debug!("Received message from Defguard core: {response:?}");
116-
connected.store(true, Ordering::Relaxed);
117-
// Discard empty payloads.
118-
if let Some(payload) = response.payload {
119-
if let Some(rx) = results.lock().unwrap().remove(&response.id) {
120-
if let Err(err) = rx.send(payload) {
121-
error!("Failed to send message to rx: {err:?}");
116+
tokio::spawn(
117+
async move {
118+
loop {
119+
match stream.message().await {
120+
Ok(Some(response)) => {
121+
debug!("Received message from Defguard core: {response:?}");
122+
connected.store(true, Ordering::Relaxed);
123+
// Discard empty payloads.
124+
if let Some(payload) = response.payload {
125+
if let Some(rx) = results.lock().unwrap().remove(&response.id) {
126+
if let Err(err) = rx.send(payload) {
127+
error!("Failed to send message to rx: {err:?}");
128+
}
129+
} else {
130+
error!("Missing receiver for response #{}", response.id);
122131
}
123-
} else {
124-
error!("Missing receiver for response #{}", response.id);
125132
}
126133
}
127-
}
128-
Ok(None) => {
129-
info!("gRPC stream has been closed");
130-
break;
131-
}
132-
Err(err) => {
133-
error!("gRPC client error: {err}");
134-
break;
134+
Ok(None) => {
135+
info!("gRPC stream has been closed");
136+
break;
137+
}
138+
Err(err) => {
139+
error!("gRPC client error: {err}");
140+
break;
141+
}
135142
}
136143
}
144+
info!("Defguard core client disconnected: {address}");
145+
connected.store(false, Ordering::Relaxed);
146+
clients.lock().unwrap().remove(&address);
137147
}
138-
info!("Defguard core client disconnected: {address}");
139-
connected.store(false, Ordering::Relaxed);
140-
clients.lock().unwrap().remove(&address);
141-
});
148+
.instrument(tracing::Span::current()),
149+
);
142150

143151
Ok(Response::new(UnboundedReceiverStream::new(rx)))
144152
}

src/http.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ use axum::{
1616
};
1717
use axum_extra::extract::cookie::Key;
1818
use clap::crate_version;
19+
use defguard_version::{server::DefguardVersionLayer, Version};
1920
use serde::Serialize;
2021
use tokio::{net::TcpListener, sync::oneshot, task::JoinSet};
2122
use tonic::transport::{Identity, Server, ServerTlsConfig};
23+
use tower::ServiceBuilder;
2224
use tower_governor::{
2325
governor::GovernorConfigBuilder, key_extractor::SmartIpKeyExtractor, GovernorLayer,
2426
};
@@ -34,6 +36,7 @@ use crate::{
3436
grpc::ProxyServer,
3537
handlers::{desktop_client_mfa, enrollment, password_reset, polling},
3638
proto::proxy_server,
39+
VERSION,
3740
};
3841

3942
pub(crate) static ENROLLMENT_COOKIE_NAME: &str = "defguard_proxy";
@@ -166,8 +169,11 @@ pub async fn run_server(config: Config) -> anyhow::Result<()> {
166169
} else {
167170
Server::builder()
168171
};
172+
let versioned_service = ServiceBuilder::new()
173+
.layer(DefguardVersionLayer::new(Version::parse(VERSION)?))
174+
.service(proxy_server::ProxyServer::new(grpc_server));
169175
builder
170-
.add_service(proxy_server::ProxyServer::new(grpc_server))
176+
.add_service(versioned_service)
171177
.serve(addr)
172178
.await
173179
.context("Error running gRPC server")

0 commit comments

Comments
 (0)