mirror of
https://github.com/timothymiller/cloudflare-ddns.git
synced 2026-05-06 09:53:40 -03:00
Add CachedCloudflareFilter
Introduce CachedCloudflareFilter that caches Cloudflare IP ranges and refreshes every 24 hours. If a refresh fails the previously cached ranges are retained and a warning is emitted. Wire the cache through main and updater so Cloudflare fetches reuse the cached result. Update tests and bump crate version to 2.0.7
This commit is contained in:
18
src/main.rs
18
src/main.rs
@@ -116,12 +116,14 @@ async fn main() {
|
||||
// Start heartbeat
|
||||
heartbeat.start().await;
|
||||
|
||||
let mut cf_cache = cf_ip_filter::CachedCloudflareFilter::new();
|
||||
|
||||
if app_config.legacy_mode {
|
||||
// --- Legacy mode (original cloudflare-ddns behavior) ---
|
||||
run_legacy_mode(&app_config, &handle, ¬ifier, &heartbeat, &ppfmt, running).await;
|
||||
run_legacy_mode(&app_config, &handle, ¬ifier, &heartbeat, &ppfmt, running, &mut cf_cache).await;
|
||||
} else {
|
||||
// --- Env var mode (cf-ddns behavior) ---
|
||||
run_env_mode(&app_config, &handle, ¬ifier, &heartbeat, &ppfmt, running).await;
|
||||
run_env_mode(&app_config, &handle, ¬ifier, &heartbeat, &ppfmt, running, &mut cf_cache).await;
|
||||
}
|
||||
|
||||
// On shutdown: delete records if configured
|
||||
@@ -143,6 +145,7 @@ async fn run_legacy_mode(
|
||||
heartbeat: &Heartbeat,
|
||||
ppfmt: &PP,
|
||||
running: Arc<AtomicBool>,
|
||||
cf_cache: &mut cf_ip_filter::CachedCloudflareFilter,
|
||||
) {
|
||||
let legacy = match &config.legacy_config {
|
||||
Some(l) => l,
|
||||
@@ -165,7 +168,7 @@ async fn run_legacy_mode(
|
||||
}
|
||||
|
||||
while running.load(Ordering::SeqCst) {
|
||||
updater::update_once(config, handle, notifier, heartbeat, ppfmt).await;
|
||||
updater::update_once(config, handle, notifier, heartbeat, cf_cache, ppfmt).await;
|
||||
|
||||
for _ in 0..legacy.ttl {
|
||||
if !running.load(Ordering::SeqCst) {
|
||||
@@ -175,7 +178,7 @@ async fn run_legacy_mode(
|
||||
}
|
||||
}
|
||||
} else {
|
||||
updater::update_once(config, handle, notifier, heartbeat, ppfmt).await;
|
||||
updater::update_once(config, handle, notifier, heartbeat, cf_cache, ppfmt).await;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -186,11 +189,12 @@ async fn run_env_mode(
|
||||
heartbeat: &Heartbeat,
|
||||
ppfmt: &PP,
|
||||
running: Arc<AtomicBool>,
|
||||
cf_cache: &mut cf_ip_filter::CachedCloudflareFilter,
|
||||
) {
|
||||
match &config.update_cron {
|
||||
CronSchedule::Once => {
|
||||
if config.update_on_start {
|
||||
updater::update_once(config, handle, notifier, heartbeat, ppfmt).await;
|
||||
updater::update_once(config, handle, notifier, heartbeat, cf_cache, ppfmt).await;
|
||||
}
|
||||
}
|
||||
schedule => {
|
||||
@@ -206,7 +210,7 @@ async fn run_env_mode(
|
||||
|
||||
// Update on start if configured
|
||||
if config.update_on_start {
|
||||
updater::update_once(config, handle, notifier, heartbeat, ppfmt).await;
|
||||
updater::update_once(config, handle, notifier, heartbeat, cf_cache, ppfmt).await;
|
||||
}
|
||||
|
||||
// Main loop
|
||||
@@ -233,7 +237,7 @@ async fn run_env_mode(
|
||||
return;
|
||||
}
|
||||
|
||||
updater::update_once(config, handle, notifier, heartbeat, ppfmt).await;
|
||||
updater::update_once(config, handle, notifier, heartbeat, cf_cache, ppfmt).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user