Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dreamhost-ddns"
version = "0.1.1"
version = "0.2.0"
edition = "2021"

[dependencies]
Expand All @@ -14,6 +14,7 @@ dotenvy = "0.15"
log = "0.4"
env_logger = "0.11"
trust-dns-resolver = "0.23"
rand = "0.8"

[profile.release]
lto = true
Expand Down
Binary file modified binaries/linux-aarch64/dreamhost-ddns
Binary file not shown.
Binary file modified binaries/linux-rpi-armv7/dreamhost-ddns
Binary file not shown.
Binary file modified binaries/linux-x86_64/dreamhost-ddns
Binary file not shown.
Binary file modified binaries/windows/dreamhost-ddns.exe
Binary file not shown.
36 changes: 26 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use serde::Deserialize;
use std::net::IpAddr;
use std::sync::mpsc;
use std::thread;
use rand::seq::SliceRandom;

#[derive(Parser)]
#[command(
Expand All @@ -26,9 +27,6 @@ struct Args {
#[arg(long)]
record: Option<String>,

#[arg(long, default_value_t = 300)]
interval: u64,

#[arg(long)]
dry_run: bool,
}
Expand Down Expand Up @@ -95,6 +93,7 @@ impl DreamhostClient {
}

fn update_dns(&self, record: &str, old_ip: &str, new_ip: &str) -> Result<()> {

info!("Adding new DNS record {} -> {}", record, new_ip);

self.call(&[
Expand All @@ -104,6 +103,9 @@ impl DreamhostClient {
("value", new_ip),
])?;

info!("Waiting briefly for DNS propagation...");
std::thread::sleep(std::time::Duration::from_secs(3));

info!("Removing old DNS record {} -> {}", record, old_ip);

self.call(&[
Expand Down Expand Up @@ -135,11 +137,10 @@ fn main() -> Result<()> {
let record = args.record.unwrap_or(config.dns_record);

info!("Record: {}", record);
info!("Check interval: {} seconds", args.interval);

let client = Client::builder()
.timeout(std::time::Duration::from_secs(5))
.user_agent("dreamhost-ddns/1.0")
.user_agent(format!("dreamhost-ddns/{}", env!("CARGO_PKG_VERSION")))
.build()?;

let dh = DreamhostClient {
Expand Down Expand Up @@ -168,7 +169,7 @@ fn main() -> Result<()> {
} else {
info!("Updating DNS...");
dh.update_dns(&record, &dns_ip, &wan_ip.to_string())?;
info!("DNS updated successfully");
info!("DNS updated successfully to {}", wan_ip);
}


Expand Down Expand Up @@ -231,28 +232,43 @@ fn load_config(path: &str) -> Result<Config> {
}

fn get_wan_ip(client: &Client) -> Result<IpAddr> {
let services = [
let mut services = vec![
"https://icanhazip.com",
"https://api.ipify.org",
"https://ifconfig.me/ip",
"https://checkip.amazonaws.com",
];

services.shuffle(&mut rand::thread_rng());

let (tx, rx) = mpsc::channel();
let cancel = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));

for url in services {
let tx = tx.clone();
let client = client.clone();
let cancel = cancel.clone();
let url = url.to_string();

thread::spawn(move || {
let result = client.get(&url).send()

if cancel.load(std::sync::atomic::Ordering::Relaxed) {
return;
}

let result = client
.get(&url)
.send()
.and_then(|r| r.text())
.ok()
.and_then(|text| text.trim().parse::<IpAddr>().ok());

if let Some(ip) = result {
let _ = tx.send((url, ip));

if !cancel.swap(true, std::sync::atomic::Ordering::Relaxed) {
let _ = tx.send((url, ip));
}

}
});
}
Expand All @@ -261,7 +277,7 @@ fn get_wan_ip(client: &Client) -> Result<IpAddr> {

match rx.recv() {
Ok((url, ip)) => {
info!("WAN IP detected via {}: {}", url, ip);
info!("WAN IP detected via {}", url);
Ok(ip)
}
Err(_) => Err(anyhow!("All WAN IP detection services failed")),
Expand Down