mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-29 09:41:34 +00:00
feat(libp2p): add SwarmBuilder
Introduce the new `libp2p::SwarmBuilder`. Users should use the new `libp2p::SwarmBuilder` instead of the now deprecated `libp2p::swarm::SwarmBuilder`. See `libp2p::SwarmBuilder` docs on how to use the new builder. Fixes #3657. Fixes #3563. Fixes #3179. Pull-Request: #4120.
This commit is contained in:
@ -21,10 +21,10 @@
|
||||
#![doc = include_str!("../../README.md")]
|
||||
|
||||
use clap::Parser;
|
||||
use futures::prelude::*;
|
||||
use futures::StreamExt;
|
||||
use libp2p::core::multiaddr::Protocol;
|
||||
use libp2p::core::{upgrade::Version, Multiaddr, Transport};
|
||||
use libp2p::swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent};
|
||||
use libp2p::core::Multiaddr;
|
||||
use libp2p::swarm::{NetworkBehaviour, SwarmEvent};
|
||||
use libp2p::{autonat, identify, identity, noise, tcp, yamux, PeerId};
|
||||
use std::error::Error;
|
||||
use std::net::Ipv4Addr;
|
||||
@ -49,18 +49,16 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
|
||||
let opt = Opt::parse();
|
||||
|
||||
let local_key = identity::Keypair::generate_ed25519();
|
||||
let local_peer_id = PeerId::from(local_key.public());
|
||||
let mut swarm = libp2p::SwarmBuilder::with_new_identity()
|
||||
.with_tokio()
|
||||
.with_tcp(
|
||||
tcp::Config::default(),
|
||||
noise::Config::new,
|
||||
yamux::Config::default,
|
||||
)?
|
||||
.with_behaviour(|key| Behaviour::new(key.public()))?
|
||||
.build();
|
||||
|
||||
let transport = tcp::tokio::Transport::default()
|
||||
.upgrade(Version::V1Lazy)
|
||||
.authenticate(noise::Config::new(&local_key)?)
|
||||
.multiplex(yamux::Config::default())
|
||||
.boxed();
|
||||
|
||||
let behaviour = Behaviour::new(local_key.public());
|
||||
|
||||
let mut swarm = SwarmBuilder::with_tokio_executor(transport, behaviour, local_peer_id).build();
|
||||
swarm.listen_on(
|
||||
Multiaddr::empty()
|
||||
.with(Protocol::Ip4(Ipv4Addr::UNSPECIFIED))
|
||||
|
@ -21,10 +21,10 @@
|
||||
#![doc = include_str!("../../README.md")]
|
||||
|
||||
use clap::Parser;
|
||||
use futures::prelude::*;
|
||||
use libp2p::core::{multiaddr::Protocol, upgrade::Version, Multiaddr, Transport};
|
||||
use libp2p::swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent};
|
||||
use libp2p::{autonat, identify, identity, noise, tcp, yamux, PeerId};
|
||||
use futures::StreamExt;
|
||||
use libp2p::core::{multiaddr::Protocol, Multiaddr};
|
||||
use libp2p::swarm::{NetworkBehaviour, SwarmEvent};
|
||||
use libp2p::{autonat, identify, identity, noise, tcp, yamux};
|
||||
use std::error::Error;
|
||||
use std::net::Ipv4Addr;
|
||||
|
||||
@ -41,18 +41,16 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
|
||||
let opt = Opt::parse();
|
||||
|
||||
let local_key = identity::Keypair::generate_ed25519();
|
||||
let local_peer_id = PeerId::from(local_key.public());
|
||||
let mut swarm = libp2p::SwarmBuilder::with_new_identity()
|
||||
.with_tokio()
|
||||
.with_tcp(
|
||||
tcp::Config::default(),
|
||||
noise::Config::new,
|
||||
yamux::Config::default,
|
||||
)?
|
||||
.with_behaviour(|key| Behaviour::new(key.public()))?
|
||||
.build();
|
||||
|
||||
let transport = tcp::tokio::Transport::default()
|
||||
.upgrade(Version::V1Lazy)
|
||||
.authenticate(noise::Config::new(&local_key)?)
|
||||
.multiplex(yamux::Config::default())
|
||||
.boxed();
|
||||
|
||||
let behaviour = Behaviour::new(local_key.public());
|
||||
|
||||
let mut swarm = SwarmBuilder::with_tokio_executor(transport, behaviour, local_peer_id).build();
|
||||
swarm.listen_on(
|
||||
Multiaddr::empty()
|
||||
.with(Protocol::Ip4(Ipv4Addr::UNSPECIFIED))
|
||||
|
@ -6,6 +6,7 @@ use libp2p::core::Multiaddr;
|
||||
use libp2p::identity::{Keypair, PeerId};
|
||||
use libp2p::ping;
|
||||
use libp2p::swarm::{keep_alive, NetworkBehaviour, SwarmBuilder, SwarmEvent};
|
||||
use libp2p::webrtc_websys;
|
||||
use std::convert::From;
|
||||
use std::io;
|
||||
use wasm_bindgen::prelude::*;
|
||||
@ -18,19 +19,16 @@ pub async fn run(libp2p_endpoint: String) -> Result<(), JsError> {
|
||||
let body = Body::from_current_window()?;
|
||||
body.append_p("Let's ping the WebRTC Server!")?;
|
||||
|
||||
let local_key = Keypair::generate_ed25519();
|
||||
let local_peer_id = PeerId::from(local_key.public());
|
||||
let mut swarm = SwarmBuilder::with_wasm_executor(
|
||||
libp2p_webrtc_websys::Transport::new(libp2p_webrtc_websys::Config::new(&local_key)).boxed(),
|
||||
Behaviour {
|
||||
let swarm = libp2p::SwarmBuilder::with_new_identity()
|
||||
.with_wasm_bindgen()
|
||||
.with_other_transport(|key| {
|
||||
webrtc_websys::Transport::new(webrtc_websys::Config::new(&key))
|
||||
})?
|
||||
.with_behaviour(|_| Behaviour {
|
||||
ping: ping::Behaviour::new(ping::Config::new()),
|
||||
keep_alive: keep_alive::Behaviour,
|
||||
},
|
||||
local_peer_id,
|
||||
)
|
||||
.build();
|
||||
|
||||
log::info!("Initialize swarm with identity: {local_peer_id}");
|
||||
})?
|
||||
.build();
|
||||
|
||||
let addr = libp2p_endpoint.parse::<Multiaddr>()?;
|
||||
log::info!("Dialing {addr}");
|
||||
|
@ -10,10 +10,9 @@ use futures::StreamExt;
|
||||
use libp2p::{
|
||||
core::muxing::StreamMuxerBox,
|
||||
core::Transport,
|
||||
identity,
|
||||
multiaddr::{Multiaddr, Protocol},
|
||||
ping,
|
||||
swarm::{SwarmBuilder, SwarmEvent},
|
||||
swarm::SwarmEvent,
|
||||
};
|
||||
use libp2p_webrtc as webrtc;
|
||||
use rand::thread_rng;
|
||||
@ -28,19 +27,22 @@ async fn main() -> anyhow::Result<()> {
|
||||
.parse_default_env()
|
||||
.init();
|
||||
|
||||
let id_keys = identity::Keypair::generate_ed25519();
|
||||
let local_peer_id = id_keys.public().to_peer_id();
|
||||
let transport = webrtc::tokio::Transport::new(
|
||||
id_keys,
|
||||
webrtc::tokio::Certificate::generate(&mut thread_rng())?,
|
||||
)
|
||||
.map(|(peer_id, conn), _| (peer_id, StreamMuxerBox::new(conn)))
|
||||
.boxed();
|
||||
|
||||
let mut swarm =
|
||||
SwarmBuilder::with_tokio_executor(transport, ping::Behaviour::default(), local_peer_id)
|
||||
.idle_connection_timeout(Duration::from_secs(30)) // Allows us to observe the pings.
|
||||
.build();
|
||||
let mut swarm = libp2p::SwarmBuilder::with_new_identity()
|
||||
.with_tokio()
|
||||
.with_other_transport(|id_keys| {
|
||||
Ok(webrtc::tokio::Transport::new(
|
||||
id_keys.clone(),
|
||||
webrtc::tokio::Certificate::generate(&mut thread_rng())?,
|
||||
)
|
||||
.map(|(peer_id, conn), _| (peer_id, StreamMuxerBox::new(conn))))
|
||||
})?
|
||||
.with_behaviour(|_| ping::Behaviour::default())?
|
||||
.with_swarm_config(|cfg| {
|
||||
cfg.with_idle_connection_timeout(
|
||||
Duration::from_secs(30), // Allows us to observe the pings.
|
||||
)
|
||||
})
|
||||
.build();
|
||||
|
||||
let address_webrtc = Multiaddr::from(Ipv4Addr::UNSPECIFIED)
|
||||
.with(Protocol::Udp(0))
|
||||
|
@ -20,14 +20,8 @@
|
||||
|
||||
#![doc = include_str!("../README.md")]
|
||||
|
||||
use futures::{future::Either, stream::StreamExt};
|
||||
use libp2p::{
|
||||
core::{muxing::StreamMuxerBox, transport::OrTransport, upgrade},
|
||||
gossipsub, identity, mdns, noise, quic,
|
||||
swarm::NetworkBehaviour,
|
||||
swarm::{SwarmBuilder, SwarmEvent},
|
||||
tcp, yamux, PeerId, Transport,
|
||||
};
|
||||
use futures::stream::StreamExt;
|
||||
use libp2p::{gossipsub, mdns, noise, swarm::NetworkBehaviour, swarm::SwarmEvent, tcp, yamux};
|
||||
use std::collections::hash_map::DefaultHasher;
|
||||
use std::error::Error;
|
||||
use std::hash::{Hash, Hasher};
|
||||
@ -43,58 +37,46 @@ struct MyBehaviour {
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn Error>> {
|
||||
// Create a random PeerId
|
||||
env_logger::init();
|
||||
let id_keys = identity::Keypair::generate_ed25519();
|
||||
let local_peer_id = PeerId::from(id_keys.public());
|
||||
let mut swarm = libp2p::SwarmBuilder::with_new_identity()
|
||||
.with_tokio()
|
||||
.with_tcp(
|
||||
tcp::Config::default(),
|
||||
noise::Config::new,
|
||||
yamux::Config::default,
|
||||
)?
|
||||
.with_quic()
|
||||
.with_behaviour(|key| {
|
||||
// To content-address message, we can take the hash of message and use it as an ID.
|
||||
let message_id_fn = |message: &gossipsub::Message| {
|
||||
let mut s = DefaultHasher::new();
|
||||
message.data.hash(&mut s);
|
||||
gossipsub::MessageId::from(s.finish().to_string())
|
||||
};
|
||||
|
||||
// Set up an encrypted DNS-enabled TCP Transport over the yamux protocol.
|
||||
let tcp_transport = tcp::tokio::Transport::new(tcp::Config::default().nodelay(true))
|
||||
.upgrade(upgrade::Version::V1Lazy)
|
||||
.authenticate(noise::Config::new(&id_keys).expect("signing libp2p-noise static keypair"))
|
||||
.multiplex(yamux::Config::default())
|
||||
.timeout(std::time::Duration::from_secs(20))
|
||||
.boxed();
|
||||
let quic_transport = quic::tokio::Transport::new(quic::Config::new(&id_keys));
|
||||
let transport = OrTransport::new(quic_transport, tcp_transport)
|
||||
.map(|either_output, _| match either_output {
|
||||
Either::Left((peer_id, muxer)) => (peer_id, StreamMuxerBox::new(muxer)),
|
||||
Either::Right((peer_id, muxer)) => (peer_id, StreamMuxerBox::new(muxer)),
|
||||
})
|
||||
.boxed();
|
||||
// Set a custom gossipsub configuration
|
||||
let gossipsub_config = gossipsub::ConfigBuilder::default()
|
||||
.heartbeat_interval(Duration::from_secs(10)) // This is set to aid debugging by not cluttering the log space
|
||||
.validation_mode(gossipsub::ValidationMode::Strict) // This sets the kind of message validation. The default is Strict (enforce message signing)
|
||||
.message_id_fn(message_id_fn) // content-address messages. No two messages of the same content will be propagated.
|
||||
.build()
|
||||
.map_err(|msg| io::Error::new(io::ErrorKind::Other, msg))?; // Temporary hack because `build` does not return a proper `std::error::Error`.
|
||||
|
||||
// To content-address message, we can take the hash of message and use it as an ID.
|
||||
let message_id_fn = |message: &gossipsub::Message| {
|
||||
let mut s = DefaultHasher::new();
|
||||
message.data.hash(&mut s);
|
||||
gossipsub::MessageId::from(s.finish().to_string())
|
||||
};
|
||||
// build a gossipsub network behaviour
|
||||
let gossipsub = gossipsub::Behaviour::new(
|
||||
gossipsub::MessageAuthenticity::Signed(key.clone()),
|
||||
gossipsub_config,
|
||||
)?;
|
||||
|
||||
// Set a custom gossipsub configuration
|
||||
let gossipsub_config = gossipsub::ConfigBuilder::default()
|
||||
.heartbeat_interval(Duration::from_secs(10)) // This is set to aid debugging by not cluttering the log space
|
||||
.validation_mode(gossipsub::ValidationMode::Strict) // This sets the kind of message validation. The default is Strict (enforce message signing)
|
||||
.message_id_fn(message_id_fn) // content-address messages. No two messages of the same content will be propagated.
|
||||
.build()
|
||||
.expect("Valid config");
|
||||
let mdns =
|
||||
mdns::tokio::Behaviour::new(mdns::Config::default(), key.public().to_peer_id())?;
|
||||
Ok(MyBehaviour { gossipsub, mdns })
|
||||
})?
|
||||
.build();
|
||||
|
||||
// build a gossipsub network behaviour
|
||||
let mut gossipsub = gossipsub::Behaviour::new(
|
||||
gossipsub::MessageAuthenticity::Signed(id_keys),
|
||||
gossipsub_config,
|
||||
)
|
||||
.expect("Correct configuration");
|
||||
// Create a Gossipsub topic
|
||||
let topic = gossipsub::IdentTopic::new("test-net");
|
||||
// subscribes to our topic
|
||||
gossipsub.subscribe(&topic)?;
|
||||
|
||||
// Create a Swarm to manage peers and events
|
||||
let mut swarm = {
|
||||
let mdns = mdns::tokio::Behaviour::new(mdns::Config::default(), local_peer_id)?;
|
||||
let behaviour = MyBehaviour { gossipsub, mdns };
|
||||
SwarmBuilder::with_tokio_executor(transport, behaviour, local_peer_id).build()
|
||||
};
|
||||
swarm.behaviour_mut().gossipsub.subscribe(&topic)?;
|
||||
|
||||
// Read full lines from stdin
|
||||
let mut stdin = io::BufReader::new(io::stdin()).lines();
|
||||
|
@ -10,8 +10,9 @@ clap = { version = "4.3.23", features = ["derive"] }
|
||||
env_logger = "0.10.0"
|
||||
futures = "0.3.28"
|
||||
futures-timer = "3.0"
|
||||
libp2p = { path = "../../libp2p", features = [ "async-std", "dns", "dcutr", "identify", "macros", "noise", "ping", "quic", "relay", "rendezvous", "tcp", "tokio", "yamux"] }
|
||||
libp2p = { path = "../../libp2p", features = [ "dns", "dcutr", "identify", "macros", "noise", "ping", "quic", "relay", "rendezvous", "tcp", "tokio", "yamux"] }
|
||||
log = "0.4"
|
||||
tokio = { version = "1.29", features = ["macros", "net", "rt", "signal"] }
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
@ -21,20 +21,11 @@
|
||||
#![doc = include_str!("../README.md")]
|
||||
|
||||
use clap::Parser;
|
||||
use futures::{
|
||||
executor::{block_on, ThreadPool},
|
||||
future::{Either, FutureExt},
|
||||
stream::StreamExt,
|
||||
};
|
||||
use futures::{executor::block_on, future::FutureExt, stream::StreamExt};
|
||||
use libp2p::{
|
||||
core::{
|
||||
multiaddr::{Multiaddr, Protocol},
|
||||
muxing::StreamMuxerBox,
|
||||
transport::Transport,
|
||||
upgrade,
|
||||
},
|
||||
dcutr, dns, identify, identity, noise, ping, quic, relay,
|
||||
swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent},
|
||||
core::multiaddr::{Multiaddr, Protocol},
|
||||
dcutr, identify, identity, noise, ping, relay,
|
||||
swarm::{NetworkBehaviour, SwarmEvent},
|
||||
tcp, yamux, PeerId,
|
||||
};
|
||||
use log::info;
|
||||
@ -78,37 +69,12 @@ impl FromStr for Mode {
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn Error>> {
|
||||
env_logger::init();
|
||||
|
||||
let opts = Opts::parse();
|
||||
|
||||
let local_key = generate_ed25519(opts.secret_key_seed);
|
||||
let local_peer_id = PeerId::from(local_key.public());
|
||||
|
||||
let (relay_transport, client) = relay::client::new(local_peer_id);
|
||||
|
||||
let transport = {
|
||||
let relay_tcp_quic_transport = relay_transport
|
||||
.or_transport(tcp::async_io::Transport::new(
|
||||
tcp::Config::default().port_reuse(true),
|
||||
))
|
||||
.upgrade(upgrade::Version::V1)
|
||||
.authenticate(noise::Config::new(&local_key).unwrap())
|
||||
.multiplex(yamux::Config::default())
|
||||
.or_transport(quic::async_std::Transport::new(quic::Config::new(
|
||||
&local_key,
|
||||
)));
|
||||
|
||||
block_on(dns::async_std::Transport::system(relay_tcp_quic_transport))
|
||||
.unwrap()
|
||||
.map(|either_output, _| match either_output {
|
||||
Either::Left((peer_id, muxer)) => (peer_id, StreamMuxerBox::new(muxer)),
|
||||
Either::Right((peer_id, muxer)) => (peer_id, StreamMuxerBox::new(muxer)),
|
||||
})
|
||||
.boxed()
|
||||
};
|
||||
|
||||
#[derive(NetworkBehaviour)]
|
||||
struct Behaviour {
|
||||
relay_client: relay::client::Behaviour,
|
||||
@ -117,21 +83,27 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
dcutr: dcutr::Behaviour,
|
||||
}
|
||||
|
||||
let behaviour = Behaviour {
|
||||
relay_client: client,
|
||||
ping: ping::Behaviour::new(ping::Config::new()),
|
||||
identify: identify::Behaviour::new(identify::Config::new(
|
||||
"/TODO/0.0.1".to_string(),
|
||||
local_key.public(),
|
||||
)),
|
||||
dcutr: dcutr::Behaviour::new(local_peer_id),
|
||||
};
|
||||
|
||||
let mut swarm = match ThreadPool::new() {
|
||||
Ok(tp) => SwarmBuilder::with_executor(transport, behaviour, local_peer_id, tp),
|
||||
Err(_) => SwarmBuilder::without_executor(transport, behaviour, local_peer_id),
|
||||
}
|
||||
.build();
|
||||
let mut swarm =
|
||||
libp2p::SwarmBuilder::with_existing_identity(generate_ed25519(opts.secret_key_seed))
|
||||
.with_tokio()
|
||||
.with_tcp(
|
||||
tcp::Config::default().port_reuse(true).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()),
|
||||
})?
|
||||
.build();
|
||||
|
||||
swarm
|
||||
.listen_on("/ip4/0.0.0.0/udp/0/quic-v1".parse().unwrap())
|
||||
|
@ -10,7 +10,7 @@ async-std = { version = "1.12", features = ["attributes"] }
|
||||
async-trait = "0.1"
|
||||
env_logger = "0.10"
|
||||
futures = "0.3.28"
|
||||
libp2p = { path = "../../libp2p", features = [ "async-std", "dns", "kad", "mdns", "noise", "macros", "tcp", "websocket", "yamux"] }
|
||||
libp2p = { path = "../../libp2p", features = [ "async-std", "dns", "kad", "mdns", "noise", "macros", "tcp", "yamux"] }
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
@ -22,14 +22,13 @@
|
||||
|
||||
use async_std::io;
|
||||
use futures::{prelude::*, select};
|
||||
use libp2p::core::upgrade::Version;
|
||||
use libp2p::kad;
|
||||
use libp2p::kad::record::store::MemoryStore;
|
||||
use libp2p::kad::Mode;
|
||||
use libp2p::{
|
||||
identity, mdns, noise,
|
||||
swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent},
|
||||
tcp, yamux, PeerId, Transport,
|
||||
mdns, noise,
|
||||
swarm::{NetworkBehaviour, SwarmEvent},
|
||||
tcp, yamux,
|
||||
};
|
||||
use std::error::Error;
|
||||
|
||||
@ -37,16 +36,6 @@ use std::error::Error;
|
||||
async fn main() -> Result<(), Box<dyn Error>> {
|
||||
env_logger::init();
|
||||
|
||||
// Create a random key for ourselves.
|
||||
let local_key = identity::Keypair::generate_ed25519();
|
||||
let local_peer_id = PeerId::from(local_key.public());
|
||||
|
||||
let transport = tcp::async_io::Transport::default()
|
||||
.upgrade(Version::V1Lazy)
|
||||
.authenticate(noise::Config::new(&local_key)?)
|
||||
.multiplex(yamux::Config::default())
|
||||
.boxed();
|
||||
|
||||
// We create a custom network behaviour that combines Kademlia and mDNS.
|
||||
#[derive(NetworkBehaviour)]
|
||||
struct Behaviour {
|
||||
@ -54,15 +43,26 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
mdns: mdns::async_io::Behaviour,
|
||||
}
|
||||
|
||||
// Create a swarm to manage peers and events.
|
||||
let mut swarm = {
|
||||
// Create a Kademlia behaviour.
|
||||
let store = MemoryStore::new(local_peer_id);
|
||||
let kademlia = kad::Behaviour::new(local_peer_id, store);
|
||||
let mdns = mdns::async_io::Behaviour::new(mdns::Config::default(), local_peer_id)?;
|
||||
let behaviour = Behaviour { kademlia, mdns };
|
||||
SwarmBuilder::with_async_std_executor(transport, behaviour, local_peer_id).build()
|
||||
};
|
||||
let mut swarm = libp2p::SwarmBuilder::with_new_identity()
|
||||
.with_async_std()
|
||||
.with_tcp(
|
||||
tcp::Config::default(),
|
||||
noise::Config::new,
|
||||
yamux::Config::default,
|
||||
)?
|
||||
.with_behaviour(|key| {
|
||||
Ok(Behaviour {
|
||||
kademlia: kad::Behaviour::new(
|
||||
key.public().to_peer_id(),
|
||||
MemoryStore::new(key.public().to_peer_id()),
|
||||
),
|
||||
mdns: mdns::async_io::Behaviour::new(
|
||||
mdns::Config::default(),
|
||||
key.public().to_peer_id(),
|
||||
)?,
|
||||
})
|
||||
})?
|
||||
.build();
|
||||
|
||||
swarm.behaviour_mut().kademlia.set_mode(Some(Mode::Server));
|
||||
|
||||
|
@ -9,11 +9,10 @@ use libp2p::{
|
||||
multiaddr::Protocol,
|
||||
noise,
|
||||
request_response::{self, ProtocolSupport, RequestId, ResponseChannel},
|
||||
swarm::{NetworkBehaviour, Swarm, SwarmBuilder, SwarmEvent},
|
||||
tcp, yamux, PeerId, Transport,
|
||||
swarm::{NetworkBehaviour, Swarm, SwarmEvent},
|
||||
tcp, yamux, PeerId,
|
||||
};
|
||||
|
||||
use libp2p::core::upgrade::Version;
|
||||
use libp2p::StreamProtocol;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::{hash_map, HashMap, HashSet};
|
||||
@ -41,18 +40,18 @@ pub(crate) async fn new(
|
||||
};
|
||||
let peer_id = id_keys.public().to_peer_id();
|
||||
|
||||
let transport = tcp::async_io::Transport::default()
|
||||
.upgrade(Version::V1Lazy)
|
||||
.authenticate(noise::Config::new(&id_keys)?)
|
||||
.multiplex(yamux::Config::default())
|
||||
.boxed();
|
||||
|
||||
// Build the Swarm, connecting the lower layer transport logic with the
|
||||
// higher layer network behaviour logic.
|
||||
let mut swarm = SwarmBuilder::with_async_std_executor(
|
||||
transport,
|
||||
Behaviour {
|
||||
kademlia: kad::Behaviour::new(peer_id, kad::record::store::MemoryStore::new(peer_id)),
|
||||
let mut swarm = libp2p::SwarmBuilder::with_existing_identity(id_keys)
|
||||
.with_async_std()
|
||||
.with_tcp(
|
||||
tcp::Config::default(),
|
||||
noise::Config::new,
|
||||
yamux::Config::default,
|
||||
)?
|
||||
.with_behaviour(|key| Behaviour {
|
||||
kademlia: kad::Behaviour::new(
|
||||
peer_id,
|
||||
kad::store::MemoryStore::new(key.public().to_peer_id()),
|
||||
),
|
||||
request_response: request_response::cbor::Behaviour::new(
|
||||
[(
|
||||
StreamProtocol::new("/file-exchange/1"),
|
||||
@ -60,10 +59,8 @@ pub(crate) async fn new(
|
||||
)],
|
||||
request_response::Config::default(),
|
||||
),
|
||||
},
|
||||
peer_id,
|
||||
)
|
||||
.build();
|
||||
})?
|
||||
.build();
|
||||
|
||||
swarm
|
||||
.behaviour_mut()
|
||||
|
@ -20,35 +20,28 @@
|
||||
|
||||
#![doc = include_str!("../README.md")]
|
||||
|
||||
use futures::prelude::*;
|
||||
use libp2p::{
|
||||
core::{multiaddr::Multiaddr, upgrade::Version},
|
||||
identify, identity, noise,
|
||||
swarm::{SwarmBuilder, SwarmEvent},
|
||||
tcp, yamux, PeerId, Transport,
|
||||
};
|
||||
use futures::StreamExt;
|
||||
use libp2p::{core::multiaddr::Multiaddr, identify, noise, swarm::SwarmEvent, tcp, yamux};
|
||||
use std::error::Error;
|
||||
|
||||
#[async_std::main]
|
||||
async fn main() -> Result<(), Box<dyn Error>> {
|
||||
env_logger::init();
|
||||
let local_key = identity::Keypair::generate_ed25519();
|
||||
let local_peer_id = PeerId::from(local_key.public());
|
||||
|
||||
let transport = tcp::async_io::Transport::default()
|
||||
.upgrade(Version::V1Lazy)
|
||||
.authenticate(noise::Config::new(&local_key).unwrap())
|
||||
.multiplex(yamux::Config::default())
|
||||
.boxed();
|
||||
|
||||
// Create a identify network behaviour.
|
||||
let behaviour = identify::Behaviour::new(identify::Config::new(
|
||||
"/ipfs/id/1.0.0".to_string(),
|
||||
local_key.public(),
|
||||
));
|
||||
|
||||
let mut swarm =
|
||||
SwarmBuilder::with_async_std_executor(transport, behaviour, local_peer_id).build();
|
||||
let mut swarm = libp2p::SwarmBuilder::with_new_identity()
|
||||
.with_async_std()
|
||||
.with_tcp(
|
||||
tcp::Config::default(),
|
||||
noise::Config::new,
|
||||
yamux::Config::default,
|
||||
)?
|
||||
.with_behaviour(|key| {
|
||||
identify::Behaviour::new(identify::Config::new(
|
||||
"/ipfs/id/1.0.0".to_string(),
|
||||
key.public(),
|
||||
))
|
||||
})?
|
||||
.build();
|
||||
|
||||
// Tell the swarm to listen on all interfaces and a random, OS-assigned
|
||||
// port.
|
||||
|
@ -12,7 +12,7 @@ clap = { version = "4.3.23", features = ["derive"] }
|
||||
env_logger = "0.10"
|
||||
futures = "0.3.28"
|
||||
anyhow = "1.0.75"
|
||||
libp2p = { path = "../../libp2p", features = [ "tokio", "dns", "kad", "noise", "tcp", "websocket", "yamux", "rsa"] }
|
||||
libp2p = { path = "../../libp2p", features = [ "tokio", "dns", "kad", "noise", "tcp", "yamux", "rsa"] }
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
@ -27,12 +27,7 @@ use std::time::{Duration, Instant};
|
||||
use anyhow::{bail, Result};
|
||||
use clap::Parser;
|
||||
use futures::StreamExt;
|
||||
use libp2p::{
|
||||
bytes::BufMut,
|
||||
identity, kad,
|
||||
swarm::{SwarmBuilder, SwarmEvent},
|
||||
tokio_development_transport, PeerId,
|
||||
};
|
||||
use libp2p::{bytes::BufMut, identity, kad, noise, swarm::SwarmEvent, tcp, yamux, PeerId};
|
||||
|
||||
const BOOTNODES: [&str; 4] = [
|
||||
"QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
|
||||
@ -47,28 +42,32 @@ async fn main() -> Result<()> {
|
||||
|
||||
// Create a random key for ourselves.
|
||||
let local_key = identity::Keypair::generate_ed25519();
|
||||
let local_peer_id = PeerId::from(local_key.public());
|
||||
|
||||
// Set up a an encrypted DNS-enabled TCP Transport over the yamux protocol
|
||||
let transport = tokio_development_transport(local_key.clone())?;
|
||||
let mut swarm = libp2p::SwarmBuilder::with_existing_identity(local_key.clone())
|
||||
.with_tokio()
|
||||
.with_tcp(
|
||||
tcp::Config::default(),
|
||||
noise::Config::new,
|
||||
yamux::Config::default,
|
||||
)?
|
||||
.with_dns()?
|
||||
.with_behaviour(|key| {
|
||||
// Create a Kademlia behaviour.
|
||||
let mut cfg = kad::Config::default();
|
||||
cfg.set_query_timeout(Duration::from_secs(5 * 60));
|
||||
let store = kad::store::MemoryStore::new(key.public().to_peer_id());
|
||||
kad::Behaviour::with_config(key.public().to_peer_id(), store, cfg)
|
||||
})?
|
||||
.build();
|
||||
|
||||
// Create a swarm to manage peers and events.
|
||||
let mut swarm = {
|
||||
// Create a Kademlia behaviour.
|
||||
let mut cfg = kad::Config::default();
|
||||
cfg.set_query_timeout(Duration::from_secs(5 * 60));
|
||||
let store = kad::store::MemoryStore::new(local_peer_id);
|
||||
let mut behaviour = kad::Behaviour::with_config(local_peer_id, store, cfg);
|
||||
|
||||
// Add the bootnodes to the local routing table. `libp2p-dns` built
|
||||
// into the `transport` resolves the `dnsaddr` when Kademlia tries
|
||||
// to dial these nodes.
|
||||
for peer in &BOOTNODES {
|
||||
behaviour.add_address(&peer.parse()?, "/dnsaddr/bootstrap.libp2p.io".parse()?);
|
||||
}
|
||||
|
||||
SwarmBuilder::with_tokio_executor(transport, behaviour, local_peer_id).build()
|
||||
};
|
||||
// Add the bootnodes to the local routing table. `libp2p-dns` built
|
||||
// into the `transport` resolves the `dnsaddr` when Kademlia tries
|
||||
// to dial these nodes.
|
||||
for peer in &BOOTNODES {
|
||||
swarm
|
||||
.behaviour_mut()
|
||||
.add_address(&peer.parse()?, "/dnsaddr/bootstrap.libp2p.io".parse()?);
|
||||
}
|
||||
|
||||
let cli_opt = Opt::parse();
|
||||
|
||||
@ -83,11 +82,11 @@ async fn main() -> Result<()> {
|
||||
|
||||
let mut pk_record_key = vec![];
|
||||
pk_record_key.put_slice("/pk/".as_bytes());
|
||||
pk_record_key.put_slice(local_peer_id.to_bytes().as_slice());
|
||||
pk_record_key.put_slice(swarm.local_peer_id().to_bytes().as_slice());
|
||||
|
||||
let mut pk_record =
|
||||
kad::Record::new(pk_record_key, local_key.public().encode_protobuf());
|
||||
pk_record.publisher = Some(local_peer_id);
|
||||
pk_record.publisher = Some(*swarm.local_peer_id());
|
||||
pk_record.expires = Some(Instant::now().add(Duration::from_secs(60)));
|
||||
|
||||
swarm
|
||||
|
@ -23,40 +23,17 @@
|
||||
use either::Either;
|
||||
use futures::prelude::*;
|
||||
use libp2p::{
|
||||
core::{muxing::StreamMuxerBox, transport, transport::upgrade::Version},
|
||||
gossipsub, identify, identity,
|
||||
core::transport::upgrade::Version,
|
||||
gossipsub, identify,
|
||||
multiaddr::Protocol,
|
||||
noise, ping,
|
||||
pnet::{PnetConfig, PreSharedKey},
|
||||
swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent},
|
||||
tcp, yamux, Multiaddr, PeerId, Transport,
|
||||
swarm::{NetworkBehaviour, SwarmEvent},
|
||||
tcp, yamux, Multiaddr, Transport,
|
||||
};
|
||||
use std::{env, error::Error, fs, path::Path, str::FromStr, time::Duration};
|
||||
use std::{env, error::Error, fs, path::Path, str::FromStr};
|
||||
use tokio::{io, io::AsyncBufReadExt, select};
|
||||
|
||||
/// Builds the transport that serves as a common ground for all connections.
|
||||
pub fn build_transport(
|
||||
key_pair: identity::Keypair,
|
||||
psk: Option<PreSharedKey>,
|
||||
) -> transport::Boxed<(PeerId, StreamMuxerBox)> {
|
||||
let noise_config = noise::Config::new(&key_pair).unwrap();
|
||||
let yamux_config = yamux::Config::default();
|
||||
|
||||
let base_transport = tcp::tokio::Transport::new(tcp::Config::default().nodelay(true));
|
||||
let maybe_encrypted = match psk {
|
||||
Some(psk) => Either::Left(
|
||||
base_transport.and_then(move |socket, _| PnetConfig::new(psk).handshake(socket)),
|
||||
),
|
||||
None => Either::Right(base_transport),
|
||||
};
|
||||
maybe_encrypted
|
||||
.upgrade(Version::V1Lazy)
|
||||
.authenticate(noise_config)
|
||||
.multiplex(yamux_config)
|
||||
.timeout(Duration::from_secs(20))
|
||||
.boxed()
|
||||
}
|
||||
|
||||
/// Get the current ipfs repo path, either from the IPFS_PATH environment variable or
|
||||
/// from the default $HOME/.ipfs
|
||||
fn get_ipfs_path() -> Box<Path> {
|
||||
@ -118,76 +95,67 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
.map(|text| PreSharedKey::from_str(&text))
|
||||
.transpose()?;
|
||||
|
||||
// Create a random PeerId
|
||||
let local_key = identity::Keypair::generate_ed25519();
|
||||
let local_peer_id = PeerId::from(local_key.public());
|
||||
println!("using random peer id: {local_peer_id:?}");
|
||||
if let Some(psk) = psk {
|
||||
println!("using swarm key with fingerprint: {}", psk.fingerprint());
|
||||
}
|
||||
|
||||
// Set up a an encrypted DNS-enabled TCP Transport over and Yamux protocol
|
||||
let transport = build_transport(local_key.clone(), psk);
|
||||
|
||||
// Create a Gosspipsub topic
|
||||
let gossipsub_topic = gossipsub::IdentTopic::new("chat");
|
||||
|
||||
// We create a custom network behaviour that combines gossipsub, ping and identify.
|
||||
#[derive(NetworkBehaviour)]
|
||||
#[behaviour(to_swarm = "MyBehaviourEvent")]
|
||||
struct MyBehaviour {
|
||||
gossipsub: gossipsub::Behaviour,
|
||||
identify: identify::Behaviour,
|
||||
ping: ping::Behaviour,
|
||||
}
|
||||
|
||||
enum MyBehaviourEvent {
|
||||
Gossipsub(gossipsub::Event),
|
||||
Identify(identify::Event),
|
||||
Ping(ping::Event),
|
||||
}
|
||||
let mut swarm = libp2p::SwarmBuilder::with_new_identity()
|
||||
.with_tokio()
|
||||
.with_other_transport(|key| {
|
||||
let noise_config = noise::Config::new(key).unwrap();
|
||||
let yamux_config = yamux::Config::default();
|
||||
|
||||
impl From<gossipsub::Event> for MyBehaviourEvent {
|
||||
fn from(event: gossipsub::Event) -> Self {
|
||||
MyBehaviourEvent::Gossipsub(event)
|
||||
}
|
||||
}
|
||||
let base_transport = tcp::tokio::Transport::new(tcp::Config::default().nodelay(true));
|
||||
let maybe_encrypted = match psk {
|
||||
Some(psk) => Either::Left(
|
||||
base_transport
|
||||
.and_then(move |socket, _| PnetConfig::new(psk).handshake(socket)),
|
||||
),
|
||||
None => Either::Right(base_transport),
|
||||
};
|
||||
maybe_encrypted
|
||||
.upgrade(Version::V1Lazy)
|
||||
.authenticate(noise_config)
|
||||
.multiplex(yamux_config)
|
||||
})?
|
||||
.with_dns()?
|
||||
.with_behaviour(|key| {
|
||||
let gossipsub_config = gossipsub::ConfigBuilder::default()
|
||||
.max_transmit_size(262144)
|
||||
.build()
|
||||
.map_err(|msg| io::Error::new(io::ErrorKind::Other, msg))?; // Temporary hack because `build` does not return a proper `std::error::Error`.
|
||||
Ok(MyBehaviour {
|
||||
gossipsub: gossipsub::Behaviour::new(
|
||||
gossipsub::MessageAuthenticity::Signed(key.clone()),
|
||||
gossipsub_config,
|
||||
)
|
||||
.expect("Valid configuration"),
|
||||
identify: identify::Behaviour::new(identify::Config::new(
|
||||
"/ipfs/0.1.0".into(),
|
||||
key.public(),
|
||||
)),
|
||||
ping: ping::Behaviour::new(ping::Config::new()),
|
||||
})
|
||||
})?
|
||||
.build();
|
||||
|
||||
impl From<identify::Event> for MyBehaviourEvent {
|
||||
fn from(event: identify::Event) -> Self {
|
||||
MyBehaviourEvent::Identify(event)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ping::Event> for MyBehaviourEvent {
|
||||
fn from(event: ping::Event) -> Self {
|
||||
MyBehaviourEvent::Ping(event)
|
||||
}
|
||||
}
|
||||
|
||||
// Create a Swarm to manage peers and events
|
||||
let mut swarm = {
|
||||
let gossipsub_config = gossipsub::ConfigBuilder::default()
|
||||
.max_transmit_size(262144)
|
||||
.build()
|
||||
.expect("valid config");
|
||||
let mut behaviour = MyBehaviour {
|
||||
gossipsub: gossipsub::Behaviour::new(
|
||||
gossipsub::MessageAuthenticity::Signed(local_key.clone()),
|
||||
gossipsub_config,
|
||||
)
|
||||
.expect("Valid configuration"),
|
||||
identify: identify::Behaviour::new(identify::Config::new(
|
||||
"/ipfs/0.1.0".into(),
|
||||
local_key.public(),
|
||||
)),
|
||||
ping: ping::Behaviour::new(ping::Config::new()),
|
||||
};
|
||||
|
||||
println!("Subscribing to {gossipsub_topic:?}");
|
||||
behaviour.gossipsub.subscribe(&gossipsub_topic).unwrap();
|
||||
SwarmBuilder::with_tokio_executor(transport, behaviour, local_peer_id).build()
|
||||
};
|
||||
println!("Subscribing to {gossipsub_topic:?}");
|
||||
swarm
|
||||
.behaviour_mut()
|
||||
.gossipsub
|
||||
.subscribe(&gossipsub_topic)
|
||||
.unwrap();
|
||||
|
||||
// Reach out to other nodes if specified
|
||||
for to_dial in std::env::args().skip(1) {
|
||||
|
@ -21,12 +21,10 @@
|
||||
#![doc = include_str!("../README.md")]
|
||||
|
||||
use env_logger::Env;
|
||||
use futures::executor::block_on;
|
||||
use futures::stream::StreamExt;
|
||||
use libp2p::core::{upgrade::Version, Multiaddr, Transport};
|
||||
use libp2p::identity::PeerId;
|
||||
use futures::{executor::block_on, StreamExt};
|
||||
use libp2p::core::Multiaddr;
|
||||
use libp2p::metrics::{Metrics, Recorder};
|
||||
use libp2p::swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent};
|
||||
use libp2p::swarm::{NetworkBehaviour, SwarmEvent};
|
||||
use libp2p::{identify, identity, noise, ping, tcp, yamux};
|
||||
use log::info;
|
||||
use prometheus_client::registry::Registry;
|
||||
@ -39,21 +37,16 @@ mod http_service;
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
|
||||
|
||||
let local_key = identity::Keypair::generate_ed25519();
|
||||
let local_peer_id = PeerId::from(local_key.public());
|
||||
let local_pub_key = local_key.public();
|
||||
|
||||
let mut swarm = SwarmBuilder::without_executor(
|
||||
tcp::async_io::Transport::default()
|
||||
.upgrade(Version::V1Lazy)
|
||||
.authenticate(noise::Config::new(&local_key)?)
|
||||
.multiplex(yamux::Config::default())
|
||||
.boxed(),
|
||||
Behaviour::new(local_pub_key),
|
||||
local_peer_id,
|
||||
)
|
||||
.idle_connection_timeout(Duration::from_secs(60))
|
||||
.build();
|
||||
let mut swarm = libp2p::SwarmBuilder::with_new_identity()
|
||||
.with_async_std()
|
||||
.with_tcp(
|
||||
tcp::Config::default(),
|
||||
noise::Config::new,
|
||||
yamux::Config::default,
|
||||
)?
|
||||
.with_behaviour(|key| Behaviour::new(key.public()))?
|
||||
.with_swarm_config(|cfg| cfg.with_idle_connection_timeout(Duration::from_secs(u64::MAX)))
|
||||
.build();
|
||||
|
||||
swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?;
|
||||
|
||||
|
@ -21,31 +21,21 @@
|
||||
#![doc = include_str!("../README.md")]
|
||||
|
||||
use futures::prelude::*;
|
||||
use libp2p::core::upgrade::Version;
|
||||
use libp2p::{
|
||||
identity, noise, ping,
|
||||
swarm::{SwarmBuilder, SwarmEvent},
|
||||
tcp, yamux, Multiaddr, PeerId, Transport,
|
||||
};
|
||||
use std::error::Error;
|
||||
use std::time::Duration;
|
||||
use libp2p::{noise, ping, swarm::SwarmEvent, tcp, yamux, Multiaddr};
|
||||
use std::{error::Error, time::Duration};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn Error>> {
|
||||
env_logger::init();
|
||||
let local_key = identity::Keypair::generate_ed25519();
|
||||
let local_peer_id = PeerId::from(local_key.public());
|
||||
|
||||
let transport = tcp::tokio::Transport::default()
|
||||
.upgrade(Version::V1Lazy)
|
||||
.authenticate(noise::Config::new(&local_key)?)
|
||||
.multiplex(yamux::Config::default())
|
||||
.boxed();
|
||||
|
||||
let mut swarm =
|
||||
SwarmBuilder::with_tokio_executor(transport, ping::Behaviour::default(), local_peer_id)
|
||||
.idle_connection_timeout(Duration::from_secs(60)) // For illustrative purposes, keep idle connections alive for a minute so we can observe a few pings.
|
||||
.build();
|
||||
let mut swarm = libp2p::SwarmBuilder::with_new_identity()
|
||||
.with_tokio()
|
||||
.with_tcp(
|
||||
tcp::Config::default(),
|
||||
noise::Config::new,
|
||||
yamux::Config::default,
|
||||
)?
|
||||
.with_behaviour(|_| ping::Behaviour::default())?
|
||||
.with_swarm_config(|cfg| cfg.with_idle_connection_timeout(Duration::from_secs(u64::MAX)))
|
||||
.build();
|
||||
|
||||
// Tell the swarm to listen on all interfaces and a random, OS-assigned
|
||||
// port.
|
||||
|
@ -22,18 +22,14 @@
|
||||
#![doc = include_str!("../README.md")]
|
||||
|
||||
use clap::Parser;
|
||||
use futures::executor::block_on;
|
||||
use futures::stream::StreamExt;
|
||||
use futures::{executor::block_on, future::Either};
|
||||
use libp2p::{
|
||||
core::multiaddr::Protocol,
|
||||
core::muxing::StreamMuxerBox,
|
||||
core::upgrade,
|
||||
core::{Multiaddr, Transport},
|
||||
identify, identity,
|
||||
identity::PeerId,
|
||||
noise, ping, quic, relay,
|
||||
swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent},
|
||||
tcp,
|
||||
core::Multiaddr,
|
||||
identify, identity, noise, ping, relay,
|
||||
swarm::{NetworkBehaviour, SwarmEvent},
|
||||
tcp, yamux,
|
||||
};
|
||||
use std::error::Error;
|
||||
use std::net::{Ipv4Addr, Ipv6Addr};
|
||||
@ -42,41 +38,27 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
env_logger::init();
|
||||
|
||||
let opt = Opt::parse();
|
||||
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());
|
||||
|
||||
let tcp_transport = tcp::async_io::Transport::default();
|
||||
|
||||
let tcp_transport = tcp_transport
|
||||
.upgrade(upgrade::Version::V1Lazy)
|
||||
.authenticate(
|
||||
noise::Config::new(&local_key).expect("Signing libp2p-noise static DH keypair failed."),
|
||||
)
|
||||
.multiplex(libp2p::yamux::Config::default());
|
||||
|
||||
let quic_transport = quic::async_std::Transport::new(quic::Config::new(&local_key));
|
||||
|
||||
let transport = quic_transport
|
||||
.or_transport(tcp_transport)
|
||||
.map(|either_output, _| match either_output {
|
||||
Either::Left((peer_id, muxer)) => (peer_id, StreamMuxerBox::new(muxer)),
|
||||
Either::Right((peer_id, muxer)) => (peer_id, StreamMuxerBox::new(muxer)),
|
||||
})
|
||||
.boxed();
|
||||
|
||||
let behaviour = Behaviour {
|
||||
relay: relay::Behaviour::new(local_peer_id, Default::default()),
|
||||
ping: ping::Behaviour::new(ping::Config::new()),
|
||||
identify: identify::Behaviour::new(identify::Config::new(
|
||||
"/TODO/0.0.1".to_string(),
|
||||
local_key.public(),
|
||||
)),
|
||||
};
|
||||
|
||||
let mut swarm = SwarmBuilder::without_executor(transport, behaviour, local_peer_id).build();
|
||||
let mut swarm = libp2p::SwarmBuilder::with_existing_identity(local_key)
|
||||
.with_async_std()
|
||||
.with_tcp(
|
||||
tcp::Config::default(),
|
||||
noise::Config::new,
|
||||
yamux::Config::default,
|
||||
)?
|
||||
.with_quic()
|
||||
.with_behaviour(|key| Behaviour {
|
||||
relay: relay::Behaviour::new(key.public().to_peer_id(), Default::default()),
|
||||
ping: ping::Behaviour::new(ping::Config::new()),
|
||||
identify: identify::Behaviour::new(identify::Config::new(
|
||||
"/TODO/0.0.1".to_string(),
|
||||
key.public(),
|
||||
)),
|
||||
})?
|
||||
.build();
|
||||
|
||||
// Listen on all interfaces
|
||||
let listen_addr_tcp = Multiaddr::empty()
|
||||
|
@ -20,41 +20,38 @@
|
||||
|
||||
use futures::StreamExt;
|
||||
use libp2p::{
|
||||
core::transport::upgrade::Version,
|
||||
identity,
|
||||
multiaddr::Protocol,
|
||||
noise, ping, rendezvous,
|
||||
swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent},
|
||||
tcp, yamux, Multiaddr, PeerId, Transport,
|
||||
swarm::{NetworkBehaviour, SwarmEvent},
|
||||
tcp, yamux, Multiaddr,
|
||||
};
|
||||
use std::error::Error;
|
||||
use std::time::Duration;
|
||||
|
||||
const NAMESPACE: &str = "rendezvous";
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
async fn main() -> Result<(), Box<dyn Error>> {
|
||||
env_logger::init();
|
||||
|
||||
let key_pair = identity::Keypair::generate_ed25519();
|
||||
let rendezvous_point_address = "/ip4/127.0.0.1/tcp/62649".parse::<Multiaddr>().unwrap();
|
||||
let rendezvous_point = "12D3KooWDpJ7As7BWAwRMfu1VU2WCqNjvq387JEYKDBj4kx6nXTN"
|
||||
.parse()
|
||||
.unwrap();
|
||||
|
||||
let mut swarm = SwarmBuilder::with_tokio_executor(
|
||||
tcp::tokio::Transport::default()
|
||||
.upgrade(Version::V1Lazy)
|
||||
.authenticate(noise::Config::new(&key_pair).unwrap())
|
||||
.multiplex(yamux::Config::default())
|
||||
.boxed(),
|
||||
MyBehaviour {
|
||||
rendezvous: rendezvous::client::Behaviour::new(key_pair.clone()),
|
||||
let mut swarm = libp2p::SwarmBuilder::with_new_identity()
|
||||
.with_tokio()
|
||||
.with_tcp(
|
||||
tcp::Config::default(),
|
||||
noise::Config::new,
|
||||
yamux::Config::default,
|
||||
)?
|
||||
.with_behaviour(|key| MyBehaviour {
|
||||
rendezvous: rendezvous::client::Behaviour::new(key.clone()),
|
||||
ping: ping::Behaviour::new(ping::Config::new().with_interval(Duration::from_secs(1))),
|
||||
},
|
||||
PeerId::from(key_pair.public()),
|
||||
)
|
||||
.idle_connection_timeout(Duration::from_secs(5))
|
||||
.build();
|
||||
})?
|
||||
.with_swarm_config(|cfg| cfg.with_idle_connection_timeout(Duration::from_secs(5)))
|
||||
.build();
|
||||
|
||||
swarm.dial(rendezvous_point_address.clone()).unwrap();
|
||||
|
||||
|
@ -20,10 +20,9 @@
|
||||
|
||||
use futures::StreamExt;
|
||||
use libp2p::{
|
||||
core::transport::upgrade::Version,
|
||||
identify, identity, noise, ping, rendezvous,
|
||||
swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent},
|
||||
tcp, yamux, Multiaddr, PeerId, Transport,
|
||||
identify, noise, ping, rendezvous,
|
||||
swarm::{NetworkBehaviour, SwarmEvent},
|
||||
tcp, yamux, Multiaddr,
|
||||
};
|
||||
use std::time::Duration;
|
||||
|
||||
@ -31,30 +30,30 @@ use std::time::Duration;
|
||||
async fn main() {
|
||||
env_logger::init();
|
||||
|
||||
let key_pair = identity::Keypair::generate_ed25519();
|
||||
let rendezvous_point_address = "/ip4/127.0.0.1/tcp/62649".parse::<Multiaddr>().unwrap();
|
||||
let rendezvous_point = "12D3KooWDpJ7As7BWAwRMfu1VU2WCqNjvq387JEYKDBj4kx6nXTN"
|
||||
.parse()
|
||||
.unwrap();
|
||||
|
||||
let mut swarm = SwarmBuilder::with_tokio_executor(
|
||||
tcp::tokio::Transport::default()
|
||||
.upgrade(Version::V1Lazy)
|
||||
.authenticate(noise::Config::new(&key_pair).unwrap())
|
||||
.multiplex(yamux::Config::default())
|
||||
.boxed(),
|
||||
MyBehaviour {
|
||||
let mut swarm = libp2p::SwarmBuilder::with_new_identity()
|
||||
.with_tokio()
|
||||
.with_tcp(
|
||||
tcp::Config::default(),
|
||||
noise::Config::new,
|
||||
yamux::Config::default,
|
||||
)
|
||||
.unwrap()
|
||||
.with_behaviour(|key| MyBehaviour {
|
||||
identify: identify::Behaviour::new(identify::Config::new(
|
||||
"rendezvous-example/1.0.0".to_string(),
|
||||
key_pair.public(),
|
||||
key.public(),
|
||||
)),
|
||||
rendezvous: rendezvous::client::Behaviour::new(key_pair.clone()),
|
||||
rendezvous: rendezvous::client::Behaviour::new(key.clone()),
|
||||
ping: ping::Behaviour::new(ping::Config::new().with_interval(Duration::from_secs(1))),
|
||||
},
|
||||
PeerId::from(key_pair.public()),
|
||||
)
|
||||
.idle_connection_timeout(Duration::from_secs(5))
|
||||
.build();
|
||||
})
|
||||
.unwrap()
|
||||
.with_swarm_config(|cfg| cfg.with_idle_connection_timeout(Duration::from_secs(5)))
|
||||
.build();
|
||||
|
||||
let _ = swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse().unwrap());
|
||||
|
||||
|
@ -20,10 +20,9 @@
|
||||
|
||||
use futures::StreamExt;
|
||||
use libp2p::{
|
||||
core::transport::upgrade::Version,
|
||||
identity, noise, ping, rendezvous,
|
||||
swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent},
|
||||
tcp, yamux, Multiaddr, PeerId, Transport,
|
||||
noise, ping, rendezvous,
|
||||
swarm::{NetworkBehaviour, SwarmEvent},
|
||||
tcp, yamux, Multiaddr,
|
||||
};
|
||||
use std::time::Duration;
|
||||
|
||||
@ -31,26 +30,26 @@ use std::time::Duration;
|
||||
async fn main() {
|
||||
env_logger::init();
|
||||
|
||||
let key_pair = identity::Keypair::generate_ed25519();
|
||||
let rendezvous_point_address = "/ip4/127.0.0.1/tcp/62649".parse::<Multiaddr>().unwrap();
|
||||
let rendezvous_point = "12D3KooWDpJ7As7BWAwRMfu1VU2WCqNjvq387JEYKDBj4kx6nXTN"
|
||||
.parse()
|
||||
.unwrap();
|
||||
|
||||
let mut swarm = SwarmBuilder::with_tokio_executor(
|
||||
tcp::tokio::Transport::default()
|
||||
.upgrade(Version::V1Lazy)
|
||||
.authenticate(noise::Config::new(&key_pair).unwrap())
|
||||
.multiplex(yamux::Config::default())
|
||||
.boxed(),
|
||||
MyBehaviour {
|
||||
rendezvous: rendezvous::client::Behaviour::new(key_pair.clone()),
|
||||
let mut swarm = libp2p::SwarmBuilder::with_new_identity()
|
||||
.with_tokio()
|
||||
.with_tcp(
|
||||
tcp::Config::default(),
|
||||
noise::Config::new,
|
||||
yamux::Config::default,
|
||||
)
|
||||
.unwrap()
|
||||
.with_behaviour(|key| MyBehaviour {
|
||||
rendezvous: rendezvous::client::Behaviour::new(key.clone()),
|
||||
ping: ping::Behaviour::new(ping::Config::new().with_interval(Duration::from_secs(1))),
|
||||
},
|
||||
PeerId::from(key_pair.public()),
|
||||
)
|
||||
.idle_connection_timeout(Duration::from_secs(5))
|
||||
.build();
|
||||
})
|
||||
.unwrap()
|
||||
.with_swarm_config(|cfg| cfg.with_idle_connection_timeout(Duration::from_secs(5)))
|
||||
.build();
|
||||
|
||||
// In production the external address should be the publicly facing IP address of the rendezvous point.
|
||||
// This address is recorded in the registration entry by the rendezvous point.
|
||||
|
@ -22,37 +22,34 @@
|
||||
|
||||
use futures::StreamExt;
|
||||
use libp2p::{
|
||||
core::transport::upgrade::Version,
|
||||
identify, identity, noise, ping, rendezvous,
|
||||
swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent},
|
||||
tcp, yamux, PeerId, Transport,
|
||||
identify, noise, ping, rendezvous,
|
||||
swarm::{NetworkBehaviour, SwarmEvent},
|
||||
tcp, yamux,
|
||||
};
|
||||
use std::error::Error;
|
||||
use std::time::Duration;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
async fn main() -> Result<(), Box<dyn Error>> {
|
||||
env_logger::init();
|
||||
|
||||
let key_pair = identity::Keypair::generate_ed25519();
|
||||
|
||||
let mut swarm = SwarmBuilder::with_tokio_executor(
|
||||
tcp::tokio::Transport::default()
|
||||
.upgrade(Version::V1Lazy)
|
||||
.authenticate(noise::Config::new(&key_pair).unwrap())
|
||||
.multiplex(yamux::Config::default())
|
||||
.boxed(),
|
||||
MyBehaviour {
|
||||
let mut swarm = libp2p::SwarmBuilder::with_new_identity()
|
||||
.with_tokio()
|
||||
.with_tcp(
|
||||
tcp::Config::default(),
|
||||
noise::Config::new,
|
||||
yamux::Config::default,
|
||||
)?
|
||||
.with_behaviour(|key| MyBehaviour {
|
||||
identify: identify::Behaviour::new(identify::Config::new(
|
||||
"rendezvous-example/1.0.0".to_string(),
|
||||
key_pair.public(),
|
||||
key.public(),
|
||||
)),
|
||||
rendezvous: rendezvous::server::Behaviour::new(rendezvous::server::Config::default()),
|
||||
ping: ping::Behaviour::new(ping::Config::new().with_interval(Duration::from_secs(1))),
|
||||
},
|
||||
PeerId::from(key_pair.public()),
|
||||
)
|
||||
.idle_connection_timeout(Duration::from_secs(5))
|
||||
.build();
|
||||
})?
|
||||
.with_swarm_config(|cfg| cfg.with_idle_connection_timeout(Duration::from_secs(5)))
|
||||
.build();
|
||||
|
||||
let _ = swarm.listen_on("/ip4/0.0.0.0/tcp/62649".parse().unwrap());
|
||||
|
||||
@ -90,6 +87,8 @@ async fn main() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(NetworkBehaviour)]
|
||||
|
@ -8,7 +8,7 @@ license = "MIT"
|
||||
[dependencies]
|
||||
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
|
||||
futures = "0.3.28"
|
||||
libp2p = { path = "../../libp2p", features = ["tokio", "dns", "macros", "noise", "ping", "tcp", "websocket", "yamux", "upnp"] }
|
||||
libp2p = { path = "../../libp2p", features = ["tokio", "dns", "macros", "noise", "ping", "tcp", "yamux", "upnp"] }
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
@ -21,32 +21,20 @@
|
||||
#![doc = include_str!("../README.md")]
|
||||
|
||||
use futures::prelude::*;
|
||||
use libp2p::core::upgrade::Version;
|
||||
use libp2p::{
|
||||
identity, noise,
|
||||
swarm::{SwarmBuilder, SwarmEvent},
|
||||
tcp, upnp, yamux, Multiaddr, PeerId, Transport,
|
||||
};
|
||||
use libp2p::{noise, swarm::SwarmEvent, upnp, yamux, Multiaddr};
|
||||
use std::error::Error;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn Error>> {
|
||||
let local_key = identity::Keypair::generate_ed25519();
|
||||
let local_peer_id = PeerId::from(local_key.public());
|
||||
println!("Local peer id: {local_peer_id:?}");
|
||||
|
||||
let transport = tcp::tokio::Transport::default()
|
||||
.upgrade(Version::V1Lazy)
|
||||
.authenticate(noise::Config::new(&local_key)?)
|
||||
.multiplex(yamux::Config::default())
|
||||
.boxed();
|
||||
|
||||
let mut swarm = SwarmBuilder::with_tokio_executor(
|
||||
transport,
|
||||
upnp::tokio::Behaviour::default(),
|
||||
local_peer_id,
|
||||
)
|
||||
.build();
|
||||
let mut swarm = libp2p::SwarmBuilder::with_new_identity()
|
||||
.with_tokio()
|
||||
.with_tcp(
|
||||
Default::default(),
|
||||
noise::Config::new,
|
||||
yamux::Config::default,
|
||||
)?
|
||||
.with_behaviour(|_| upnp::tokio::Behaviour::default())?
|
||||
.build();
|
||||
|
||||
// Tell the swarm to listen on all interfaces and a random, OS-assigned
|
||||
// port.
|
||||
|
Reference in New Issue
Block a user