diff --git a/common/frontend/src/lib.rs b/common/frontend/src/lib.rs index f46e754c53..97120a75d4 100644 --- a/common/frontend/src/lib.rs +++ b/common/frontend/src/lib.rs @@ -60,7 +60,7 @@ where "veloren_server::events::entity_manipulation=info", "hyper=info", "prometheus_hyper=info", - "mio::pool=info", + "mio::poll=info", "mio::sys::windows=info", "h2=info", "tokio_util=info", diff --git a/server-cli/src/web/chat.rs b/server-cli/src/web/chat.rs index 1d7f71f6f0..3396bc94cc 100644 --- a/server-cli/src/web/chat.rs +++ b/server-cli/src/web/chat.rs @@ -1,5 +1,5 @@ use axum::{ - extract::{Query, State}, + extract::{ConnectInfo, Query, State}, middleware::Next, response::{IntoResponse, Response}, routing::get, @@ -9,7 +9,13 @@ use chrono::DateTime; use hyper::{Request, StatusCode}; use serde::{Deserialize, Deserializer}; use server::chat::ChatCache; -use std::str::FromStr; +use std::{ + collections::HashSet, + net::{IpAddr, SocketAddr}, + str::FromStr, + sync::Arc, +}; +use tokio::sync::Mutex; /// Keep Size small, so we dont have to Clone much for each request. #[derive(Clone)] @@ -17,6 +23,11 @@ struct ChatToken { secret_token: Option, } +#[derive(Clone, Default)] +struct IpAddresses { + users: Arc>>, +} + async fn validate_secret( State(token): State, req: Request, @@ -38,10 +49,29 @@ async fn validate_secret( Ok(next.run(req).await) } +/// Logs each new IP address that accesses this API authenticated +async fn log_users( + State(ip_addresses): State, + ConnectInfo(addr): ConnectInfo, + req: Request, + next: Next, +) -> Result { + let mut ip_addresses = ip_addresses.users.lock().await; + let ip_addr = addr.ip(); + if !ip_addresses.contains(&ip_addr) { + ip_addresses.insert(ip_addr); + let users_so_far = ip_addresses.len(); + tracing::info!(?ip_addr, ?users_so_far, "Is accessing the /chat endpoint"); + } + Ok(next.run(req).await) +} + pub fn router(cache: ChatCache, secret_token: Option) -> Router { let token = ChatToken { secret_token }; + let ip_addrs = IpAddresses::default(); Router::new() .route("/history", get(history)) + .layer(axum::middleware::from_fn_with_state(ip_addrs, log_users)) .layer(axum::middleware::from_fn_with_state(token, validate_secret)) .with_state(cache) } diff --git a/server-cli/src/web/mod.rs b/server-cli/src/web/mod.rs index edf9f2bbe9..a73f95b456 100644 --- a/server-cli/src/web/mod.rs +++ b/server-cli/src/web/mod.rs @@ -30,7 +30,8 @@ where // run it let addr = addr.into(); - let server = axum::Server::bind(&addr).serve(app.into_make_service()); + let server = + axum::Server::bind(&addr).serve(app.into_make_service_with_connect_info::()); let server = server.with_graceful_shutdown(shutdown); tracing::info!("listening on {}", addr); match server.await {