2021-08-25 15:20:31 +00:00
|
|
|
use std::net::SocketAddrV4;
|
|
|
|
|
2021-09-20 09:30:00 +00:00
|
|
|
use anyhow::{Context, Result};
|
2021-09-20 14:07:58 +00:00
|
|
|
use igd::{aio::*, PortMappingProtocol};
|
2020-05-21 20:25:33 +00:00
|
|
|
use log::*;
|
2020-05-22 20:01:27 +00:00
|
|
|
use tokio::{
|
2021-09-20 09:30:00 +00:00
|
|
|
select,
|
|
|
|
sync::watch,
|
|
|
|
time::{self, Duration},
|
|
|
|
};
|
2021-08-25 15:20:31 +00:00
|
|
|
|
2021-09-20 14:07:58 +00:00
|
|
|
use crate::{config::RuntimeConfigIgd, messages};
|
2020-05-21 15:51:30 +00:00
|
|
|
|
2020-05-22 17:21:11 +00:00
|
|
|
pub struct IgdActor {
|
2021-08-25 15:20:31 +00:00
|
|
|
expire: Duration,
|
2020-05-21 20:25:33 +00:00
|
|
|
gateway: Gateway,
|
2021-08-25 15:20:31 +00:00
|
|
|
last_ports: messages::PublicExposedPorts,
|
|
|
|
private_ip: String,
|
2020-05-22 20:01:27 +00:00
|
|
|
refresh: Duration,
|
2021-08-25 15:20:31 +00:00
|
|
|
|
|
|
|
rx_ports: watch::Receiver<messages::PublicExposedPorts>,
|
2020-05-21 20:25:33 +00:00
|
|
|
}
|
2020-05-22 17:21:11 +00:00
|
|
|
|
|
|
|
impl IgdActor {
|
2021-09-20 09:30:00 +00:00
|
|
|
pub async fn new(
|
|
|
|
config: Option<RuntimeConfigIgd>,
|
|
|
|
rxp: &watch::Receiver<messages::PublicExposedPorts>,
|
|
|
|
) -> Result<Option<Self>> {
|
2021-09-22 12:47:58 +00:00
|
|
|
match config {
|
|
|
|
None => Ok(None),
|
|
|
|
Some(c) => {
|
|
|
|
let gw = search_gateway(Default::default())
|
|
|
|
.await
|
|
|
|
.context("Failed to find IGD gateway")?;
|
|
|
|
info!("IGD gateway: {}", gw);
|
|
|
|
|
|
|
|
Ok(Some(Self {
|
|
|
|
expire: c.expiration_time,
|
|
|
|
gateway: gw,
|
|
|
|
last_ports: messages::PublicExposedPorts::new(),
|
|
|
|
private_ip: c.private_ip,
|
|
|
|
refresh: c.refresh_time,
|
|
|
|
rx_ports: rxp.clone(),
|
|
|
|
}))
|
|
|
|
}
|
2021-08-25 15:20:31 +00:00
|
|
|
}
|
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
|
2021-09-20 09:30:00 +00:00
|
|
|
if let Some(p) = new_ports {
|
|
|
|
self.last_ports = p;
|
|
|
|
}
|
2020-05-23 13:38:08 +00:00
|
|
|
|
|
|
|
// 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 = [
|
2021-09-20 09:30:00 +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);
|
2021-09-20 09:30:00 +00:00
|
|
|
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?;
|
2020-05-22 20:01:27 +00:00
|
|
|
debug!("IGD request successful for {:#?} {}", proto, service);
|
|
|
|
}
|
2020-05-22 17:21:11 +00:00
|
|
|
}
|
2020-05-22 20:01:27 +00:00
|
|
|
|
2021-09-20 14:07:58 +00:00
|
|
|
return Ok(())
|
2020-05-21 15:51:30 +00:00
|
|
|
}
|
|
|
|
}
|