|
| 1 | +// Copyright 2021 Protocol Labs. |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a |
| 4 | +// copy of this software and associated documentation files (the "Software"), |
| 5 | +// to deal in the Software without restriction, including without limitation |
| 6 | +// the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 7 | +// and/or sell copies of the Software, and to permit persons to whom the |
| 8 | +// Software is furnished to do so, subject to the following conditions: |
| 9 | +// |
| 10 | +// The above copyright notice and this permission notice shall be included in |
| 11 | +// all copies or substantial portions of the Software. |
| 12 | +// |
| 13 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 14 | +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 18 | +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 19 | +// DEALINGS IN THE SOFTWARE. |
| 20 | + |
| 21 | +//! Basic example that combines the AutoNAT and identify protocols. |
| 22 | +//! |
| 23 | +//! The identify protocol informs the local peer of its external addresses, that are then send in AutoNAT dial-back |
| 24 | +//! requests to the server. |
| 25 | +//! |
| 26 | +//! To run this example, follow the instructions in `examples/server` to start a server, then run in a new terminal: |
| 27 | +//! ```sh |
| 28 | +//! cargo run --example client -- --server-address <server-addr> --server-peer-id <server-peer-id> --listen_port <port> |
| 29 | +//! ``` |
| 30 | +//! The `listen_port` parameter is optional and allows to set a fixed port at which the local client should listen. |
| 31 | +
|
| 32 | +use futures::prelude::*; |
| 33 | +use libp2p::autonat; |
| 34 | +use libp2p::identify::{Identify, IdentifyConfig, IdentifyEvent}; |
| 35 | +use libp2p::multiaddr::Protocol; |
| 36 | +use libp2p::swarm::{Swarm, SwarmEvent}; |
| 37 | +use libp2p::{identity, Multiaddr, NetworkBehaviour, PeerId}; |
| 38 | +use std::error::Error; |
| 39 | +use std::net::Ipv4Addr; |
| 40 | +use std::time::Duration; |
| 41 | +use structopt::StructOpt; |
| 42 | + |
| 43 | +#[derive(Debug, StructOpt)] |
| 44 | +#[structopt(name = "libp2p autonat")] |
| 45 | +struct Opt { |
| 46 | + #[structopt(long)] |
| 47 | + listen_port: Option<u16>, |
| 48 | + |
| 49 | + #[structopt(long)] |
| 50 | + server_address: Multiaddr, |
| 51 | + |
| 52 | + #[structopt(long)] |
| 53 | + server_peer_id: PeerId, |
| 54 | +} |
| 55 | + |
| 56 | +#[async_std::main] |
| 57 | +async fn main() -> Result<(), Box<dyn Error>> { |
| 58 | + env_logger::init(); |
| 59 | + |
| 60 | + let opt = Opt::from_args(); |
| 61 | + |
| 62 | + let local_key = identity::Keypair::generate_ed25519(); |
| 63 | + let local_peer_id = PeerId::from(local_key.public()); |
| 64 | + println!("Local peer id: {:?}", local_peer_id); |
| 65 | + |
| 66 | + let transport = libp2p::development_transport(local_key.clone()).await?; |
| 67 | + |
| 68 | + let behaviour = Behaviour::new(local_key.public()); |
| 69 | + |
| 70 | + let mut swarm = Swarm::new(transport, behaviour, local_peer_id); |
| 71 | + swarm.listen_on( |
| 72 | + Multiaddr::empty() |
| 73 | + .with(Protocol::Ip4(Ipv4Addr::UNSPECIFIED)) |
| 74 | + .with(Protocol::Tcp(opt.listen_port.unwrap_or(0))), |
| 75 | + )?; |
| 76 | + |
| 77 | + swarm |
| 78 | + .behaviour_mut() |
| 79 | + .auto_nat |
| 80 | + .add_server(opt.server_peer_id, Some(opt.server_address)); |
| 81 | + |
| 82 | + loop { |
| 83 | + match swarm.select_next_some().await { |
| 84 | + SwarmEvent::NewListenAddr { address, .. } => println!("Listening on {:?}", address), |
| 85 | + SwarmEvent::Behaviour(event) => println!("{:?}", event), |
| 86 | + e => println!("{:?}", e), |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +#[derive(NetworkBehaviour)] |
| 92 | +#[behaviour(out_event = "Event")] |
| 93 | +struct Behaviour { |
| 94 | + identify: Identify, |
| 95 | + auto_nat: autonat::Behaviour, |
| 96 | +} |
| 97 | + |
| 98 | +impl Behaviour { |
| 99 | + fn new(local_public_key: identity::PublicKey) -> Self { |
| 100 | + Self { |
| 101 | + identify: Identify::new(IdentifyConfig::new( |
| 102 | + "/ipfs/0.1.0".into(), |
| 103 | + local_public_key.clone(), |
| 104 | + )), |
| 105 | + auto_nat: autonat::Behaviour::new( |
| 106 | + local_public_key.to_peer_id(), |
| 107 | + autonat::Config { |
| 108 | + retry_interval: Duration::from_secs(10), |
| 109 | + refresh_interval: Duration::from_secs(30), |
| 110 | + boot_delay: Duration::from_secs(5), |
| 111 | + throttle_server_period: Duration::ZERO, |
| 112 | + ..Default::default() |
| 113 | + }, |
| 114 | + ), |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +#[derive(Debug)] |
| 120 | +enum Event { |
| 121 | + AutoNat(autonat::Event), |
| 122 | + Identify(IdentifyEvent), |
| 123 | +} |
| 124 | + |
| 125 | +impl From<IdentifyEvent> for Event { |
| 126 | + fn from(v: IdentifyEvent) -> Self { |
| 127 | + Self::Identify(v) |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +impl From<autonat::Event> for Event { |
| 132 | + fn from(v: autonat::Event) -> Self { |
| 133 | + Self::AutoNat(v) |
| 134 | + } |
| 135 | +} |
0 commit comments