diff options
Diffstat (limited to 'src/igd_actor.rs')
-rw-r--r-- | src/igd_actor.rs | 32 |
1 files changed, 18 insertions, 14 deletions
diff --git a/src/igd_actor.rs b/src/igd_actor.rs index 1263902..68d20df 100644 --- a/src/igd_actor.rs +++ b/src/igd_actor.rs @@ -33,7 +33,8 @@ impl IgdActor { rx_ports: rxp.clone(), private_ip: priv_ip.to_string(), refresh: refresh, - expire: expire + expire: expire, + last_ports: messages::PublicExposedPorts::new() }; return Ok(ctx); @@ -42,31 +43,34 @@ impl IgdActor { pub async fn listen(&mut self) -> Result<()> { let mut interval = time::interval(self.refresh); loop { - let res = select! { - msg = self.rx_ports.recv() => match msg { - Some(ports) => { self.last_ports = ports ; return self.do_igd().await; } , - None => return Ok(()) // Sender dropped, terminate loop. - } - _ = interval.tick() => self.do_igd().await + // 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. }; - match res { + // 2. Update last ports if needed + if let Some(p) = new_ports { self.last_ports = p; } + + // 3. Flush IGD requests + match self.do_igd().await { Ok(()) => debug!("Successfully updated IGD"), - Err(e) => error!("An error occured while updating IGD. {}. {:#?}", e, self.last_ports), + Err(e) => error!("An error occured while updating IGD. {}", e), } } } 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 { - for port in list { + for (proto, list) in actions.iter() { + for port in *list { let service_str = format!("{}:{}", self.private_ip, port); - let service: SocketAddrV4 = service_str.parse()?.context("Invalid socket address"); + 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); } |