mirror of
				https://codeberg.org/slatian/service.echoip-slatecave.git
				synced 2025-10-31 17:08:08 +01:00 
			
		
		
		
	Added configurable user agents that get served the text version by default
This commit is contained in:
		
							
								
								
									
										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