From 64498da57a750bb5aca92788fb4865fe37680215 Mon Sep 17 00:00:00 2001 From: Slatian Date: Tue, 21 Feb 2023 10:51:28 +0100 Subject: [PATCH] Added echoing of user agents --- src/main.rs | 20 ++++++++++++++++++-- src/templating_engine.rs | 2 +- templates/index.html | 7 +++++++ 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 189bb58..5de8f2a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -225,6 +225,7 @@ async fn main() { .route("/dig/:name", get(handle_dig_route_with_path)) .route("/ip", get(handle_ip_route)) .route("/ip/:address", get(handle_ip_route_with_path)) + .route("/ua", get(user_agent_handler)) .route("/hi", get(hello_world_handler)) .with_state(shared_state) .layer( @@ -271,7 +272,6 @@ async fn format_and_language_middleware( next.run(req).await } -#[axum::debug_handler] async fn hello_world_handler( State(arc_state): State>, Extension(settings): Extension, @@ -287,9 +287,16 @@ async fn hello_world_handler( ).await } +async fn user_agent_handler( + TypedHeader(user_agent): TypedHeader, +) -> String { + user_agent.to_string() +} + async fn handle_default_route( State(arc_state): State>, Extension(settings): Extension, + user_agent_header: Option>, SecureClientIp(address): SecureClientIp ) -> Response { @@ -301,9 +308,18 @@ async fn handle_default_route( let result = get_ip_result(&ip_query, &settings.lang, &state).await; + let user_agent: Option = match user_agent_header { + Some(TypedHeader(user_agent)) => Some(user_agent.to_string()), + None => None, + }; + state.templating_engine.render_view( &settings, - &View::Index{query: ip_query, result: result} + &View::Index{ + query: ip_query, + result: result, + user_agent: user_agent, + } ).await } diff --git a/src/templating_engine.rs b/src/templating_engine.rs index 51f9fa5..123722a 100644 --- a/src/templating_engine.rs +++ b/src/templating_engine.rs @@ -54,7 +54,7 @@ pub struct TemplateSettings { #[serde(untagged)] pub enum View { Dig { query: DigQuery, result: simple_dns::DnsLookupResult }, - Index { query: IpQuery, result: IpResult }, + Index { query: IpQuery, result: IpResult, user_agent: Option }, Ip { query: IpQuery, result: IpResult }, Message{ title: String, message: String }, #[serde(rename="404")] diff --git a/templates/index.html b/templates/index.html index 409ea82..6121417 100644 --- a/templates/index.html +++ b/templates/index.html @@ -57,5 +57,12 @@

{% endif %} + {% if data.user_agent %} +
+

User Agent

+

Independent of your IP-Address your browser sends a User Agent header with each http request.

+

Your browser sent: {{data.user_agent}}

+
+ {% endif %}