2022-01-14 19:58:28 +01:00
|
|
|
// Copyright 2020 Parity Technologies (UK) Ltd.
|
|
|
|
// 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.
|
|
|
|
|
2022-04-05 21:56:44 +02:00
|
|
|
use clap::Parser;
|
2022-01-14 19:58:28 +01:00
|
|
|
use futures::executor::block_on;
|
|
|
|
use futures::stream::StreamExt;
|
|
|
|
use libp2p::core::upgrade;
|
2022-10-04 01:17:31 +01:00
|
|
|
use libp2p::identify;
|
2022-01-14 19:58:28 +01:00
|
|
|
use libp2p::multiaddr::Protocol;
|
2022-10-24 15:41:08 +11:00
|
|
|
use libp2p::ping;
|
2022-01-14 19:58:28 +01:00
|
|
|
use libp2p::relay::v2::relay::{self, Relay};
|
2022-11-13 10:59:14 +11:00
|
|
|
use libp2p::swarm::{NetworkBehaviour, Swarm, SwarmEvent};
|
2022-10-24 15:41:08 +11:00
|
|
|
use libp2p::tcp;
|
|
|
|
use libp2p::Transport;
|
2022-11-13 10:59:14 +11:00
|
|
|
use libp2p::{identity, PeerId};
|
2022-01-14 19:58:28 +01:00
|
|
|
use libp2p::{noise, Multiaddr};
|
|
|
|
use std::error::Error;
|
|
|
|
use std::net::{Ipv4Addr, Ipv6Addr};
|
|
|
|
|
|
|
|
fn main() -> Result<(), Box<dyn Error>> {
|
|
|
|
env_logger::init();
|
|
|
|
|
2022-04-05 21:56:44 +02:00
|
|
|
let opt = Opt::parse();
|
2022-01-14 19:58:28 +01:00
|
|
|
println!("opt: {:?}", opt);
|
|
|
|
|
|
|
|
// Create a static known PeerId based on given secret
|
|
|
|
let local_key: identity::Keypair = generate_ed25519(opt.secret_key_seed);
|
|
|
|
let local_peer_id = PeerId::from(local_key.public());
|
|
|
|
println!("Local peer id: {:?}", local_peer_id);
|
|
|
|
|
2022-10-24 15:41:08 +11:00
|
|
|
let tcp_transport = tcp::async_io::Transport::default();
|
2022-01-14 19:58:28 +01:00
|
|
|
|
|
|
|
let transport = tcp_transport
|
|
|
|
.upgrade(upgrade::Version::V1)
|
2022-09-16 11:41:35 +10:00
|
|
|
.authenticate(
|
|
|
|
noise::NoiseAuthenticated::xx(&local_key)
|
|
|
|
.expect("Signing libp2p-noise static DH keypair failed."),
|
|
|
|
)
|
2022-09-19 17:32:02 +10:00
|
|
|
.multiplex(libp2p::yamux::YamuxConfig::default())
|
2022-01-14 19:58:28 +01:00
|
|
|
.boxed();
|
|
|
|
|
|
|
|
let behaviour = Behaviour {
|
|
|
|
relay: Relay::new(local_peer_id, Default::default()),
|
2022-10-01 00:19:34 +10:00
|
|
|
ping: ping::Behaviour::new(ping::Config::new()),
|
2022-10-04 01:17:31 +01:00
|
|
|
identify: identify::Behaviour::new(identify::Config::new(
|
2022-01-14 19:58:28 +01:00
|
|
|
"/TODO/0.0.1".to_string(),
|
|
|
|
local_key.public(),
|
|
|
|
)),
|
|
|
|
};
|
|
|
|
|
2022-11-15 15:26:03 +01:00
|
|
|
let mut swarm = Swarm::without_executor(transport, behaviour, local_peer_id);
|
2022-01-14 19:58:28 +01:00
|
|
|
|
|
|
|
// Listen on all interfaces
|
|
|
|
let listen_addr = Multiaddr::empty()
|
|
|
|
.with(match opt.use_ipv6 {
|
|
|
|
Some(true) => Protocol::from(Ipv6Addr::UNSPECIFIED),
|
|
|
|
_ => Protocol::from(Ipv4Addr::UNSPECIFIED),
|
|
|
|
})
|
|
|
|
.with(Protocol::Tcp(opt.port));
|
|
|
|
swarm.listen_on(listen_addr)?;
|
|
|
|
|
|
|
|
block_on(async {
|
|
|
|
loop {
|
|
|
|
match swarm.next().await.expect("Infinite Stream.") {
|
|
|
|
SwarmEvent::Behaviour(Event::Relay(event)) => {
|
|
|
|
println!("{:?}", event)
|
|
|
|
}
|
|
|
|
SwarmEvent::NewListenAddr { address, .. } => {
|
|
|
|
println!("Listening on {:?}", address);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(NetworkBehaviour)]
|
|
|
|
#[behaviour(out_event = "Event", event_process = false)]
|
|
|
|
struct Behaviour {
|
|
|
|
relay: Relay,
|
2022-10-01 00:19:34 +10:00
|
|
|
ping: ping::Behaviour,
|
2022-10-04 01:17:31 +01:00
|
|
|
identify: identify::Behaviour,
|
2022-01-14 19:58:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum Event {
|
2022-10-01 00:19:34 +10:00
|
|
|
Ping(ping::Event),
|
2022-10-04 01:17:31 +01:00
|
|
|
Identify(identify::Event),
|
2022-01-14 19:58:28 +01:00
|
|
|
Relay(relay::Event),
|
|
|
|
}
|
|
|
|
|
2022-10-01 00:19:34 +10:00
|
|
|
impl From<ping::Event> for Event {
|
|
|
|
fn from(e: ping::Event) -> Self {
|
2022-01-14 19:58:28 +01:00
|
|
|
Event::Ping(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-04 01:17:31 +01:00
|
|
|
impl From<identify::Event> for Event {
|
|
|
|
fn from(e: identify::Event) -> Self {
|
2022-01-14 19:58:28 +01:00
|
|
|
Event::Identify(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<relay::Event> for Event {
|
|
|
|
fn from(e: relay::Event) -> Self {
|
|
|
|
Event::Relay(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn generate_ed25519(secret_key_seed: u8) -> identity::Keypair {
|
|
|
|
let mut bytes = [0u8; 32];
|
|
|
|
bytes[0] = secret_key_seed;
|
|
|
|
|
|
|
|
let secret_key = identity::ed25519::SecretKey::from_bytes(&mut bytes)
|
|
|
|
.expect("this returns `Err` only if the length is wrong; the length is correct; qed");
|
|
|
|
identity::Keypair::Ed25519(secret_key.into())
|
|
|
|
}
|
|
|
|
|
2022-04-05 21:56:44 +02:00
|
|
|
#[derive(Debug, Parser)]
|
|
|
|
#[clap(name = "libp2p relay")]
|
2022-01-14 19:58:28 +01:00
|
|
|
struct Opt {
|
|
|
|
/// Determine if the relay listen on ipv6 or ipv4 loopback address. the default is ipv4
|
2022-04-05 21:56:44 +02:00
|
|
|
#[clap(long)]
|
2022-01-14 19:58:28 +01:00
|
|
|
use_ipv6: Option<bool>,
|
|
|
|
|
|
|
|
/// Fixed value to generate deterministic peer id
|
2022-04-05 21:56:44 +02:00
|
|
|
#[clap(long)]
|
2022-01-14 19:58:28 +01:00
|
|
|
secret_key_seed: u8,
|
|
|
|
|
|
|
|
/// The port used to listen on all interfaces
|
2022-04-05 21:56:44 +02:00
|
|
|
#[clap(long)]
|
2022-01-14 19:58:28 +01:00
|
|
|
port: u16,
|
|
|
|
}
|