2021-03-20 02:31:41 +00:00
|
|
|
use std::net::SocketAddr;
|
|
|
|
|
2021-03-27 17:23:19 +00:00
|
|
|
use serde::{Deserialize, Deserializer};
|
|
|
|
use chrono::Duration;
|
2021-03-20 02:31:41 +00:00
|
|
|
|
|
|
|
|
2022-04-22 19:01:26 +00:00
|
|
|
use rocket::{Request, State, http::Status, request::{FromRequest, Outcome}};
|
|
|
|
use rocket::outcome::try_outcome;
|
|
|
|
|
|
|
|
use crate::dns::{DnsClient, DnsConnectorClient, RecordConnector, ZoneConnector};
|
|
|
|
|
|
|
|
|
2021-03-27 17:23:19 +00:00
|
|
|
#[derive(Debug, Deserialize)]
|
2021-03-20 02:31:41 +00:00
|
|
|
pub struct Config {
|
2022-04-22 17:16:57 +00:00
|
|
|
pub dns: DnsClientConfig,
|
2021-03-27 17:23:19 +00:00
|
|
|
pub web_app: WebAppConfig,
|
2021-03-20 02:31:41 +00:00
|
|
|
}
|
|
|
|
|
2021-03-27 17:23:19 +00:00
|
|
|
#[derive(Debug, Deserialize)]
|
2022-04-22 17:16:57 +00:00
|
|
|
pub struct DnsClientConfig {
|
2021-03-27 17:23:19 +00:00
|
|
|
pub server: SocketAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
pub struct WebAppConfig {
|
|
|
|
#[serde(deserialize_with = "from_duration")]
|
|
|
|
pub token_duration: Duration,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_duration<'de, D>(deserializer: D) -> Result<Duration, D::Error>
|
|
|
|
where D: Deserializer<'de>
|
|
|
|
{
|
|
|
|
use serde::de::Error;
|
|
|
|
String::deserialize(deserializer)
|
|
|
|
.and_then(|string| humantime::parse_duration(&string).map_err(|err| Error::custom(err.to_string())))
|
|
|
|
.and_then(|duration| Duration::from_std(duration).map_err(|err| Error::custom(err.to_string())))
|
2021-03-20 02:31:41 +00:00
|
|
|
}
|
|
|
|
|
2022-04-22 19:01:26 +00:00
|
|
|
// TODO: Maybe remove this
|
|
|
|
#[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);
|
|
|
|
match DnsClient::new(config.dns.server).await {
|
|
|
|
Err(e) => {
|
2022-04-22 22:45:57 +00:00
|
|
|
error!("Failed to connect to DNS server: {}", e);
|
2022-04-22 19:01:26 +00:00
|
|
|
Outcome::Failure((Status::InternalServerError, ()))
|
|
|
|
},
|
|
|
|
Ok(c) => Outcome::Success(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[rocket::async_trait]
|
|
|
|
impl<'r> FromRequest<'r> for Box<dyn RecordConnector> {
|
|
|
|
type Error = ();
|
|
|
|
async fn from_request(request: &'r Request<'_>) -> Outcome<Self, Self::Error> {
|
|
|
|
let config = try_outcome!(request.guard::<&State<Config>>().await);
|
|
|
|
match DnsClient::new(config.dns.server).await {
|
|
|
|
Err(e) => {
|
2022-04-22 22:45:57 +00:00
|
|
|
error!("Failed to connect to DNS server: {}", e);
|
2022-04-22 19:01:26 +00:00
|
|
|
Outcome::Failure((Status::InternalServerError, ()))
|
|
|
|
},
|
|
|
|
Ok(c) => Outcome::Success(Box::new(DnsConnectorClient::new(c)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[rocket::async_trait]
|
|
|
|
impl<'r> FromRequest<'r> for Box<dyn ZoneConnector> {
|
|
|
|
type Error = ();
|
|
|
|
async fn from_request(request: &'r Request<'_>) -> Outcome<Self, Self::Error> {
|
|
|
|
let config = try_outcome!(request.guard::<&State<Config>>().await);
|
|
|
|
match DnsClient::new(config.dns.server).await {
|
|
|
|
Err(e) => {
|
2022-04-22 22:45:57 +00:00
|
|
|
error!("Failed to connect to DNS server: {}", e);
|
2022-04-22 19:01:26 +00:00
|
|
|
Outcome::Failure((Status::InternalServerError, ()))
|
|
|
|
},
|
|
|
|
Ok(c) => Outcome::Success(Box::new(DnsConnectorClient::new(c)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|