relay_server_example/
main.rs

1// Copyright 2020 Parity Technologies (UK) Ltd.
2// Copyright 2021 Protocol Labs.
3//
4// Permission is hereby granted, free of charge, to any person obtaining a
5// copy of this software and associated documentation files (the "Software"),
6// to deal in the Software without restriction, including without limitation
7// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8// and/or sell copies of the Software, and to permit persons to whom the
9// Software is furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20// DEALINGS IN THE SOFTWARE.
21
22#![doc = include_str!("../README.md")]
23
24use std::{
25    error::Error,
26    net::{Ipv4Addr, Ipv6Addr},
27};
28
29use clap::Parser;
30use futures::StreamExt;
31use libp2p::{
32    core::{multiaddr::Protocol, Multiaddr},
33    identify, identity, noise, ping, relay,
34    swarm::{NetworkBehaviour, SwarmEvent},
35    tcp, yamux,
36};
37use tracing_subscriber::EnvFilter;
38
39#[tokio::main]
40async fn main() -> Result<(), Box<dyn Error>> {
41    let _ = tracing_subscriber::fmt()
42        .with_env_filter(EnvFilter::from_default_env())
43        .try_init();
44
45    let opt = Opt::parse();
46
47    // Create a static known PeerId based on given secret
48    let local_key: identity::Keypair = generate_ed25519(opt.secret_key_seed);
49
50    let mut swarm = libp2p::SwarmBuilder::with_existing_identity(local_key)
51        .with_tokio()
52        .with_tcp(
53            tcp::Config::default(),
54            noise::Config::new,
55            yamux::Config::default,
56        )?
57        .with_quic()
58        .with_behaviour(|key| Behaviour {
59            relay: relay::Behaviour::new(key.public().to_peer_id(), Default::default()),
60            ping: ping::Behaviour::new(ping::Config::new()),
61            identify: identify::Behaviour::new(identify::Config::new(
62                "/TODO/0.0.1".to_string(),
63                key.public(),
64            )),
65        })?
66        .build();
67
68    // Listen on all interfaces
69    let listen_addr_tcp = Multiaddr::empty()
70        .with(match opt.use_ipv6 {
71            Some(true) => Protocol::from(Ipv6Addr::UNSPECIFIED),
72            _ => Protocol::from(Ipv4Addr::UNSPECIFIED),
73        })
74        .with(Protocol::Tcp(opt.port));
75    swarm.listen_on(listen_addr_tcp)?;
76
77    let listen_addr_quic = Multiaddr::empty()
78        .with(match opt.use_ipv6 {
79            Some(true) => Protocol::from(Ipv6Addr::UNSPECIFIED),
80            _ => Protocol::from(Ipv4Addr::UNSPECIFIED),
81        })
82        .with(Protocol::Udp(opt.port))
83        .with(Protocol::QuicV1);
84    swarm.listen_on(listen_addr_quic)?;
85
86    loop {
87        match swarm.next().await.expect("Infinite Stream.") {
88            SwarmEvent::Behaviour(event) => {
89                if let BehaviourEvent::Identify(identify::Event::Received {
90                    info: identify::Info { observed_addr, .. },
91                    ..
92                }) = &event
93                {
94                    swarm.add_external_address(observed_addr.clone());
95                }
96
97                println!("{event:?}")
98            }
99            SwarmEvent::NewListenAddr { address, .. } => {
100                println!("Listening on {address:?}");
101            }
102            _ => {}
103        }
104    }
105}
106
107#[derive(NetworkBehaviour)]
108struct Behaviour {
109    relay: relay::Behaviour,
110    ping: ping::Behaviour,
111    identify: identify::Behaviour,
112}
113
114fn generate_ed25519(secret_key_seed: u8) -> identity::Keypair {
115    let mut bytes = [0u8; 32];
116    bytes[0] = secret_key_seed;
117
118    identity::Keypair::ed25519_from_bytes(bytes).expect("only errors on wrong length")
119}
120
121#[derive(Debug, Parser)]
122#[command(name = "libp2p relay")]
123struct Opt {
124    /// Determine if the relay listen on ipv6 or ipv4 loopback address. the default is ipv4
125    #[arg(long)]
126    use_ipv6: Option<bool>,
127
128    /// Fixed value to generate deterministic peer id
129    #[arg(long)]
130    secret_key_seed: u8,
131
132    /// The port used to listen on all interfaces
133    #[arg(long)]
134    port: u16,
135}