2021-09-10 16:41:39 +00:00
|
|
|
use anyhow::Result;
|
|
|
|
use log::*;
|
|
|
|
use tokio::{
|
2021-09-11 16:55:26 +00:00
|
|
|
select,
|
|
|
|
sync::watch,
|
|
|
|
time::{self, Duration},
|
|
|
|
};
|
2021-09-10 16:41:39 +00:00
|
|
|
|
|
|
|
use crate::config::RuntimeConfigAcme;
|
|
|
|
use crate::messages;
|
|
|
|
|
|
|
|
pub struct AcmeActor {
|
|
|
|
email: String,
|
|
|
|
last_ports: messages::PublicExposedPorts,
|
|
|
|
refresh: Duration,
|
|
|
|
|
|
|
|
rx_ports: watch::Receiver<messages::PublicExposedPorts>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AcmeActor {
|
|
|
|
pub async fn new(
|
2021-09-11 16:55:26 +00:00
|
|
|
config: Option<RuntimeConfigAcme>,
|
|
|
|
rxp: &watch::Receiver<messages::PublicExposedPorts>,
|
|
|
|
) -> Result<Option<Self>> {
|
2021-09-10 16:41:39 +00:00
|
|
|
if config.is_none() {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
let config = config.unwrap();
|
|
|
|
|
2021-09-11 16:55:26 +00:00
|
|
|
let ctx = Self {
|
2021-09-10 16:41:39 +00:00
|
|
|
email: config.email,
|
2021-09-11 16:55:26 +00:00
|
|
|
last_ports: messages::PublicExposedPorts::new(),
|
2021-09-10 16:41:39 +00:00
|
|
|
refresh: config.refresh_time,
|
|
|
|
rx_ports: rxp.clone(),
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(Some(ctx))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn listen(&mut self) -> Result<()> {
|
|
|
|
let mut interval = time::interval(self.refresh);
|
|
|
|
loop {
|
|
|
|
// 1. Wait for an event
|
|
|
|
let new_ports = select! {
|
|
|
|
Some(ports) = self.rx_ports.recv() => Some(ports),
|
|
|
|
_ = interval.tick() => None,
|
|
|
|
else => return Ok(()) // Sender dropped, terminate loop.
|
|
|
|
};
|
|
|
|
|
|
|
|
// 2. Update last ports if needed
|
2021-09-11 16:55:26 +00:00
|
|
|
if let Some(p) = new_ports {
|
|
|
|
self.last_ports = p;
|
|
|
|
}
|
2021-09-10 16:41:39 +00:00
|
|
|
|
|
|
|
// 3. Flush IGD requests
|
|
|
|
match self.do_acme().await {
|
|
|
|
Ok(()) => debug!("Successfully updated ACME"),
|
|
|
|
Err(e) => error!("An error occured while updating ACME. {}", e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn do_acme(&self) -> Result<()> {
|
|
|
|
debug!("Doing ACME!!!");
|
|
|
|
debug!("{:#?}", self.last_ports);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-09-11 16:55:26 +00:00
|
|
|
}
|