mirror of
https://codeberg.org/slatian/service.echoip-slatecave.git
synced 2025-07-17 06:23:29 +02:00
Added an opt-in to looking up own IP-Address
This commit is contained in:
42
src/main.rs
42
src/main.rs
@ -67,6 +67,7 @@ pub struct SettingsQuery {
|
||||
format: Option<ResponseFormat>,
|
||||
lang: Option<String>,
|
||||
dns: Option<String>,
|
||||
dns_self_lookup: Option<bool>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Clone)]
|
||||
@ -82,6 +83,7 @@ pub struct IpResult {
|
||||
location: Option<LocationResult>,
|
||||
ip_info: AddressInfo,
|
||||
used_dns_resolver: Option<Arc<str>>,
|
||||
reverse_dns_disabled_for_privacy: bool,
|
||||
}
|
||||
|
||||
// We need this one to hide the partial lookup field when irelevant
|
||||
@ -357,6 +359,7 @@ async fn settings_query_middleware(
|
||||
lang: query.lang.unwrap_or("en".to_string()),
|
||||
available_dns_resolvers: derived_config.dns_resolver_selectables,
|
||||
dns_resolver_id: dns_resolver_id,
|
||||
dns_disable_self_lookup: !query.dns_self_lookup.unwrap_or(false),
|
||||
});
|
||||
next.run(req).await
|
||||
}
|
||||
@ -400,7 +403,7 @@ async fn handle_default_route(
|
||||
State(arc_state): State<Arc<ServiceSharedState>>,
|
||||
Extension(settings): Extension<QuerySettings>,
|
||||
user_agent_header: Option<TypedHeader<headers::UserAgent>>,
|
||||
SecureClientIp(address): SecureClientIp
|
||||
SecureClientIp(client_ip): SecureClientIp
|
||||
) -> Response {
|
||||
|
||||
let state = Arc::clone(&arc_state);
|
||||
@ -411,12 +414,20 @@ async fn handle_default_route(
|
||||
search_query,
|
||||
false,
|
||||
settings,
|
||||
state
|
||||
state,
|
||||
&client_ip
|
||||
).await;
|
||||
}
|
||||
}
|
||||
|
||||
let result = get_ip_result(&address, &settings.lang, &"default".into(), &state).await;
|
||||
let result = get_ip_result(
|
||||
&client_ip,
|
||||
&settings.lang,
|
||||
&settings.dns_resolver_id,
|
||||
settings.dns_disable_self_lookup,
|
||||
&client_ip,
|
||||
&state,
|
||||
).await;
|
||||
|
||||
let user_agent: Option<String> = match user_agent_header {
|
||||
Some(TypedHeader(user_agent)) => Some(user_agent.to_string()),
|
||||
@ -438,6 +449,7 @@ async fn handle_search_request(
|
||||
this_should_have_been_an_ip: bool,
|
||||
settings: QuerySettings,
|
||||
arc_state: Arc<ServiceSharedState>,
|
||||
client_ip: &IpAddr,
|
||||
) -> Response {
|
||||
|
||||
let mut search_query = search_query.trim().to_string();
|
||||
@ -474,7 +486,7 @@ async fn handle_search_request(
|
||||
|
||||
// Try to interpret as an IP-Address
|
||||
if let Ok(address) = search_query.parse() {
|
||||
return handle_ip_request(address, settings, arc_state).await;
|
||||
return handle_ip_request(address, settings, arc_state, client_ip).await;
|
||||
}
|
||||
|
||||
// Fall back to treating it as a hostname
|
||||
@ -522,11 +534,12 @@ async fn handle_ip_route_with_path(
|
||||
Extension(settings): Extension<QuerySettings>,
|
||||
State(arc_state): State<Arc<ServiceSharedState>>,
|
||||
extract::Path(query): extract::Path<String>,
|
||||
SecureClientIp(client_ip): SecureClientIp
|
||||
) -> Response {
|
||||
if let Ok(address) = query.parse() {
|
||||
return handle_ip_request(address, settings, arc_state).await
|
||||
return handle_ip_request(address, settings, arc_state, &client_ip).await
|
||||
} else {
|
||||
return handle_search_request(query, true, settings, arc_state).await;
|
||||
return handle_search_request(query, true, settings, arc_state, &client_ip).await;
|
||||
}
|
||||
}
|
||||
|
||||
@ -534,6 +547,7 @@ async fn handle_ip_request(
|
||||
address: IpAddr,
|
||||
settings: QuerySettings,
|
||||
arc_state: Arc<ServiceSharedState>,
|
||||
client_ip: &IpAddr,
|
||||
) -> Response {
|
||||
|
||||
let state = Arc::clone(&arc_state);
|
||||
@ -541,6 +555,8 @@ async fn handle_ip_request(
|
||||
&address,
|
||||
&settings.lang,
|
||||
&settings.dns_resolver_id,
|
||||
settings.dns_disable_self_lookup,
|
||||
client_ip,
|
||||
&state).await;
|
||||
|
||||
state.templating_engine.render_view(
|
||||
@ -553,9 +569,19 @@ async fn get_ip_result(
|
||||
address: &IpAddr,
|
||||
lang: &String,
|
||||
dns_resolver_name: &Arc<str>,
|
||||
dns_disable_self_lookup: bool,
|
||||
client_ip: &IpAddr,
|
||||
state: &ServiceSharedState,
|
||||
) -> IpResult {
|
||||
|
||||
let mut reverse_dns_disabled_for_privacy = false;
|
||||
|
||||
if state.config.dns.allow_reverse_lookup {
|
||||
if address == client_ip && dns_disable_self_lookup {
|
||||
reverse_dns_disabled_for_privacy = true;
|
||||
}
|
||||
}
|
||||
|
||||
let ip_info = AddressInfo::new(&address);
|
||||
|
||||
if !(ip_info.scope == AddressScope::Global || ip_info.scope == AddressScope::Shared) || ip_info.cast != AddressCast::Unicast {
|
||||
@ -567,6 +593,7 @@ async fn get_ip_result(
|
||||
location: None,
|
||||
ip_info: ip_info,
|
||||
used_dns_resolver: None,
|
||||
reverse_dns_disabled_for_privacy: reverse_dns_disabled_for_privacy,
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -574,7 +601,7 @@ async fn get_ip_result(
|
||||
// do reverse lookup
|
||||
let mut hostname: Option<String> = None;
|
||||
let mut used_dns_resolver: Option<Arc<str>> = None;
|
||||
if state.config.dns.allow_reverse_lookup {
|
||||
if state.config.dns.allow_reverse_lookup && !reverse_dns_disabled_for_privacy {
|
||||
if let Some(dns_resolver) = &state.dns_resolvers.get(dns_resolver_name) {
|
||||
hostname = simple_dns::reverse_lookup(&dns_resolver, &address).await;
|
||||
used_dns_resolver = Some(dns_resolver_name.clone());
|
||||
@ -605,6 +632,7 @@ async fn get_ip_result(
|
||||
location: location_result,
|
||||
ip_info: ip_info,
|
||||
used_dns_resolver: used_dns_resolver,
|
||||
reverse_dns_disabled_for_privacy: reverse_dns_disabled_for_privacy,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -17,6 +17,7 @@ pub struct QuerySettings {
|
||||
pub lang: String,
|
||||
pub available_dns_resolvers: Vec<Selectable>,
|
||||
pub dns_resolver_id: Arc<str>,
|
||||
pub dns_disable_self_lookup: bool,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Clone)]
|
||||
|
Reference in New Issue
Block a user