|
| 1 | +// Copyright 2023 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 | +#![doc = include_str!("../README.md")] |
| 22 | + |
| 23 | +use futures::prelude::*; |
| 24 | +use libp2p::core::upgrade::Version; |
| 25 | +use libp2p::{ |
| 26 | + identity, noise, |
| 27 | + swarm::{SwarmBuilder, SwarmEvent}, |
| 28 | + tcp, upnp, yamux, Multiaddr, PeerId, Transport, |
| 29 | +}; |
| 30 | +use std::error::Error; |
| 31 | + |
| 32 | +#[tokio::main] |
| 33 | +async fn main() -> Result<(), Box<dyn Error>> { |
| 34 | + let local_key = identity::Keypair::generate_ed25519(); |
| 35 | + let local_peer_id = PeerId::from(local_key.public()); |
| 36 | + println!("Local peer id: {local_peer_id:?}"); |
| 37 | + |
| 38 | + let transport = tcp::tokio::Transport::default() |
| 39 | + .upgrade(Version::V1Lazy) |
| 40 | + .authenticate(noise::Config::new(&local_key)?) |
| 41 | + .multiplex(yamux::Config::default()) |
| 42 | + .boxed(); |
| 43 | + |
| 44 | + let mut swarm = SwarmBuilder::with_tokio_executor( |
| 45 | + transport, |
| 46 | + upnp::tokio::Behaviour::default(), |
| 47 | + local_peer_id, |
| 48 | + ) |
| 49 | + .build(); |
| 50 | + |
| 51 | + // Tell the swarm to listen on all interfaces and a random, OS-assigned |
| 52 | + // port. |
| 53 | + swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?; |
| 54 | + |
| 55 | + // Dial the peer identified by the multi-address given as the second |
| 56 | + // command-line argument, if any. |
| 57 | + if let Some(addr) = std::env::args().nth(1) { |
| 58 | + let remote: Multiaddr = addr.parse()?; |
| 59 | + swarm.dial(remote)?; |
| 60 | + println!("Dialed {addr}") |
| 61 | + } |
| 62 | + |
| 63 | + loop { |
| 64 | + match swarm.select_next_some().await { |
| 65 | + SwarmEvent::NewListenAddr { address, .. } => println!("Listening on {address:?}"), |
| 66 | + SwarmEvent::Behaviour(upnp::Event::NewExternalAddr(addr)) => { |
| 67 | + println!("New external address: {addr}"); |
| 68 | + } |
| 69 | + SwarmEvent::Behaviour(upnp::Event::GatewayNotFound) => { |
| 70 | + println!("Gateway does not support UPnP"); |
| 71 | + break; |
| 72 | + } |
| 73 | + SwarmEvent::Behaviour(upnp::Event::NonRoutableGateway) => { |
| 74 | + println!("Gateway is not exposed directly to the public Internet, i.e. it itself has a private IP address."); |
| 75 | + break; |
| 76 | + } |
| 77 | + _ => {} |
| 78 | + } |
| 79 | + } |
| 80 | + Ok(()) |
| 81 | +} |
0 commit comments