mirror of
https://codeberg.org/slatian/service.echoip-slatecave.git
synced 2025-07-16 14:03:28 +02:00
All features in the config file work now 🎉
This commit is contained in:
51
src/main.rs
51
src/main.rs
@ -6,13 +6,13 @@ use axum::{
|
||||
Router,
|
||||
routing::get,
|
||||
};
|
||||
use axum_client_ip::{SecureClientIp, SecureClientIpSource};
|
||||
use axum_client_ip::SecureClientIp;
|
||||
use clap::Parser;
|
||||
use tera::Tera;
|
||||
use trust_dns_resolver::{
|
||||
TokioAsyncResolver,
|
||||
config::ResolverOpts,
|
||||
config::ResolverConfig,
|
||||
// config::ResolverOpts,
|
||||
// config::ResolverConfig,
|
||||
};
|
||||
|
||||
use std::fs;
|
||||
@ -20,10 +20,11 @@ use std::net::IpAddr;
|
||||
use std::sync::Arc;
|
||||
use std::path::Path;
|
||||
|
||||
mod config;
|
||||
mod geoip;
|
||||
mod ipinfo;
|
||||
mod simple_dns;
|
||||
mod templating_engine;
|
||||
mod geoip;
|
||||
mod config;
|
||||
|
||||
use crate::geoip::QueryAsn;
|
||||
use crate::geoip::QueryLocation;
|
||||
@ -33,6 +34,8 @@ use geoip::LocationResult;
|
||||
use crate::templating_engine::View;
|
||||
use crate::templating_engine::ResponseFormat;
|
||||
|
||||
use crate::ipinfo::{AddressCast,AddressInfo,AddressScope};
|
||||
|
||||
#[derive(serde::Deserialize, serde::Serialize)]
|
||||
pub struct BaseQuery {
|
||||
format: Option<ResponseFormat>,
|
||||
@ -57,6 +60,7 @@ pub struct IpResult {
|
||||
hostname: Option<String>,
|
||||
asn: Option<AsnResult>,
|
||||
location: Option<LocationResult>,
|
||||
ip_info: AddressInfo,
|
||||
}
|
||||
|
||||
struct ServiceSharedState {
|
||||
@ -163,7 +167,8 @@ async fn main() {
|
||||
};
|
||||
|
||||
let listen_on = config.server.listen_on;
|
||||
|
||||
let ip_header = config.server.ip_header.clone();
|
||||
|
||||
// Initialize shared state
|
||||
let shared_state = Arc::new(
|
||||
ServiceSharedState {
|
||||
@ -185,7 +190,7 @@ async fn main() {
|
||||
.route("/ip/:address", get(handle_ip_route_with_path))
|
||||
.route("/hi", get(hello_world_handler))
|
||||
.with_state(shared_state)
|
||||
.layer(SecureClientIpSource::RightmostXForwardedFor.into_extension())
|
||||
.layer(ip_header.into_extension())
|
||||
;
|
||||
|
||||
println!("Starting Server ...");
|
||||
@ -223,23 +228,7 @@ async fn handle_default_route(
|
||||
|
||||
let state = Arc::clone(&arc_state);
|
||||
|
||||
// do reverse lookup
|
||||
let hostname = simple_dns::reverse_lookup(&state.dns_resolver, &address);
|
||||
|
||||
// asn lookup
|
||||
let asn_result = state.asn_db.query_asn_for_ip(address);
|
||||
|
||||
// location lookup
|
||||
let location_result = state.location_db.query_location_for_ip(
|
||||
address,
|
||||
&vec![&ip_query.lang.as_ref().unwrap_or(&"en".to_string()), &"en".to_string()]
|
||||
);
|
||||
|
||||
let result = IpResult{
|
||||
hostname: hostname.await,
|
||||
asn: asn_result,
|
||||
location: location_result,
|
||||
};
|
||||
let result = get_ip_result(&ip_query, &state).await;
|
||||
|
||||
state.templating_engine.render_view(
|
||||
format,
|
||||
@ -287,6 +276,19 @@ async fn get_ip_result(
|
||||
) -> IpResult {
|
||||
let address = ip_query.ip;
|
||||
|
||||
let ip_info = AddressInfo::new(&address);
|
||||
|
||||
if !(ip_info.scope == AddressScope::Global || ip_info.scope == AddressScope::Shared) || ip_info.cast != AddressCast::Unicast {
|
||||
if !((ip_info.scope == AddressScope::Private || ip_info.scope == AddressScope::LinkLocal) && state.config.server.allow_private_ip_lookup) {
|
||||
return IpResult {
|
||||
hostname: None,
|
||||
asn: None,
|
||||
location: None,
|
||||
ip_info: ip_info,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// do reverse lookup
|
||||
let hostname = if state.config.dns.allow_reverse_lookup {
|
||||
simple_dns::reverse_lookup(&state.dns_resolver, &address).await
|
||||
@ -319,6 +321,7 @@ async fn get_ip_result(
|
||||
hostname: final_hostname,
|
||||
asn: asn_result,
|
||||
location: location_result,
|
||||
ip_info: ip_info,
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user