-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2242 from subspace/autonat-wrapper
Autonat and Kademlia settings updates.
- Loading branch information
Showing
5 changed files
with
220 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub(crate) mod autonat_wrapper; | ||
pub(crate) mod connected_peers; | ||
pub mod peer_info; | ||
pub mod request_response; | ||
|
150 changes: 150 additions & 0 deletions
150
crates/subspace-networking/src/protocols/autonat_wrapper.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
use crate::utils::is_global_address_or_dns; | ||
use libp2p::autonat::{Behaviour as Autonat, Config as AutonatConfig, Event as AutonatEvent}; | ||
use libp2p::core::Endpoint; | ||
use libp2p::multiaddr::Protocol; | ||
use libp2p::swarm::{ | ||
ConnectionDenied, ConnectionId, FromSwarm, NetworkBehaviour, THandler, THandlerInEvent, | ||
THandlerOutEvent, ToSwarm, | ||
}; | ||
use libp2p::{Multiaddr, PeerId}; | ||
use std::collections::HashSet; | ||
use std::task::{Context, Poll}; | ||
use tracing::debug; | ||
|
||
pub(crate) struct Config { | ||
pub(crate) inner_config: AutonatConfig, | ||
pub(crate) local_peer_id: PeerId, | ||
} | ||
|
||
pub(crate) struct Behaviour { | ||
inner: Autonat, | ||
config: Config, | ||
listen_addresses: HashSet<Multiaddr>, | ||
} | ||
|
||
impl Behaviour { | ||
pub(crate) fn new(config: Config) -> Self { | ||
Self { | ||
inner: Autonat::new(config.local_peer_id, config.inner_config.clone()), | ||
config, | ||
listen_addresses: Default::default(), | ||
} | ||
} | ||
|
||
fn private_ips_enabled(&self) -> bool { | ||
!self.config.inner_config.only_global_ips | ||
} | ||
|
||
fn address_corresponds_to_listening_addresses(&self, addr: &Multiaddr) -> bool { | ||
let candidate_protocol = addr | ||
.iter() | ||
.find_map(|protocol| match protocol { | ||
udp @ Protocol::Udp(_) => Some(udp), | ||
tcp @ Protocol::Tcp(_) => Some(tcp), | ||
_ => None, | ||
}) | ||
.expect("Either TCP or UDP protocol should be enabled."); | ||
|
||
let address_result = self | ||
.listen_addresses | ||
.iter() | ||
.any(|addr| addr.iter().any(|protocol| protocol == candidate_protocol)); | ||
|
||
debug!( | ||
%address_result, | ||
?addr, | ||
listen_addresses=?self.listen_addresses, | ||
"Address candidate corresponds to listening addresses." | ||
); | ||
|
||
address_result | ||
} | ||
|
||
pub(crate) fn public_address(&self) -> Option<&Multiaddr> { | ||
self.inner.public_address() | ||
} | ||
|
||
pub(crate) fn confidence(&self) -> usize { | ||
self.inner.confidence() | ||
} | ||
} | ||
|
||
impl NetworkBehaviour for Behaviour { | ||
type ConnectionHandler = <Autonat as NetworkBehaviour>::ConnectionHandler; | ||
type ToSwarm = AutonatEvent; | ||
|
||
fn handle_established_inbound_connection( | ||
&mut self, | ||
connection_id: ConnectionId, | ||
peer: PeerId, | ||
local_addr: &Multiaddr, | ||
remote_addr: &Multiaddr, | ||
) -> Result<THandler<Self>, ConnectionDenied> { | ||
self.inner.handle_established_inbound_connection( | ||
connection_id, | ||
peer, | ||
local_addr, | ||
remote_addr, | ||
) | ||
} | ||
|
||
fn handle_established_outbound_connection( | ||
&mut self, | ||
connection_id: ConnectionId, | ||
peer: PeerId, | ||
addr: &Multiaddr, | ||
role_override: Endpoint, | ||
) -> Result<THandler<Self>, ConnectionDenied> { | ||
self.inner | ||
.handle_established_outbound_connection(connection_id, peer, addr, role_override) | ||
} | ||
|
||
fn on_swarm_event(&mut self, event: FromSwarm) { | ||
match event { | ||
new_listen_addr_event @ FromSwarm::NewListenAddr(_) => { | ||
if let FromSwarm::NewListenAddr(addr) = new_listen_addr_event { | ||
//TODO: handle listener address change | ||
self.listen_addresses.insert(addr.addr.clone()); | ||
|
||
if self.private_ips_enabled() || is_global_address_or_dns(addr.addr) { | ||
self.inner.on_swarm_event(new_listen_addr_event); | ||
} else { | ||
debug!(addr=?addr.addr, "Skipped listening address in AutonatWrapper."); | ||
} | ||
} | ||
} | ||
new_external_addr_event @ FromSwarm::NewExternalAddrCandidate(_) => { | ||
if let FromSwarm::NewExternalAddrCandidate(addr) = new_external_addr_event { | ||
if self.address_corresponds_to_listening_addresses(addr.addr) { | ||
self.inner.on_swarm_event(new_external_addr_event); | ||
} else { | ||
debug!( | ||
addr=?addr.addr, | ||
"Skipped external address candidate in AutonatWrapper." | ||
); | ||
} | ||
} | ||
} | ||
event => { | ||
self.inner.on_swarm_event(event); | ||
} | ||
} | ||
} | ||
|
||
fn on_connection_handler_event( | ||
&mut self, | ||
peer_id: PeerId, | ||
connection_id: ConnectionId, | ||
event: THandlerOutEvent<Self>, | ||
) { | ||
self.inner | ||
.on_connection_handler_event(peer_id, connection_id, event) | ||
} | ||
|
||
fn poll( | ||
&mut self, | ||
cx: &mut Context<'_>, | ||
) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> { | ||
self.inner.poll(cx) | ||
} | ||
} |