Added timer based execution and changed change printing

This commit is contained in:
Neshura 2023-02-27 15:04:12 +01:00
parent 3bae2f68de
commit b6dd98f224
No known key found for this signature in database
GPG key ID: ACDF5B6EBECF6B0A
3 changed files with 252 additions and 27 deletions

180
Cargo.lock generated
View file

@ -2,6 +2,15 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3 version = 3
[[package]]
name = "android_system_properties"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "autocfg" name = "autocfg"
version = "1.1.0" version = "1.1.0"
@ -44,10 +53,26 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chrono"
version = "0.4.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "16b0a3d9ed01224b22057780a37bb8c5dbfe1be8ba48678e7bf57ec4b385411f"
dependencies = [
"iana-time-zone",
"js-sys",
"num-integer",
"num-traits",
"time",
"wasm-bindgen",
"winapi",
]
[[package]] [[package]]
name = "cloudflare-dns-updater" name = "cloudflare-dns-updater"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"chrono",
"reqwest", "reqwest",
"serde", "serde",
"serde_derive", "serde_derive",
@ -55,6 +80,16 @@ dependencies = [
"strum_macros", "strum_macros",
] ]
[[package]]
name = "codespan-reporting"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3538270d33cc669650c4b093848450d380def10c331d38c768e34cac80576e6e"
dependencies = [
"termcolor",
"unicode-width",
]
[[package]] [[package]]
name = "core-foundation" name = "core-foundation"
version = "0.9.3" version = "0.9.3"
@ -71,6 +106,50 @@ version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc" checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc"
[[package]]
name = "cxx"
version = "1.0.91"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "86d3488e7665a7a483b57e25bdd90d0aeb2bc7608c8d0346acf2ad3f1caf1d62"
dependencies = [
"cc",
"cxxbridge-flags",
"cxxbridge-macro",
"link-cplusplus",
]
[[package]]
name = "cxx-build"
version = "1.0.91"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "48fcaf066a053a41a81dfb14d57d99738b767febb8b735c3016e469fac5da690"
dependencies = [
"cc",
"codespan-reporting",
"once_cell",
"proc-macro2",
"quote",
"scratch",
"syn",
]
[[package]]
name = "cxxbridge-flags"
version = "1.0.91"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2ef98b8b717a829ca5603af80e1f9e2e48013ab227b68ef37872ef84ee479bf"
[[package]]
name = "cxxbridge-macro"
version = "1.0.91"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "086c685979a698443656e5cf7856c95c642295a38599f12fb1ff76fb28d19892"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "encoding_rs" name = "encoding_rs"
version = "0.8.32" version = "0.8.32"
@ -278,6 +357,30 @@ dependencies = [
"tokio-native-tls", "tokio-native-tls",
] ]
[[package]]
name = "iana-time-zone"
version = "0.1.53"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "64c122667b287044802d6ce17ee2ddf13207ed924c712de9a66a5814d5b64765"
dependencies = [
"android_system_properties",
"core-foundation-sys",
"iana-time-zone-haiku",
"js-sys",
"wasm-bindgen",
"winapi",
]
[[package]]
name = "iana-time-zone-haiku"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0703ae284fc167426161c2e3f1da3ea71d94b21bedbcc9494e92b28e334e3dca"
dependencies = [
"cxx",
"cxx-build",
]
[[package]] [[package]]
name = "idna" name = "idna"
version = "0.3.0" version = "0.3.0"
@ -340,6 +443,15 @@ version = "0.2.139"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79" checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79"
[[package]]
name = "link-cplusplus"
version = "1.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ecd207c9c713c34f95a097a5b029ac2ce6010530c7b49d7fea24d977dede04f5"
dependencies = [
"cc",
]
[[package]] [[package]]
name = "log" name = "log"
version = "0.4.17" version = "0.4.17"
@ -369,7 +481,7 @@ checksum = "5b9d9a46eff5b4ff64b45a9e316a6d1e0bc719ef429cbec4dc630684212bfdf9"
dependencies = [ dependencies = [
"libc", "libc",
"log", "log",
"wasi", "wasi 0.11.0+wasi-snapshot-preview1",
"windows-sys 0.45.0", "windows-sys 0.45.0",
] ]
@ -391,6 +503,25 @@ dependencies = [
"tempfile", "tempfile",
] ]
[[package]]
name = "num-integer"
version = "0.1.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9"
dependencies = [
"autocfg",
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd"
dependencies = [
"autocfg",
]
[[package]] [[package]]
name = "num_cpus" name = "num_cpus"
version = "1.15.0" version = "1.15.0"
@ -570,6 +701,12 @@ dependencies = [
"windows-sys 0.42.0", "windows-sys 0.42.0",
] ]
[[package]]
name = "scratch"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ddccb15bcce173023b3fedd9436f882a0739b8dfb45e4f6b6002bee5929f61b2"
[[package]] [[package]]
name = "security-framework" name = "security-framework"
version = "2.8.2" version = "2.8.2"
@ -690,6 +827,26 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "termcolor"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be55cf8942feac5c765c2c993422806843c9a9a45d4d5c407ad6dd2ea95eb9b6"
dependencies = [
"winapi-util",
]
[[package]]
name = "time"
version = "0.1.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b797afad3f312d1c66a56d11d0316f916356d11bd158fbc6ca6389ff6bf805a"
dependencies = [
"libc",
"wasi 0.10.0+wasi-snapshot-preview1",
"winapi",
]
[[package]] [[package]]
name = "tinyvec" name = "tinyvec"
version = "1.6.0" version = "1.6.0"
@ -799,6 +956,12 @@ dependencies = [
"tinyvec", "tinyvec",
] ]
[[package]]
name = "unicode-width"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b"
[[package]] [[package]]
name = "url" name = "url"
version = "2.3.1" version = "2.3.1"
@ -826,6 +989,12 @@ dependencies = [
"try-lock", "try-lock",
] ]
[[package]]
name = "wasi"
version = "0.10.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f"
[[package]] [[package]]
name = "wasi" name = "wasi"
version = "0.11.0+wasi-snapshot-preview1" version = "0.11.0+wasi-snapshot-preview1"
@ -924,6 +1093,15 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi",
]
[[package]] [[package]]
name = "winapi-x86_64-pc-windows-gnu" name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0" version = "0.4.0"

View file

@ -6,6 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
chrono = "0.4.23"
reqwest = { version = "0.11.14", features = ["blocking", "json"] } reqwest = { version = "0.11.14", features = ["blocking", "json"] }
serde = "1.0.152" serde = "1.0.152"
serde_derive = "1.0.152" serde_derive = "1.0.152"

View file

@ -1,12 +1,49 @@
use reqwest::blocking::get; use reqwest::blocking::get;
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use std::{fs, process::exit}; use std::{fs, process::exit, thread::{sleep}};
use chrono::{Utc, Duration, Date};
mod cloudflare; mod cloudflare;
fn default_key() -> String { fn default_key() -> String {
return "".to_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)] #[derive(Serialize, Deserialize)]
struct DnsEntry { struct DnsEntry {
name: String, name: String,
@ -113,7 +150,7 @@ impl Ips {
} }
} }
fn main() { fn update_dns() {
let mut config = DnsConfig::new(); let mut config = DnsConfig::new();
config.load(); config.load();
@ -122,15 +159,9 @@ fn main() {
let mut ips = Ips::new(); let mut ips = Ips::new();
ips.get(&config.ipv6_interface); ips.get(&config.ipv6_interface);
let mut created4: u16 = 0; let mut change4 = ChangeTracker::new();
let mut updated4: u16 = 0;
let mut unchanged4: u16 = 0;
let mut error4: u16 = 0;
let mut created6: u16 = 0; let mut change6 = ChangeTracker::new();
let mut updated6: u16 = 0;
let mut unchanged6: u16 = 0;
let mut error6: u16 = 0;
for zone in config.zones { for zone in config.zones {
let mut agent = cloudflare::Instance::new(&config.api_key); let mut agent = cloudflare::Instance::new(&config.api_key);
@ -148,12 +179,12 @@ fn main() {
if cloudflare_entry.is_ip_new(&ips.ipv4) { if cloudflare_entry.is_ip_new(&ips.ipv4) {
let success = agent.update_entry(cloudflare_entry, &ips.ipv4); let success = agent.update_entry(cloudflare_entry, &ips.ipv4);
if success { if success {
updated4 += 1 change4.updated += 1
} else { } else {
error4 += 1 change4.error += 1
} }
} else { } else {
unchanged4 += 1; change4.unchanged += 1;
} }
} }
@ -165,12 +196,12 @@ fn main() {
if cloudflare_entry.is_ip_new(&ipv6) { if cloudflare_entry.is_ip_new(&ipv6) {
let success = agent.update_entry(cloudflare_entry, &ipv6); let success = agent.update_entry(cloudflare_entry, &ipv6);
if success { if success {
updated6 += 1 change6.updated += 1
} else { } else {
error6 += 1 change6.error += 1
} }
} else { } else {
unchanged6 += 1; change6.unchanged += 1;
} }
} }
} }
@ -181,7 +212,7 @@ fn main() {
if !found4 && entry.type4 { if !found4 && entry.type4 {
let success = agent.create_entry(&zone.id, "A", &entry.name, &ips.ipv4); let success = agent.create_entry(&zone.id, "A", &entry.name, &ips.ipv4);
if success { if success {
created4 += 1 change4.created += 1
}; };
} }
@ -189,7 +220,7 @@ fn main() {
let ipv6 = ips.ipv6base.clone() + entry.interface.as_ref().unwrap(); let ipv6 = ips.ipv6base.clone() + entry.interface.as_ref().unwrap();
let success = agent.create_entry(&zone.id, "AAAA", &entry.name, &ipv6); let success = agent.create_entry(&zone.id, "AAAA", &entry.name, &ipv6);
if success { if success {
created6 += 1 change6.created += 1
}; };
} }
} }
@ -197,13 +228,28 @@ fn main() {
//println!("{:#?}", zone.domain); // DEBUG //println!("{:#?}", zone.domain); // DEBUG
} }
println!("++++A Records+++"); println!("++++A Records+++");
println!("{} created", created4); change4.print("simple");
println!("{} updated", updated4);
println!("{} unchanged", unchanged4);
println!("{} errors", error4);
println!("++AAAA Records++"); println!("++AAAA Records++");
println!("{} created", created6); change6.print("simple")
println!("{} updated", updated6); }
println!("{} unchanged", unchanged6);
println!("{} errors", error6); 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));
}
}
} }