-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrelay_server.rs
More file actions
87 lines (79 loc) · 2.67 KB
/
relay_server.rs
File metadata and controls
87 lines (79 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#![allow(missing_docs)]
//! Relay server example demonstrating a standalone libp2p relay node.
//!
//! This example shows how to run a relay server that allows peers to:
//! - Make relay reservations to be reachable when behind NAT/firewalls
//! - Create relay circuits to connect to other peers through the relay
//! - Query relay multiaddrs via HTTP endpoint
//!
//! ## What is a Relay Server?
//!
//! A relay server acts as an intermediary for P2P connections when direct
//! connections are not possible due to NAT, firewalls, or network topology.
//! Peers can:
//! 1. Reserve a slot on the relay (relay reservation)
//! 2. Be reached by other peers through the relay (relay circuit)
//!
//! ## Usage
//!
//! Run the relay server with default settings:
//! ```bash
//! cargo run --example relay_server
//! ```
//!
//! The server will:
//! - Generate a random keypair for the relay's peer identity
//! - Listen on a random available TCP port (0.0.0.0:0)
//! - Serve relay multiaddrs via HTTP on port 8888
//! - Accept up to 100 relay connections
//! - Allow up to 10 reservations per peer
//!
//! ## Configuration
//!
//! The example uses:
//! - `max_conns`: Maximum concurrent relay connections (100)
//! - `max_res_per_peer`: Maximum reservations per peer (10)
//! - `http_addr`: HTTP server address for ENR queries (0.0.0.0:8888)
//! - Random TCP port (0) to let the OS choose an available port
//!
//! ## Querying Relay Addresses
//!
//! Once running, query the relay's multiaddrs:
//! ```bash
//! curl http://localhost:8888
//! ```
//!
//! This returns the relay's multiaddrs that clients can use to connect.
use k256::SecretKey;
use pluto_p2p::config::P2PConfig;
use pluto_relay_server::{config::Config, p2p::run_relay_p2p_node};
use pluto_tracing::TracingConfig;
use rand::rngs::OsRng;
use tokio_util::sync::CancellationToken;
use tracing::info;
#[tokio::main]
async fn main() {
pluto_tracing::init(&TracingConfig::default()).expect("Failed to initialize tracing");
let config = Config::builder()
.p2p_config(
P2PConfig::builder()
.with_tcp_addrs(vec!["0.0.0.0:0".to_string()])
.build(),
)
.max_conns(100)
.max_res_per_peer(10)
.http_addr("0.0.0.0:8888".to_string())
.build();
let key = SecretKey::random(&mut OsRng);
let ct = CancellationToken::new();
tokio::select! {
result = run_relay_p2p_node(&config, key, ct.child_token()) => {
result.expect("Failed to run relay P2P node");
}
_ = tokio::signal::ctrl_c() => {
info!("Shutdown signal received, shutting down gracefully...");
ct.cancel();
}
}
info!("Shutdown complete");
}