fix identation
This commit is contained in:
parent
5fb6545470
commit
5f18e32615
1 changed files with 123 additions and 123 deletions
|
@ -11,176 +11,176 @@ use super::api::{RecordApi, ZoneApi};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum DnsApiError {
|
pub enum DnsApiError {
|
||||||
ClientError(ClientError),
|
ClientError(ClientError),
|
||||||
ResponceNotOk {
|
ResponceNotOk {
|
||||||
code: ResponseCode,
|
code: ResponseCode,
|
||||||
zone: Name,
|
zone: Name,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct DnsApiClient {
|
pub struct DnsApiClient {
|
||||||
client: DnsClient
|
client: DnsClient
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DnsApiClient {
|
impl DnsApiClient {
|
||||||
pub fn new(client: DnsClient) -> Self {
|
pub fn new(client: DnsClient) -> Self {
|
||||||
DnsApiClient {
|
DnsApiClient {
|
||||||
client
|
client
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl RecordApi for DnsApiClient {
|
impl RecordApi for DnsApiClient {
|
||||||
type Error = DnsApiError;
|
type Error = DnsApiError;
|
||||||
|
|
||||||
async fn get_records(&mut self, zone: Name, class: DNSClass) -> Result<Vec<Record>, Self::Error>
|
async fn get_records(&mut self, zone: Name, class: DNSClass) -> Result<Vec<Record>, Self::Error>
|
||||||
{
|
{
|
||||||
let response = {
|
let response = {
|
||||||
let query = self.client.query(zone.clone(), class, RecordType::AXFR);
|
let query = self.client.query(zone.clone(), class, RecordType::AXFR);
|
||||||
query.await.map_err(|e| DnsApiError::ClientError(e))?
|
query.await.map_err(|e| DnsApiError::ClientError(e))?
|
||||||
};
|
};
|
||||||
|
|
||||||
if response.response_code() != ResponseCode::NoError {
|
if response.response_code() != ResponseCode::NoError {
|
||||||
return Err(DnsApiError::ResponceNotOk {
|
return Err(DnsApiError::ResponceNotOk {
|
||||||
code: response.response_code(),
|
code: response.response_code(),
|
||||||
zone: zone,
|
zone: zone,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
let answers = response.answers();
|
let answers = response.answers();
|
||||||
let mut records: Vec<_> = answers.to_vec().into_iter()
|
let mut records: Vec<_> = answers.to_vec().into_iter()
|
||||||
.filter(|record| !matches!(record.rdata(), RData::NULL { .. } | RData::DNSSEC(_)))
|
.filter(|record| !matches!(record.rdata(), RData::NULL { .. } | RData::DNSSEC(_)))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
// AXFR response ends with SOA, we remove it so it is not doubled in the response.
|
// AXFR response ends with SOA, we remove it so it is not doubled in the response.
|
||||||
records.pop();
|
records.pop();
|
||||||
Ok(records)
|
Ok(records)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn add_records(&mut self, zone: Name, class: DNSClass, new_records: Vec<Record>) -> Result<(), Self::Error>
|
async fn add_records(&mut self, zone: Name, class: DNSClass, new_records: Vec<Record>) -> Result<(), Self::Error>
|
||||||
{
|
{
|
||||||
// Taken from trust_dns_client::op::update_message::append
|
// Taken from trust_dns_client::op::update_message::append
|
||||||
// The original function can not be used as is because it takes a RecordSet and not a Record list
|
// The original function can not be used as is because it takes a RecordSet and not a Record list
|
||||||
|
|
||||||
let mut zone_query = Query::new();
|
let mut zone_query = Query::new();
|
||||||
zone_query.set_name(zone.clone())
|
zone_query.set_name(zone.clone())
|
||||||
.set_query_class(class)
|
.set_query_class(class)
|
||||||
.set_query_type(RecordType::SOA);
|
.set_query_type(RecordType::SOA);
|
||||||
|
|
||||||
let mut message = Message::new();
|
let mut message = Message::new();
|
||||||
|
|
||||||
// TODO: set random / time based id
|
// TODO: set random / time based id
|
||||||
message
|
message
|
||||||
.set_id(0)
|
.set_id(0)
|
||||||
.set_message_type(MessageType::Query)
|
.set_message_type(MessageType::Query)
|
||||||
.set_op_code(OpCode::Update)
|
.set_op_code(OpCode::Update)
|
||||||
.set_recursion_desired(false);
|
.set_recursion_desired(false);
|
||||||
message.add_zone(zone_query);
|
message.add_zone(zone_query);
|
||||||
message.add_updates(new_records);
|
message.add_updates(new_records);
|
||||||
|
|
||||||
{
|
{
|
||||||
let edns = message.edns_mut();
|
let edns = message.edns_mut();
|
||||||
edns.set_max_payload(1232);
|
edns.set_max_payload(1232);
|
||||||
edns.set_version(0);
|
edns.set_version(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
let response = ClientResponse(self.client.send(message)).await.map_err(|e| DnsApiError::ClientError(e))?;
|
let response = ClientResponse(self.client.send(message)).await.map_err(|e| DnsApiError::ClientError(e))?;
|
||||||
|
|
||||||
if response.response_code() != ResponseCode::NoError {
|
if response.response_code() != ResponseCode::NoError {
|
||||||
return Err(DnsApiError::ResponceNotOk {
|
return Err(DnsApiError::ResponceNotOk {
|
||||||
code: response.response_code(),
|
code: response.response_code(),
|
||||||
zone: zone,
|
zone: zone,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn update_records(&mut self, zone: Name, class: DNSClass, old_records: Vec<Record>, new_records: Vec<Record>) -> Result<(), Self::Error>
|
async fn update_records(&mut self, zone: Name, class: DNSClass, old_records: Vec<Record>, new_records: Vec<Record>) -> Result<(), Self::Error>
|
||||||
{
|
{
|
||||||
|
|
||||||
// Taken from trust_dns_client::op::update_message::compare_and_swap
|
// Taken from trust_dns_client::op::update_message::compare_and_swap
|
||||||
// The original function can not be used as is because it takes a RecordSet and not a Record list
|
// The original function can not be used as is because it takes a RecordSet and not a Record list
|
||||||
|
|
||||||
// for updates, the query section is used for the zone
|
// for updates, the query section is used for the zone
|
||||||
let mut zone_query: Query = Query::new();
|
let mut zone_query: Query = Query::new();
|
||||||
zone_query.set_name(zone.clone())
|
zone_query.set_name(zone.clone())
|
||||||
.set_query_class(class)
|
.set_query_class(class)
|
||||||
.set_query_type(RecordType::SOA);
|
.set_query_type(RecordType::SOA);
|
||||||
|
|
||||||
let mut message: Message = Message::new();
|
let mut message: Message = Message::new();
|
||||||
|
|
||||||
// build the message
|
// build the message
|
||||||
// TODO: set random / time based id
|
// TODO: set random / time based id
|
||||||
message
|
message
|
||||||
.set_id(0)
|
.set_id(0)
|
||||||
.set_message_type(MessageType::Query)
|
.set_message_type(MessageType::Query)
|
||||||
.set_op_code(OpCode::Update)
|
.set_op_code(OpCode::Update)
|
||||||
.set_recursion_desired(false);
|
.set_recursion_desired(false);
|
||||||
message.add_zone(zone_query);
|
message.add_zone(zone_query);
|
||||||
|
|
||||||
// make sure the record is what is expected
|
// make sure the record is what is expected
|
||||||
let mut prerequisite = old_records.clone();
|
let mut prerequisite = old_records.clone();
|
||||||
for record in prerequisite.iter_mut() {
|
for record in prerequisite.iter_mut() {
|
||||||
record.set_ttl(0);
|
record.set_ttl(0);
|
||||||
}
|
}
|
||||||
message.add_pre_requisites(prerequisite);
|
message.add_pre_requisites(prerequisite);
|
||||||
|
|
||||||
// add the delete for the old record
|
// add the delete for the old record
|
||||||
let mut delete = old_records;
|
let mut delete = old_records;
|
||||||
// the class must be none for delete
|
// the class must be none for delete
|
||||||
for record in delete.iter_mut() {
|
for record in delete.iter_mut() {
|
||||||
record.set_dns_class(DNSClass::NONE);
|
record.set_dns_class(DNSClass::NONE);
|
||||||
// the TTL should be 0
|
// the TTL should be 0
|
||||||
record.set_ttl(0);
|
record.set_ttl(0);
|
||||||
}
|
}
|
||||||
message.add_updates(delete);
|
message.add_updates(delete);
|
||||||
|
|
||||||
// insert the new record...
|
// insert the new record...
|
||||||
message.add_updates(new_records);
|
message.add_updates(new_records);
|
||||||
|
|
||||||
// Extended dns
|
// Extended dns
|
||||||
{
|
{
|
||||||
let edns = message.edns_mut();
|
let edns = message.edns_mut();
|
||||||
edns.set_max_payload(1232);
|
edns.set_max_payload(1232);
|
||||||
edns.set_version(0);
|
edns.set_version(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
let response = ClientResponse(self.client.send(message)).await.map_err(|e| DnsApiError::ClientError(e))?;
|
let response = ClientResponse(self.client.send(message)).await.map_err(|e| DnsApiError::ClientError(e))?;
|
||||||
|
|
||||||
if response.response_code() != ResponseCode::NoError {
|
if response.response_code() != ResponseCode::NoError {
|
||||||
return Err(DnsApiError::ResponceNotOk {
|
return Err(DnsApiError::ResponceNotOk {
|
||||||
code: response.response_code(),
|
code: response.response_code(),
|
||||||
zone: zone,
|
zone: zone,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl ZoneApi for DnsApiClient {
|
impl ZoneApi for DnsApiClient {
|
||||||
type Error = DnsApiError;
|
type Error = DnsApiError;
|
||||||
|
|
||||||
async fn zone_exists(&mut self, zone: Name, class: DNSClass) -> Result<(), Self::Error>
|
async fn zone_exists(&mut self, zone: Name, class: DNSClass) -> Result<(), Self::Error>
|
||||||
{
|
{
|
||||||
let response = {
|
let response = {
|
||||||
let query = self.client.query(zone.clone(), class, RecordType::SOA);
|
let query = self.client.query(zone.clone(), class, RecordType::SOA);
|
||||||
query.await.map_err(|e| DnsApiError::ClientError(e))?
|
query.await.map_err(|e| DnsApiError::ClientError(e))?
|
||||||
};
|
};
|
||||||
|
|
||||||
if response.response_code() != ResponseCode::NoError {
|
if response.response_code() != ResponseCode::NoError {
|
||||||
return Err(DnsApiError::ResponceNotOk {
|
return Err(DnsApiError::ResponceNotOk {
|
||||||
code: response.response_code(),
|
code: response.response_code(),
|
||||||
zone: zone,
|
zone: zone,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in a new issue