Skip to content

Commit 5bd09a3

Browse files
DewyerBarnabas Ratki
andauthored
chore: Add extra logging to ws connection (#522)
Co-authored-by: Barnabas Ratki <barna@dourolabs.xyz>
1 parent 309cbb8 commit 5bd09a3

1 file changed

Lines changed: 37 additions & 6 deletions

File tree

  • auction-server/src/api

auction-server/src/api/ws.rs

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,15 +149,25 @@ pub async fn ws_route_handler(
149149
headers: HeaderMap,
150150
) -> impl IntoResponse {
151151
let ws_state = &store.store.ws;
152-
let requester_ip = headers
152+
let ip_header = headers
153153
.get(ws_state.requester_ip_header_name.as_str())
154-
.and_then(|value| value.to_str().ok())
155-
.and_then(|value| value.split(',').next()) // Only take the first ip if there are multiple
156-
.and_then(|value| value.parse().ok());
154+
.and_then(|value| value.to_str().ok());
157155

158-
if requester_ip.is_none() {
156+
let requester_ip = if let Some(ip_header) = ip_header {
157+
let parts = ip_header.split(',').collect::<Vec<_>>();
158+
// Only take the first ip if there are multiple
159+
let client_ip = parts.first().and_then(|ip| ip.parse::<IpAddr>().ok());
160+
let obfuscated_proxy_chain = parts.into_iter().map(obfuscate_ip);
161+
162+
tracing::info!(
163+
"Websocket connection established with: `{:?}`",
164+
obfuscated_proxy_chain
165+
);
166+
client_ip
167+
} else {
159168
tracing::warn!("Failed to get requester IP address");
160-
}
169+
None
170+
};
161171

162172
match ws_state.get_new_subscriber_id(requester_ip).await {
163173
Some(subscriber_id) => ws.on_upgrade(move |socket| {
@@ -609,3 +619,24 @@ pub fn get_routes(store: Arc<StoreNew>) -> Router<Arc<StoreNew>> {
609619
.route(Route::Ws, ws_route_handler)
610620
.router
611621
}
622+
623+
/// Keeps the first 8 characters of the ip
624+
fn obfuscate_ip(ip: &str) -> String {
625+
let mut chars = ip.chars();
626+
let first_8: String = chars.by_ref().take(8).collect();
627+
let stars = "*".repeat(chars.count());
628+
629+
first_8 + &stars
630+
}
631+
632+
#[cfg(test)]
633+
mod tests {
634+
use crate::api::ws::obfuscate_ip;
635+
636+
#[test]
637+
fn test_obfuscate_ip() {
638+
let obf = obfuscate_ip("127.188.42.1");
639+
640+
assert_eq!(obf, "127.188.****");
641+
}
642+
}

0 commit comments

Comments
 (0)