Watch for ZT-Domain config changes and auto-renew certificates
(proxy: Proxy)
| 575 | |
| 576 | /// Watch for ZT-Domain config changes and auto-renew certificates |
| 577 | fn start_zt_domain_watch_task(proxy: Proxy) { |
| 578 | let kv_store = proxy.kv_store.clone(); |
| 579 | let certbot = proxy.certbot.clone(); |
| 580 | |
| 581 | let mut rx = kv_store.watch_zt_domain_configs(); |
| 582 | tokio::spawn(async move { |
| 583 | // Track known domains to detect additions |
| 584 | let mut known_domains = kv_store |
| 585 | .list_zt_domain_configs() |
| 586 | .into_iter() |
| 587 | .map(|c| c.domain) |
| 588 | .collect::<HashSet<_>>(); |
| 589 | |
| 590 | loop { |
| 591 | if rx.changed().await.is_err() { |
| 592 | break; |
| 593 | } |
| 594 | |
| 595 | // Get current domains |
| 596 | let current_domains: HashSet<String> = kv_store |
| 597 | .list_zt_domain_configs() |
| 598 | .into_iter() |
| 599 | .map(|c| c.domain) |
| 600 | .collect(); |
| 601 | |
| 602 | // Find newly added domains |
| 603 | let new_domains: Vec<String> = current_domains |
| 604 | .iter() |
| 605 | .filter(|d| !known_domains.contains(*d)) |
| 606 | .cloned() |
| 607 | .collect(); |
| 608 | |
| 609 | // Update known domains |
| 610 | known_domains = current_domains; |
| 611 | |
| 612 | // Trigger renewal for new domains |
| 613 | for domain in new_domains { |
| 614 | info!("ZT-Domain added: {domain}, attempting certificate request..."); |
| 615 | let certbot = certbot.clone(); |
| 616 | tokio::spawn(async move { |
| 617 | match certbot.try_renew(&domain, false).await { |
| 618 | Ok(renewed) => { |
| 619 | if renewed { |
| 620 | info!("cert[{domain}]: successfully issued/renewed"); |
| 621 | } else { |
| 622 | info!("cert[{domain}]: renewal not needed or another node is handling it"); |
| 623 | } |
| 624 | } |
| 625 | Err(e) => { |
| 626 | warn!("cert[{domain}]: auto-renewal failed: {e:?}"); |
| 627 | } |
| 628 | } |
| 629 | }); |
| 630 | } |
| 631 | } |
| 632 | }); |
| 633 | info!("ZT-Domain watch task started"); |
| 634 | } |
no test coverage detected