Added timer based execution and changed change printing
This commit is contained in:
parent
3bae2f68de
commit
b6dd98f224
3 changed files with 252 additions and 27 deletions
src
98
src/main.rs
98
src/main.rs
|
@ -1,12 +1,49 @@
|
|||
use reqwest::blocking::get;
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use std::{fs, process::exit};
|
||||
use std::{fs, process::exit, thread::{sleep}};
|
||||
use chrono::{Utc, Duration, Date};
|
||||
mod cloudflare;
|
||||
|
||||
fn default_key() -> String {
|
||||
return "".to_string();
|
||||
}
|
||||
|
||||
struct ChangeTracker {
|
||||
created: u16,
|
||||
updated: u16,
|
||||
unchanged: u16,
|
||||
error: u16
|
||||
}
|
||||
|
||||
impl ChangeTracker {
|
||||
fn new() -> ChangeTracker {
|
||||
ChangeTracker {
|
||||
created: 0,
|
||||
updated: 0,
|
||||
unchanged: 0,
|
||||
error: 0
|
||||
}
|
||||
}
|
||||
|
||||
fn print(&self, f: &str) {
|
||||
match f {
|
||||
"" => {
|
||||
self.print("simple");
|
||||
}
|
||||
"simple" => {
|
||||
println!("{} created", self.created);
|
||||
println!("{} updated", self.updated);
|
||||
println!("{} unchanged", self.unchanged);
|
||||
println!("{} errors", self.error);
|
||||
}
|
||||
&_ => {
|
||||
println!("unknown format pattern '{}', defaulting to 'simple'", f);
|
||||
self.print("simple");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct DnsEntry {
|
||||
name: String,
|
||||
|
@ -113,7 +150,7 @@ impl Ips {
|
|||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
fn update_dns() {
|
||||
let mut config = DnsConfig::new();
|
||||
config.load();
|
||||
|
||||
|
@ -122,15 +159,9 @@ fn main() {
|
|||
let mut ips = Ips::new();
|
||||
ips.get(&config.ipv6_interface);
|
||||
|
||||
let mut created4: u16 = 0;
|
||||
let mut updated4: u16 = 0;
|
||||
let mut unchanged4: u16 = 0;
|
||||
let mut error4: u16 = 0;
|
||||
let mut change4 = ChangeTracker::new();
|
||||
|
||||
let mut created6: u16 = 0;
|
||||
let mut updated6: u16 = 0;
|
||||
let mut unchanged6: u16 = 0;
|
||||
let mut error6: u16 = 0;
|
||||
let mut change6 = ChangeTracker::new();
|
||||
|
||||
for zone in config.zones {
|
||||
let mut agent = cloudflare::Instance::new(&config.api_key);
|
||||
|
@ -148,12 +179,12 @@ fn main() {
|
|||
if cloudflare_entry.is_ip_new(&ips.ipv4) {
|
||||
let success = agent.update_entry(cloudflare_entry, &ips.ipv4);
|
||||
if success {
|
||||
updated4 += 1
|
||||
change4.updated += 1
|
||||
} else {
|
||||
error4 += 1
|
||||
change4.error += 1
|
||||
}
|
||||
} else {
|
||||
unchanged4 += 1;
|
||||
change4.unchanged += 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -165,12 +196,12 @@ fn main() {
|
|||
if cloudflare_entry.is_ip_new(&ipv6) {
|
||||
let success = agent.update_entry(cloudflare_entry, &ipv6);
|
||||
if success {
|
||||
updated6 += 1
|
||||
change6.updated += 1
|
||||
} else {
|
||||
error6 += 1
|
||||
change6.error += 1
|
||||
}
|
||||
} else {
|
||||
unchanged6 += 1;
|
||||
change6.unchanged += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -181,7 +212,7 @@ fn main() {
|
|||
if !found4 && entry.type4 {
|
||||
let success = agent.create_entry(&zone.id, "A", &entry.name, &ips.ipv4);
|
||||
if success {
|
||||
created4 += 1
|
||||
change4.created += 1
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -189,7 +220,7 @@ fn main() {
|
|||
let ipv6 = ips.ipv6base.clone() + entry.interface.as_ref().unwrap();
|
||||
let success = agent.create_entry(&zone.id, "AAAA", &entry.name, &ipv6);
|
||||
if success {
|
||||
created6 += 1
|
||||
change6.created += 1
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -197,13 +228,28 @@ fn main() {
|
|||
//println!("{:#?}", zone.domain); // DEBUG
|
||||
}
|
||||
println!("++++A Records+++");
|
||||
println!("{} created", created4);
|
||||
println!("{} updated", updated4);
|
||||
println!("{} unchanged", unchanged4);
|
||||
println!("{} errors", error4);
|
||||
change4.print("simple");
|
||||
println!("++AAAA Records++");
|
||||
println!("{} created", created6);
|
||||
println!("{} updated", updated6);
|
||||
println!("{} unchanged", unchanged6);
|
||||
println!("{} errors", error6);
|
||||
change6.print("simple")
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
let mut now = Utc::now() - Duration::seconds(59);
|
||||
let mut current = now;
|
||||
|
||||
println!("Initial Time is {} {}", now.format("%H:%M:%S"), now.timezone());
|
||||
|
||||
loop {
|
||||
now = Utc::now();
|
||||
if now >= current + Duration::seconds(60) {
|
||||
current = now;
|
||||
|
||||
println!("Starting DNS Update at {} {}", now.format("%H:%M:%S"), now.timezone());
|
||||
update_dns();
|
||||
}
|
||||
else {
|
||||
sleep(std::time::Duration::from_millis(500));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue