refactor
This commit is contained in:
@@ -176,7 +176,7 @@ impl IGDManager {
|
||||
mapped_port: u16,
|
||||
) -> Option<()> {
|
||||
let this = self.clone();
|
||||
intf::blocking_wrapper(move || {
|
||||
blocking_wrapper(move || {
|
||||
let mut inner = this.inner.lock();
|
||||
|
||||
// If we already have this port mapped, just return the existing portmap
|
||||
@@ -215,7 +215,7 @@ impl IGDManager {
|
||||
expected_external_address: Option<IpAddr>,
|
||||
) -> Option<SocketAddr> {
|
||||
let this = self.clone();
|
||||
intf::blocking_wrapper(move || {
|
||||
blocking_wrapper(move || {
|
||||
let mut inner = this.inner.lock();
|
||||
|
||||
// If we already have this port mapped, just return the existing portmap
|
||||
@@ -275,7 +275,7 @@ impl IGDManager {
|
||||
};
|
||||
|
||||
// Add to mapping list to keep alive
|
||||
let timestamp = intf::get_timestamp();
|
||||
let timestamp = get_timestamp();
|
||||
inner.port_maps.insert(PortMapKey {
|
||||
llpt,
|
||||
at,
|
||||
@@ -301,7 +301,7 @@ impl IGDManager {
|
||||
let mut renews: Vec<(PortMapKey, PortMapValue)> = Vec::new();
|
||||
{
|
||||
let inner = self.inner.lock();
|
||||
let now = intf::get_timestamp();
|
||||
let now = get_timestamp();
|
||||
|
||||
for (k, v) in &inner.port_maps {
|
||||
let mapping_lifetime = now.saturating_sub(v.timestamp);
|
||||
@@ -323,7 +323,7 @@ impl IGDManager {
|
||||
}
|
||||
|
||||
let this = self.clone();
|
||||
intf::blocking_wrapper(move || {
|
||||
blocking_wrapper(move || {
|
||||
let mut inner = this.inner.lock();
|
||||
|
||||
// Process full renewals
|
||||
@@ -356,7 +356,7 @@ impl IGDManager {
|
||||
inner.port_maps.insert(k, PortMapValue {
|
||||
ext_ip: v.ext_ip,
|
||||
mapped_port,
|
||||
timestamp: intf::get_timestamp(),
|
||||
timestamp: get_timestamp(),
|
||||
renewal_lifetime: (UPNP_MAPPING_LIFETIME_MS / 2) as u64 * 1000u64,
|
||||
renewal_attempts: 0,
|
||||
});
|
||||
@@ -397,7 +397,7 @@ impl IGDManager {
|
||||
inner.port_maps.insert(k, PortMapValue {
|
||||
ext_ip: v.ext_ip,
|
||||
mapped_port: v.mapped_port,
|
||||
timestamp: intf::get_timestamp(),
|
||||
timestamp: get_timestamp(),
|
||||
renewal_lifetime: (UPNP_MAPPING_LIFETIME_MS / 2) as u64 * 1000u64,
|
||||
renewal_attempts: 0,
|
||||
});
|
||||
|
@@ -1,5 +1,4 @@
|
||||
mod igd_manager;
|
||||
mod natpmp_manager;
|
||||
mod network_class_discovery;
|
||||
mod network_tcp;
|
||||
mod network_udp;
|
||||
@@ -94,11 +93,9 @@ struct NetworkUnlockedInner {
|
||||
update_network_class_task: TickTask<EyreReport>,
|
||||
network_interfaces_task: TickTask<EyreReport>,
|
||||
upnp_task: TickTask<EyreReport>,
|
||||
natpmp_task: TickTask<EyreReport>,
|
||||
|
||||
// Managers
|
||||
igd_manager: igd_manager::IGDManager,
|
||||
natpmp_manager: natpmp_manager::NATPMPManager,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -150,9 +147,7 @@ impl Network {
|
||||
update_network_class_task: TickTask::new(1),
|
||||
network_interfaces_task: TickTask::new(5),
|
||||
upnp_task: TickTask::new(1),
|
||||
natpmp_task: TickTask::new(1),
|
||||
igd_manager: igd_manager::IGDManager::new(config.clone()),
|
||||
natpmp_manager: natpmp_manager::NATPMPManager::new(config),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -196,13 +191,6 @@ impl Network {
|
||||
.upnp_task
|
||||
.set_routine(move |s, l, t| Box::pin(this2.clone().upnp_task_routine(s, l, t)));
|
||||
}
|
||||
// Set natpmp tick task
|
||||
{
|
||||
let this2 = this.clone();
|
||||
this.unlocked_inner
|
||||
.natpmp_task
|
||||
.set_routine(move |s, l, t| Box::pin(this2.clone().natpmp_task_routine(s, l, t)));
|
||||
}
|
||||
|
||||
this
|
||||
}
|
||||
@@ -904,31 +892,11 @@ impl Network {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[instrument(level = "trace", skip(self), err)]
|
||||
pub async fn natpmp_task_routine(
|
||||
self,
|
||||
stop_token: StopToken,
|
||||
_l: u64,
|
||||
_t: u64,
|
||||
) -> EyreResult<()> {
|
||||
if !self.unlocked_inner.natpmp_manager.tick().await? {
|
||||
info!("natpmp failed, restarting local network");
|
||||
let mut inner = self.inner.lock();
|
||||
inner.network_needs_restart = true;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn tick(&self) -> EyreResult<()> {
|
||||
let (detect_address_changes, upnp, natpmp) = {
|
||||
let (detect_address_changes, upnp) = {
|
||||
let config = self.network_manager().config();
|
||||
let c = config.get();
|
||||
(
|
||||
c.network.detect_address_changes,
|
||||
c.network.upnp,
|
||||
c.network.natpmp,
|
||||
)
|
||||
(c.network.detect_address_changes, c.network.upnp)
|
||||
};
|
||||
|
||||
// If we need to figure out our network class, tick the task for it
|
||||
@@ -962,11 +930,6 @@ impl Network {
|
||||
self.unlocked_inner.upnp_task.tick().await?;
|
||||
}
|
||||
|
||||
// If we need to tick natpmp, do it
|
||||
if natpmp && !self.needs_restart() {
|
||||
self.unlocked_inner.natpmp_task.tick().await?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
@@ -1,18 +0,0 @@
|
||||
use super::*;
|
||||
|
||||
pub struct NATPMPManager {
|
||||
config: VeilidConfig,
|
||||
}
|
||||
|
||||
impl NATPMPManager {
|
||||
//
|
||||
|
||||
pub fn new(config: VeilidConfig) -> Self {
|
||||
Self { config }
|
||||
}
|
||||
|
||||
pub async fn tick(&self) -> EyreResult<bool> {
|
||||
// xxx
|
||||
Ok(true)
|
||||
}
|
||||
}
|
@@ -275,7 +275,7 @@ impl DiscoveryContext {
|
||||
LowLevelProtocolType::UDP => "udp",
|
||||
LowLevelProtocolType::TCP => "tcp",
|
||||
});
|
||||
intf::sleep(PORT_MAP_VALIDATE_DELAY_MS).await
|
||||
sleep(PORT_MAP_VALIDATE_DELAY_MS).await
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
@@ -304,9 +304,9 @@ impl DiscoveryContext {
|
||||
|
||||
#[instrument(level = "trace", skip(self), ret)]
|
||||
async fn try_port_mapping(&self) -> Option<DialInfo> {
|
||||
let (enable_upnp, _enable_natpmp) = {
|
||||
let enable_upnp = {
|
||||
let c = self.net.config.get();
|
||||
(c.network.upnp, c.network.natpmp)
|
||||
c.network.upnp
|
||||
};
|
||||
|
||||
if enable_upnp {
|
||||
|
@@ -58,7 +58,7 @@ impl Network {
|
||||
// Don't waste more than N seconds getting it though, in case someone
|
||||
// is trying to DoS us with a bunch of connections or something
|
||||
// read a chunk of the stream
|
||||
intf::timeout(
|
||||
timeout(
|
||||
tls_connection_initial_timeout_ms,
|
||||
ps.peek_exact(&mut first_packet),
|
||||
)
|
||||
|
@@ -10,7 +10,7 @@ impl Network {
|
||||
c.network.protocol.udp.socket_pool_size
|
||||
};
|
||||
if task_count == 0 {
|
||||
task_count = intf::get_concurrency() / 2;
|
||||
task_count = get_concurrency() / 2;
|
||||
if task_count == 0 {
|
||||
task_count = 1;
|
||||
}
|
||||
|
@@ -196,7 +196,7 @@ pub async fn nonblocking_connect(
|
||||
let async_stream = Async::new(std::net::TcpStream::from(socket))?;
|
||||
|
||||
// The stream becomes writable when connected
|
||||
timeout_or_try!(intf::timeout(timeout_ms, async_stream.writable())
|
||||
timeout_or_try!(timeout(timeout_ms, async_stream.writable())
|
||||
.await
|
||||
.into_timeout_or()
|
||||
.into_result()?);
|
||||
|
Reference in New Issue
Block a user