Skip to content

Commit

Permalink
add webRTC listener (#675)
Browse files Browse the repository at this point in the history
  • Loading branch information
sh3ll3x3c authored Aug 20, 2024
1 parent 5d33920 commit ce5f8a9
Show file tree
Hide file tree
Showing 10 changed files with 687 additions and 26 deletions.
644 changes: 633 additions & 11 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,10 @@ http_server_port = 7007
# If set to key, a valid ed25519 private key must be provided, else the client will fail
# If `secret_key` is not set, random seed will be used.
secret_key = { seed={seed} }
# P2P service port (default: 37000).
# P2P TCP listener port (default: 37000).
port = 37000
# P2P WebRTC listener port (default: 37001).
webrtc_port = 37001
# Configures AutoNAT behaviour to reject probes as a server for clients that are observed at a non-global ip address (default: false)
autonat_only_global_ips = false
# AutoNat throttle period for re-using a peer as server for a dial-request. (default: 1s)
Expand Down
3 changes: 3 additions & 0 deletions client/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ pub struct CliOpts {
/// P2P port
#[arg(short, long)]
pub port: Option<u16>,
/// P2P WebRTC port
#[arg(short, long)]
pub webrtc_port: Option<u16>,
/// HTTP port
#[arg(long)]
pub http_server_port: Option<u16>,
Expand Down
25 changes: 18 additions & 7 deletions client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,20 @@ async fn run(
cfg.libp2p.kademlia.kad_record_ttl,
);

// Start listening on provided port
let addrs = vec![
cfg.libp2p.tcp_multiaddress(),
cfg.libp2p.webrtc_multiaddress(),
];

// Start the TCP and WebRTC listeners
p2p_client
.start_listening(cfg.libp2p.multiaddress())
.start_listening(addrs)
.await
.wrap_err("Listening on TCP not to fail.")?;
info!("TCP listener started on port {}", cfg.libp2p.port);
.wrap_err("Error starting li.")?;
info!(
"TCP listener started on port {}. WebRTC listening on port {}.",
cfg.libp2p.port, cfg.libp2p.webrtc_port
);

let p2p_clone = p2p_client.to_owned();
let cfg_clone = cfg.to_owned();
Expand Down Expand Up @@ -409,11 +417,11 @@ async fn run_fat(
cfg.libp2p.kademlia.kad_record_ttl,
);

// Start listening on provided port
// Start listening on P2P port
p2p_client
.start_listening(cfg.libp2p.multiaddress())
.start_listening(vec![cfg.libp2p.tcp_multiaddress()])
.await
.wrap_err("Listening on TCP not to fail.")?;
.wrap_err("Error starting listeners.")?;
info!("TCP listener started on port {}", cfg.libp2p.port);

let p2p_clone = p2p_client.to_owned();
Expand Down Expand Up @@ -554,6 +562,9 @@ pub fn load_runtime_config(opts: &CliOpts) -> Result<RuntimeConfig> {
if let Some(http_port) = opts.http_server_port {
cfg.api.http_server_port = http_port;
}
if let Some(webrtc_port) = opts.webrtc_port {
cfg.libp2p.webrtc_port = webrtc_port;
}
if let Some(avail_path) = &opts.avail_path {
cfg.avail_path = avail_path.to_string();
}
Expand Down
1 change: 1 addition & 0 deletions core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## [1.12.0]

- Add `WebRTC` listener with config parameter and CLI option for port setting
- Remove old crate patches that were required when `subxt` was directly used
- Dial peer at mdns step to trigger identify
- Update expected system version to 2.2
Expand Down
1 change: 1 addition & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ jsonrpsee-core = { version = "0.21.0", features = ["client"] }
libc = "0.2.150"
libp2p = { workspace = true }
libp2p-allow-block-list = { workspace = true }
libp2p-webrtc = { version = "=0.7.1-alpha", features = ["tokio"] }
mockall = "0.11.3"
multihash = { workspace = true }
num = "0.4.0"
Expand Down
8 changes: 8 additions & 0 deletions core/src/network/p2p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ use libp2p::{
swarm::NetworkBehaviour,
tcp, upnp, yamux, Multiaddr, PeerId, Swarm, SwarmBuilder,
};
use libp2p_webrtc as webrtc;
use multihash::{self, Hasher};
use rand::thread_rng;
use semver::Version;
use serde::{Deserialize, Serialize};
use std::{fmt, net::Ipv4Addr, str::FromStr};
Expand Down Expand Up @@ -234,6 +236,12 @@ async fn build_swarm(
noise::Config::new,
yamux::Config::default,
)?
.with_other_transport(|id_keys| {
Ok(webrtc::tokio::Transport::new(
id_keys.clone(),
webrtc::tokio::Certificate::generate(&mut thread_rng())?,
))
})?
.with_dns()?
.with_relay_client(noise::Config::new, yamux::Config::default)?
.with_behaviour(behaviour)?
Expand Down
9 changes: 6 additions & 3 deletions core/src/network/p2p/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,15 @@ impl Client {
.wrap_err("sender should not be dropped")?
}

pub async fn start_listening(&self, addr: Multiaddr) -> Result<ListenerId> {
pub async fn start_listening(&self, addrs: Vec<Multiaddr>) -> Result<Vec<ListenerId>> {
self.execute_sync(|response_sender| {
Box::new(move |context: &mut EventLoop| {
let result = context.swarm.listen_on(addr.clone());
let results: Result<Vec<ListenerId>, _> = addrs
.into_iter()
.map(|addr| context.swarm.listen_on(addr))
.collect();
response_sender
.send(result.map_err(Into::into))
.send(results.map_err(Into::into))
.map_err(|e| {
eyre!("Encountered error while sending Start Listening response: {e:?}")
})?;
Expand Down
14 changes: 12 additions & 2 deletions core/src/network/p2p/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,11 @@ pub struct LibP2PConfig {
/// If set to key, a valid ed25519 private key must be provided, else the client will fail
/// If `secret_key` is not set, random seed will be used.
pub secret_key: Option<SecretKey>,
/// P2P service port (default: 37000).
/// P2P TCP listener port (default: 37000).
pub port: u16,
/// P2P WebRTC listener port (default: 37001).
pub webrtc_port: u16,
/// P2P WebSocket switch. Note: it's mutually exclusive with the TCP listener (default: false)
pub ws_transport_enable: bool,
/// AutoNAT configuration
#[serde(flatten)]
Expand Down Expand Up @@ -157,6 +160,7 @@ impl Default for LibP2PConfig {
Self {
secret_key: None,
port: 37000,
webrtc_port: 37001,
ws_transport_enable: false,
autonat: Default::default(),
kademlia: Default::default(),
Expand All @@ -174,7 +178,7 @@ impl Default for LibP2PConfig {
}

impl LibP2PConfig {
pub fn multiaddress(&self) -> Multiaddr {
pub fn tcp_multiaddress(&self) -> Multiaddr {
let tcp_multiaddress = Multiaddr::empty()
.with(Protocol::from(Ipv4Addr::UNSPECIFIED))
.with(Protocol::Tcp(self.port));
Expand All @@ -185,6 +189,12 @@ impl LibP2PConfig {
tcp_multiaddress
}
}

pub fn webrtc_multiaddress(&self) -> Multiaddr {
Multiaddr::from(Ipv4Addr::UNSPECIFIED)
.with(Protocol::Udp(self.webrtc_port))
.with(Protocol::WebRTCDirect)
}
}

impl From<&LibP2PConfig> for kad::Config {
Expand Down
4 changes: 2 additions & 2 deletions crawler/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ async fn run(config: Config, db: RocksDB, shutdown: Controller<String>) -> Resul
);

p2p_client
.start_listening(config.libp2p.multiaddress())
.start_listening(vec![config.libp2p.tcp_multiaddress()])
.await
.wrap_err("Listening on TCP not to fail.")?;
.wrap_err("Error starting listeners.")?;
info!("TCP listener started on port {}", config.libp2p.port);

let bootstrap_p2p_client = p2p_client.clone();
Expand Down

0 comments on commit ce5f8a9

Please sign in to comment.