| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | mod as_client; |
| | mod as_server; |
| |
|
| | use crate::protocol::{AutoNatCodec, DialRequest, DialResponse, ResponseError}; |
| | use crate::DEFAULT_PROTOCOL_NAME; |
| | use as_client::AsClient; |
| | pub use as_client::{OutboundProbeError, OutboundProbeEvent}; |
| | use as_server::AsServer; |
| | pub use as_server::{InboundProbeError, InboundProbeEvent}; |
| | use futures_timer::Delay; |
| | use libp2p_core::transport::PortUse; |
| | use libp2p_core::{multiaddr::Protocol, ConnectedPoint, Endpoint, Multiaddr}; |
| | use libp2p_identity::PeerId; |
| | use libp2p_request_response::{ |
| | self as request_response, InboundRequestId, OutboundRequestId, ProtocolSupport, ResponseChannel, |
| | }; |
| | use libp2p_swarm::{ |
| | behaviour::{AddressChange, ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm}, |
| | ConnectionDenied, ConnectionId, ListenAddresses, NetworkBehaviour, THandler, THandlerInEvent, |
| | THandlerOutEvent, ToSwarm, |
| | }; |
| | use std::{ |
| | collections::{HashMap, HashSet, VecDeque}, |
| | iter, |
| | task::{Context, Poll}, |
| | time::Duration, |
| | }; |
| | use web_time::Instant; |
| |
|
| | |
| | #[derive(Debug, Clone, PartialEq, Eq)] |
| | pub struct Config { |
| | |
| | pub timeout: Duration, |
| |
|
| | |
| | |
| | pub boot_delay: Duration, |
| | |
| | pub refresh_interval: Duration, |
| | |
| | |
| | pub retry_interval: Duration, |
| | |
| | pub throttle_server_period: Duration, |
| | |
| | pub use_connected: bool, |
| | |
| | |
| | pub confidence_max: usize, |
| |
|
| | |
| | |
| | pub max_peer_addresses: usize, |
| | |
| | pub throttle_clients_global_max: usize, |
| | |
| | pub throttle_clients_peer_max: usize, |
| | |
| | pub throttle_clients_period: Duration, |
| | |
| | |
| | |
| | |
| | pub only_global_ips: bool, |
| | } |
| |
|
| | impl Default for Config { |
| | fn default() -> Self { |
| | Config { |
| | timeout: Duration::from_secs(30), |
| | boot_delay: Duration::from_secs(15), |
| | retry_interval: Duration::from_secs(90), |
| | refresh_interval: Duration::from_secs(15 * 60), |
| | throttle_server_period: Duration::from_secs(90), |
| | use_connected: true, |
| | confidence_max: 3, |
| | max_peer_addresses: 16, |
| | throttle_clients_global_max: 30, |
| | throttle_clients_peer_max: 3, |
| | throttle_clients_period: Duration::from_secs(1), |
| | only_global_ips: true, |
| | } |
| | } |
| | } |
| |
|
| | |
| | #[derive(Debug, Clone, PartialEq, Eq)] |
| | pub enum NatStatus { |
| | Public(Multiaddr), |
| | Private, |
| | Unknown, |
| | } |
| |
|
| | impl NatStatus { |
| | pub fn is_public(&self) -> bool { |
| | matches!(self, NatStatus::Public(..)) |
| | } |
| | } |
| |
|
| | |
| | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| | pub struct ProbeId(usize); |
| |
|
| | impl ProbeId { |
| | fn next(&mut self) -> ProbeId { |
| | let current = *self; |
| | self.0 += 1; |
| | current |
| | } |
| | } |
| |
|
| | |
| | #[derive(Debug)] |
| | pub enum Event { |
| | |
| | InboundProbe(InboundProbeEvent), |
| | |
| | OutboundProbe(OutboundProbeEvent), |
| | |
| | StatusChanged { |
| | |
| | old: NatStatus, |
| | |
| | new: NatStatus, |
| | }, |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | pub struct Behaviour { |
| | |
| | local_peer_id: PeerId, |
| |
|
| | |
| | inner: request_response::Behaviour<AutoNatCodec>, |
| |
|
| | config: Config, |
| |
|
| | |
| | servers: HashSet<PeerId>, |
| |
|
| | |
| | nat_status: NatStatus, |
| |
|
| | |
| | confidence: usize, |
| |
|
| | |
| | schedule_probe: Delay, |
| |
|
| | |
| | ongoing_inbound: HashMap< |
| | PeerId, |
| | ( |
| | ProbeId, |
| | InboundRequestId, |
| | Vec<Multiaddr>, |
| | ResponseChannel<DialResponse>, |
| | ), |
| | >, |
| |
|
| | |
| | ongoing_outbound: HashMap<OutboundRequestId, ProbeId>, |
| |
|
| | |
| | |
| | |
| | connected: HashMap<PeerId, HashMap<ConnectionId, Option<Multiaddr>>>, |
| |
|
| | |
| | throttled_servers: Vec<(PeerId, Instant)>, |
| |
|
| | |
| | throttled_clients: Vec<(PeerId, Instant)>, |
| |
|
| | last_probe: Option<Instant>, |
| |
|
| | pending_actions: VecDeque<ToSwarm<<Self as NetworkBehaviour>::ToSwarm, THandlerInEvent<Self>>>, |
| |
|
| | probe_id: ProbeId, |
| |
|
| | listen_addresses: ListenAddresses, |
| | other_candidates: HashSet<Multiaddr>, |
| | } |
| |
|
| | impl Behaviour { |
| | pub fn new(local_peer_id: PeerId, config: Config) -> Self { |
| | let protocols = iter::once((DEFAULT_PROTOCOL_NAME, ProtocolSupport::Full)); |
| | let inner = request_response::Behaviour::with_codec( |
| | AutoNatCodec, |
| | protocols, |
| | request_response::Config::default().with_request_timeout(config.timeout), |
| | ); |
| | Self { |
| | local_peer_id, |
| | inner, |
| | schedule_probe: Delay::new(config.boot_delay), |
| | config, |
| | servers: HashSet::new(), |
| | ongoing_inbound: HashMap::default(), |
| | ongoing_outbound: HashMap::default(), |
| | connected: HashMap::default(), |
| | nat_status: NatStatus::Unknown, |
| | confidence: 0, |
| | throttled_servers: Vec::new(), |
| | throttled_clients: Vec::new(), |
| | last_probe: None, |
| | pending_actions: VecDeque::new(), |
| | probe_id: ProbeId(0), |
| | listen_addresses: Default::default(), |
| | other_candidates: Default::default(), |
| | } |
| | } |
| |
|
| | |
| | |
| | pub fn public_address(&self) -> Option<&Multiaddr> { |
| | match &self.nat_status { |
| | NatStatus::Public(address) => Some(address), |
| | _ => None, |
| | } |
| | } |
| |
|
| | |
| | pub fn nat_status(&self) -> NatStatus { |
| | self.nat_status.clone() |
| | } |
| |
|
| | |
| | pub fn confidence(&self) -> usize { |
| | self.confidence |
| | } |
| |
|
| | |
| | |
| | |
| | pub fn add_server(&mut self, peer: PeerId, address: Option<Multiaddr>) { |
| | self.servers.insert(peer); |
| | if let Some(addr) = address { |
| | #[allow(deprecated)] |
| | self.inner.add_address(&peer, addr); |
| | } |
| | } |
| |
|
| | |
| | |
| | pub fn remove_server(&mut self, peer: &PeerId) { |
| | self.servers.retain(|p| p != peer); |
| | } |
| |
|
| | |
| | pub fn probe_address(&mut self, candidate: Multiaddr) { |
| | self.other_candidates.insert(candidate); |
| | self.as_client().on_new_address(); |
| | } |
| |
|
| | fn as_client(&mut self) -> AsClient { |
| | AsClient { |
| | inner: &mut self.inner, |
| | local_peer_id: self.local_peer_id, |
| | config: &self.config, |
| | connected: &self.connected, |
| | probe_id: &mut self.probe_id, |
| | servers: &self.servers, |
| | throttled_servers: &mut self.throttled_servers, |
| | nat_status: &mut self.nat_status, |
| | confidence: &mut self.confidence, |
| | ongoing_outbound: &mut self.ongoing_outbound, |
| | last_probe: &mut self.last_probe, |
| | schedule_probe: &mut self.schedule_probe, |
| | listen_addresses: &self.listen_addresses, |
| | other_candidates: &self.other_candidates, |
| | } |
| | } |
| |
|
| | fn as_server(&mut self) -> AsServer { |
| | AsServer { |
| | inner: &mut self.inner, |
| | config: &self.config, |
| | connected: &self.connected, |
| | probe_id: &mut self.probe_id, |
| | throttled_clients: &mut self.throttled_clients, |
| | ongoing_inbound: &mut self.ongoing_inbound, |
| | } |
| | } |
| |
|
| | fn on_connection_established( |
| | &mut self, |
| | ConnectionEstablished { |
| | peer_id: peer, |
| | connection_id: conn, |
| | endpoint, |
| | .. |
| | }: ConnectionEstablished, |
| | ) { |
| | let connections = self.connected.entry(peer).or_default(); |
| | let addr = endpoint.get_remote_address(); |
| | let observed_addr = |
| | if !endpoint.is_relayed() && (!self.config.only_global_ips || addr.is_global_ip()) { |
| | Some(addr.clone()) |
| | } else { |
| | None |
| | }; |
| | connections.insert(conn, observed_addr); |
| |
|
| | match endpoint { |
| | ConnectedPoint::Dialer { |
| | address, |
| | role_override: Endpoint::Dialer, |
| | port_use: _, |
| | } => { |
| | if let Some(event) = self.as_server().on_outbound_connection(&peer, address) { |
| | self.pending_actions |
| | .push_back(ToSwarm::GenerateEvent(Event::InboundProbe(event))); |
| | } |
| | } |
| | ConnectedPoint::Dialer { |
| | address: _, |
| | role_override: Endpoint::Listener, |
| | port_use: _, |
| | } => { |
| | |
| | |
| | |
| | } |
| | ConnectedPoint::Listener { .. } => self.as_client().on_inbound_connection(), |
| | } |
| | } |
| |
|
| | fn on_connection_closed( |
| | &mut self, |
| | ConnectionClosed { |
| | peer_id, |
| | connection_id, |
| | remaining_established, |
| | .. |
| | }: ConnectionClosed, |
| | ) { |
| | if remaining_established == 0 { |
| | self.connected.remove(&peer_id); |
| | } else { |
| | let connections = self |
| | .connected |
| | .get_mut(&peer_id) |
| | .expect("Peer is connected."); |
| | connections.remove(&connection_id); |
| | } |
| | } |
| |
|
| | fn on_dial_failure(&mut self, DialFailure { peer_id, error, .. }: DialFailure) { |
| | if let Some(event) = self.as_server().on_outbound_dial_error(peer_id, error) { |
| | self.pending_actions |
| | .push_back(ToSwarm::GenerateEvent(Event::InboundProbe(event))); |
| | } |
| | } |
| |
|
| | fn on_address_change( |
| | &mut self, |
| | AddressChange { |
| | peer_id: peer, |
| | connection_id: conn, |
| | old, |
| | new, |
| | }: AddressChange, |
| | ) { |
| | if old.is_relayed() && new.is_relayed() { |
| | return; |
| | } |
| | let connections = self.connected.get_mut(&peer).expect("Peer is connected."); |
| | let addr = new.get_remote_address(); |
| | let observed_addr = |
| | if !new.is_relayed() && (!self.config.only_global_ips || addr.is_global_ip()) { |
| | Some(addr.clone()) |
| | } else { |
| | None |
| | }; |
| | connections.insert(conn, observed_addr); |
| | } |
| | } |
| |
|
| | impl NetworkBehaviour for Behaviour { |
| | type ConnectionHandler = |
| | <request_response::Behaviour<AutoNatCodec> as NetworkBehaviour>::ConnectionHandler; |
| | type ToSwarm = Event; |
| |
|
| | #[tracing::instrument(level = "trace", name = "NetworkBehaviour::poll", skip(self, cx))] |
| | fn poll( |
| | &mut self, |
| | cx: &mut Context<'_>, |
| | ) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> { |
| | loop { |
| | if let Some(event) = self.pending_actions.pop_front() { |
| | return Poll::Ready(event); |
| | } |
| |
|
| | match self.inner.poll(cx) { |
| | Poll::Ready(ToSwarm::GenerateEvent(event)) => { |
| | let actions = match event { |
| | request_response::Event::Message { |
| | message: request_response::Message::Response { .. }, |
| | .. |
| | } |
| | | request_response::Event::OutboundFailure { .. } => { |
| | self.as_client().handle_event(event) |
| | } |
| | request_response::Event::Message { |
| | message: request_response::Message::Request { .. }, |
| | .. |
| | } |
| | | request_response::Event::InboundFailure { .. } => { |
| | self.as_server().handle_event(event) |
| | } |
| | request_response::Event::ResponseSent { .. } => VecDeque::new(), |
| | }; |
| |
|
| | self.pending_actions.extend(actions); |
| | continue; |
| | } |
| | Poll::Ready(action) => { |
| | self.pending_actions |
| | .push_back(action.map_out(|_| unreachable!())); |
| | continue; |
| | } |
| | Poll::Pending => {} |
| | } |
| |
|
| | match self.as_client().poll_auto_probe(cx) { |
| | Poll::Ready(event) => { |
| | self.pending_actions |
| | .push_back(ToSwarm::GenerateEvent(Event::OutboundProbe(event))); |
| | continue; |
| | } |
| | Poll::Pending => {} |
| | } |
| |
|
| | return Poll::Pending; |
| | } |
| | } |
| |
|
| | fn handle_pending_inbound_connection( |
| | &mut self, |
| | connection_id: ConnectionId, |
| | local_addr: &Multiaddr, |
| | remote_addr: &Multiaddr, |
| | ) -> Result<(), ConnectionDenied> { |
| | self.inner |
| | .handle_pending_inbound_connection(connection_id, local_addr, remote_addr) |
| | } |
| |
|
| | 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_pending_outbound_connection( |
| | &mut self, |
| | connection_id: ConnectionId, |
| | maybe_peer: Option<PeerId>, |
| | addresses: &[Multiaddr], |
| | effective_role: Endpoint, |
| | ) -> Result<Vec<Multiaddr>, ConnectionDenied> { |
| | self.inner.handle_pending_outbound_connection( |
| | connection_id, |
| | maybe_peer, |
| | addresses, |
| | effective_role, |
| | ) |
| | } |
| |
|
| | fn handle_established_outbound_connection( |
| | &mut self, |
| | connection_id: ConnectionId, |
| | peer: PeerId, |
| | addr: &Multiaddr, |
| | role_override: Endpoint, |
| | port_use: PortUse, |
| | ) -> Result<THandler<Self>, ConnectionDenied> { |
| | self.inner.handle_established_outbound_connection( |
| | connection_id, |
| | peer, |
| | addr, |
| | role_override, |
| | port_use, |
| | ) |
| | } |
| |
|
| | fn on_swarm_event(&mut self, event: FromSwarm) { |
| | self.listen_addresses.on_swarm_event(&event); |
| | self.inner.on_swarm_event(event); |
| |
|
| | match event { |
| | FromSwarm::ConnectionEstablished(e) => self.on_connection_established(e), |
| | FromSwarm::ConnectionClosed(e) => self.on_connection_closed(e), |
| | FromSwarm::DialFailure(e) => self.on_dial_failure(e), |
| | FromSwarm::AddressChange(e) => self.on_address_change(e), |
| | FromSwarm::NewListenAddr(_) => { |
| | self.as_client().on_new_address(); |
| | } |
| | FromSwarm::ExpiredListenAddr(e) => { |
| | self.as_client().on_expired_address(e.addr); |
| | } |
| | FromSwarm::ExternalAddrExpired(e) => { |
| | self.as_client().on_expired_address(e.addr); |
| | } |
| | FromSwarm::NewExternalAddrCandidate(e) => { |
| | self.probe_address(e.addr.to_owned()); |
| | } |
| | _ => {} |
| | } |
| | } |
| |
|
| | 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) |
| | } |
| | } |
| |
|
| | type Action = ToSwarm<<Behaviour as NetworkBehaviour>::ToSwarm, THandlerInEvent<Behaviour>>; |
| |
|
| | |
| | trait HandleInnerEvent { |
| | fn handle_event( |
| | &mut self, |
| | event: request_response::Event<DialRequest, DialResponse>, |
| | ) -> VecDeque<Action>; |
| | } |
| |
|
| | trait GlobalIp { |
| | fn is_global_ip(&self) -> bool; |
| | } |
| |
|
| | impl GlobalIp for Multiaddr { |
| | fn is_global_ip(&self) -> bool { |
| | match self.iter().next() { |
| | Some(Protocol::Ip4(a)) => a.is_global_ip(), |
| | Some(Protocol::Ip6(a)) => a.is_global_ip(), |
| | _ => false, |
| | } |
| | } |
| | } |
| |
|
| | impl GlobalIp for std::net::Ipv4Addr { |
| | |
| | |
| | |
| | fn is_global_ip(&self) -> bool { |
| | |
| | |
| | if u32::from_be_bytes(self.octets()) == 0xc0000009 |
| | || u32::from_be_bytes(self.octets()) == 0xc000000a |
| | { |
| | return true; |
| | } |
| |
|
| | |
| | fn is_shared(addr: &std::net::Ipv4Addr) -> bool { |
| | addr.octets()[0] == 100 && (addr.octets()[1] & 0b1100_0000 == 0b0100_0000) |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | fn is_reserved(addr: &std::net::Ipv4Addr) -> bool { |
| | addr.octets()[0] & 240 == 240 && !addr.is_broadcast() |
| | } |
| |
|
| | |
| | fn is_benchmarking(addr: &std::net::Ipv4Addr) -> bool { |
| | addr.octets()[0] == 198 && (addr.octets()[1] & 0xfe) == 18 |
| | } |
| |
|
| | !self.is_private() |
| | && !self.is_loopback() |
| | && !self.is_link_local() |
| | && !self.is_broadcast() |
| | && !self.is_documentation() |
| | && !is_shared(self) |
| | |
| | && !(self.octets()[0] == 192 && self.octets()[1] == 0 && self.octets()[2] == 0) |
| | && !is_reserved(self) |
| | && !is_benchmarking(self) |
| | |
| | && self.octets()[0] != 0 |
| | } |
| | } |
| |
|
| | impl GlobalIp for std::net::Ipv6Addr { |
| | |
| | |
| | |
| | |
| | |
| | |
| | fn is_global_ip(&self) -> bool { |
| | |
| | fn is_unicast(addr: &std::net::Ipv6Addr) -> bool { |
| | !addr.is_multicast() |
| | } |
| | |
| | fn is_unicast_link_local(addr: &std::net::Ipv6Addr) -> bool { |
| | (addr.segments()[0] & 0xffc0) == 0xfe80 |
| | } |
| | |
| | fn is_unique_local(addr: &std::net::Ipv6Addr) -> bool { |
| | (addr.segments()[0] & 0xfe00) == 0xfc00 |
| | } |
| | |
| | fn is_documentation(addr: &std::net::Ipv6Addr) -> bool { |
| | (addr.segments()[0] == 0x2001) && (addr.segments()[1] == 0xdb8) |
| | } |
| |
|
| | |
| | fn is_unicast_global(addr: &std::net::Ipv6Addr) -> bool { |
| | is_unicast(addr) |
| | && !addr.is_loopback() |
| | && !is_unicast_link_local(addr) |
| | && !is_unique_local(addr) |
| | && !addr.is_unspecified() |
| | && !is_documentation(addr) |
| | } |
| |
|
| | |
| | |
| | |
| | fn is_multicast_scope_global(addr: &std::net::Ipv6Addr) -> Option<bool> { |
| | match addr.segments()[0] & 0x000f { |
| | 14 => Some(true), |
| | 1..=5 | 8 => Some(false), |
| | _ => None, |
| | } |
| | } |
| |
|
| | match is_multicast_scope_global(self) { |
| | Some(true) => true, |
| | None => is_unicast_global(self), |
| | _ => false, |
| | } |
| | } |
| | } |
| |
|