Added echoing of user agents

This commit is contained in:
Slatian 2023-02-21 10:51:28 +01:00
parent c7ba9a6346
commit 64498da57a
3 changed files with 26 additions and 3 deletions

View File

@ -225,6 +225,7 @@ async fn main() {
.route("/dig/:name", get(handle_dig_route_with_path)) .route("/dig/:name", get(handle_dig_route_with_path))
.route("/ip", get(handle_ip_route)) .route("/ip", get(handle_ip_route))
.route("/ip/:address", get(handle_ip_route_with_path)) .route("/ip/:address", get(handle_ip_route_with_path))
.route("/ua", get(user_agent_handler))
.route("/hi", get(hello_world_handler)) .route("/hi", get(hello_world_handler))
.with_state(shared_state) .with_state(shared_state)
.layer( .layer(
@ -271,7 +272,6 @@ async fn format_and_language_middleware<B>(
next.run(req).await next.run(req).await
} }
#[axum::debug_handler]
async fn hello_world_handler( async fn hello_world_handler(
State(arc_state): State<Arc<ServiceSharedState>>, State(arc_state): State<Arc<ServiceSharedState>>,
Extension(settings): Extension<TemplateSettings>, Extension(settings): Extension<TemplateSettings>,
@ -287,9 +287,16 @@ async fn hello_world_handler(
).await ).await
} }
async fn user_agent_handler(
TypedHeader(user_agent): TypedHeader<headers::UserAgent>,
) -> String {
user_agent.to_string()
}
async fn handle_default_route( async fn handle_default_route(
State(arc_state): State<Arc<ServiceSharedState>>, State(arc_state): State<Arc<ServiceSharedState>>,
Extension(settings): Extension<TemplateSettings>, Extension(settings): Extension<TemplateSettings>,
user_agent_header: Option<TypedHeader<headers::UserAgent>>,
SecureClientIp(address): SecureClientIp SecureClientIp(address): SecureClientIp
) -> Response { ) -> Response {
@ -301,9 +308,18 @@ async fn handle_default_route(
let result = get_ip_result(&ip_query, &settings.lang, &state).await; let result = get_ip_result(&ip_query, &settings.lang, &state).await;
let user_agent: Option<String> = match user_agent_header {
Some(TypedHeader(user_agent)) => Some(user_agent.to_string()),
None => None,
};
state.templating_engine.render_view( state.templating_engine.render_view(
&settings, &settings,
&View::Index{query: ip_query, result: result} &View::Index{
query: ip_query,
result: result,
user_agent: user_agent,
}
).await ).await
} }

View File

@ -54,7 +54,7 @@ pub struct TemplateSettings {
#[serde(untagged)] #[serde(untagged)]
pub enum View { pub enum View {
Dig { query: DigQuery, result: simple_dns::DnsLookupResult }, Dig { query: DigQuery, result: simple_dns::DnsLookupResult },
Index { query: IpQuery, result: IpResult }, Index { query: IpQuery, result: IpResult, user_agent: Option<String> },
Ip { query: IpQuery, result: IpResult }, Ip { query: IpQuery, result: IpResult },
Message{ title: String, message: String }, Message{ title: String, message: String },
#[serde(rename="404")] #[serde(rename="404")]

View File

@ -57,5 +57,12 @@
</small></p> </small></p>
</section> </section>
{% endif %} {% endif %}
{% if data.user_agent %}
<section>
<h2>User Agent</h2>
<p>Independent of your IP-Address your browser sends a <a href="https://en.wikipedia.org/wiki/User_agent">User Agent</a> header with each http request.</p>
<p>Your browser sent: <code>{{data.user_agent}}</code></p>
</section>
{% endif %}
</body> </body>
</html> </html>