From 662bbb296f2d463bd51866a5aecb58316805e20c Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Wed, 21 Jun 2023 11:28:33 -0700 Subject: [PATCH] add header debug endpoint --- web3_proxy/src/frontend/mod.rs | 1 + web3_proxy/src/frontend/status.rs | 43 ++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/web3_proxy/src/frontend/mod.rs b/web3_proxy/src/frontend/mod.rs index b9b5168f..329f35ba 100644 --- a/web3_proxy/src/frontend/mod.rs +++ b/web3_proxy/src/frontend/mod.rs @@ -142,6 +142,7 @@ pub async fn serve( .route("/health", get(status::health)) .route("/status", get(status::status)) .route("/status/backups_needed", get(status::backups_needed)) + .route("/status/debug_request", get(status::debug_request)) // // User stuff // diff --git a/web3_proxy/src/frontend/status.rs b/web3_proxy/src/frontend/status.rs index 35a595f0..58639a8b 100644 --- a/web3_proxy/src/frontend/status.rs +++ b/web3_proxy/src/frontend/status.rs @@ -9,9 +9,12 @@ use axum::{ body::{Bytes, Full}, http::StatusCode, response::{IntoResponse, Response}, - Extension, + Extension, Json, }; +use axum_client_ip::InsecureClientIp; use axum_macros::debug_handler; +use hashbrown::HashMap; +use http::HeaderMap; use log::trace; use moka::future::Cache; use once_cell::sync::Lazy; @@ -28,6 +31,44 @@ static BACKUPS_NEEDED_FALSE: Lazy = Lazy::new(|| Bytes::from("false\n")); static CONTENT_TYPE_JSON: &str = "application/json"; static CONTENT_TYPE_PLAIN: &str = "text/plain"; +#[debug_handler] +pub async fn debug_request( + Extension(app): Extension>, + ip: InsecureClientIp, + headers: HeaderMap, +) -> impl IntoResponse { + let (_, _, status) = _status(app).await; + + let status: serde_json::Value = serde_json::from_slice(&status).unwrap(); + + let headers: HashMap<_, _> = headers + .into_iter() + .filter_map(|(k, v)| { + if let Some(k) = k { + let k = k.to_string(); + + let v = if let Ok(v) = std::str::from_utf8(v.as_bytes()) { + v.to_string() + } else { + format!("{:?}", v) + }; + + Some((k, v)) + } else { + None + } + }) + .collect(); + + let x = json!({ + "ip": format!("{:?}", ip), + "status": status, + "headers": headers, + }); + + Json(x) +} + /// Health check page for load balancers to use. #[debug_handler] pub async fn health(