Skip to content

Commit

Permalink
Update edition to 2021 and use cargo fmt
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Gunnerson <[email protected]>
  • Loading branch information
chenxiaolong committed Oct 14, 2023
1 parent db39fe8 commit 5ce401f
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 138 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ repository = "https://github.com/chenxiaolong/ddns-updater"
readme = "README.md"
license = "GPL-3.0"
version = "0.1.8"
edition = "2018"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ The resulting executable will be in `target/release/ddns-updater` or `target\rel

If an issue occurs, the best way to troubleshoot is to set `log_level` to `debug` or `trace` in the config file. The `debug` level includes information like the detected IP addresses, while the `trace` level will also print out the raw DNS update request and response. Note that the `trace` output is not safe to paste online because it includes a dump of the TSIG key.

To enable trace logging for everything, including the underlying trust-dns library, set the `RUST_LOG` environment variable to `trace`.
To enable trace logging for everything, including the underlying hickory-dns library, set the `RUST_LOG` environment variable to `trace`.

## Limitations

Expand Down
2 changes: 1 addition & 1 deletion config.sample.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ name_server = ""
#
# Due to a bug, using `tcp` will currently result in a 5 second delay after the
# DDNS update response is received. See:
# https://github.com/bluejekyll/trust-dns/issues/1607
# https://github.com/hickory-dns/hickory-dns/issues/1607
#protocol = "udp"

# The interface to query IP addresses for populating A/AAAA records. If no
Expand Down
22 changes: 5 additions & 17 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
use {
std::{
fmt,
path::Path,
time::Duration,
},
serde::Deserialize,
serde_with::{
base64::Base64,
serde_as,
DisplayFromStr,
},
hickory_client::rr::{
rdata::tsig::TsigAlgorithm,
Name,
},
};
use std::{fmt, path::Path, time::Duration};

use hickory_client::rr::{rdata::tsig::TsigAlgorithm, Name};
use serde::Deserialize;
use serde_with::{base64::Base64, serde_as, DisplayFromStr};

const DEFAULT_TTL: u32 = 300;
const DEFAULT_TIMEOUT: u64 = 5;
Expand Down
72 changes: 33 additions & 39 deletions src/dns.rs
Original file line number Diff line number Diff line change
@@ -1,49 +1,46 @@
use {
std::{
future::Future,
net::{IpAddr, SocketAddr},
pin::Pin,
time::Duration,
use hickory_client::{
client::{AsyncClient, Client, SyncClient},
error::ClientResult,
op::{Edns, Message, MessageType, OpCode, Query, UpdateMessage},
proto::{
error::ProtoError,
rr::{dnssec::tsig::TSigner, DNSClass, Name, RData, Record, RecordType},
xfer::DnsExchangeSend,
},
hickory_client::{
client::{AsyncClient, Client, SyncClient},
error::ClientResult,
op::{Edns, Message, MessageType, OpCode, Query, UpdateMessage},
proto::{
error::ProtoError,
rr::{
dnssec::tsig::TSigner,
DNSClass, Name, RData, Record, RecordType,
},
xfer::DnsExchangeSend,
},
tcp::TcpClientConnection,
udp::UdpClientConnection,
},
crate::config::Protocol,
tcp::TcpClientConnection,
udp::UdpClientConnection,
};

use std::{
future::Future,
net::{IpAddr, SocketAddr},
pin::Pin,
time::Duration,
};

/// `trust_dns_client::client::NewFutureObj` is not public
use crate::config::Protocol;

/// `hickory_client::client::NewFutureObj` is not public
type NewFutureObj<H> = Pin<
Box<
dyn Future<
Output = Result<
(
H,
Box<dyn Future<Output = Result<(), ProtoError>> + 'static + Send + Unpin>,
),
ProtoError,
>,
>
+ 'static
+ Send,
Output = Result<
(
H,
Box<dyn Future<Output = Result<(), ProtoError>> + 'static + Send + Unpin>,
),
ProtoError,
>,
>
+ 'static
+ Send,
>,
>;

/// Small wrapper to avoid callers needing to distinguish between TCP/UDP.
pub enum DnsClient {
Tcp(SyncClient::<TcpClientConnection>),
Udp(SyncClient::<UdpClientConnection>),
Tcp(SyncClient<TcpClientConnection>),
Udp(SyncClient<UdpClientConnection>),
}

impl DnsClient {
Expand Down Expand Up @@ -87,9 +84,6 @@ pub fn replace_addrs_message(
ttl: u32,
addrs: &[IpAddr],
) -> Message {
// trust_dns_client::client::AsyncClient::MAX_PAYLOAD_LEN is not public
const MAX_PAYLOAD_LEN: u16 = 1232;

let mut zone = Query::new();
zone.set_name(zone_origin.clone())
.set_query_class(DNSClass::IN)
Expand Down Expand Up @@ -121,7 +115,7 @@ pub fn replace_addrs_message(
message
.extensions_mut()
.get_or_insert_with(Edns::new)
.set_max_payload(MAX_PAYLOAD_LEN)
.set_max_payload(hickory_client::proto::op::update_message::MAX_PAYLOAD_LEN)
.set_version(0);

message
Expand Down
26 changes: 12 additions & 14 deletions src/iface.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use {
std::{
io,
net::{IpAddr, SocketAddr, TcpStream},
},
netif::Interface,
use std::{
io,
net::{IpAddr, SocketAddr, TcpStream},
};

use netif::Interface;

#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Could not connect to: {0}: {1}")]
Expand All @@ -24,19 +23,17 @@ pub struct Interfaces {

impl Interfaces {
pub fn new() -> Result<Self> {
let ifaces = netif::up()
.map_err(Error::QueryInterface)?
.collect();
let ifaces = netif::up().map_err(Error::QueryInterface)?.collect();

Ok(Self {
ifaces,
})
Ok(Self { ifaces })
}

pub fn get_addrs_by_name(&self, name: &str) -> Option<Vec<IpAddr>> {
let mut found = false;

let addrs = self.ifaces.iter()
let addrs = self
.ifaces
.iter()
.filter(|iface| iface.name() == name)
.inspect(|_| found = true)
.map(|iface| *iface.address())
Expand All @@ -52,7 +49,8 @@ impl Interfaces {
pub fn get_iface_by_tcp_source_ip(&self, server: SocketAddr) -> Result<&str> {
let source_ip = Self::get_tcp_source_ip(server)?;

self.ifaces.iter()
self.ifaces
.iter()
.find(|iface| *iface.address() == source_ip)
.map(Interface::name)
.ok_or(Error::InterfaceNotFound(source_ip))
Expand Down
Loading

0 comments on commit 5ce401f

Please sign in to comment.