Threw out some unneeded api

This commit is contained in:
Slatian 2023-02-22 21:32:10 +01:00
parent 64498da57a
commit 70f06ac501
3 changed files with 54 additions and 66 deletions

View File

@ -50,11 +50,11 @@ pub struct MMDBCarrier {
} }
pub trait QueryLocation { pub trait QueryLocation {
fn query_location_for_ip(&self, address: IpAddr, laguages: &Vec<&String>) -> Option<LocationResult>; fn query_location_for_ip(&self, address: &IpAddr, laguages: &Vec<&String>) -> Option<LocationResult>;
} }
pub trait QueryAsn { pub trait QueryAsn {
fn query_asn_for_ip(&self, address: IpAddr) -> Option<AsnResult>; fn query_asn_for_ip(&self, address: &IpAddr) -> Option<AsnResult>;
} }
/* Converters */ /* Converters */
@ -119,10 +119,10 @@ pub fn geoip2_subdivision_to_named_location(item: geoip2::model::Subdivision, la
/* Implementation */ /* Implementation */
impl QueryAsn for MMDBCarrier { impl QueryAsn for MMDBCarrier {
fn query_asn_for_ip(&self, address: IpAddr) -> Option<AsnResult> { fn query_asn_for_ip(&self, address: &IpAddr) -> Option<AsnResult> {
match &self.mmdb { match &self.mmdb {
Some(mmdb) => { Some(mmdb) => {
match mmdb.lookup::<geoip2::Asn>(address) { match mmdb.lookup::<geoip2::Asn>(*address) {
Ok(res) => { Ok(res) => {
Some(AsnResult { Some(AsnResult {
asn: res.autonomous_system_number, asn: res.autonomous_system_number,
@ -141,10 +141,10 @@ impl QueryAsn for MMDBCarrier {
} }
impl QueryLocation for MMDBCarrier { impl QueryLocation for MMDBCarrier {
fn query_location_for_ip(&self, address: IpAddr, languages: &Vec<&String>) -> Option<LocationResult> { fn query_location_for_ip(&self, address: &IpAddr, languages: &Vec<&String>) -> Option<LocationResult> {
match &self.mmdb { match &self.mmdb {
Some(mmdb) => { Some(mmdb) => {
match mmdb.lookup::<geoip2::City>(address) { match mmdb.lookup::<geoip2::City>(*address) {
Ok(res) => { Ok(res) => {
Some(LocationResult { Some(LocationResult {
continent: continent:

View File

@ -48,19 +48,14 @@ use crate::templating_engine::{
use crate::ipinfo::{AddressCast,AddressInfo,AddressScope}; use crate::ipinfo::{AddressCast,AddressInfo,AddressScope};
#[derive(serde::Deserialize, serde::Serialize, Clone)] #[derive(serde::Deserialize, serde::Serialize, Clone)]
pub struct BaseQuery { pub struct SettingsQuery {
format: Option<ResponseFormat>, format: Option<ResponseFormat>,
lang: Option<String>, lang: Option<String>,
} }
#[derive(serde::Deserialize, serde::Serialize, Clone)] #[derive(serde::Deserialize, serde::Serialize, Clone)]
pub struct IpQuery { pub struct SearchQuery {
ip: IpAddr, query: Option<String>,
}
#[derive(serde::Deserialize, serde::Serialize, Clone)]
pub struct DigQuery {
name: String,
} }
#[derive(serde::Deserialize, serde::Serialize, Clone)] #[derive(serde::Deserialize, serde::Serialize, Clone)]
@ -221,9 +216,7 @@ async fn main() {
// Initalize axum server // Initalize axum server
let app = Router::new() let app = Router::new()
.route("/", get(handle_default_route)) .route("/", get(handle_default_route))
.route("/dig", get(handle_dig_route))
.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/:address", get(handle_ip_route_with_path)) .route("/ip/:address", get(handle_ip_route_with_path))
.route("/ua", get(user_agent_handler)) .route("/ua", get(user_agent_handler))
.route("/hi", get(hello_world_handler)) .route("/hi", get(hello_world_handler))
@ -245,7 +238,7 @@ async fn main() {
} }
async fn format_and_language_middleware<B>( async fn format_and_language_middleware<B>(
Query(query): Query<BaseQuery>, Query(query): Query<SettingsQuery>,
Extension(config): Extension<config::EchoIpServiceConfig>, Extension(config): Extension<config::EchoIpServiceConfig>,
user_agent_header: Option<TypedHeader<headers::UserAgent>>, user_agent_header: Option<TypedHeader<headers::UserAgent>>,
mut req: Request<B>, mut req: Request<B>,
@ -294,19 +287,16 @@ async fn user_agent_handler(
} }
async fn handle_default_route( async fn handle_default_route(
Query(search_query): Query<SearchQuery>,
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>>, user_agent_header: Option<TypedHeader<headers::UserAgent>>,
SecureClientIp(address): SecureClientIp SecureClientIp(address): SecureClientIp
) -> Response { ) -> Response {
let ip_query = IpQuery {
ip: address,
};
let state = Arc::clone(&arc_state); let state = Arc::clone(&arc_state);
let result = get_ip_result(&ip_query, &settings.lang, &state).await; let result = get_ip_result(&address, &settings.lang, &state).await;
let user_agent: Option<String> = match user_agent_header { let user_agent: Option<String> = match user_agent_header {
Some(TypedHeader(user_agent)) => Some(user_agent.to_string()), Some(TypedHeader(user_agent)) => Some(user_agent.to_string()),
@ -316,52 +306,60 @@ async fn handle_default_route(
state.templating_engine.render_view( state.templating_engine.render_view(
&settings, &settings,
&View::Index{ &View::Index{
query: ip_query, query: address,
result: result, result: result,
user_agent: user_agent, user_agent: user_agent,
} }
).await ).await
} }
async fn handle_ip_route( async fn handle_search_request(
Query(ip_query): Query<IpQuery>, search_query: String,
Extension(settings): Extension<TemplateSettings>, this_should_have_been_an_ip: bool,
State(arc_state): State<Arc<ServiceSharedState>>,
) -> Response {
return handle_ip_request(ip_query, settings, arc_state).await
}
async fn handle_ip_route_with_path(
Extension(settings): Extension<TemplateSettings>,
State(arc_state): State<Arc<ServiceSharedState>>,
extract::Path(address): extract::Path<IpAddr>,
) -> Response {
return handle_ip_request(IpQuery {
ip: address,
}, settings, arc_state).await
}
async fn handle_ip_request(
ip_query: IpQuery,
settings: TemplateSettings, settings: TemplateSettings,
arc_state: Arc<ServiceSharedState>, arc_state: Arc<ServiceSharedState>,
) -> Response { ) -> Response {
let state = Arc::clone(&arc_state); let state = Arc::clone(&arc_state);
let result = get_ip_result(&ip_query, &settings.lang, &state).await;
state.templating_engine.render_view( state.templating_engine.render_view(
&settings, &settings,
&View::Ip{query: ip_query, result: result} &View::Message{title: "Your Search query was:".to_string(), message: search_query}
).await
}
async fn handle_ip_route_with_path(
Extension(settings): Extension<TemplateSettings>,
State(arc_state): State<Arc<ServiceSharedState>>,
extract::Path(query): extract::Path<String>,
) -> Response {
if let Ok(address) = query.parse() {
return handle_ip_request(address, settings, arc_state).await
} else {
return handle_search_request(query, true, settings, arc_state).await;
}
}
async fn handle_ip_request(
address: IpAddr,
settings: TemplateSettings,
arc_state: Arc<ServiceSharedState>,
) -> Response {
let state = Arc::clone(&arc_state);
let result = get_ip_result(&address, &settings.lang, &state).await;
state.templating_engine.render_view(
&settings,
&View::Ip{query: address, result: result}
).await ).await
} }
async fn get_ip_result( async fn get_ip_result(
ip_query: &IpQuery, address: &IpAddr,
lang: &String, lang: &String,
state: &ServiceSharedState, state: &ServiceSharedState,
) -> IpResult { ) -> IpResult {
let address = ip_query.ip;
let ip_info = AddressInfo::new(&address); let ip_info = AddressInfo::new(&address);
@ -412,26 +410,16 @@ async fn get_ip_result(
} }
} }
async fn handle_dig_route(
Query(dig_query): Query<DigQuery>,
Extension(settings): Extension<TemplateSettings>,
State(arc_state): State<Arc<ServiceSharedState>>,
) -> Response {
return handle_dig_request(dig_query, settings, arc_state).await
}
async fn handle_dig_route_with_path( async fn handle_dig_route_with_path(
Extension(settings): Extension<TemplateSettings>, Extension(settings): Extension<TemplateSettings>,
State(arc_state): State<Arc<ServiceSharedState>>, State(arc_state): State<Arc<ServiceSharedState>>,
extract::Path(name): extract::Path<String>, extract::Path(name): extract::Path<String>,
) -> Response { ) -> Response {
return handle_dig_request(DigQuery { return handle_dig_request(name, settings, arc_state).await
name: name,
}, settings, arc_state).await
} }
async fn handle_dig_request( async fn handle_dig_request(
dig_query: DigQuery, dig_query: String,
settings: TemplateSettings, settings: TemplateSettings,
arc_state: Arc<ServiceSharedState>, arc_state: Arc<ServiceSharedState>,
) -> Response { ) -> Response {
@ -448,10 +436,10 @@ async fn handle_dig_request(
} }
async fn get_dig_result( async fn get_dig_result(
dig_query: &DigQuery, dig_query: &String,
state: &ServiceSharedState, state: &ServiceSharedState,
) -> simple_dns::DnsLookupResult { ) -> simple_dns::DnsLookupResult {
let name = &dig_query.name.trim().trim_end_matches(".").to_string(); let name = &dig_query.trim().trim_end_matches(".").to_string();
if match_domain_hidden_list(&name, &state.config.dns.hidden_suffixes) { if match_domain_hidden_list(&name, &state.config.dns.hidden_suffixes) {
Default::default() Default::default()
} else { } else {

View File

@ -13,10 +13,10 @@ use axum::{
use tera::Tera; use tera::Tera;
use toml::Table; use toml::Table;
use std::net::IpAddr;
use crate::simple_dns; use crate::simple_dns;
use crate::IpResult; use crate::IpResult;
use crate::IpQuery;
use crate::DigQuery;
/* Response format */ /* Response format */
@ -53,9 +53,9 @@ pub struct TemplateSettings {
#[derive(serde::Deserialize, serde::Serialize, Clone)] #[derive(serde::Deserialize, serde::Serialize, Clone)]
#[serde(untagged)] #[serde(untagged)]
pub enum View { pub enum View {
Dig { query: DigQuery, result: simple_dns::DnsLookupResult }, Dig { query: String, result: simple_dns::DnsLookupResult },
Index { query: IpQuery, result: IpResult, user_agent: Option<String> }, Index { query: IpAddr, result: IpResult, user_agent: Option<String> },
Ip { query: IpQuery, result: IpResult }, Ip { query: IpAddr, result: IpResult },
Message{ title: String, message: String }, Message{ title: String, message: String },
#[serde(rename="404")] #[serde(rename="404")]
NotFound, NotFound,