mirror of
https://codeberg.org/slatian/service.echoip-slatecave.git
synced 2025-07-17 14:33:27 +02:00
Compare commits
29 Commits
Author | SHA1 | Date | |
---|---|---|---|
aaecdb84bb | |||
b08c98376c | |||
51877fc4c3 | |||
396bbdb348 | |||
a582c74d18 | |||
e8a21ac95f | |||
d706e7c614 | |||
0bffa0fd96 | |||
fb0ce1dc0b | |||
a67631fa9b | |||
636e10f786 | |||
0076db531a | |||
64e639b0df | |||
2f9f01e947 | |||
f2e9e36e99 | |||
912a119361 | |||
5adca4fb80 | |||
5ac056ef99 | |||
51aa05fe13 | |||
de179ea7fa | |||
bfa383ddbe | |||
a33473fdc9 | |||
20fb7ee2ff | |||
c5a7597561 | |||
c56cc6edbd | |||
5c74de5685 | |||
223abdd804 | |||
639d4579e9 | |||
4b3a8d5e08 |
1350
Cargo.lock
generated
1350
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
28
Cargo.toml
28
Cargo.toml
@ -1,27 +1,31 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "echoip-slatecave"
|
name = "echoip-slatecave"
|
||||||
version = "0.1.0"
|
version = "1.2.4"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
authors = ["Slatian <baschdel@disroot.org>"]
|
authors = ["Slatian <baschdel@disroot.org>"]
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
axum = { version = "0.6", features = ["macros", "headers"] }
|
lib-humus = { version="0.2", features=["axum-view+cookie"], git="https://codeberg.org/slatian/lib-humus.git" }
|
||||||
axum-extra = { version = "0.7", features = ["cookie"] }
|
|
||||||
axum-client-ip = "0.4"
|
axum = { version = "0.7", features = ["macros"] }
|
||||||
|
axum-extra = { version = "0.9", features = ["cookie", "typed-header"] }
|
||||||
|
axum-client-ip = "0.5"
|
||||||
clap = { version = "4", features = ["derive"] }
|
clap = { version = "4", features = ["derive"] }
|
||||||
governor = "0.5"
|
governor = "0.6"
|
||||||
idna = "0.3"
|
idna = "0.4"
|
||||||
lazy_static = "1.4.0"
|
lazy_static = "1.4.0"
|
||||||
parking_lot = "0.12"
|
parking_lot = "0.12"
|
||||||
regex = "1.7"
|
regex = "1.10"
|
||||||
serde = { version = "1", features = ["derive"] }
|
serde = { version = "1", features = ["derive","rc"] }
|
||||||
tokio = { version = "1", features = ["macros","signal"] }
|
tokio = { version = "1", features = ["macros","signal"] }
|
||||||
tera = "1"
|
tera = "1"
|
||||||
toml = "0.7"
|
toml = "0.8"
|
||||||
tower = "0.4"
|
tower = "0.4"
|
||||||
tower-http = { version = "0.4", features = ["fs"] }
|
tower-http = { version = "0.5", features = ["fs"] }
|
||||||
trust-dns-proto = "0.22"
|
hickory-proto = "0.24"
|
||||||
trust-dns-resolver = { version = "0.22", features = ["dns-over-rustls","dns-over-https","dns-over-quic"] }
|
hickory-resolver = { version = "0.24", features = ["dns-over-rustls","dns-over-https","dns-over-quic","native-certs"] }
|
||||||
maxminddb = "0.23"
|
maxminddb = "0.23"
|
||||||
|
mime = "0.3"
|
||||||
|
http = "1.0"
|
||||||
|
25
README.md
25
README.md
@ -34,12 +34,33 @@ A less sane, but better for testing version can be found in [echoip_test.toml](e
|
|||||||
|
|
||||||
Templates in the templates folder exist for every rich page that `echoip-slatecave` supports.
|
Templates in the templates folder exist for every rich page that `echoip-slatecave` supports.
|
||||||
|
|
||||||
The code that rendeers them can be found in [src/templating_engine.rs](src/templating_engine.rs).
|
|
||||||
|
|
||||||
There is a configuration file for templates which by default is the `extra.toml` file in the template directory. Its content is exposed to the templates in the `extra` struct.
|
There is a configuration file for templates which by default is the `extra.toml` file in the template directory. Its content is exposed to the templates in the `extra` struct.
|
||||||
|
|
||||||
The default templates should make use of everything exposed to the templating part, the `data.result` or `data` object is usually what you get when you ask for the json version.
|
The default templates should make use of everything exposed to the templating part, the `data.result` or `data` object is usually what you get when you ask for the json version.
|
||||||
|
|
||||||
|
In addition to that the following fields are accessible from inside the template:
|
||||||
|
|
||||||
|
`view`
|
||||||
|
: The views name (the basename of the template file, i.e. `404` or `ip`)
|
||||||
|
|
||||||
|
`format`
|
||||||
|
: The format name (`html`, `text`, `json`)
|
||||||
|
|
||||||
|
`mimetype`
|
||||||
|
: The resulting mimetype (i.e. `text/plain; charset=utf-8`)
|
||||||
|
|
||||||
|
`http_status`
|
||||||
|
: The numeric HTTP Status-Code at the time of rendering the template.
|
||||||
|
|
||||||
|
`language`
|
||||||
|
: The language requested by the browser.
|
||||||
|
|
||||||
|
`dns_resolvers`
|
||||||
|
: A list of [Selectable](src/settings.rs) structs representing the available DNS-Resolvers.
|
||||||
|
|
||||||
|
`dns_resolver_id`
|
||||||
|
: The id of the currently selected DNS-Resolver
|
||||||
|
|
||||||
The templates are covered by the AGPL as well, please share them with your users if you modified them.
|
The templates are covered by the AGPL as well, please share them with your users if you modified them.
|
||||||
|
|
||||||
### GeoLite2 database
|
### GeoLite2 database
|
||||||
|
@ -113,7 +113,7 @@ tls_dns_name = "cloudflare-dns.com"
|
|||||||
|
|
||||||
[dns.resolver.google]
|
[dns.resolver.google]
|
||||||
display_name = "Google"
|
display_name = "Google"
|
||||||
info_url = "https://www.cloudflare.com/dns/"
|
info_url = "https://developers.google.com/speed/public-dns/docs/using"
|
||||||
aliases = ["goo","8888"]
|
aliases = ["goo","8888"]
|
||||||
weight = 440
|
weight = 440
|
||||||
|
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
use serde::{Deserialize,Serialize};
|
use serde::{Deserialize,Serialize};
|
||||||
use trust_dns_resolver::config::Protocol;
|
use hickory_resolver::config::Protocol;
|
||||||
|
use hickory_resolver::config::ResolverConfig as HickoryResolverConfig;
|
||||||
|
use hickory_resolver::config::NameServerConfig;
|
||||||
|
|
||||||
|
use std::sync::Arc;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
|
|
||||||
@ -10,12 +13,12 @@ pub struct DnsConfig {
|
|||||||
pub allow_forward_lookup: bool,
|
pub allow_forward_lookup: bool,
|
||||||
pub allow_reverse_lookup: bool,
|
pub allow_reverse_lookup: bool,
|
||||||
pub hidden_suffixes: Vec<String>,
|
pub hidden_suffixes: Vec<String>,
|
||||||
pub resolver: HashMap<String,DnsResolverConfig>,
|
pub resolver: HashMap<Arc<str>,DnsResolverConfig>,
|
||||||
|
|
||||||
pub enable_system_resolver: bool,
|
pub enable_system_resolver: bool,
|
||||||
pub system_resolver_name: String,
|
pub system_resolver_name: Arc<str>,
|
||||||
pub system_resolver_weight: i32,
|
pub system_resolver_weight: i32,
|
||||||
pub system_resolver_id: String,
|
pub system_resolver_id: Arc<str>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize, Clone)]
|
#[derive(Deserialize, Serialize, Clone)]
|
||||||
@ -30,20 +33,20 @@ pub enum DnsProtocol {
|
|||||||
|
|
||||||
#[derive(Deserialize, Serialize, Clone)]
|
#[derive(Deserialize, Serialize, Clone)]
|
||||||
pub struct DnsResolverConfig {
|
pub struct DnsResolverConfig {
|
||||||
pub display_name: String,
|
pub display_name: Arc<str>,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub info_url: Option<String>,
|
pub info_url: Option<Arc<str>>,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub aliases: Vec<String>,
|
pub aliases: Vec<Arc<str>>,
|
||||||
#[serde(default="zero")]
|
#[serde(default="zero")]
|
||||||
pub weight: i32,
|
pub weight: i32,
|
||||||
pub servers: Vec<SocketAddr>,
|
pub servers: Vec<SocketAddr>,
|
||||||
pub protocol: DnsProtocol,
|
pub protocol: DnsProtocol,
|
||||||
pub tls_dns_name: Option<String>,
|
pub tls_dns_name: Option<Arc<str>>,
|
||||||
#[serde(skip_serializing)] //Don't leak our bind address to the outside
|
#[serde(skip_serializing)] //Don't leak our bind address to the outside
|
||||||
pub bind_address: Option<SocketAddr>,
|
pub bind_address: Option<SocketAddr>,
|
||||||
#[serde(default="default_true")]
|
#[serde(default="default_true", alias="trust_nx_responses")]
|
||||||
pub trust_nx_responses: bool,
|
pub trust_negative_responses: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn zero() -> i32 {
|
fn zero() -> i32 {
|
||||||
@ -63,9 +66,9 @@ impl Default for DnsConfig {
|
|||||||
resolver: Default::default(),
|
resolver: Default::default(),
|
||||||
|
|
||||||
enable_system_resolver: true,
|
enable_system_resolver: true,
|
||||||
system_resolver_name: "System".to_string(),
|
system_resolver_name: "System".into(),
|
||||||
system_resolver_weight: 1000,
|
system_resolver_weight: 1000,
|
||||||
system_resolver_id: "system".to_string(),
|
system_resolver_id: "system".into(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -85,14 +88,14 @@ impl Into<Protocol> for DnsProtocol {
|
|||||||
impl DnsResolverConfig {
|
impl DnsResolverConfig {
|
||||||
pub fn to_trust_resolver_config(
|
pub fn to_trust_resolver_config(
|
||||||
&self
|
&self
|
||||||
) -> trust_dns_resolver::config::ResolverConfig {
|
) -> HickoryResolverConfig {
|
||||||
let mut resolver = trust_dns_resolver::config::ResolverConfig::new();
|
let mut resolver = HickoryResolverConfig::new();
|
||||||
for server in &self.servers {
|
for server in &self.servers {
|
||||||
resolver.add_name_server(trust_dns_resolver::config::NameServerConfig{
|
resolver.add_name_server(NameServerConfig{
|
||||||
socket_addr: *server,
|
socket_addr: *server,
|
||||||
protocol: self.protocol.clone().into(),
|
protocol: self.protocol.clone().into(),
|
||||||
tls_dns_name: self.tls_dns_name.clone(),
|
tls_dns_name: self.tls_dns_name.clone().map(|s| s.to_string()),
|
||||||
trust_nx_responses: self.trust_nx_responses,
|
trust_negative_responses: self.trust_negative_responses,
|
||||||
tls_config: None,
|
tls_config: None,
|
||||||
bind_addr: self.bind_address,
|
bind_addr: self.bind_address,
|
||||||
});
|
});
|
||||||
|
204
src/main.rs
204
src/main.rs
@ -1,29 +1,29 @@
|
|||||||
use axum::{
|
use axum::{
|
||||||
|
body::Body,
|
||||||
extract::{
|
extract::{
|
||||||
self,
|
self,
|
||||||
Query,
|
Query,
|
||||||
State,
|
State,
|
||||||
Extension,
|
Extension,
|
||||||
},
|
},
|
||||||
headers,
|
|
||||||
http::Request,
|
|
||||||
handler::Handler,
|
handler::Handler,
|
||||||
|
http::Request,
|
||||||
middleware::{self, Next},
|
middleware::{self, Next},
|
||||||
response::Response,
|
response::Response,
|
||||||
Router,
|
Router,
|
||||||
routing::get,
|
routing::get,
|
||||||
TypedHeader,
|
|
||||||
};
|
};
|
||||||
use axum_client_ip::SecureClientIp;
|
use axum_client_ip::SecureClientIp;
|
||||||
|
use axum_extra::headers;
|
||||||
|
use axum_extra::TypedHeader;
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use serde::{Deserialize,Serialize};
|
use serde::{Deserialize,Serialize};
|
||||||
use tera::Tera;
|
|
||||||
use tower::ServiceBuilder;
|
use tower::ServiceBuilder;
|
||||||
use tower_http::services::ServeDir;
|
use tower_http::services::ServeDir;
|
||||||
use trust_dns_resolver::Name;
|
use hickory_resolver::Name;
|
||||||
use trust_dns_resolver::TokioAsyncResolver;
|
use hickory_resolver::TokioAsyncResolver;
|
||||||
|
|
||||||
use tokio::signal::unix::{
|
use tokio::signal::unix::{
|
||||||
signal,
|
signal,
|
||||||
@ -32,18 +32,21 @@ use tokio::signal::unix::{
|
|||||||
use tokio::task;
|
use tokio::task;
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fs;
|
|
||||||
use std::net::IpAddr;
|
use std::net::IpAddr;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use lib_humus::TemplateEngineLoader;
|
||||||
|
use lib_humus::read_toml_from_file;
|
||||||
|
use lib_humus::HumusEngine;
|
||||||
|
|
||||||
mod config;
|
mod config;
|
||||||
mod geoip;
|
mod geoip;
|
||||||
|
mod idna;
|
||||||
mod ipinfo;
|
mod ipinfo;
|
||||||
mod ratelimit;
|
mod ratelimit;
|
||||||
mod settings;
|
mod settings;
|
||||||
mod simple_dns;
|
mod simple_dns;
|
||||||
mod templating_engine;
|
mod view;
|
||||||
mod idna;
|
|
||||||
|
|
||||||
use crate::geoip::{
|
use crate::geoip::{
|
||||||
QueryAsn,
|
QueryAsn,
|
||||||
@ -54,9 +57,11 @@ use crate::geoip::{
|
|||||||
use crate::idna::IdnaName;
|
use crate::idna::IdnaName;
|
||||||
use crate::simple_dns::DnsLookupResult;
|
use crate::simple_dns::DnsLookupResult;
|
||||||
use crate::settings::*;
|
use crate::settings::*;
|
||||||
use crate::templating_engine::View;
|
use crate::view::View;
|
||||||
use crate::ipinfo::{AddressCast,AddressInfo,AddressScope};
|
use crate::ipinfo::{AddressCast,AddressInfo,AddressScope};
|
||||||
|
|
||||||
|
type TemplatingEngine = HumusEngine<View,QuerySettings,ResponseFormat>;
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize, Clone)]
|
#[derive(Deserialize, Serialize, Clone)]
|
||||||
pub struct SettingsQuery {
|
pub struct SettingsQuery {
|
||||||
format: Option<ResponseFormat>,
|
format: Option<ResponseFormat>,
|
||||||
@ -76,26 +81,26 @@ pub struct IpResult {
|
|||||||
asn: Option<AsnResult>,
|
asn: Option<AsnResult>,
|
||||||
location: Option<LocationResult>,
|
location: Option<LocationResult>,
|
||||||
ip_info: AddressInfo,
|
ip_info: AddressInfo,
|
||||||
used_dns_resolver: Option<String>,
|
used_dns_resolver: Option<Arc<str>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
// We need this one to hide the partial lookup field when irelevant
|
// We need this one to hide the partial lookup field when irelevant
|
||||||
pub fn not(b: &bool) -> bool { !b }
|
pub fn not(b: &bool) -> bool { !b }
|
||||||
|
|
||||||
#[derive(Serialize, Default, Clone)]
|
#[derive(Serialize, Clone)]
|
||||||
pub struct DigResult {
|
pub struct DigResult {
|
||||||
records: simple_dns::DnsLookupResult,
|
records: simple_dns::DnsLookupResult,
|
||||||
#[serde(skip_serializing_if = "IdnaName::was_ascii")]
|
#[serde(skip_serializing_if = "IdnaName::was_ascii")]
|
||||||
idn: IdnaName,
|
idn: IdnaName,
|
||||||
#[serde(skip_serializing_if = "not")]
|
#[serde(skip_serializing_if = "not")]
|
||||||
partial_lookup: bool,
|
partial_lookup: bool,
|
||||||
used_dns_resolver: String,
|
used_dns_resolver: Arc<str>,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ServiceSharedState {
|
struct ServiceSharedState {
|
||||||
templating_engine: templating_engine::Engine,
|
templating_engine: TemplatingEngine,
|
||||||
dns_resolvers: HashMap<String,TokioAsyncResolver>,
|
dns_resolvers: HashMap<Arc<str>,TokioAsyncResolver>,
|
||||||
dns_resolver_aliases: HashMap<String,String>,
|
dns_resolver_aliases: HashMap<Arc<str>,Arc<str>>,
|
||||||
asn_db: geoip::MMDBCarrier,
|
asn_db: geoip::MMDBCarrier,
|
||||||
location_db: geoip::MMDBCarrier,
|
location_db: geoip::MMDBCarrier,
|
||||||
config: config::EchoIpServiceConfig,
|
config: config::EchoIpServiceConfig,
|
||||||
@ -105,7 +110,7 @@ struct ServiceSharedState {
|
|||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct DerivedConfiguration {
|
struct DerivedConfiguration {
|
||||||
dns_resolver_selectables: Vec<Selectable>,
|
dns_resolver_selectables: Vec<Selectable>,
|
||||||
default_resolver: String,
|
default_resolver: Arc<str>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
@ -133,23 +138,6 @@ fn match_domain_hidden_list(domain: &String, hidden_list: &Vec<String>) -> bool
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_toml_from_file<T: for<'de> serde::Deserialize<'de>>(path: &String) -> Option<T> {
|
|
||||||
let text = match fs::read_to_string(path) {
|
|
||||||
Ok(t) => t,
|
|
||||||
Err(e) => {
|
|
||||||
println!("Error while reading file '{path}': {e}");
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
match toml::from_str(&text) {
|
|
||||||
Ok(t) => Some(t),
|
|
||||||
Err(e) => {
|
|
||||||
println!("Unable to parse file '{path}':\n{e}");
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
// Parse Command line arguments
|
// Parse Command line arguments
|
||||||
@ -159,9 +147,11 @@ async fn main() {
|
|||||||
let config: config::EchoIpServiceConfig = match cli_args.config {
|
let config: config::EchoIpServiceConfig = match cli_args.config {
|
||||||
Some(config_path) => {
|
Some(config_path) => {
|
||||||
match read_toml_from_file::<config::EchoIpServiceConfig>(&config_path) {
|
match read_toml_from_file::<config::EchoIpServiceConfig>(&config_path) {
|
||||||
Some(c) => c,
|
Ok(c) => c,
|
||||||
None => {
|
Err(e) => {
|
||||||
println!("Could not read confuration file, exiting.");
|
println!("Could not read confuration file!");
|
||||||
|
println!("{e}");
|
||||||
|
println!("Exiting ...");
|
||||||
::std::process::exit(1);
|
::std::process::exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -170,47 +160,25 @@ async fn main() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Initalize Tera templates
|
// Initalize Tera templates
|
||||||
let mut template_base_dir = match cli_args.template_location {
|
let template_loader = TemplateEngineLoader::new(
|
||||||
Some(template_base_dir) => template_base_dir,
|
config.template.template_location.clone(),
|
||||||
None => (&config.template.template_location).to_owned(),
|
config.template.extra_config.clone()
|
||||||
};
|
)
|
||||||
if !template_base_dir.ends_with("/") {
|
.cli_template_location(cli_args.template_location)
|
||||||
template_base_dir = template_base_dir + "/";
|
.cli_extra_config_location(cli_args.extra_config);
|
||||||
}
|
|
||||||
let template_extra_config = match &cli_args.extra_config {
|
|
||||||
Some(path) => read_toml_from_file(path),
|
let templating_engine = match template_loader.load_templates() {
|
||||||
None => match &config.template.extra_config {
|
Ok(t) => t.into(),
|
||||||
Some(path) => read_toml_from_file(path),
|
|
||||||
None => {
|
|
||||||
println!("Trying to read default template configuration ...");
|
|
||||||
println!("(If this fails that may be ok, depending on your template)");
|
|
||||||
read_toml_from_file(&(template_base_dir.clone()+"extra.toml"))
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
let template_glob = template_base_dir.clone()+"*";
|
|
||||||
println!("Parsing Templates from '{}' ...", &template_glob);
|
|
||||||
let res = Tera::new((template_glob).as_str());
|
|
||||||
let tera = match res {
|
|
||||||
Ok(t) => t,
|
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
println!("Template parsing error(s): {}", e);
|
println!("{e}");
|
||||||
::std::process::exit(1);
|
::std::process::exit(1);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let templating_engine = templating_engine::Engine{
|
|
||||||
tera: tera,
|
|
||||||
template_config: template_extra_config,
|
|
||||||
};
|
|
||||||
|
|
||||||
// Static file directory
|
// Static file directory
|
||||||
|
|
||||||
let static_file_directory = cli_args.static_location.unwrap_or(
|
let static_file_directory = template_loader.base_dir()+"/static";
|
||||||
config.server.static_location.clone().unwrap_or(
|
|
||||||
template_base_dir+"/static"
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
println!("Static files will be served from: {static_file_directory}");
|
println!("Static files will be served from: {static_file_directory}");
|
||||||
|
|
||||||
@ -234,8 +202,8 @@ async fn main() {
|
|||||||
println!("Initalizing dns resolvers ...");
|
println!("Initalizing dns resolvers ...");
|
||||||
|
|
||||||
let mut dns_resolver_selectables = Vec::<Selectable>::new();
|
let mut dns_resolver_selectables = Vec::<Selectable>::new();
|
||||||
let mut dns_resolver_map: HashMap<String,TokioAsyncResolver> = HashMap::new();
|
let mut dns_resolver_map: HashMap<Arc<str>,TokioAsyncResolver> = HashMap::new();
|
||||||
let mut dns_resolver_aliases: HashMap<String,String> = HashMap::new();
|
let mut dns_resolver_aliases: HashMap<Arc<str>,Arc<str>> = HashMap::new();
|
||||||
|
|
||||||
if config.dns.enable_system_resolver {
|
if config.dns.enable_system_resolver {
|
||||||
println!("Initalizing System resolver ...");
|
println!("Initalizing System resolver ...");
|
||||||
@ -261,7 +229,7 @@ async fn main() {
|
|||||||
let resolver = TokioAsyncResolver::tokio(
|
let resolver = TokioAsyncResolver::tokio(
|
||||||
resolver_config.to_trust_resolver_config(),
|
resolver_config.to_trust_resolver_config(),
|
||||||
Default::default()
|
Default::default()
|
||||||
).unwrap();
|
);
|
||||||
dns_resolver_map.insert(key.clone(), resolver);
|
dns_resolver_map.insert(key.clone(), resolver);
|
||||||
dns_resolver_selectables.push(Selectable {
|
dns_resolver_selectables.push(Selectable {
|
||||||
id: key.clone(),
|
id: key.clone(),
|
||||||
@ -290,7 +258,7 @@ async fn main() {
|
|||||||
dns_resolver_selectables.sort_by(|a,b| b.weight.cmp(&a.weight));
|
dns_resolver_selectables.sort_by(|a,b| b.weight.cmp(&a.weight));
|
||||||
let default_resolver = dns_resolver_selectables.get(0)
|
let default_resolver = dns_resolver_selectables.get(0)
|
||||||
.map(|s| s.id.clone() )
|
.map(|s| s.id.clone() )
|
||||||
.unwrap_or("none".to_string());
|
.unwrap_or("none".into());
|
||||||
let derived_config = DerivedConfiguration {
|
let derived_config = DerivedConfiguration {
|
||||||
dns_resolver_selectables: dns_resolver_selectables,
|
dns_resolver_selectables: dns_resolver_selectables,
|
||||||
default_resolver: default_resolver,
|
default_resolver: default_resolver,
|
||||||
@ -344,30 +312,30 @@ async fn main() {
|
|||||||
|
|
||||||
println!("Starting Server on {} ...",listen_on);
|
println!("Starting Server on {} ...",listen_on);
|
||||||
|
|
||||||
axum::Server::bind(&listen_on)
|
let listener = tokio::net::TcpListener::bind(&listen_on).await.unwrap();
|
||||||
.serve(app.into_make_service_with_connect_info::<std::net::SocketAddr>())
|
axum::serve(listener, app.into_make_service_with_connect_info::<std::net::SocketAddr>())
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
async fn settings_query_middleware<B>(
|
async fn settings_query_middleware(
|
||||||
Query(query): Query<SettingsQuery>,
|
Query(query): Query<SettingsQuery>,
|
||||||
Extension(config): Extension<config::EchoIpServiceConfig>,
|
Extension(config): Extension<config::EchoIpServiceConfig>,
|
||||||
Extension(derived_config): Extension<DerivedConfiguration>,
|
Extension(derived_config): Extension<DerivedConfiguration>,
|
||||||
cookie_header: Option<TypedHeader<headers::Cookie>>,
|
cookie_header: Option<TypedHeader<headers::Cookie>>,
|
||||||
user_agent_header: Option<TypedHeader<headers::UserAgent>>,
|
user_agent_header: Option<TypedHeader<headers::UserAgent>>,
|
||||||
mut req: Request<B>,
|
mut req: Request<Body>,
|
||||||
next: Next<B>
|
next: Next
|
||||||
) -> Response {
|
) -> Response {
|
||||||
let mut format = query.format;
|
let mut format = query.format;
|
||||||
let mut dns_resolver_id = derived_config.default_resolver;
|
let mut dns_resolver_id = derived_config.default_resolver;
|
||||||
|
|
||||||
if let Some(resolver_id) = query.dns {
|
if let Some(resolver_id) = query.dns {
|
||||||
dns_resolver_id = resolver_id;
|
dns_resolver_id = resolver_id.into();
|
||||||
} else if let Some(cookie_header) = cookie_header {
|
} else if let Some(cookie_header) = cookie_header {
|
||||||
if let Some(resolver_id) = cookie_header.0.get("dns_resolver") {
|
if let Some(resolver_id) = cookie_header.0.get("dns_resolver") {
|
||||||
dns_resolver_id = resolver_id.to_string();
|
dns_resolver_id = resolver_id.into();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -377,7 +345,7 @@ async fn settings_query_middleware<B>(
|
|||||||
let ua = user_agent.as_str();
|
let ua = user_agent.as_str();
|
||||||
for tua in config.template.text_user_agents {
|
for tua in config.template.text_user_agents {
|
||||||
if ua.starts_with(&tua) {
|
if ua.starts_with(&tua) {
|
||||||
format = Some(ResponseFormat::TextPlain);
|
format = Some(ResponseFormat::Text);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -385,7 +353,7 @@ async fn settings_query_middleware<B>(
|
|||||||
}
|
}
|
||||||
// Add the request settings extension
|
// Add the request settings extension
|
||||||
req.extensions_mut().insert(QuerySettings{
|
req.extensions_mut().insert(QuerySettings{
|
||||||
format: format.unwrap_or(ResponseFormat::TextHtml),
|
format: format.unwrap_or(ResponseFormat::Html),
|
||||||
lang: query.lang.unwrap_or("en".to_string()),
|
lang: query.lang.unwrap_or("en".to_string()),
|
||||||
available_dns_resolvers: derived_config.dns_resolver_selectables,
|
available_dns_resolvers: derived_config.dns_resolver_selectables,
|
||||||
dns_resolver_id: dns_resolver_id,
|
dns_resolver_id: dns_resolver_id,
|
||||||
@ -401,8 +369,8 @@ async fn not_found_handler(
|
|||||||
|
|
||||||
state.templating_engine.render_view(
|
state.templating_engine.render_view(
|
||||||
&settings,
|
&settings,
|
||||||
&View::NotFound,
|
View::NotFound,
|
||||||
).await
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn hello_world_handler(
|
async fn hello_world_handler(
|
||||||
@ -413,11 +381,11 @@ async fn hello_world_handler(
|
|||||||
|
|
||||||
state.templating_engine.render_view(
|
state.templating_engine.render_view(
|
||||||
&settings,
|
&settings,
|
||||||
&View::Message{
|
View::Message{
|
||||||
title: "Hey There!".to_string(),
|
title: "Hey There!".to_string(),
|
||||||
message: "You,You are an awesome Creature!".to_string()
|
message: "You are an awesome Creature!".to_string()
|
||||||
},
|
},
|
||||||
).await
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -448,7 +416,7 @@ async fn handle_default_route(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let result = get_ip_result(&address, &settings.lang, &"default".to_string(), &state).await;
|
let result = get_ip_result(&address, &settings.lang, &"default".into(), &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()),
|
||||||
@ -457,11 +425,11 @@ async fn handle_default_route(
|
|||||||
|
|
||||||
state.templating_engine.render_view(
|
state.templating_engine.render_view(
|
||||||
&settings,
|
&settings,
|
||||||
&View::Index{
|
View::Index{
|
||||||
result: result,
|
result: result,
|
||||||
user_agent: user_agent,
|
user_agent: user_agent,
|
||||||
}
|
}
|
||||||
).await
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -487,17 +455,17 @@ async fn handle_search_request(
|
|||||||
let state = Arc::clone(&arc_state);
|
let state = Arc::clone(&arc_state);
|
||||||
return state.templating_engine.render_view(
|
return state.templating_engine.render_view(
|
||||||
&settings,
|
&settings,
|
||||||
&View::Asn{asn: asn},
|
View::Asn{asn: asn},
|
||||||
).await
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(via_cap) = VIA_REGEX.captures(&search_query) {
|
if let Some(via_cap) = VIA_REGEX.captures(&search_query) {
|
||||||
if let Some(via) = via_cap.get(1).map(|c| c.as_str().to_string()) {
|
if let Some(via) = via_cap.get(1) {
|
||||||
let state = Arc::clone(&arc_state);
|
let state = Arc::clone(&arc_state);
|
||||||
if state.dns_resolvers.contains_key(&via) {
|
if state.dns_resolvers.contains_key(via.as_str()) {
|
||||||
settings.dns_resolver_id = via;
|
settings.dns_resolver_id = via.as_str().into();
|
||||||
} else if let Some(alias) = state.dns_resolver_aliases.get(&via) {
|
} else if let Some(alias) = state.dns_resolver_aliases.get(via.as_str()) {
|
||||||
settings.dns_resolver_id = alias.clone();
|
settings.dns_resolver_id = alias.clone();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -526,8 +494,8 @@ async fn handle_dns_resolver_route(
|
|||||||
let state = Arc::clone(&arc_state);
|
let state = Arc::clone(&arc_state);
|
||||||
state.templating_engine.render_view(
|
state.templating_engine.render_view(
|
||||||
&settings,
|
&settings,
|
||||||
&View::DnsResolverList,
|
View::DnsResolverList,
|
||||||
).await
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -537,16 +505,16 @@ async fn handle_dns_resolver_route_with_path(
|
|||||||
extract::Path(query): extract::Path<String>,
|
extract::Path(query): extract::Path<String>,
|
||||||
) -> Response {
|
) -> Response {
|
||||||
let state = Arc::clone(&arc_state);
|
let state = Arc::clone(&arc_state);
|
||||||
if let Some(resolver) = state.config.dns.resolver.get(&query) {
|
if let Some(resolver) = state.config.dns.resolver.get(query.as_str()) {
|
||||||
state.templating_engine.render_view(
|
state.templating_engine.render_view(
|
||||||
&settings,
|
&settings,
|
||||||
&View::DnsResolver{ config: resolver.clone() },
|
View::DnsResolver{ config: resolver.clone() },
|
||||||
).await
|
)
|
||||||
} else {
|
} else {
|
||||||
state.templating_engine.render_view(
|
state.templating_engine.render_view(
|
||||||
&settings,
|
&settings,
|
||||||
&View::NotFound,
|
View::NotFound,
|
||||||
).await
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -577,14 +545,14 @@ async fn handle_ip_request(
|
|||||||
|
|
||||||
state.templating_engine.render_view(
|
state.templating_engine.render_view(
|
||||||
&settings,
|
&settings,
|
||||||
&View::Ip{result: result}
|
View::Ip{result: result}
|
||||||
).await
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_ip_result(
|
async fn get_ip_result(
|
||||||
address: &IpAddr,
|
address: &IpAddr,
|
||||||
lang: &String,
|
lang: &String,
|
||||||
dns_resolver_name: &String,
|
dns_resolver_name: &Arc<str>,
|
||||||
state: &ServiceSharedState,
|
state: &ServiceSharedState,
|
||||||
) -> IpResult {
|
) -> IpResult {
|
||||||
|
|
||||||
@ -605,7 +573,7 @@ async fn get_ip_result(
|
|||||||
|
|
||||||
// do reverse lookup
|
// do reverse lookup
|
||||||
let mut hostname: Option<String> = None;
|
let mut hostname: Option<String> = None;
|
||||||
let mut used_dns_resolver: 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 {
|
||||||
if let Some(dns_resolver) = &state.dns_resolvers.get(dns_resolver_name) {
|
if let Some(dns_resolver) = &state.dns_resolvers.get(dns_resolver_name) {
|
||||||
hostname = simple_dns::reverse_lookup(&dns_resolver, &address).await;
|
hostname = simple_dns::reverse_lookup(&dns_resolver, &address).await;
|
||||||
@ -666,20 +634,20 @@ async fn handle_dig_request(
|
|||||||
|
|
||||||
state.templating_engine.render_view(
|
state.templating_engine.render_view(
|
||||||
&settings,
|
&settings,
|
||||||
&View::Dig{ query: dig_query, result: dig_result}
|
View::Dig{ query: dig_query, result: dig_result}
|
||||||
).await
|
)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_dig_result(
|
async fn get_dig_result(
|
||||||
dig_query: &String,
|
dig_query: &String,
|
||||||
dns_resolver_name: &String,
|
dns_resolver_name: &Arc<str>,
|
||||||
state: &ServiceSharedState,
|
state: &ServiceSharedState,
|
||||||
do_full_lookup: bool,
|
do_full_lookup: bool,
|
||||||
) -> DigResult {
|
) -> DigResult {
|
||||||
let name = &dig_query.trim().trim_end_matches(".").to_string();
|
let name = &dig_query.trim().trim_end_matches(".").to_string();
|
||||||
let idna_name = IdnaName::from_string(&name);
|
let idna_name = IdnaName::from_string(&name);
|
||||||
if let Some(dns_resolver) = &state.dns_resolvers.get(dns_resolver_name) {
|
if let Some(dns_resolver) = state.dns_resolvers.get(dns_resolver_name) {
|
||||||
if let Ok(domain_name) = Name::from_str_relaxed(name.to_owned()+".") {
|
if let Ok(domain_name) = Name::from_str_relaxed(name.to_owned()+".") {
|
||||||
if match_domain_hidden_list(&name, &state.config.dns.hidden_suffixes) {
|
if match_domain_hidden_list(&name, &state.config.dns.hidden_suffixes) {
|
||||||
// Try to hide the fact that we didn't do dns resolution at all
|
// Try to hide the fact that we didn't do dns resolution at all
|
||||||
@ -707,21 +675,27 @@ async fn get_dig_result(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
// Invalid domain name
|
||||||
return DigResult {
|
return DigResult {
|
||||||
records: DnsLookupResult{
|
records: DnsLookupResult{
|
||||||
invalid_name: true,
|
invalid_name: true,
|
||||||
.. Default::default()
|
.. Default::default()
|
||||||
},
|
},
|
||||||
.. Default::default()
|
idn: idna_name,
|
||||||
|
partial_lookup: !do_full_lookup,
|
||||||
|
used_dns_resolver: dns_resolver_name.clone(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
// Unknown resolver name
|
||||||
return DigResult {
|
return DigResult {
|
||||||
records: DnsLookupResult{
|
records: DnsLookupResult{
|
||||||
unkown_resolver: true,
|
unkown_resolver: true,
|
||||||
.. Default::default()
|
.. Default::default()
|
||||||
},
|
},
|
||||||
.. Default::default()
|
idn: idna_name,
|
||||||
|
partial_lookup: !do_full_lookup,
|
||||||
|
used_dns_resolver: "unkown_resolver".into(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
use axum_client_ip::SecureClientIp;
|
use axum_client_ip::SecureClientIp;
|
||||||
use axum::{
|
use axum::{
|
||||||
|
body::Body,
|
||||||
extract::Extension,
|
extract::Extension,
|
||||||
http::{
|
http::{
|
||||||
Request,
|
Request,
|
||||||
@ -40,11 +41,11 @@ pub fn build_rate_limiting_state(
|
|||||||
Extension(arc_limiter)
|
Extension(arc_limiter)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn rate_limit_middleware<B>(
|
pub async fn rate_limit_middleware(
|
||||||
SecureClientIp(address): SecureClientIp,
|
SecureClientIp(address): SecureClientIp,
|
||||||
Extension(arc_limiter): Extension<Arc<SimpleRateLimiter<IpAddr>>>,
|
Extension(arc_limiter): Extension<Arc<SimpleRateLimiter<IpAddr>>>,
|
||||||
req: Request<B>,
|
req: Request<Body>,
|
||||||
next: Next<B>
|
next: Next
|
||||||
) -> Response {
|
) -> Response {
|
||||||
let limiter = Arc::clone(&arc_limiter);
|
let limiter = Arc::clone(&arc_limiter);
|
||||||
|
|
||||||
|
@ -1,36 +1,13 @@
|
|||||||
use serde::{Deserialize,Serialize};
|
use serde::{Deserialize,Serialize};
|
||||||
|
|
||||||
|
use lib_humus::HtmlTextJsonFormat;
|
||||||
|
use lib_humus::HumusQuerySettings;
|
||||||
|
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
/* Response format */
|
/* Response format */
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize, Clone, Copy)]
|
pub type ResponseFormat = HtmlTextJsonFormat;
|
||||||
pub enum ResponseFormat {
|
|
||||||
#[serde(rename="text/plain", alias="text")]
|
|
||||||
TextPlain,
|
|
||||||
#[serde(rename="text/html", alias="html")]
|
|
||||||
TextHtml,
|
|
||||||
#[serde(rename="application/json", alias="json")]
|
|
||||||
ApplicationJson,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ToString for ResponseFormat {
|
|
||||||
fn to_string(&self) -> String {
|
|
||||||
match self {
|
|
||||||
ResponseFormat::TextPlain => "text/plain",
|
|
||||||
ResponseFormat::TextHtml => "text/html",
|
|
||||||
ResponseFormat::ApplicationJson => "application/json",
|
|
||||||
}.to_string()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ResponseFormat {
|
|
||||||
pub fn to_file_extension(&self) -> String {
|
|
||||||
match self {
|
|
||||||
ResponseFormat::TextPlain => ".txt",
|
|
||||||
ResponseFormat::TextHtml => ".html",
|
|
||||||
ResponseFormat::ApplicationJson => ".json",
|
|
||||||
}.to_string()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Query and Template Settings */
|
/* Query and Template Settings */
|
||||||
|
|
||||||
@ -39,13 +16,26 @@ pub struct QuerySettings {
|
|||||||
pub format: ResponseFormat,
|
pub format: ResponseFormat,
|
||||||
pub lang: String,
|
pub lang: String,
|
||||||
pub available_dns_resolvers: Vec<Selectable>,
|
pub available_dns_resolvers: Vec<Selectable>,
|
||||||
pub dns_resolver_id: String,
|
pub dns_resolver_id: Arc<str>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize, Clone)]
|
#[derive(Deserialize, Serialize, Clone)]
|
||||||
pub struct Selectable {
|
pub struct Selectable {
|
||||||
pub id: String,
|
pub id: Arc<str>,
|
||||||
pub name: String,
|
pub name: Arc<str>,
|
||||||
pub weight: i32,
|
pub weight: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl HumusQuerySettings<ResponseFormat> for QuerySettings {
|
||||||
|
|
||||||
|
fn initalize_template_context(&self, context: &mut tera::Context) {
|
||||||
|
context.insert("language", &self.lang);
|
||||||
|
context.insert("dns_resolvers", &self.available_dns_resolvers);
|
||||||
|
context.insert("dns_resolver_id", &self.dns_resolver_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_format(&self) -> ResponseFormat {
|
||||||
|
self.format.clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
@ -1,17 +1,16 @@
|
|||||||
/*
|
|
||||||
* This module wraps the trust_dns_resolver library
|
|
||||||
* to generate results thaat are ready for serializing
|
|
||||||
* or templating.
|
|
||||||
* It does not aim to be reusable for any other purpose,
|
|
||||||
* the trust_dns_resolver library already does that.
|
|
||||||
*/
|
|
||||||
|
|
||||||
use trust_dns_proto::op::response_code::ResponseCode;
|
//! This module wraps the hickory_resolver library
|
||||||
use trust_dns_proto::rr::{
|
//! to generate results thaat are ready for serializing
|
||||||
|
//! or templating.
|
||||||
|
//! It does not aim to be reusable for any other purpose,
|
||||||
|
//! the hickory_resolver library already does that.
|
||||||
|
|
||||||
|
use hickory_proto::op::response_code::ResponseCode;
|
||||||
|
use hickory_proto::rr::{
|
||||||
RData,
|
RData,
|
||||||
record_type::RecordType,
|
record_type::RecordType,
|
||||||
};
|
};
|
||||||
use trust_dns_resolver::{
|
use hickory_resolver::{
|
||||||
error::ResolveError,
|
error::ResolveError,
|
||||||
error::ResolveErrorKind,
|
error::ResolveErrorKind,
|
||||||
lookup::Lookup,
|
lookup::Lookup,
|
||||||
@ -121,9 +120,9 @@ pub fn set_default_if_none<T>(opt_vec: &mut Option<Vec<T>>) {
|
|||||||
|
|
||||||
pub fn add_record_to_lookup_result(result: &mut DnsLookupResult, record: &RData){
|
pub fn add_record_to_lookup_result(result: &mut DnsLookupResult, record: &RData){
|
||||||
match record {
|
match record {
|
||||||
RData::AAAA(address) => opush(&mut result.aaaa, std::net::IpAddr::V6(*address)),
|
RData::AAAA(aaaa) => opush(&mut result.aaaa, std::net::IpAddr::V6(aaaa.0)),
|
||||||
RData::ANAME(aname) => opush(&mut result.aname, aname.to_string()),
|
RData::ANAME(aname) => opush(&mut result.aname, aname.to_string()),
|
||||||
RData::A(address) => opush(&mut result.a, std::net::IpAddr::V4(*address)),
|
RData::A(a) => opush(&mut result.a, std::net::IpAddr::V4(a.0)),
|
||||||
RData::CAA(caa) => opush(&mut result.caa, caa.to_string()),
|
RData::CAA(caa) => opush(&mut result.caa, caa.to_string()),
|
||||||
RData::CNAME(cname) => opush(&mut result.cname, cname.to_string()),
|
RData::CNAME(cname) => opush(&mut result.cname, cname.to_string()),
|
||||||
RData::MX(mx) => opush(&mut result.mx, MxRecord{
|
RData::MX(mx) => opush(&mut result.mx, MxRecord{
|
||||||
|
@ -1,135 +0,0 @@
|
|||||||
/*
|
|
||||||
* This is the echoip-slatecave templating engine.
|
|
||||||
* It wraps around tera in is specialized for echoip-slatecave.
|
|
||||||
*/
|
|
||||||
|
|
||||||
use axum::{
|
|
||||||
headers::HeaderValue,
|
|
||||||
http::StatusCode,
|
|
||||||
http::header::SET_COOKIE,
|
|
||||||
response::Html,
|
|
||||||
response::IntoResponse,
|
|
||||||
response::Response,
|
|
||||||
response::Json,
|
|
||||||
};
|
|
||||||
use axum_extra::extract::cookie::Cookie;
|
|
||||||
use axum_extra::extract::cookie;
|
|
||||||
use tera::Tera;
|
|
||||||
use toml::Table;
|
|
||||||
|
|
||||||
use crate::DigResult;
|
|
||||||
use crate::IpResult;
|
|
||||||
use crate::config::DnsResolverConfig;
|
|
||||||
use crate::settings::*;
|
|
||||||
|
|
||||||
|
|
||||||
/* The echoip view */
|
|
||||||
|
|
||||||
#[derive(serde::Serialize, Clone)]
|
|
||||||
#[serde(untagged)]
|
|
||||||
pub enum View {
|
|
||||||
Asn { asn: u32 },
|
|
||||||
Dig { query: String, result: DigResult },
|
|
||||||
DnsResolver{ config: DnsResolverConfig },
|
|
||||||
DnsResolverList,
|
|
||||||
Index { result: IpResult, user_agent: Option<String> },
|
|
||||||
Ip { result: IpResult },
|
|
||||||
Message{ title: String, message: String },
|
|
||||||
#[serde(rename="404")]
|
|
||||||
NotFound,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl View {
|
|
||||||
pub fn template_name(&self) -> String {
|
|
||||||
match self {
|
|
||||||
View::Asn{..} => "asn",
|
|
||||||
View::Dig{..} => "dig",
|
|
||||||
View::DnsResolver{..} => "dns_resolver",
|
|
||||||
View::DnsResolverList => "dns_resolver_list",
|
|
||||||
View::Index{..} => "index",
|
|
||||||
View::Ip{..} => "ip",
|
|
||||||
View::Message{..} => "message",
|
|
||||||
View::NotFound => "404",
|
|
||||||
}.to_string()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* The engine itself */
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct Engine {
|
|
||||||
pub tera: Tera,
|
|
||||||
pub template_config: Option<Table>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Engine {
|
|
||||||
pub async fn render_view(
|
|
||||||
&self,
|
|
||||||
settings: &QuerySettings,
|
|
||||||
view: &View,
|
|
||||||
) -> Response {
|
|
||||||
let mut response = match settings.format {
|
|
||||||
ResponseFormat::TextHtml | ResponseFormat::TextPlain => {
|
|
||||||
let template_name = view.template_name();
|
|
||||||
|
|
||||||
let mut context = tera::Context::new();
|
|
||||||
context.insert("view", &template_name);
|
|
||||||
//intented for shared macros
|
|
||||||
context.insert("format", &settings.format.to_string());
|
|
||||||
context.insert("language", &settings.lang);
|
|
||||||
context.insert("dns_resolvers", &settings.available_dns_resolvers);
|
|
||||||
context.insert("dns_resolver_id", &settings.dns_resolver_id);
|
|
||||||
context.insert("data", &view);
|
|
||||||
context.insert("extra", &self.template_config);
|
|
||||||
|
|
||||||
match self.tera.render(&(template_name+&settings.format.to_file_extension()), &context) {
|
|
||||||
Ok(text) =>
|
|
||||||
match settings.format {
|
|
||||||
ResponseFormat::TextHtml => Html(text).into_response(),
|
|
||||||
_ => text.into_response(),
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
println!("There was an error while rendering template {}: {e:?}", view.template_name());
|
|
||||||
(
|
|
||||||
StatusCode::INTERNAL_SERVER_ERROR,
|
|
||||||
format!("Template error in {}, contact owner or see logs.\n", view.template_name())
|
|
||||||
).into_response()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//TODO: Plain Text should have its own matcher
|
|
||||||
ResponseFormat::ApplicationJson => {
|
|
||||||
match view {
|
|
||||||
View::Dig{result, ..} => {
|
|
||||||
Json(result).into_response()
|
|
||||||
},
|
|
||||||
View::Index{result, ..} | View::Ip{result, ..} => {
|
|
||||||
Json(result).into_response()
|
|
||||||
},
|
|
||||||
View::DnsResolverList => {
|
|
||||||
Json(settings.available_dns_resolvers.clone()).into_response()
|
|
||||||
},
|
|
||||||
View::DnsResolver{ config } => {
|
|
||||||
Json(config).into_response()
|
|
||||||
}
|
|
||||||
_ => Json(view).into_response(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
match view {
|
|
||||||
View::NotFound => *response.status_mut() = StatusCode::NOT_FOUND,
|
|
||||||
_ => {},
|
|
||||||
}
|
|
||||||
let cookie = Cookie::build("dns_resolver",settings.dns_resolver_id.clone())
|
|
||||||
.path("/")
|
|
||||||
.same_site(cookie::SameSite::Strict)
|
|
||||||
.finish();
|
|
||||||
if let Ok(header_value) = HeaderValue::from_str(&cookie.to_string()) {
|
|
||||||
response.headers_mut().append(
|
|
||||||
SET_COOKIE,
|
|
||||||
header_value,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
response
|
|
||||||
}
|
|
||||||
}
|
|
80
src/view.rs
Normal file
80
src/view.rs
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
|
||||||
|
use axum::http::status::StatusCode;
|
||||||
|
use axum::Json;
|
||||||
|
use axum::response::IntoResponse;
|
||||||
|
use axum::response::Response;
|
||||||
|
use axum_extra::extract::cookie::Cookie;
|
||||||
|
use axum_extra::extract::cookie;
|
||||||
|
use lib_humus::HumusView;
|
||||||
|
|
||||||
|
use crate::DigResult;
|
||||||
|
use crate::IpResult;
|
||||||
|
use crate::config::DnsResolverConfig;
|
||||||
|
use crate::settings::QuerySettings;
|
||||||
|
use crate::settings::ResponseFormat;
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(serde::Serialize, Clone)]
|
||||||
|
#[serde(untagged)]
|
||||||
|
pub enum View {
|
||||||
|
Asn { asn: u32 },
|
||||||
|
Dig { query: String, result: DigResult },
|
||||||
|
DnsResolver{ config: DnsResolverConfig },
|
||||||
|
DnsResolverList,
|
||||||
|
Index { result: IpResult, user_agent: Option<String> },
|
||||||
|
Ip { result: IpResult },
|
||||||
|
Message{ title: String, message: String },
|
||||||
|
#[serde(rename="404")]
|
||||||
|
NotFound,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl HumusView<QuerySettings, ResponseFormat> for View {
|
||||||
|
fn get_template_name(&self) -> String {
|
||||||
|
match self {
|
||||||
|
View::Asn{..} => "asn",
|
||||||
|
View::Dig{..} => "dig",
|
||||||
|
View::DnsResolver{..} => "dns_resolver",
|
||||||
|
View::DnsResolverList => "dns_resolver_list",
|
||||||
|
View::Index{..} => "index",
|
||||||
|
View::Ip{..} => "ip",
|
||||||
|
View::Message{..} => "message",
|
||||||
|
View::NotFound => "404",
|
||||||
|
}.to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_status_code(&self, _: &QuerySettings) -> StatusCode {
|
||||||
|
match self {
|
||||||
|
Self::NotFound => StatusCode::NOT_FOUND,
|
||||||
|
_ => StatusCode::OK,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_cookie_header(&self, settings: &QuerySettings) -> Option<String> {
|
||||||
|
Some(
|
||||||
|
Cookie::build(Cookie::new("dns_resolver",settings.dns_resolver_id.to_string()))
|
||||||
|
.path("/")
|
||||||
|
.same_site(cookie::SameSite::Strict)
|
||||||
|
.build()
|
||||||
|
.to_string()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_api_response(self, settings: &QuerySettings) -> Response {
|
||||||
|
match self {
|
||||||
|
Self::Dig{result, ..} => {
|
||||||
|
Json(result).into_response()
|
||||||
|
},
|
||||||
|
Self::Index{result, ..} | Self::Ip{result, ..} => {
|
||||||
|
Json(result).into_response()
|
||||||
|
},
|
||||||
|
Self::DnsResolverList => {
|
||||||
|
Json(settings.available_dns_resolvers.clone()).into_response()
|
||||||
|
},
|
||||||
|
Self::DnsResolver{ config } => {
|
||||||
|
Json(config).into_response()
|
||||||
|
}
|
||||||
|
_ => Json(self).into_response(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -31,12 +31,6 @@
|
|||||||
<dd>{{ helper::dig(extra=extra, name=c.tls_dns_name) }}</dd>
|
<dd>{{ helper::dig(extra=extra, name=c.tls_dns_name) }}</dd>
|
||||||
{%- endif %}
|
{%- endif %}
|
||||||
|
|
||||||
{%- if c.search | length > 0 %}
|
|
||||||
<dt>Search</dt>
|
|
||||||
{%- for s in c.search %}
|
|
||||||
<dd>{{s}}</dd>
|
|
||||||
{%- endfor %}
|
|
||||||
{%- endif %}
|
|
||||||
</dl>
|
</dl>
|
||||||
{%- if c.info_url %}
|
{%- if c.info_url %}
|
||||||
<p class="button-paragraph"><a href="{{c.info_url}}">More about the {{c.display_name}} DNS Server <small>(external link)</small></a></p>
|
<p class="button-paragraph"><a href="{{c.info_url}}">More about the {{c.display_name}} DNS Server <small>(external link)</small></a></p>
|
||||||
|
@ -16,14 +16,6 @@ Protocol: {{ c.protocol }}
|
|||||||
{%-if c.tls_dns_name %}
|
{%-if c.tls_dns_name %}
|
||||||
DNS Name: {{ c.tls_dns_name }}
|
DNS Name: {{ c.tls_dns_name }}
|
||||||
{%- endif %}
|
{%- endif %}
|
||||||
{%- if c.search | length == 1 %}
|
|
||||||
Search: {{ c.search | first }}
|
|
||||||
{%- elif c.search | length > 1 %}
|
|
||||||
Search:
|
|
||||||
{%- for s in c.search %}
|
|
||||||
* {{s}}
|
|
||||||
{%- endfor %}
|
|
||||||
{%- endif %}
|
|
||||||
{%- if c.aliases | length == 1 %}
|
{%- if c.aliases | length == 1 %}
|
||||||
Alias: {{ c.aliases | first }}
|
Alias: {{ c.aliases | first }}
|
||||||
{%- elif c.aliases | length > 1 %}
|
{%- elif c.aliases | length > 1 %}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{% macro place_dl(place, label="", iso_code_prefix="") -%}
|
{% macro place_dl(place, label="", iso_code_prefix="") -%}
|
||||||
{%- if place -%}
|
{%- if place -%}
|
||||||
{%- if format=="text/html" %}
|
{%- if format=="html" %}
|
||||||
{% if label %}<dt>{{label}}</dt>{% endif %}
|
{% if label %}<dt>{{label}}</dt>{% endif %}
|
||||||
<dd>{{place.name}} {% if place.iso_code%}({% if iso_code_prefix %}{{iso_code_prefix}}-{% endif %}{{place.iso_code}}){% endif %}</dd>
|
<dd>{{place.name}} {% if place.iso_code%}({% if iso_code_prefix %}{{iso_code_prefix}}-{% endif %}{{place.iso_code}}){% endif %}</dd>
|
||||||
{% else -%}
|
{% else -%}
|
||||||
|
@ -16,12 +16,13 @@
|
|||||||
{% macro domain_name_links(name) %}
|
{% macro domain_name_links(name) %}
|
||||||
<p>Look up <code>{{name}}</code></p>
|
<p>Look up <code>{{name}}</code></p>
|
||||||
<ul class="link-list">
|
<ul class="link-list">
|
||||||
<li><a target="_blank" href="https://www.shodan.io/domain/{{ name }}">… on shodan.io <small>(limited query's per day, wants an account)</small></a></li>
|
<li><a target="_blank" href="https://www.shodan.io/domain/{{ name | urlencode_strict }}">… on shodan.io <small>(limited query's per day, wants an account)</small></a></li>
|
||||||
<li><a target="_blank" href="https://search.censys.io/search?resource=hosts&sort=RELEVANCE&per_page=25&virtual_hosts=EXCLUDE&q={{ name }}">… on search.censys.io <small>(10 query's per day, wants an account)</small></a></li>
|
<li><a target="_blank" href="https://search.censys.io/search?resource=hosts&sort=RELEVANCE&per_page=25&virtual_hosts=EXCLUDE&q={{ name | urlencode_strict }}">… on search.censys.io <small>(10 query's per day, wants an account)</small></a></li>
|
||||||
<li><a target="_blank" href="https://www.virustotal.com/gui/domain/{{ name }}">… on virustotal.com</a></li>
|
<li><a target="_blank" href="https://www.virustotal.com/gui/domain/{{ name | urlencode_strict }}">… on virustotal.com</a></li>
|
||||||
<li><a target="_blank" href="https://observatory.mozilla.org/analyze/{{ name }}">… on the Mozilla Observatory (http and tls checks)</a></li>
|
<li><a target="_blank" href="https://observatory.mozilla.org/analyze/{{ name | urlencode_strict }}">… on the Mozilla Observatory (http and tls checks)</a></li>
|
||||||
<li><a target="_blank" href="https://internet.nl/site/{{ name }}">… on the Internet.nl Website test</a></li>
|
<li><a target="_blank" href="https://internet.nl/site/{{ name | urlencode_strict }}">… on the Internet.nl Website test</a></li>
|
||||||
<li><a target="_blank" href="https://client.rdap.org/?type=domain&object={{ name }}">… on client.rdap.org <small>(a modern whois, make sure to allow xhr to 3rd parties)</small></a></li>
|
<li><a target="_blank" href="https://client.rdap.org/?type=domain&object={{ name | urlencode_strict }}">… on client.rdap.org <small>(a modern whois, make sure to allow xhr to 3rd parties)</small></a></li>
|
||||||
|
<li><a target="_blank" href="https://crt.sh/?Identity={{ name | urlencode_strict }}&match==">… on crt.sh <small>(Certificate Transparancy Monitor)</small></a></li>
|
||||||
</ul>
|
</ul>
|
||||||
{% endmacro domain_name_links %}
|
{% endmacro domain_name_links %}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user