2022-01-14 10:27:28 +01:00
|
|
|
// 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
|
2022-03-13 08:23:15 +09:00
|
|
|
//! cargo run --example client -- --server-address <server-addr> --server-peer-id <server-peer-id> --listen-port <port>
|
2022-01-14 10:27:28 +01:00
|
|
|
//! ```
|
2022-03-13 08:23:15 +09:00
|
|
|
//! The `listen-port` parameter is optional and allows to set a fixed port at which the local client should listen.
|
2022-01-14 10:27:28 +01:00
|
|
|
|
2022-04-05 21:56:44 +02:00
|
|
|
use clap::Parser;
|
2022-01-14 10:27:28 +01:00
|
|
|
use futures::prelude::*;
|
|
|
|
use libp2p::autonat;
|
2022-10-04 01:17:31 +01:00
|
|
|
use libp2p::identify;
|
2022-01-14 10:27:28 +01:00
|
|
|
use libp2p::multiaddr::Protocol;
|
2022-11-13 10:59:14 +11:00
|
|
|
use libp2p::swarm::{NetworkBehaviour, Swarm, SwarmEvent};
|
|
|
|
use libp2p::{identity, Multiaddr, PeerId};
|
2022-01-14 10:27:28 +01:00
|
|
|
use std::error::Error;
|
|
|
|
use std::net::Ipv4Addr;
|
|
|
|
use std::time::Duration;
|
|
|
|
|
2022-04-05 21:56:44 +02:00
|
|
|
#[derive(Debug, Parser)]
|
|
|
|
#[clap(name = "libp2p autonat")]
|
2022-01-14 10:27:28 +01:00
|
|
|
struct Opt {
|
2022-04-05 21:56:44 +02:00
|
|
|
#[clap(long)]
|
2022-01-14 10:27:28 +01:00
|
|
|
listen_port: Option<u16>,
|
|
|
|
|
2022-04-05 21:56:44 +02:00
|
|
|
#[clap(long)]
|
2022-01-14 10:27:28 +01:00
|
|
|
server_address: Multiaddr,
|
|
|
|
|
2022-04-05 21:56:44 +02:00
|
|
|
#[clap(long)]
|
2022-01-14 10:27:28 +01:00
|
|
|
server_peer_id: PeerId,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_std::main]
|
|
|
|
async fn main() -> Result<(), Box<dyn Error>> {
|
|
|
|
env_logger::init();
|
|
|
|
|
2022-04-05 21:56:44 +02:00
|
|
|
let opt = Opt::parse();
|
2022-01-14 10:27:28 +01:00
|
|
|
|
|
|
|
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 = libp2p::development_transport(local_key.clone()).await?;
|
|
|
|
|
|
|
|
let behaviour = Behaviour::new(local_key.public());
|
|
|
|
|
2022-11-15 15:26:03 +01:00
|
|
|
let mut swarm = Swarm::with_async_std_executor(transport, behaviour, local_peer_id);
|
2022-01-14 10:27:28 +01:00
|
|
|
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")]
|
|
|
|
struct Behaviour {
|
2022-10-04 01:17:31 +01:00
|
|
|
identify: identify::Behaviour,
|
2022-01-14 10:27:28 +01:00
|
|
|
auto_nat: autonat::Behaviour,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Behaviour {
|
|
|
|
fn new(local_public_key: identity::PublicKey) -> Self {
|
|
|
|
Self {
|
2022-10-04 01:17:31 +01:00
|
|
|
identify: identify::Behaviour::new(identify::Config::new(
|
2022-01-14 10:27:28 +01:00
|
|
|
"/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,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2022-10-04 18:24:38 +11:00
|
|
|
#[allow(clippy::large_enum_variant)]
|
2022-01-14 10:27:28 +01:00
|
|
|
enum Event {
|
|
|
|
AutoNat(autonat::Event),
|
2022-10-04 01:17:31 +01:00
|
|
|
Identify(identify::Event),
|
2022-01-14 10:27:28 +01:00
|
|
|
}
|
|
|
|
|
2022-10-04 01:17:31 +01:00
|
|
|
impl From<identify::Event> for Event {
|
|
|
|
fn from(v: identify::Event) -> Self {
|
2022-01-14 10:27:28 +01:00
|
|
|
Self::Identify(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<autonat::Event> for Event {
|
|
|
|
fn from(v: autonat::Event) -> Self {
|
|
|
|
Self::AutoNat(v)
|
|
|
|
}
|
|
|
|
}
|