2021-04-02 20:09:51 +00:00
|
|
|
use rocket::http::Status;
|
|
|
|
|
|
|
|
use rocket_contrib::json::Json;
|
|
|
|
|
2021-04-03 06:16:54 +00:00
|
|
|
use trust_dns_client::client::ClientHandle;
|
2021-04-02 20:09:51 +00:00
|
|
|
use trust_dns_client::op::{DnsResponse, ResponseCode};
|
2021-04-05 21:21:40 +00:00
|
|
|
use trust_dns_client::rr::{DNSClass, RecordType};
|
2021-04-02 20:09:51 +00:00
|
|
|
|
|
|
|
use crate::models::dns;
|
2021-04-03 06:16:54 +00:00
|
|
|
use crate::models::errors::{ErrorResponse, make_500};
|
2021-04-02 20:09:51 +00:00
|
|
|
use crate::models::users::UserInfo;
|
|
|
|
|
|
|
|
|
|
|
|
#[get("/zones/<zone>/records")]
|
2021-04-03 06:16:54 +00:00
|
|
|
pub async fn get_zone_records(
|
2021-04-05 21:21:40 +00:00
|
|
|
mut client: dns::DnsClient,
|
2021-04-02 21:12:29 +00:00
|
|
|
user_info: Result<UserInfo, ErrorResponse>,
|
2021-04-05 21:21:40 +00:00
|
|
|
zone: dns::AbsoluteName
|
2021-04-02 21:12:29 +00:00
|
|
|
) -> Result<Json<Vec<dns::Record>>, ErrorResponse> {
|
2021-04-03 06:16:54 +00:00
|
|
|
println!("{:#?}", user_info?);
|
2021-04-02 20:09:51 +00:00
|
|
|
|
2021-04-03 06:16:54 +00:00
|
|
|
let response: DnsResponse = {
|
2021-04-05 21:21:40 +00:00
|
|
|
let query = client.query((*zone).clone(), DNSClass::IN, RecordType::AXFR);
|
2021-04-03 06:16:54 +00:00
|
|
|
query.await.map_err(make_500)?
|
|
|
|
};
|
2021-04-02 20:09:51 +00:00
|
|
|
|
|
|
|
if response.response_code() != ResponseCode::NoError {
|
|
|
|
return ErrorResponse::new(
|
|
|
|
Status::NotFound,
|
2021-04-05 21:21:40 +00:00
|
|
|
format!("zone {} could not be found", *zone)
|
2021-04-02 20:09:51 +00:00
|
|
|
).err()
|
|
|
|
}
|
|
|
|
|
|
|
|
let answers = response.answers();
|
|
|
|
let mut records: Vec<_> = answers.to_vec().into_iter()
|
2021-04-05 01:05:39 +00:00
|
|
|
.map(dns::Record::from)
|
|
|
|
.filter(|record| !matches!(record.rdata, dns::RData::NULL { .. } | dns::RData::DNSSEC(_)))
|
|
|
|
.collect();
|
2021-04-02 20:09:51 +00:00
|
|
|
|
|
|
|
// AXFR response ends with SOA, we remove it so it is not doubled in the response.
|
|
|
|
records.pop();
|
|
|
|
|
|
|
|
Ok(Json(records))
|
|
|
|
}
|