From 27e2cb0c16d33772adecbe338730d7dc2416d190 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Thu, 23 Mar 2023 22:10:52 +0100 Subject: [PATCH] chore(autonat): move example to `examples/` Related: #3111. Pull-Request: #3662. --- Cargo.toml | 14 ---- examples/autonat_client.rs | 147 ------------------------------------- examples/autonat_server.rs | 127 -------------------------------- 3 files changed, 288 deletions(-) delete mode 100644 examples/autonat_client.rs delete mode 100644 examples/autonat_server.rs diff --git a/Cargo.toml b/Cargo.toml index 8fc4f15c1f4..150a791e648 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,13 +25,7 @@ quick-protobuf = "0.8" [dev-dependencies] async-std = { version = "1.10", features = ["attributes"] } -clap = { version = "4.1.11", features = ["derive"] } env_logger = "0.10" -libp2p-identify = { path = "../identify" } -libp2p-noise = { path = "../../transports/noise" } -libp2p-swarm = { path = "../../swarm", features = ["async-std", "macros"] } -libp2p-tcp = { path = "../../transports/tcp", features = ["async-io"] } -libp2p-yamux = { path = "../../muxers/yamux" } libp2p-swarm-test = { path = "../../swarm-test" } # Passing arguments to the docsrs builder in order to properly document cfg's. @@ -40,11 +34,3 @@ libp2p-swarm-test = { path = "../../swarm-test" } all-features = true rustdoc-args = ["--cfg", "docsrs"] rustc-args = ["--cfg", "docsrs"] - -[[example]] -name = "client" -path = "examples/autonat_client.rs" - -[[example]] -name = "server" -path = "examples/autonat_server.rs" diff --git a/examples/autonat_client.rs b/examples/autonat_client.rs deleted file mode 100644 index 9e09a6d556b..00000000000 --- a/examples/autonat_client.rs +++ /dev/null @@ -1,147 +0,0 @@ -// Copyright 2021 Protocol Labs. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the "Software"), -// to deal in the Software without restriction, including without limitation -// the rights to use, copy, modify, merge, publish, distribute, sublicense, -// and/or sell copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -// DEALINGS IN THE SOFTWARE. - -//! Basic example that combines the AutoNAT and identify protocols. -//! -//! The identify protocol informs the local peer of its external addresses, that are then send in AutoNAT dial-back -//! requests to the server. -//! -//! To run this example, follow the instructions in `examples/server` to start a server, then run in a new terminal: -//! ```sh -//! cargo run --example client -- --server-address --server-peer-id --listen-port -//! ``` -//! The `listen-port` parameter is optional and allows to set a fixed port at which the local client should listen. - -use clap::Parser; -use futures::prelude::*; -use libp2p_autonat as autonat; -use libp2p_core::multiaddr::Protocol; -use libp2p_core::{upgrade::Version, Multiaddr, Transport}; -use libp2p_identify as identify; -use libp2p_identity as identity; -use libp2p_identity::PeerId; -use libp2p_noise as noise; -use libp2p_swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent}; -use libp2p_tcp as tcp; -use libp2p_yamux as yamux; -use std::error::Error; -use std::net::Ipv4Addr; -use std::time::Duration; - -#[derive(Debug, Parser)] -#[clap(name = "libp2p autonat")] -struct Opt { - #[clap(long)] - listen_port: Option, - - #[clap(long)] - server_address: Multiaddr, - - #[clap(long)] - server_peer_id: PeerId, -} - -#[async_std::main] -async fn main() -> Result<(), Box> { - env_logger::init(); - - let opt = Opt::parse(); - - let local_key = identity::Keypair::generate_ed25519(); - let local_peer_id = PeerId::from(local_key.public()); - println!("Local peer id: {local_peer_id:?}"); - - let transport = tcp::async_io::Transport::default() - .upgrade(Version::V1) - .authenticate(noise::NoiseAuthenticated::xx(&local_key)?) - .multiplex(yamux::YamuxConfig::default()) - .boxed(); - - let behaviour = Behaviour::new(local_key.public()); - - let mut swarm = - SwarmBuilder::with_async_std_executor(transport, behaviour, local_peer_id).build(); - swarm.listen_on( - Multiaddr::empty() - .with(Protocol::Ip4(Ipv4Addr::UNSPECIFIED)) - .with(Protocol::Tcp(opt.listen_port.unwrap_or(0))), - )?; - - swarm - .behaviour_mut() - .auto_nat - .add_server(opt.server_peer_id, Some(opt.server_address)); - - loop { - match swarm.select_next_some().await { - SwarmEvent::NewListenAddr { address, .. } => println!("Listening on {address:?}"), - SwarmEvent::Behaviour(event) => println!("{event:?}"), - e => println!("{e:?}"), - } - } -} - -#[derive(NetworkBehaviour)] -#[behaviour(out_event = "Event", prelude = "libp2p_swarm::derive_prelude")] -struct Behaviour { - identify: identify::Behaviour, - auto_nat: autonat::Behaviour, -} - -impl Behaviour { - fn new(local_public_key: identity::PublicKey) -> Self { - Self { - identify: identify::Behaviour::new(identify::Config::new( - "/ipfs/0.1.0".into(), - local_public_key.clone(), - )), - auto_nat: autonat::Behaviour::new( - local_public_key.to_peer_id(), - autonat::Config { - retry_interval: Duration::from_secs(10), - refresh_interval: Duration::from_secs(30), - boot_delay: Duration::from_secs(5), - throttle_server_period: Duration::ZERO, - only_global_ips: false, - ..Default::default() - }, - ), - } - } -} - -#[derive(Debug)] -#[allow(clippy::large_enum_variant)] -enum Event { - AutoNat(autonat::Event), - Identify(identify::Event), -} - -impl From for Event { - fn from(v: identify::Event) -> Self { - Self::Identify(v) - } -} - -impl From for Event { - fn from(v: autonat::Event) -> Self { - Self::AutoNat(v) - } -} diff --git a/examples/autonat_server.rs b/examples/autonat_server.rs deleted file mode 100644 index 22da0e8ee4f..00000000000 --- a/examples/autonat_server.rs +++ /dev/null @@ -1,127 +0,0 @@ -// Copyright 2021 Protocol Labs. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the "Software"), -// to deal in the Software without restriction, including without limitation -// the rights to use, copy, modify, merge, publish, distribute, sublicense, -// and/or sell copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -// DEALINGS IN THE SOFTWARE. - -//! Basic example for a AutoNAT server that supports the /libp2p/autonat/1.0.0 and "/ipfs/0.1.0" protocols. -//! -//! To start the server run: -//! ```sh -//! cargo run --example server -- --listen-port -//! ``` -//! The `listen-port` parameter is optional and allows to set a fixed port at which the local peer should listen. - -use clap::Parser; -use futures::prelude::*; -use libp2p_autonat as autonat; -use libp2p_core::{multiaddr::Protocol, upgrade::Version, Multiaddr, Transport}; -use libp2p_identify as identify; -use libp2p_identity as identity; -use libp2p_identity::PeerId; -use libp2p_noise as noise; -use libp2p_swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent}; -use libp2p_tcp as tcp; -use libp2p_yamux as yamux; -use std::error::Error; -use std::net::Ipv4Addr; - -#[derive(Debug, Parser)] -#[clap(name = "libp2p autonat")] -struct Opt { - #[clap(long)] - listen_port: Option, -} - -#[async_std::main] -async fn main() -> Result<(), Box> { - env_logger::init(); - - let opt = Opt::parse(); - - let local_key = identity::Keypair::generate_ed25519(); - let local_peer_id = PeerId::from(local_key.public()); - println!("Local peer id: {local_peer_id:?}"); - - let transport = tcp::async_io::Transport::default() - .upgrade(Version::V1) - .authenticate(noise::NoiseAuthenticated::xx(&local_key)?) - .multiplex(yamux::YamuxConfig::default()) - .boxed(); - - let behaviour = Behaviour::new(local_key.public()); - - let mut swarm = - SwarmBuilder::with_async_std_executor(transport, behaviour, local_peer_id).build(); - swarm.listen_on( - Multiaddr::empty() - .with(Protocol::Ip4(Ipv4Addr::UNSPECIFIED)) - .with(Protocol::Tcp(opt.listen_port.unwrap_or(0))), - )?; - - loop { - match swarm.select_next_some().await { - SwarmEvent::NewListenAddr { address, .. } => println!("Listening on {address:?}"), - SwarmEvent::Behaviour(event) => println!("{event:?}"), - e => println!("{e:?}"), - } - } -} - -#[derive(NetworkBehaviour)] -#[behaviour(out_event = "Event", prelude = "libp2p_swarm::derive_prelude")] -struct Behaviour { - identify: identify::Behaviour, - auto_nat: autonat::Behaviour, -} - -impl Behaviour { - fn new(local_public_key: identity::PublicKey) -> Self { - Self { - identify: identify::Behaviour::new(identify::Config::new( - "/ipfs/0.1.0".into(), - local_public_key.clone(), - )), - auto_nat: autonat::Behaviour::new( - local_public_key.to_peer_id(), - autonat::Config { - only_global_ips: false, - ..Default::default() - }, - ), - } - } -} - -#[derive(Debug)] -#[allow(clippy::large_enum_variant)] -enum Event { - AutoNat(autonat::Event), - Identify(identify::Event), -} - -impl From for Event { - fn from(v: identify::Event) -> Self { - Self::Identify(v) - } -} - -impl From for Event { - fn from(v: autonat::Event) -> Self { - Self::AutoNat(v) - } -}