diplonat/src/igd_actor.rs

82 lines
2.2 KiB
Rust
Raw Normal View History

2020-05-21 20:25:33 +00:00
use igd::aio::*;
2020-05-22 20:01:27 +00:00
use igd::PortMappingProtocol;
use std::net::SocketAddrV4;
2020-05-21 20:25:33 +00:00
use log::*;
use anyhow::{Result, Context};
2020-05-22 20:01:27 +00:00
use tokio::{
select,
sync::watch,
time::{
self,
Duration
}};
2020-05-22 17:21:11 +00:00
use crate::messages;
2020-05-21 15:51:30 +00:00
2020-05-22 17:21:11 +00:00
pub struct IgdActor {
2020-05-22 20:01:27 +00:00
last_ports: messages::PublicExposedPorts,
2020-05-22 17:21:11 +00:00
rx_ports: watch::Receiver<messages::PublicExposedPorts>,
2020-05-21 20:25:33 +00:00
gateway: Gateway,
2020-05-22 20:01:27 +00:00
refresh: Duration,
expire: Duration,
private_ip: String
2020-05-21 20:25:33 +00:00
}
2020-05-22 17:21:11 +00:00
impl IgdActor {
2020-05-22 20:01:27 +00:00
pub async fn new(priv_ip: &str, refresh: Duration, expire: Duration, rxp: &watch::Receiver<messages::PublicExposedPorts>) -> Result<Self> {
2020-05-21 20:25:33 +00:00
let gw = search_gateway(Default::default())
.await
.context("Failed to find IGD gateway")?;
info!("IGD gateway: {}", gw);
2020-05-21 20:25:33 +00:00
let ctx = Self {
2020-05-22 17:21:11 +00:00
gateway: gw,
2020-05-22 20:01:27 +00:00
rx_ports: rxp.clone(),
private_ip: priv_ip.to_string(),
refresh: refresh,
2020-05-23 13:38:08 +00:00
expire: expire,
last_ports: messages::PublicExposedPorts::new()
2020-05-21 20:25:33 +00:00
};
2020-05-22 17:21:11 +00:00
2020-05-21 20:25:33 +00:00
return Ok(ctx);
2020-05-21 15:51:30 +00:00
}
2020-05-21 20:25:33 +00:00
2020-05-22 17:21:11 +00:00
pub async fn listen(&mut self) -> Result<()> {
2020-05-22 20:01:27 +00:00
let mut interval = time::interval(self.refresh);
loop {
2020-05-23 13:38:08 +00:00
// 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.
2020-05-22 20:01:27 +00:00
};
2020-05-23 13:38:08 +00:00
// 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 {
2020-05-22 20:01:27 +00:00
Ok(()) => debug!("Successfully updated IGD"),
2020-05-23 13:38:08 +00:00
Err(e) => error!("An error occured while updating IGD. {}", e),
2020-05-22 20:01:27 +00:00
}
}
}
pub async fn do_igd(&self) -> Result<()> {
let actions = [
2020-05-23 13:38:08 +00:00
(PortMappingProtocol::TCP, &self.last_ports.tcp_ports),
(PortMappingProtocol::UDP, &self.last_ports.udp_ports)
2020-05-22 20:01:27 +00:00
];
2020-05-23 13:38:08 +00:00
for (proto, list) in actions.iter() {
for port in *list {
2020-05-22 20:01:27 +00:00
let service_str = format!("{}:{}", self.private_ip, port);
2020-05-23 13:38:08 +00:00
let service = service_str.parse::<SocketAddrV4>().context("Invalid socket address")?;
2020-05-22 20:01:27 +00:00
self.gateway.add_port(*proto, *port, service, self.expire.as_secs() as u32, "diplonat").await?;
debug!("IGD request successful for {:#?} {}", proto, service);
}
2020-05-22 17:21:11 +00:00
}
2020-05-22 20:01:27 +00:00
2020-05-21 15:51:30 +00:00
return Ok(());
}
}