feat(plugins): implement native Dynu DDNS updater plugin

- Implement DdnsUpdater trait for Dynu IP update protocol v2
- Add response code parsing for good, nochg, badauth, and errors
- Support configurable endpoint for mock testing
- Add unit tests validating protocol handling against mock server
This commit is contained in:
kiskaadee committed 2026-10-27 08:03:31 -05:00
1 parent fbc434478d
commit ff1e15987a
4 files changed
+209

No files matched your search

+1
View File
@@ -1,4 +1,5 @@
pub mod config; pub mod config;
pub mod domain; pub mod domain;
pub mod infra; pub mod infra;
pub mod plugins;
pub mod service; pub mod service;
+105
View File
@@ -0,0 +1,105 @@
use std::net::Ipv4Addr;
use std::time::Duration;
use ureq::Agent;
use crate::domain::errors::UpdateError;
use crate::domain::traits::DdnsUpdater;
pub const DEFAULT_DYNU_ENDPOINT: &str = "https://api.dynu.com/nic/update";
/// DDNS Updater plugin for Dynu Systems.
///
/// Communicates directly with the Dynu IP update protocol, eliminating
/// the external `ddclient` daemon dependency.
pub struct DynuUpdater {
domain: String,
credential: String,
endpoint: String,
agent: Agent,
}
impl DynuUpdater {
pub fn new(domain: &str, credential: &str, timeout: Duration) -> Self {
Self::with_endpoint(domain, credential, DEFAULT_DYNU_ENDPOINT, timeout)
}
pub fn with_endpoint<S: Into<String>>(
domain: &str,
credential: &str,
endpoint: S,
timeout: Duration,
) -> Self {
let config = ureq::config::Config::builder()
.timeout_global(Some(timeout))
.build();
let agent = Agent::new_with_config(config);
Self {
domain: domain.to_string(),
credential: credential.to_string(),
endpoint: endpoint.into(),
agent,
}
}
}
impl DdnsUpdater for DynuUpdater {
fn name(&self) -> &str {
&self.domain
}
fn update(&self, ip: Ipv4Addr) -> Result<(), UpdateError> {
log::info!("Updating Dynu record for domain '{}' to {}", self.domain, ip);
let resp = self
.agent
.get(&self.endpoint)
.query("hostname", &self.domain)
.query("myip", ip.to_string())
.query("password", &self.credential)
.header("User-Agent", "ddns-monitor/0.1.0")
.call();
let mut response = resp.map_err(|e| UpdateError::NetworkError {
provider: format!("dynu:{}", self.domain),
details: e.to_string(),
})?;
let status_code = response.status();
let body = response
.body_mut()
.read_to_string()
.map_err(|e| UpdateError::NetworkError {
provider: format!("dynu:{}", self.domain),
details: format!("Failed reading response: {}", e),
})?;
let trimmed = body.trim();
log::debug!(
"Dynu response (status {}): '{}'",
status_code,
trimmed
);
if !status_code.is_success() {
return Err(UpdateError::NetworkError {
provider: format!("dynu:{}", self.domain),
details: format!("HTTP {} from Dynu: {}", status_code, trimmed),
});
}
if trimmed.starts_with("good") || trimmed.starts_with("nochg") {
log::info!("Dynu update confirmed for '{}': {}", self.domain, trimmed);
Ok(())
} else if trimmed.starts_with("badauth") {
Err(UpdateError::AuthError {
provider: format!("dynu:{}", self.domain),
details: "Invalid Dynu credentials or password".to_string(),
})
} else {
Err(UpdateError::ProviderRejected {
provider: format!("dynu:{}", self.domain),
message: trimmed.to_string(),
})
}
}
}
+1
View File
@@ -0,0 +1 @@
pub mod dynu;
+102
View File
@@ -0,0 +1,102 @@
use ddns_monitor::domain::traits::DdnsUpdater;
use ddns_monitor::plugins::dynu::DynuUpdater;
use std::net::Ipv4Addr;
use std::time::Duration;
#[test]
fn test_dynu_updater_good_response() {
let mut server = mockito::Server::new();
let _m = server
.mock("GET", "/nic/update")
.match_query(mockito::Matcher::AllOf(vec![
mockito::Matcher::UrlEncoded("hostname".into(), "test.example.com".into()),
mockito::Matcher::UrlEncoded("myip".into(), "198.51.100.5".into()),
mockito::Matcher::UrlEncoded("password".into(), "secret_pass".into()),
]))
.with_status(200)
.with_body("good 198.51.100.5\n")
.create();
let endpoint = format!("{}/nic/update", server.url());
let updater = DynuUpdater::with_endpoint(
"test.example.com",
"secret_pass",
endpoint,
Duration::from_secs(2),
);
let res = updater.update(Ipv4Addr::new(198, 51, 100, 5));
assert!(res.is_ok());
}
#[test]
fn test_dynu_updater_nochg_response() {
let mut server = mockito::Server::new();
let _m = server
.mock("GET", "/nic/update")
.match_query(mockito::Matcher::AllOf(vec![
mockito::Matcher::UrlEncoded("hostname".into(), "test.example.com".into()),
mockito::Matcher::UrlEncoded("myip".into(), "198.51.100.5".into()),
mockito::Matcher::UrlEncoded("password".into(), "secret_pass".into()),
]))
.with_status(200)
.with_body("nochg 198.51.100.5\n")
.create();
let endpoint = format!("{}/nic/update", server.url());
let updater = DynuUpdater::with_endpoint(
"test.example.com",
"secret_pass",
endpoint,
Duration::from_secs(2),
);
let res = updater.update(Ipv4Addr::new(198, 51, 100, 5));
assert!(res.is_ok());
}
#[test]
fn test_dynu_updater_badauth_response() {
let mut server = mockito::Server::new();
let _m = server
.mock("GET", "/nic/update")
.match_query(mockito::Matcher::Any)
.with_status(200)
.with_body("badauth\n")
.create();
let endpoint = format!("{}/nic/update", server.url());
let updater = DynuUpdater::with_endpoint(
"test.example.com",
"wrong_pass",
endpoint,
Duration::from_secs(2),
);
let res = updater.update(Ipv4Addr::new(198, 51, 100, 5));
assert!(res.is_err());
let err = res.unwrap_err();
assert!(err.to_string().contains("Authentication failed"));
}
#[test]
fn test_dynu_updater_server_error() {
let mut server = mockito::Server::new();
let _m = server
.mock("GET", "/nic/update")
.match_query(mockito::Matcher::Any)
.with_status(500)
.with_body("Internal Server Error")
.create();
let endpoint = format!("{}/nic/update", server.url());
let updater = DynuUpdater::with_endpoint(
"test.example.com",
"pass",
endpoint,
Duration::from_secs(2),
);
let res = updater.update(Ipv4Addr::new(198, 51, 100, 5));
assert!(res.is_err());
}