Bugfix: DNS Updater would not work for freshly created DNS records or records with outdated info
All checks were successful
Run Tests on Code / run-tests (push) Successful in 18s

This commit is contained in:
Neshura 2023-12-26 18:53:22 +01:00
parent 6714f447de
commit f969e21418
Signed by: Neshura
GPG key ID: B6983AAA6B9A7A6C
2 changed files with 115 additions and 114 deletions

View file

@ -354,5 +354,5 @@ pub(crate) struct CloudflareDnsRecord {
pub(crate) id: String, pub(crate) id: String,
pub(crate) name: String, pub(crate) name: String,
pub(crate) r#type: DnsRecordType, pub(crate) r#type: DnsRecordType,
content: String pub(crate) content: String
} }

View file

@ -57,9 +57,7 @@ impl Addresses {
Ok(ret) Ok(ret)
} }
fn check_new(&mut self, update: bool) -> bool { fn update(&mut self) {
let mut new = false;
match self.get_v4() { match self.get_v4() {
Ok(ip) => { Ok(ip) => {
if ip != self.ipv4 { if ip != self.ipv4 {
@ -68,8 +66,7 @@ impl Addresses {
true => info!("[INFO] {info_msg}"), true => info!("[INFO] {info_msg}"),
false => println!("[INFO] {info_msg}"), false => println!("[INFO] {info_msg}"),
} }
new = true; self.ipv4 = ip;
if update { self.ipv4 = ip }
} }
} }
Err(e) => { Err(e) => {
@ -89,8 +86,7 @@ impl Addresses {
true => info!("[INFO] {info_msg}"), true => info!("[INFO] {info_msg}"),
false => println!("[INFO] {info_msg}"), false => println!("[INFO] {info_msg}"),
} }
new = true; self.ipv6 = ip;
if update { self.ipv6 = ip }
} }
} }
Err(e) => { Err(e) => {
@ -101,8 +97,6 @@ impl Addresses {
} }
} }
} }
new
} }
fn get_v4(&self) -> Result<Ipv4Addr, reqwest::Error> { fn get_v4(&self) -> Result<Ipv4Addr, reqwest::Error> {
@ -194,113 +188,120 @@ fn main() {
} }
} }
if ips.check_new(false) { ips.update();
let mut error = false; for zone in &zone_cfgs {
for zone in &zone_cfgs { let cf_zone = match CloudflareZone::new(zone) {
let cf_zone = match CloudflareZone::new(zone) { Ok(data) => data,
Ok(data) => data, Err(e) => {
Err(e) => { let err_msg = format!("Cloudflare Token likely not set. Error: {}", e);
let err_msg = format!("Cloudflare Token likely not set. Error: {}", e); match connected_to_journal() {
match connected_to_journal() { true => error!("[ERROR] {err_msg}"),
true => error!("[ERROR] {err_msg}"), false => eprintln!("[ERROR] {err_msg}"),
false => eprintln!("[ERROR] {err_msg}"),
}
error = true;
continue
} }
}; continue
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
} }
};
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 { else {