diff options
Diffstat (limited to 'src/igd_actor.rs')
-rw-r--r-- | src/igd_actor.rs | 35 |
1 files changed, 22 insertions, 13 deletions
diff --git a/src/igd_actor.rs b/src/igd_actor.rs index 55d9c5f..19a7e93 100644 --- a/src/igd_actor.rs +++ b/src/igd_actor.rs @@ -1,43 +1,52 @@ +use std::net::SocketAddrV4; + +use anyhow::{Result, Context}; use igd::aio::*; use igd::PortMappingProtocol; -use std::net::SocketAddrV4; use log::*; -use anyhow::{Result, Context}; use tokio::{ select, sync::watch, time::{ + Duration, self, - Duration }}; + +use crate::config::RuntimeConfigIgd; use crate::messages; pub struct IgdActor { - last_ports: messages::PublicExposedPorts, - rx_ports: watch::Receiver<messages::PublicExposedPorts>, + expire: Duration, gateway: Gateway, + last_ports: messages::PublicExposedPorts, + private_ip: String, refresh: Duration, - expire: Duration, - private_ip: String + + rx_ports: watch::Receiver<messages::PublicExposedPorts>, } impl IgdActor { - pub async fn new(priv_ip: &str, refresh: Duration, expire: Duration, rxp: &watch::Receiver<messages::PublicExposedPorts>) -> Result<Self> { + pub async fn new(config: Option<RuntimeConfigIgd>, rxp: &watch::Receiver<messages::PublicExposedPorts>) -> Result<Option<Self>> { + if config.is_none() { + return Ok(None); + } + let config = config.unwrap(); + let gw = search_gateway(Default::default()) .await .context("Failed to find IGD gateway")?; info!("IGD gateway: {}", gw); let ctx = Self { + expire: config.expiration_time, gateway: gw, + last_ports: messages::PublicExposedPorts::new(), + private_ip: config.private_ip, + refresh: config.refresh_time, rx_ports: rxp.clone(), - private_ip: priv_ip.to_string(), - refresh: refresh, - expire: expire, - last_ports: messages::PublicExposedPorts::new() }; - return Ok(ctx); + return Ok(Some(ctx)); } pub async fn listen(&mut self) -> Result<()> { |