|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#![doc = include_str!("../README.md")] |
|
|
|
|
|
use clap::Parser; |
|
|
use futures::{executor::block_on, future::FutureExt, stream::StreamExt}; |
|
|
use libp2p::{ |
|
|
core::multiaddr::{Multiaddr, Protocol}, |
|
|
dcutr, identify, identity, noise, ping, relay, |
|
|
swarm::{NetworkBehaviour, SwarmEvent}, |
|
|
tcp, yamux, PeerId, |
|
|
}; |
|
|
use std::str::FromStr; |
|
|
use std::{error::Error, time::Duration}; |
|
|
use tracing_subscriber::EnvFilter; |
|
|
|
|
|
#[derive(Debug, Parser)] |
|
|
#[clap(name = "libp2p DCUtR client")] |
|
|
struct Opts { |
|
|
|
|
|
#[clap(long)] |
|
|
mode: Mode, |
|
|
|
|
|
|
|
|
#[clap(long)] |
|
|
secret_key_seed: u8, |
|
|
|
|
|
|
|
|
#[clap(long)] |
|
|
relay_address: Multiaddr, |
|
|
|
|
|
|
|
|
#[clap(long)] |
|
|
remote_peer_id: Option<PeerId>, |
|
|
} |
|
|
|
|
|
#[derive(Clone, Debug, PartialEq, Parser)] |
|
|
enum Mode { |
|
|
Dial, |
|
|
Listen, |
|
|
} |
|
|
|
|
|
impl FromStr for Mode { |
|
|
type Err = String; |
|
|
fn from_str(mode: &str) -> Result<Self, Self::Err> { |
|
|
match mode { |
|
|
"dial" => Ok(Mode::Dial), |
|
|
"listen" => Ok(Mode::Listen), |
|
|
_ => Err("Expected either 'dial' or 'listen'".to_string()), |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
#[tokio::main] |
|
|
async fn main() -> Result<(), Box<dyn Error>> { |
|
|
let _ = tracing_subscriber::fmt() |
|
|
.with_env_filter(EnvFilter::from_default_env()) |
|
|
.try_init(); |
|
|
|
|
|
let opts = Opts::parse(); |
|
|
|
|
|
#[derive(NetworkBehaviour)] |
|
|
struct Behaviour { |
|
|
relay_client: relay::client::Behaviour, |
|
|
ping: ping::Behaviour, |
|
|
identify: identify::Behaviour, |
|
|
dcutr: dcutr::Behaviour, |
|
|
} |
|
|
|
|
|
let mut swarm = |
|
|
libp2p::SwarmBuilder::with_existing_identity(generate_ed25519(opts.secret_key_seed)) |
|
|
.with_tokio() |
|
|
.with_tcp( |
|
|
tcp::Config::default().nodelay(true), |
|
|
noise::Config::new, |
|
|
yamux::Config::default, |
|
|
)? |
|
|
.with_quic() |
|
|
.with_dns()? |
|
|
.with_relay_client(noise::Config::new, yamux::Config::default)? |
|
|
.with_behaviour(|keypair, relay_behaviour| Behaviour { |
|
|
relay_client: relay_behaviour, |
|
|
ping: ping::Behaviour::new(ping::Config::new()), |
|
|
identify: identify::Behaviour::new(identify::Config::new( |
|
|
"/TODO/0.0.1".to_string(), |
|
|
keypair.public(), |
|
|
)), |
|
|
dcutr: dcutr::Behaviour::new(keypair.public().to_peer_id()), |
|
|
})? |
|
|
.with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(60))) |
|
|
.build(); |
|
|
|
|
|
swarm |
|
|
.listen_on("/ip4/0.0.0.0/udp/0/quic-v1".parse().unwrap()) |
|
|
.unwrap(); |
|
|
swarm |
|
|
.listen_on("/ip4/0.0.0.0/tcp/0".parse().unwrap()) |
|
|
.unwrap(); |
|
|
|
|
|
|
|
|
block_on(async { |
|
|
let mut delay = futures_timer::Delay::new(std::time::Duration::from_secs(1)).fuse(); |
|
|
loop { |
|
|
futures::select! { |
|
|
event = swarm.next() => { |
|
|
match event.unwrap() { |
|
|
SwarmEvent::NewListenAddr { address, .. } => { |
|
|
tracing::info!(%address, "Listening on address"); |
|
|
} |
|
|
event => panic!("{event:?}"), |
|
|
} |
|
|
} |
|
|
_ = delay => { |
|
|
|
|
|
break; |
|
|
} |
|
|
} |
|
|
} |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
swarm.dial(opts.relay_address.clone()).unwrap(); |
|
|
block_on(async { |
|
|
let mut learned_observed_addr = false; |
|
|
let mut told_relay_observed_addr = false; |
|
|
|
|
|
loop { |
|
|
match swarm.next().await.unwrap() { |
|
|
SwarmEvent::NewListenAddr { .. } => {} |
|
|
SwarmEvent::Dialing { .. } => {} |
|
|
SwarmEvent::ConnectionEstablished { .. } => {} |
|
|
SwarmEvent::Behaviour(BehaviourEvent::Ping(_)) => {} |
|
|
SwarmEvent::Behaviour(BehaviourEvent::Identify(identify::Event::Sent { |
|
|
.. |
|
|
})) => { |
|
|
tracing::info!("Told relay its public address"); |
|
|
told_relay_observed_addr = true; |
|
|
} |
|
|
SwarmEvent::Behaviour(BehaviourEvent::Identify(identify::Event::Received { |
|
|
info: identify::Info { observed_addr, .. }, |
|
|
.. |
|
|
})) => { |
|
|
tracing::info!(address=%observed_addr, "Relay told us our observed address"); |
|
|
learned_observed_addr = true; |
|
|
} |
|
|
event => panic!("{event:?}"), |
|
|
} |
|
|
|
|
|
if learned_observed_addr && told_relay_observed_addr { |
|
|
break; |
|
|
} |
|
|
} |
|
|
}); |
|
|
|
|
|
match opts.mode { |
|
|
Mode::Dial => { |
|
|
swarm |
|
|
.dial( |
|
|
opts.relay_address |
|
|
.with(Protocol::P2pCircuit) |
|
|
.with(Protocol::P2p(opts.remote_peer_id.unwrap())), |
|
|
) |
|
|
.unwrap(); |
|
|
} |
|
|
Mode::Listen => { |
|
|
swarm |
|
|
.listen_on(opts.relay_address.with(Protocol::P2pCircuit)) |
|
|
.unwrap(); |
|
|
} |
|
|
} |
|
|
|
|
|
block_on(async { |
|
|
loop { |
|
|
match swarm.next().await.unwrap() { |
|
|
SwarmEvent::NewListenAddr { address, .. } => { |
|
|
tracing::info!(%address, "Listening on address"); |
|
|
} |
|
|
SwarmEvent::Behaviour(BehaviourEvent::RelayClient( |
|
|
relay::client::Event::ReservationReqAccepted { .. }, |
|
|
)) => { |
|
|
assert!(opts.mode == Mode::Listen); |
|
|
tracing::info!("Relay accepted our reservation request"); |
|
|
} |
|
|
SwarmEvent::Behaviour(BehaviourEvent::RelayClient(event)) => { |
|
|
tracing::info!(?event) |
|
|
} |
|
|
SwarmEvent::Behaviour(BehaviourEvent::Dcutr(event)) => { |
|
|
tracing::info!(?event) |
|
|
} |
|
|
SwarmEvent::Behaviour(BehaviourEvent::Identify(event)) => { |
|
|
tracing::info!(?event) |
|
|
} |
|
|
SwarmEvent::Behaviour(BehaviourEvent::Ping(_)) => {} |
|
|
SwarmEvent::ConnectionEstablished { |
|
|
peer_id, endpoint, .. |
|
|
} => { |
|
|
tracing::info!(peer=%peer_id, ?endpoint, "Established new connection"); |
|
|
} |
|
|
SwarmEvent::OutgoingConnectionError { peer_id, error, .. } => { |
|
|
tracing::info!(peer=?peer_id, "Outgoing connection failed: {error}"); |
|
|
} |
|
|
_ => {} |
|
|
} |
|
|
} |
|
|
}) |
|
|
} |
|
|
|
|
|
fn generate_ed25519(secret_key_seed: u8) -> identity::Keypair { |
|
|
let mut bytes = [0u8; 32]; |
|
|
bytes[0] = secret_key_seed; |
|
|
|
|
|
identity::Keypair::ed25519_from_bytes(bytes).expect("only errors on wrong length") |
|
|
} |
|
|
|