Added dns functionality

This commit is contained in:
Slatian 2023-02-12 01:24:39 +01:00
parent afdec2b8a2
commit b21aa5192f
5 changed files with 436 additions and 2 deletions

247
Cargo.lock generated
View File

@ -249,6 +249,12 @@ dependencies = [
"syn",
]
[[package]]
name = "data-encoding"
version = "2.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23d8666cb01533c39dde32bcbab8e227b4ed6679b2c925eba05feabea39508fb"
[[package]]
name = "deunicode"
version = "0.4.3"
@ -265,6 +271,18 @@ dependencies = [
"crypto-common",
]
[[package]]
name = "enum-as-inner"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c9720bba047d567ffc8a3cba48bf19126600e249ab7f128e9233e6376976a116"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "fnv"
version = "1.0.7"
@ -295,6 +313,12 @@ version = "0.3.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "04909a7a7e4633ae6c4a9ab280aeb86da1236243a77b694a49eacd659a4bd3ac"
[[package]]
name = "futures-io"
version = "0.3.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bfb8371b6fb2aeb2d280374607aeabfc99d95c72edfe51692e42d3d7f0d08531"
[[package]]
name = "futures-task"
version = "0.3.25"
@ -311,6 +335,7 @@ dependencies = [
"futures-task",
"pin-project-lite",
"pin-utils",
"slab",
]
[[package]]
@ -358,6 +383,12 @@ dependencies = [
"walkdir",
]
[[package]]
name = "heck"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
[[package]]
name = "hermit-abi"
version = "0.2.6"
@ -367,6 +398,17 @@ dependencies = [
"libc",
]
[[package]]
name = "hostname"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c731c3e10504cc8ed35cfe2f1db4c9274c3d35fa486e3b31df46f068ef3e867"
dependencies = [
"libc",
"match_cfg",
"winapi",
]
[[package]]
name = "http"
version = "0.2.8"
@ -460,6 +502,27 @@ dependencies = [
"cxx-build",
]
[[package]]
name = "idna"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "418a0a6fab821475f634efe3ccc45c013f742efe03d853e8d3355d5cb850ecf8"
dependencies = [
"matches",
"unicode-bidi",
"unicode-normalization",
]
[[package]]
name = "idna"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e14ddfc70884202db2244c223200c204c2bda1bc6e0998d11b5e024d657209e6"
dependencies = [
"unicode-bidi",
"unicode-normalization",
]
[[package]]
name = "ignore"
version = "0.4.20"
@ -477,6 +540,24 @@ dependencies = [
"winapi-util",
]
[[package]]
name = "ipconfig"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd302af1b90f2463a98fa5ad469fc212c8e3175a41c3068601bfa2727591c5be"
dependencies = [
"socket2",
"widestring",
"winapi",
"winreg",
]
[[package]]
name = "ipnet"
version = "2.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30e22bd8629359895450b59ea7a776c850561b96a3b1d31321c1949d9e6c9146"
[[package]]
name = "itoa"
version = "1.0.5"
@ -513,6 +594,12 @@ dependencies = [
"cc",
]
[[package]]
name = "linked-hash-map"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f"
[[package]]
name = "lock_api"
version = "0.4.9"
@ -532,6 +619,27 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "lru-cache"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "31e24f1ad8321ca0e8a1e0ac13f23cb668e6f5466c2c57319f6a5cf1cc8e3b1c"
dependencies = [
"linked-hash-map",
]
[[package]]
name = "match_cfg"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ffbee8634e0d45d258acb448e7eaab3fce7a0a467395d4d9f228e3c1f01fb2e4"
[[package]]
name = "matches"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2532096657941c2fea9c289d370a250971c689d4f143798ff67113ec042024a5"
[[package]]
name = "matchit"
version = "0.7.0"
@ -765,6 +873,12 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "quick-error"
version = "1.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0"
[[package]]
name = "quote"
version = "1.0.23"
@ -830,6 +944,16 @@ version = "0.6.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848"
[[package]]
name = "resolv-conf"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "52e44394d2086d010551b14b53b1f24e31647570cd1deb0379e2c21b329aba00"
dependencies = [
"hostname",
"quick-error",
]
[[package]]
name = "rust-web-thingy"
version = "0.1.0"
@ -839,6 +963,7 @@ dependencies = [
"serde",
"tera",
"tokio",
"trust-dns-resolver",
]
[[package]]
@ -952,6 +1077,15 @@ version = "0.3.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7bd3e3206899af3f8b12af284fafc038cc1dc2b41d1b89dd17297221c5d225de"
[[package]]
name = "slab"
version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4614a76b2a8be0058caa9dbbaf66d988527d86d003c11a94fbd335d7661edcef"
dependencies = [
"autocfg",
]
[[package]]
name = "slug"
version = "0.1.4"
@ -1054,6 +1188,21 @@ dependencies = [
"once_cell",
]
[[package]]
name = "tinyvec"
version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87cc5ceb3875bb20c2890005a4e226a4651264a5c75edb2421b52861a0a0cb50"
dependencies = [
"tinyvec_macros",
]
[[package]]
name = "tinyvec_macros"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20"
[[package]]
name = "tokio"
version = "1.24.2"
@ -1141,9 +1290,21 @@ dependencies = [
"cfg-if",
"log",
"pin-project-lite",
"tracing-attributes",
"tracing-core",
]
[[package]]
name = "tracing-attributes"
version = "0.1.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4017f8f45139870ca7e672686113917c71c7a6e02d4924eda67186083c03081a"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "tracing-core"
version = "0.1.30"
@ -1153,6 +1314,51 @@ dependencies = [
"once_cell",
]
[[package]]
name = "trust-dns-proto"
version = "0.22.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4f7f83d1e4a0e4358ac54c5c3681e5d7da5efc5a7a632c90bb6d6669ddd9bc26"
dependencies = [
"async-trait",
"cfg-if",
"data-encoding",
"enum-as-inner",
"futures-channel",
"futures-io",
"futures-util",
"idna 0.2.3",
"ipnet",
"lazy_static",
"rand",
"smallvec",
"thiserror",
"tinyvec",
"tokio",
"tracing",
"url",
]
[[package]]
name = "trust-dns-resolver"
version = "0.22.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aff21aa4dcefb0a1afbfac26deb0adc93888c7d295fb63ab273ef276ba2b7cfe"
dependencies = [
"cfg-if",
"futures-util",
"ipconfig",
"lazy_static",
"lru-cache",
"parking_lot",
"resolv-conf",
"smallvec",
"thiserror",
"tokio",
"tracing",
"trust-dns-proto",
]
[[package]]
name = "try-lock"
version = "0.2.4"
@ -1230,18 +1436,44 @@ dependencies = [
"unic-common",
]
[[package]]
name = "unicode-bidi"
version = "0.3.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d54675592c1dbefd78cbd98db9bacd89886e1ca50692a0692baefffdeb92dd58"
[[package]]
name = "unicode-ident"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc"
[[package]]
name = "unicode-normalization"
version = "0.1.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c5713f0fc4b5db668a2ac63cdb7bb4469d8c9fed047b1d0292cc7b0ce2ba921"
dependencies = [
"tinyvec",
]
[[package]]
name = "unicode-width"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b"
[[package]]
name = "url"
version = "2.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d68c799ae75762b8c3fe375feb6600ef5602c883c5d21eb51c09f22b83c4643"
dependencies = [
"form_urlencoded",
"idna 0.3.0",
"percent-encoding",
]
[[package]]
name = "version_check"
version = "0.9.4"
@ -1329,6 +1561,12 @@ version = "0.2.84"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0046fef7e28c3804e5e38bfa31ea2a0f73905319b677e57ebe37e49358989b5d"
[[package]]
name = "widestring"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "17882f045410753661207383517a6f62ec3dbeb6a4ed2acce01f0728238d1983"
[[package]]
name = "winapi"
version = "0.3.9"
@ -1416,3 +1654,12 @@ name = "windows_x86_64_msvc"
version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "447660ad36a13288b1db4d4248e857b510e8c3a225c822ba4fb748c0aafecffd"
[[package]]
name = "winreg"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "80d0f4e272c85def139476380b12f9ac60926689dd2e01d4923222f40580869d"
dependencies = [
"winapi",
]

View File

@ -11,3 +11,4 @@ rand = "0.8"
serde = { version = "1", features = ["derive"] }
tokio = { version = "1", features = ["full"] }
tera = "1"
trust-dns-resolver = "0.22"

View File

@ -7,6 +7,12 @@ use axum::{
routing::get,
};
use tera::Tera;
use trust_dns_resolver::{
TokioAsyncResolver,
config::ResolverOpts,
config::ResolverConfig,
error::*,
};
use std::net::{IpAddr, Ipv4Addr};
use std::sync::Arc;
@ -37,33 +43,71 @@ struct IpQuery {
format: Option<ResponseFormat>,
}
#[derive(serde::Deserialize, serde::Serialize)]
struct DigQuery {
name: String,
format: Option<ResponseFormat>
}
#[derive(serde::Deserialize, serde::Serialize)]
struct MxRecord {
preference: u16,
exchange: String,
}
#[derive(serde::Deserialize, serde::Serialize)]
struct DigResult {
a: Vec<IpAddr>,
aaaa: Vec<IpAddr>,
mx: Vec<MxRecord>,
}
struct ServiceSharedState {
tera: Tera,
dns_resolver: TokioAsyncResolver,
}
#[tokio::main]
async fn main() {
// Initalize Tera templates
// TODO: don't hardcode template directory
let tera = match Tera::new("templates/*.html") {
println!("Parsing Templates ...");
let res = Tera::new("templates/*.html");
let tera = match res {
Ok(t) => t,
Err(e) => {
println!("Template parsing error(s): {}", e);
::std::process::exit(1);
}
};
// Initalize DNS resolver with os defaults
println!("Initalizing dns resolver ...");
let res = TokioAsyncResolver::tokio(ResolverConfig::default(), ResolverOpts::default());
let dns_resolver = match res {
Ok(resolver) => resolver,
Err(e) => {
println!("Error while setting up dns resolver: {e}");
::std::process::exit(1);
}
};
// Initialize shared state
let shared_state = Arc::new(ServiceSharedState{
tera: tera,
dns_resolver: dns_resolver,
});
// Initalize axum server
let app = Router::new()
.route("/", get(handle_default_route))
.route("/dig", get(handle_dig_route))
.route("/hi", get(hello_world_handler))
.with_state(shared_state)
;
println!("Starting Server ...");
axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
.serve(app.into_make_service())
.await
@ -74,6 +118,33 @@ async fn hello_world_handler() -> &'static str {
"Hello, there, you, awesome creature!"
}
async fn simple_reverse_dns_lookup(
resolver: &TokioAsyncResolver,
address: &IpAddr,
) -> Option<String> {
let revese_res = resolver.reverse_lookup(*address);
match revese_res.await {
Ok(lookup) => {
for name in lookup {
return Some(name.to_string())
}
None
}
Err(e) => {
let kind = e.kind();
match kind {
ResolveErrorKind::NoRecordsFound { .. } => {
//Ignore, that just happens …
}
_ => {
println!("Reverse lookup on {address} failed: {kind}");
}
}
None
}
}
}
async fn handle_default_route(
Query(ip_query): Query<IpQuery>,
State(arc_state): State<Arc<ServiceSharedState>>,
@ -84,9 +155,14 @@ async fn handle_default_route(
let format_string = format.to_string();
let state = Arc::clone(&arc_state);
// do reverse lookup
let hostname = simple_reverse_dns_lookup(&state.dns_resolver, &address);
let mut context = tera::Context::new();
context.insert("ip", &address);
context.insert("format", &format_string);
context.insert("hostname", &hostname.await);
match state.tera.render("index.html", &context) {
Ok(html) => Ok(Html(html)),
@ -95,5 +171,73 @@ async fn handle_default_route(
Err(StatusCode::INTERNAL_SERVER_ERROR)
}
}
}
async fn handle_dig_route(
Query(dig_query): Query<DigQuery>,
State(arc_state): State<Arc<ServiceSharedState>>,
) -> Result<Html<String>,StatusCode> {
let state = Arc::clone(&arc_state);
let name = dig_query.name;
let ipv4_lookup_res = state.dns_resolver.ipv4_lookup(&name);
let ipv6_lookup_res = state.dns_resolver.ipv6_lookup(&name);
let mx_lookup_res = state.dns_resolver.mx_lookup(&name);
let mut dig_result = DigResult{
a: Vec::new(),
aaaa: Vec::new(),
mx: Vec::new(),
};
match ipv4_lookup_res.await {
Ok(lookup) => {
for address in lookup {
dig_result.a.push(std::net::IpAddr::V4(address));
}
}
Err(e) => {
println!("There was an error while looking A up {name}: {e}");
}
}
match ipv6_lookup_res.await {
Ok(lookup) => {
for address in lookup {
dig_result.aaaa.push(std::net::IpAddr::V6(address));
}
}
Err(e) => {
println!("There was an error while looking AAAA up {name}: {e}");
}
}
match mx_lookup_res.await {
Ok(lookup) => {
for mx in lookup {
dig_result.mx.push(MxRecord{
preference: mx.preference(),
exchange: mx.exchange().to_string(),
});
}
}
Err(e) => {
println!("There was an error while looking MX up {name}: {e}");
}
}
let mut context = tera::Context::new();
context.insert("dig_query", &name);
context.insert("dig_result", &dig_result);
match state.tera.render("dig.html", &context) {
Ok(html) => Ok(Html(html)),
Err(e) => {
println!("There was an error while rendering index.html: {e}");
Err(StatusCode::INTERNAL_SERVER_ERROR)
}
}
}

39
templates/dig.html Normal file
View File

@ -0,0 +1,39 @@
<!DOCTYPE html>
<html>
<head>
<title>Dig: {{ dig_query }}</title>
</head>
<body>
<h1>Lookup for: {{ dig_query }}</h1>
{% if dig_result.a %}
<p>A (IPv4) records:</p>
<ul>
{% for address in dig_result.a%}
<li><code>{{address}}</code></li>
{% endfor %}
</ul>
{% endif %}
{% if dig_result.aaaa %}
<p>AAAA (IPv6) records:</p>
<ul>
{% for address in dig_result.aaaa%}
<li><code>{{address}}</code></li>
{% endfor %}
</ul>
{% endif %}
{% if dig_result.mx %}
<p>MX (Mail Exchange) records:</p>
<ul>
{% for mx in dig_result.mx%}
<li>{{mx.preference}} <code><a href="/dig?name={{mx.exchange}}">{{mx.exchange}}</a></code></li>
{% endfor %}
</ul>
{% endif %}
</body>
</html>

View File

@ -6,5 +6,8 @@
<body>
<h1>Your IP-Address is: {{ ip }}</h1>
<p>Your requested format was: <b>{{format}}</b></p>
{% if hostname %}
<p>Hostname: <b>{{hostname}}</b></p>
{% endif %}
</body>
</html>