Compare commits

...

3 Commits

Author SHA1 Message Date
Simon Wicky 4116319c6a and cargo lock 2025-06-05 15:10:52 +02:00
Simon Wicky 0053f5523b peek into headers 2025-06-05 15:10:44 +02:00
Simon Wicky 847aa65d66 better ip address extraction 2025-06-05 14:05:01 +02:00
4 changed files with 29 additions and 16 deletions
Generated
+2 -1
View File
@@ -7224,10 +7224,11 @@ dependencies = [
[[package]]
name = "nym-statistics-api"
version = "0.1.1"
version = "0.1.2-dev1"
dependencies = [
"anyhow",
"axum 0.7.9",
"axum-client-ip",
"axum-extra",
"celes",
"clap",
+2 -1
View File
@@ -3,7 +3,7 @@
[package]
name = "nym-statistics-api"
version = "0.1.1"
version = "0.1.2-dev1"
authors.workspace = true
repository.workspace = true
homepage.workspace = true
@@ -16,6 +16,7 @@ rust-version.workspace = true
[dependencies]
anyhow.workspace = true
axum = { workspace = true, features = ["tokio", "macros"] }
axum-client-ip.workspace = true
axum-extra = { workspace = true, features = ["typed-header"] }
celes.workspace = true
clap = { workspace = true, features = ["cargo", "derive", "env", "string"] }
+23 -10
View File
@@ -1,9 +1,5 @@
use std::net::SocketAddr;
use axum::{
extract::{ConnectInfo, State},
Json, Router,
};
use axum::{extract::State, http::HeaderMap, Json, Router};
use axum_client_ip::InsecureClientIp;
use axum_extra::{headers::UserAgent, TypedHeader};
use nym_statistics_common::report::vpn_client::VpnClientStatsReport;
use tracing::debug;
@@ -33,12 +29,24 @@ pub(crate) fn routes() -> Router<AppState> {
#[tracing::instrument(level = "info", skip_all)]
async fn submit_stats_report(
State(mut state): State<AppState>,
ConnectInfo(addr): ConnectInfo<SocketAddr>,
TypedHeader(user_agent): TypedHeader<UserAgent>,
insecure_ip_addr: InsecureClientIp,
headers: HeaderMap,
Json(report): Json<VpnClientStatsReport>,
) -> HttpResult<Json<()>> {
let now = time::OffsetDateTime::now_utc();
let gateway_record = state.network_view().get_country_by_ip(&addr.ip()).await;
if let Some(h) = headers.get("X-Real-IP") {
debug!("X-Real-IP :{:?}", h);
}
if let Some(h) = headers.get("X-Forwarded-For") {
debug!("X-Forwarded-For :{:?}", h);
}
let gateway_record = state
.network_view()
.get_country_by_ip(&insecure_ip_addr.0)
.await;
let from_mixnet = gateway_record.is_some();
let maybe_location = gateway_record.unwrap_or_default();
@@ -50,8 +58,13 @@ async fn submit_stats_report(
}
let active_device = DailyActiveDeviceDto::new(now, &report, user_agent, from_mixnet);
let maybe_connection_info =
ConnectionInfoDto::maybe_new(now, &report, addr, maybe_location, from_mixnet);
let maybe_connection_info = ConnectionInfoDto::maybe_new(
now,
&report,
insecure_ip_addr.0.to_string(),
maybe_location,
from_mixnet,
);
state
.storage()
+2 -4
View File
@@ -1,5 +1,3 @@
use std::net::SocketAddr;
use axum_extra::headers::UserAgent;
use celes::Country;
use nym_statistics_common::report::vpn_client::VpnClientStatsReport;
@@ -53,13 +51,13 @@ impl ConnectionInfoDto {
pub(crate) fn maybe_new(
received_at: OffsetDateTime,
stats_report: &VpnClientStatsReport,
received_from: SocketAddr,
received_from: String,
maybe_country: Option<Country>,
from_mixnet: bool,
) -> Option<Self> {
stats_report.basic_usage.as_ref().map(|usage_report| Self {
received_at,
received_from: received_from.ip().to_string(),
received_from,
connection_time_ms: usage_report.connection_time_ms,
two_hop: usage_report.two_hop,
country_code: maybe_country.map(|c| c.alpha2.into()),