diff options
Diffstat (limited to 'src/igd_actor.rs')
-rw-r--r-- | src/igd_actor.rs | 56 |
1 files changed, 36 insertions, 20 deletions
diff --git a/src/igd_actor.rs b/src/igd_actor.rs index 55d9c5f..4ff5f53 100644 --- a/src/igd_actor.rs +++ b/src/igd_actor.rs @@ -1,16 +1,14 @@ +use crate::messages; +use anyhow::{Context, Result}; use igd::aio::*; use igd::PortMappingProtocol; -use std::net::SocketAddrV4; use log::*; -use anyhow::{Result, Context}; +use std::net::SocketAddrV4; use tokio::{ - select, - sync::watch, - time::{ - self, - Duration -}}; -use crate::messages; + select, + sync::watch, + time::{self, Duration}, +}; pub struct IgdActor { last_ports: messages::PublicExposedPorts, @@ -18,23 +16,28 @@ pub struct IgdActor { gateway: Gateway, refresh: Duration, expire: Duration, - private_ip: String + private_ip: String, } impl IgdActor { - pub async fn new(priv_ip: &str, refresh: Duration, expire: Duration, rxp: &watch::Receiver<messages::PublicExposedPorts>) -> Result<Self> { + pub async fn new( + priv_ip: &str, + refresh: Duration, + expire: Duration, + rxp: &watch::Receiver<messages::PublicExposedPorts>, + ) -> Result<Self> { let gw = search_gateway(Default::default()) - .await - .context("Failed to find IGD gateway")?; + .await + .context("Failed to find IGD gateway")?; info!("IGD gateway: {}", gw); - let ctx = Self { + let ctx = Self { gateway: gw, rx_ports: rxp.clone(), private_ip: priv_ip.to_string(), refresh: refresh, expire: expire, - last_ports: messages::PublicExposedPorts::new() + last_ports: messages::PublicExposedPorts::new(), }; return Ok(ctx); @@ -51,7 +54,9 @@ impl IgdActor { }; // 2. Update last ports if needed - if let Some(p) = new_ports { self.last_ports = p; } + if let Some(p) = new_ports { + self.last_ports = p; + } // 3. Flush IGD requests match self.do_igd().await { @@ -63,15 +68,26 @@ impl IgdActor { pub async fn do_igd(&self) -> Result<()> { let actions = [ - (PortMappingProtocol::TCP, &self.last_ports.tcp_ports), - (PortMappingProtocol::UDP, &self.last_ports.udp_ports) + (PortMappingProtocol::TCP, &self.last_ports.tcp_ports), + (PortMappingProtocol::UDP, &self.last_ports.udp_ports), ]; for (proto, list) in actions.iter() { for port in *list { let service_str = format!("{}:{}", self.private_ip, port); - let service = service_str.parse::<SocketAddrV4>().context("Invalid socket address")?; - self.gateway.add_port(*proto, *port, service, self.expire.as_secs() as u32, "diplonat").await?; + let service = service_str + .parse::<SocketAddrV4>() + .context("Invalid socket address")?; + self + .gateway + .add_port( + *proto, + *port, + service, + self.expire.as_secs() as u32, + "diplonat", + ) + .await?; debug!("IGD request successful for {:#?} {}", proto, service); } } |