|
|
@ -1,10 +1,22 @@ |
|
|
|
use std::net::{Ipv6Addr, Ipv4Addr}; |
|
|
|
use std::fmt; |
|
|
|
use std::ops::{Deref, DerefMut}; |
|
|
|
|
|
|
|
|
|
|
|
use rocket::{Request, State, http::Status, request::{FromParam, FromRequest, Outcome}}; |
|
|
|
|
|
|
|
use serde::{Serialize, Deserialize}; |
|
|
|
use trust_dns_client::serialize::binary::BinEncoder; |
|
|
|
|
|
|
|
use super::trust_dns_types; |
|
|
|
use tokio::{net::TcpStream as TokioTcpStream, task}; |
|
|
|
|
|
|
|
use trust_dns_client::{client::AsyncClient, serialize::binary::BinEncoder, tcp::TcpClientStream}; |
|
|
|
use trust_dns_proto::error::{ProtoError}; |
|
|
|
use trust_dns_proto::iocompat::AsyncIoTokioAsStd; |
|
|
|
|
|
|
|
|
|
|
|
use super::trust_dns_types::{self, Name}; |
|
|
|
use crate::config::Config; |
|
|
|
use crate::models::errors::make_500; |
|
|
|
|
|
|
|
|
|
|
|
#[derive(Deserialize, Serialize)] |
|
|
@ -238,3 +250,64 @@ impl From<trust_dns_types::Record> for Record { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
pub struct AbsoluteName(Name); |
|
|
|
|
|
|
|
impl<'r> FromParam<'r> for AbsoluteName { |
|
|
|
type Error = ProtoError; |
|
|
|
|
|
|
|
fn from_param(param: &'r str) -> Result<Self, Self::Error> { |
|
|
|
let mut name = Name::from_utf8(¶m).unwrap(); |
|
|
|
if !name.is_fqdn() { |
|
|
|
name.set_fqdn(true); |
|
|
|
} |
|
|
|
Ok(AbsoluteName(name)) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
impl Deref for AbsoluteName { |
|
|
|
type Target = Name; |
|
|
|
fn deref(&self) -> &Self::Target { |
|
|
|
&self.0 |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
pub struct DnsClient(AsyncClient); |
|
|
|
|
|
|
|
impl Deref for DnsClient { |
|
|
|
type Target = AsyncClient; |
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target { |
|
|
|
&self.0 |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
impl DerefMut for DnsClient { |
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target { |
|
|
|
&mut self.0 |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[rocket::async_trait] |
|
|
|
impl<'r> FromRequest<'r> for DnsClient { |
|
|
|
type Error = (); |
|
|
|
async fn from_request(request: &'r Request<'_>) -> Outcome<Self, Self::Error> { |
|
|
|
let config = try_outcome!(request.guard::<State<Config>>().await); |
|
|
|
let (stream, handle) = TcpClientStream::<AsyncIoTokioAsStd<TokioTcpStream>>::new(config.dns.server); |
|
|
|
let client = AsyncClient::with_timeout( |
|
|
|
stream, |
|
|
|
handle, |
|
|
|
std::time::Duration::from_secs(5), |
|
|
|
None); |
|
|
|
let (client, bg) = match client.await { |
|
|
|
Err(e) => { |
|
|
|
println!("Failed to connect to DNS server {:#?}", e); |
|
|
|
return Outcome::Failure((Status::InternalServerError, ())) |
|
|
|
}, |
|
|
|
Ok(c) => c |
|
|
|
}; |
|
|
|
task::spawn(bg); |
|
|
|
Outcome::Success(DnsClient(client)) |
|
|
|
} |
|
|
|
} |
|
|
|