mirror of
https://codeberg.org/slatian/service.echoip-slatecave.git
synced 2025-06-26 12:08:01 +02:00
Added configurable user agents that get served the text version by default
This commit is contained in:
@ -35,6 +35,7 @@ pub struct GeoIpConfig {
|
||||
pub struct TemplateConfig {
|
||||
pub template_location: String,
|
||||
pub extra_config: Option<String>,
|
||||
pub text_user_agents: Vec<String>,
|
||||
}
|
||||
|
||||
impl Default for ServerConfig {
|
||||
@ -71,6 +72,7 @@ impl Default for TemplateConfig {
|
||||
TemplateConfig {
|
||||
template_location: "templates/".to_string(),
|
||||
extra_config: None,
|
||||
text_user_agents: vec!["curl/".to_string()],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
20
src/main.rs
20
src/main.rs
@ -5,11 +5,13 @@ use axum::{
|
||||
State,
|
||||
Extension,
|
||||
},
|
||||
headers,
|
||||
http::Request,
|
||||
middleware::{self, Next},
|
||||
response::Response,
|
||||
Router,
|
||||
routing::get,
|
||||
TypedHeader,
|
||||
};
|
||||
use axum_client_ip::SecureClientIp;
|
||||
use clap::Parser;
|
||||
@ -244,12 +246,26 @@ async fn main() {
|
||||
async fn format_and_language_middleware<B>(
|
||||
Query(query): Query<BaseQuery>,
|
||||
Extension(config): Extension<config::EchoIpServiceConfig>,
|
||||
user_agent_header: Option<TypedHeader<headers::UserAgent>>,
|
||||
mut req: Request<B>,
|
||||
next: Next<B>
|
||||
) -> Response {
|
||||
let format = query.format.unwrap_or(ResponseFormat::TextHtml);
|
||||
let mut format = query.format;
|
||||
// Try to guess type from user agent
|
||||
if format.is_none() {
|
||||
if let Some(TypedHeader(user_agent)) = user_agent_header {
|
||||
let ua = user_agent.as_str();
|
||||
for tua in config.template.text_user_agents {
|
||||
if ua.starts_with(&tua) {
|
||||
format = Some(ResponseFormat::TextPlain);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Add the request settings extension
|
||||
req.extensions_mut().insert(TemplateSettings{
|
||||
format: format,
|
||||
format: format.unwrap_or(ResponseFormat::TextHtml),
|
||||
lang: query.lang.unwrap_or("en".to_string()),
|
||||
});
|
||||
next.run(req).await
|
||||
|
Reference in New Issue
Block a user