From f969e21418c02ce2b03e50b89b83958216b9b980 Mon Sep 17 00:00:00 2001 From: Neshura Date: Tue, 26 Dec 2023 18:53:22 +0100 Subject: [PATCH] Bugfix: DNS Updater would not work for freshly created DNS records or records with outdated info --- src/cloudflare.rs | 2 +- src/main.rs | 227 +++++++++++++++++++++++----------------------- 2 files changed, 115 insertions(+), 114 deletions(-) diff --git a/src/cloudflare.rs b/src/cloudflare.rs index ffe7bbb..8056993 100644 --- a/src/cloudflare.rs +++ b/src/cloudflare.rs @@ -354,5 +354,5 @@ pub(crate) struct CloudflareDnsRecord { pub(crate) id: String, pub(crate) name: String, pub(crate) r#type: DnsRecordType, - content: String + pub(crate) content: String } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 988247e..e3b844e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -57,9 +57,7 @@ impl Addresses { Ok(ret) } - fn check_new(&mut self, update: bool) -> bool { - let mut new = false; - + fn update(&mut self) { match self.get_v4() { Ok(ip) => { if ip != self.ipv4 { @@ -68,8 +66,7 @@ impl Addresses { true => info!("[INFO] {info_msg}"), false => println!("[INFO] {info_msg}"), } - new = true; - if update { self.ipv4 = ip } + self.ipv4 = ip; } } Err(e) => { @@ -89,8 +86,7 @@ impl Addresses { true => info!("[INFO] {info_msg}"), false => println!("[INFO] {info_msg}"), } - new = true; - if update { self.ipv6 = ip } + self.ipv6 = ip; } } Err(e) => { @@ -101,8 +97,6 @@ impl Addresses { } } } - - new } fn get_v4(&self) -> Result { @@ -194,113 +188,120 @@ fn main() { } } - if ips.check_new(false) { - let mut error = false; - for zone in &zone_cfgs { - let cf_zone = match CloudflareZone::new(zone) { - Ok(data) => data, - Err(e) => { - let err_msg = format!("Cloudflare Token likely not set. Error: {}", e); - match connected_to_journal() { - true => error!("[ERROR] {err_msg}"), - false => eprintln!("[ERROR] {err_msg}"), - } - error = true; - continue + ips.update(); + for zone in &zone_cfgs { + let cf_zone = match CloudflareZone::new(zone) { + Ok(data) => data, + Err(e) => { + let err_msg = format!("Cloudflare Token likely not set. Error: {}", e); + match connected_to_journal() { + true => error!("[ERROR] {err_msg}"), + false => eprintln!("[ERROR] {err_msg}"), } - }; - - let cf_entries = match cf_zone.get_entries() { - Ok(entries) => entries, - Err(_) => { - error = true; - continue - } - }; - - for entry in &zone.entries { - let ipv6; - let ipv4; - match entry.r#type[..] { - [DnsRecordType::AAAA, DnsRecordType::A] => { - ipv6 = match ifaces.full_v6(&entry.interface, ips.ipv6) { - Ok(ip) => Some(ip), - Err(_) => { - error = true; - continue - } - }; - ipv4 = Some(ips.ipv4); - }, - [DnsRecordType::A, DnsRecordType::AAAA] => { - ipv6 = match ifaces.full_v6(&entry.interface, ips.ipv6) { - Ok(ip) => Some(ip), - Err(_) => { - error = true; - continue - } - }; - ipv4 = Some(ips.ipv4); - }, - [DnsRecordType::AAAA] => { - ipv6 = match ifaces.full_v6(&entry.interface, ips.ipv6) { - Ok(ip) => Some(ip), - Err(_) => { - error = true; - continue - } - }; - ipv4 = None; - }, - [DnsRecordType::A] => { - ipv6 = None; - ipv4 = Some(ips.ipv4); - }, - _ => { - let warn_msg = "Config contains unsupported type identifier"; - match connected_to_journal() { - true => warn!("[WARN] {warn_msg}"), - false => println!("[WARN] {warn_msg}"), - } - continue - } - } - - for r#type in &entry.r#type { - let cf_entry = cf_entries.iter().find(|cf_entry| { - cf_entry.name == entry.name && &cf_entry.r#type == r#type - }); - - if let Some(cf_entry) = cf_entry { - match cf_zone.update(entry, r#type, &cf_entry.id, ipv6, ipv4) { - Ok(_) => { - let info_msg = format!("Updated DNS Record(s) for entry '{}' in zone '{}'", entry.name, zone.name); - match connected_to_journal() { - true => warn!("[INFO] {info_msg}"), - false => println!("[INFO] {info_msg}"), - } - }, - Err(_) => error = true, - }; - } - else { - match cf_zone.create(entry, r#type, ipv6, ipv4) { - Ok(_) => { - let info_msg = format!("Created DNS Record(s) for entry '{}' in zone '{}'", entry.name, zone.name); - match connected_to_journal() { - true => info!("[INFO] {info_msg}"), - false => println!("[INFO] {info_msg}"), - } - }, - Err(_) => error = true, - }; - } - } - - // handle return values + continue } + }; + + let cf_entries = match cf_zone.get_entries() { + Ok(entries) => entries, + Err(_) => { + continue + } + }; + + for entry in &zone.entries { + let ipv6; + let ipv4; + match entry.r#type[..] { + [DnsRecordType::AAAA, DnsRecordType::A] => { + ipv6 = match ifaces.full_v6(&entry.interface, ips.ipv6) { + Ok(ip) => Some(ip), + Err(_) => { + continue + } + }; + ipv4 = Some(ips.ipv4); + }, + [DnsRecordType::A, DnsRecordType::AAAA] => { + ipv6 = match ifaces.full_v6(&entry.interface, ips.ipv6) { + Ok(ip) => Some(ip), + Err(_) => { + continue + } + }; + ipv4 = Some(ips.ipv4); + }, + [DnsRecordType::AAAA] => { + ipv6 = match ifaces.full_v6(&entry.interface, ips.ipv6) { + Ok(ip) => Some(ip), + Err(_) => { + continue + } + }; + ipv4 = None; + }, + [DnsRecordType::A] => { + ipv6 = None; + ipv4 = Some(ips.ipv4); + }, + _ => { + let warn_msg = "Config contains unsupported type identifier"; + match connected_to_journal() { + true => warn!("[WARN] {warn_msg}"), + false => println!("[WARN] {warn_msg}"), + } + continue + } + } + + for r#type in &entry.r#type { + let cf_entry = cf_entries.iter().find(|cf_entry| { + cf_entry.name == entry.name && &cf_entry.r#type == r#type + }); + + match cf_entry.unwrap().r#type { + DnsRecordType::A => { + let cf_ip = Ipv4Addr::from_str(cf_entry.unwrap().content.as_str()).expect("Cloudflare return should always be valid IP"); + if Some(cf_ip) == ipv4 { + continue + } + }, + DnsRecordType::AAAA => { + let cf_ip = Ipv6Addr::from_str(cf_entry.unwrap().content.as_str()).expect("Cloudflare return should always be valid IP"); + if Some(cf_ip) == ipv6 { + continue + } + }, + _ => {}, + } + + if let Some(cf_entry) = cf_entry { + match cf_zone.update(entry, r#type, &cf_entry.id, ipv6, ipv4) { + Ok(_) => { + let info_msg = format!("Updated DNS Record(s) for entry '{}' in zone '{}'", entry.name, zone.name); + match connected_to_journal() { + true => warn!("[INFO] {info_msg}"), + false => println!("[INFO] {info_msg}"), + } + }, + Err(_) => {}, + }; + } + else { + match cf_zone.create(entry, r#type, ipv6, ipv4) { + Ok(_) => { + let info_msg = format!("Created DNS Record(s) for entry '{}' in zone '{}'", entry.name, zone.name); + match connected_to_journal() { + true => info!("[INFO] {info_msg}"), + false => println!("[INFO] {info_msg}"), + } + }, + Err(_) => {}, + }; + } + } + // handle return values } - if !error { ips.check_new(true); } } } else {