diff --git a/src/api.rs b/src/api.rs index f7ab043..84381ed 100644 --- a/src/api.rs +++ b/src/api.rs @@ -9,9 +9,9 @@ pub struct CurrentIP { pub struct IPAddresses { /// The fetched IPv4 address - pub ipv4: String, + pub ipv4: Option, /// The fetched IPv6 address - pub ipv6: String, + pub ipv6: Option, } /// Struct for the Cloudflares response @@ -67,7 +67,7 @@ const CF_BASE_URL: &str = "https://api.cloudflare.com/client/v4/zones/"; /// use cloudflare_dns_updater::api; /// /// let ip = api::get_current_ip().await?; -/// println!("{}", ip); +/// println!("{}", Some(ip.ip)); /// ``` /// /// # Errors @@ -82,17 +82,19 @@ const CF_BASE_URL: &str = "https://api.cloudflare.com/client/v4/zones/"; /// # See Also /// * [https://api.ipify.org](https://api.ipify.org) pub async fn get_current_ip() -> Result { - let ipv4 = reqwest::get(IPV4_ADDRESS_URL) - .await? - .json::() - .await? - .ip; - let ipv6 = reqwest::get(IPV6_ADDRESS_URL) - .await? - .json::() - .await? - .ip; - let cur_ips = IPAddresses { ipv4, ipv6 }; + let mut cur_ips = IPAddresses { + ipv4: None, + ipv6: None, + }; + match reqwest::get(IPV4_ADDRESS_URL).await { + Ok(ipv4) => cur_ips.ipv4 = Some(ipv4.json::().await?.ip), + Err(_) => (), + }; + match reqwest::get(IPV6_ADDRESS_URL).await { + Ok(ipv6) => cur_ips.ipv6 = Some(ipv6.json::().await?.ip), + Err(_) => (), + }; + Ok(cur_ips) } diff --git a/src/main.rs b/src/main.rs index 7ec9447..20f1547 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,9 +32,8 @@ async fn main() { /// * `Ok(())` - If the IP addresses were updated successfully /// * `Err(e)` - If the IP addresses could not be updated async fn check_and_update_ip(config: &Config) -> Result<(), Box> { - print!("\nGetting current IP addresses"); + println!("Getting current IP addresses..."); let cur_ip = api::get_current_ip().await?; - println!(" - {}, {}\n", cur_ip.ipv4, cur_ip.ipv6); for k in 0..config.keys.len() { println!("Updating zones for key {}", config.keys[k].auth_key); for z in 0..config.keys[k].zones.len() { @@ -56,55 +55,68 @@ async fn check_and_update_ip(config: &Config) -> Result<(), Box println!(" - Record updated"), - Err(e) => println!(" - Error: {}", e), + match cur_ip.ipv4.clone() { + Some(ipv4) => { + println!("\nCurrent IPv4 address: {}", ipv4); + for i in 0..config.keys[k].zones[z].a_records.len() { + if !a_record_ips.result.get(i).is_none() && !a_record_ips.result[i].locked { + if ipv4 != a_record_ips.result[i].content { + print!( + "Updating record {} from {} to {}", + a_record_ips.result[i].name, + a_record_ips.result[i].content, + ipv4 + ); + match api::update_record( + &a_record_ips.result[i], + &ipv4, + &config.keys[k].auth_key, + "A", + ) + .await + { + Ok(()) => println!(" - Record updated"), + Err(e) => println!(" - Error: {}", e), + } + } else { + println!("Record {} is up to date", a_record_ips.result[i].name); + } } - } else { - println!("Record {} is up to date", a_record_ips.result[i].name); } } + None => println!("No IPv4 address found, skipping A records"), } - for i in 0..config.keys[k].zones[z].aaaa_records.len() { - if !aaaa_record_ips.result.get(i).is_none() && !aaaa_record_ips.result[i].locked { - if cur_ip.ipv6 != aaaa_record_ips.result[i].content { - print!( - "Updating record {} from {} to {}", - aaaa_record_ips.result[i].name, - aaaa_record_ips.result[i].content, - cur_ip.ipv6 - ); - match api::update_record( - &aaaa_record_ips.result[i], - &cur_ip.ipv6, - &config.keys[k].auth_key, - "AAAA", - ) - .await + match cur_ip.ipv6.clone() { + Some(ipv6) => { + for i in 0..config.keys[k].zones[z].aaaa_records.len() { + if !aaaa_record_ips.result.get(i).is_none() + && !aaaa_record_ips.result[i].locked { - Ok(()) => println!(" - Record updated"), - Err(e) => println!(" - Error: {}", e), + if ipv6 != aaaa_record_ips.result[i].content { + print!( + "Updating record {} from {} to {}", + aaaa_record_ips.result[i].name, + aaaa_record_ips.result[i].content, + ipv6 + ); + match api::update_record( + &aaaa_record_ips.result[i], + &ipv6, + &config.keys[k].auth_key, + "AAAA", + ) + .await + { + Ok(()) => println!(" - Record updated"), + Err(e) => println!(" - Error: {}", e), + } + } else { + println!("Record {} is up to date", aaaa_record_ips.result[i].name); + } } - } else { - println!("Record {} is up to date", aaaa_record_ips.result[i].name); } } + None => println!("No IPv6 address found, skipping AAAA records"), } println!("Done updating zone") }