diplonat/src/igd_actor.rs

91 lines
2.4 KiB
Rust
Raw Permalink Normal View History

use std::net::SocketAddrV4;
use anyhow::{Result, Context};
2020-05-21 20:25:33 +00:00
use igd::aio::*;
2020-05-22 20:01:27 +00:00
use igd::PortMappingProtocol;
2020-05-21 20:25:33 +00:00
use log::*;
2020-05-22 20:01:27 +00:00
use tokio::{
select,
sync::watch,
time::{
Duration,
2020-05-22 20:01:27 +00:00
self,
}};
use crate::config::RuntimeConfigIgd;
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 {
expire: Duration,
2020-05-21 20:25:33 +00:00
gateway: Gateway,
last_ports: messages::PublicExposedPorts,
private_ip: String,
2020-05-22 20:01:27 +00:00
refresh: Duration,
rx_ports: watch::Receiver<messages::PublicExposedPorts>,
2020-05-21 20:25:33 +00:00
}
2020-05-22 17:21:11 +00:00
impl IgdActor {
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();
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 {
expire: config.expiration_time,
2020-05-22 17:21:11 +00:00
gateway: gw,
last_ports: messages::PublicExposedPorts::new(),
private_ip: config.private_ip,
refresh: config.refresh_time,
2020-05-22 20:01:27 +00:00
rx_ports: rxp.clone(),
2020-05-21 20:25:33 +00:00
};
2020-05-22 17:21:11 +00:00
return Ok(Some(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(());
}
}