use async_std::{io, task};
use futures::{future, prelude::*};
use tet_libp2p::{
core::{
either::EitherTransport, muxing::StreamMuxerBox, transport, transport::upgrade::Version,
},
gossipsub::{self, Gossipsub, GossipsubConfigBuilder, GossipsubEvent, MessageAuthenticity},
identify::{Identify, IdentifyEvent},
identity,
multiaddr::Protocol,
noise,
ping::{self, Ping, PingConfig, PingEvent},
pnet::{PnetConfig, PreSharedKey},
swarm::NetworkBehaviourEventProcess,
tcp::TcpConfig,
remux::RemuxConfig,
Multiaddr, NetworkBehaviour, PeerId, Swarm, Transport,
};
use std::{
env,
error::Error,
fs,
path::Path,
str::FromStr,
task::{Context, Poll},
time::Duration,
};
pub fn build_transport(
key_pair: identity::Keypair,
psk: Option<PreSharedKey>,
) -> transport::Boxed<(PeerId, StreamMuxerBox)> {
let noise_keys = noise::Keypair::<noise::X25519Spec>::new()
.into_authentic(&key_pair)
.unwrap();
let noise_config = noise::NoiseConfig::xx(noise_keys).into_authenticated();
let remux_config = RemuxConfig::default();
let base_transport = TcpConfig::new().nodelay(true);
let maybe_encrypted = match psk {
Some(psk) => EitherTransport::Left(
base_transport.and_then(move |socket, _| PnetConfig::new(psk).handshake(socket)),
),
None => EitherTransport::Right(base_transport),
};
maybe_encrypted
.upgrade(Version::V1)
.authenticate(noise_config)
.multiplex(remux_config)
.timeout(Duration::from_secs(20))
.boxed()
}
fn get_ipfs_path() -> Box<Path> {
env::var("IPFS_PATH")
.map(|ipfs_path| Path::new(&ipfs_path).into())
.unwrap_or_else(|_| {
env::var("HOME")
.map(|home| Path::new(&home).join(".ipfs"))
.expect("could not determine home directory")
.into()
})
}
fn get_psk(path: Box<Path>) -> std::io::Result<Option<String>> {
let swarm_key_file = path.join("swarm.key");
match fs::read_to_string(swarm_key_file) {
Ok(text) => Ok(Some(text)),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
Err(e) => Err(e),
}
}
fn strip_peer_id(addr: &mut Multiaddr) {
let last = addr.pop();
match last {
Some(Protocol::P2p(peer_id)) => {
let mut addr = Multiaddr::empty();
addr.push(Protocol::P2p(peer_id));
println!(
"removing peer id {} so this address can be dialed by rust-libp2p",
addr
);
}
Some(other) => addr.push(other),
_ => {}
}
}
fn parse_legacy_multiaddr(text: &str) -> Result<Multiaddr, Box<dyn Error>> {
let sanitized = text
.split('/')
.map(|part| if part == "ipfs" { "p2p" } else { part })
.collect::<Vec<_>>()
.join("/");
let mut res = Multiaddr::from_str(&sanitized)?;
strip_peer_id(&mut res);
Ok(res)
}
fn main() -> Result<(), Box<dyn Error>> {
env_logger::init();
let ipfs_path: Box<Path> = get_ipfs_path();
println!("using IPFS_PATH {:?}", ipfs_path);
let psk: Option<PreSharedKey> = get_psk(ipfs_path)?
.map(|text| PreSharedKey::from_str(&text))
.transpose()?;
let local_key = identity::Keypair::generate_ed25519();
let local_peer_id = PeerId::from(local_key.public());
println!("using random peer id: {:?}", local_peer_id);
for psk in psk {
println!("using swarm key with fingerprint: {}", psk.fingerprint());
}
let transport = build_transport(local_key.clone(), psk);
let gossipsub_topic = gossipsub::IdentTopic::new("chat");
#[derive(NetworkBehaviour)]
struct MyBehaviour {
gossipsub: Gossipsub,
identify: Identify,
ping: Ping,
}
impl NetworkBehaviourEventProcess<IdentifyEvent> for MyBehaviour {
fn inject_event(&mut self, event: IdentifyEvent) {
println!("identify: {:?}", event);
}
}
impl NetworkBehaviourEventProcess<GossipsubEvent> for MyBehaviour {
fn inject_event(&mut self, event: GossipsubEvent) {
match event {
GossipsubEvent::Message {
propagation_source: peer_id,
message_id: id,
message,
} => println!(
"Got message: {} with id: {} from peer: {:?}",
String::from_utf8_lossy(&message.data),
id,
peer_id
),
_ => {}
}
}
}
impl NetworkBehaviourEventProcess<PingEvent> for MyBehaviour {
fn inject_event(&mut self, event: PingEvent) {
use ping::handler::{PingFailure, PingSuccess};
match event {
PingEvent {
peer,
result: Result::Ok(PingSuccess::Ping { rtt }),
} => {
println!(
"ping: rtt to {} is {} ms",
peer.to_base58(),
rtt.as_millis()
);
}
PingEvent {
peer,
result: Result::Ok(PingSuccess::Pong),
} => {
println!("ping: pong from {}", peer.to_base58());
}
PingEvent {
peer,
result: Result::Err(PingFailure::Timeout),
} => {
println!("ping: timeout to {}", peer.to_base58());
}
PingEvent {
peer,
result: Result::Err(PingFailure::Other { error }),
} => {
println!("ping: failure with {}: {}", peer.to_base58(), error);
}
}
}
}
let mut swarm = {
let gossipsub_config = GossipsubConfigBuilder::default()
.max_transmit_size(262144)
.build()
.expect("valid config");
let mut behaviour = MyBehaviour {
gossipsub: Gossipsub::new(
MessageAuthenticity::Signed(local_key.clone()),
gossipsub_config,
)
.expect("Valid configuration"),
identify: Identify::new(
"/ipfs/0.1.0".into(),
"rust-ipfs-example".into(),
local_key.public(),
),
ping: Ping::new(PingConfig::new()),
};
println!("Subscribing to {:?}", gossipsub_topic);
behaviour.gossipsub.subscribe(&gossipsub_topic).unwrap();
Swarm::new(transport, behaviour, local_peer_id.clone())
};
for to_dial in std::env::args().skip(1) {
let addr: Multiaddr = parse_legacy_multiaddr(&to_dial)?;
Swarm::dial_addr(&mut swarm, addr)?;
println!("Dialed {:?}", to_dial)
}
let mut stdin = io::BufReader::new(io::stdin()).lines();
Swarm::listen_on(&mut swarm, "/ip4/0.0.0.0/tcp/0".parse()?)?;
let mut listening = false;
task::block_on(future::poll_fn(move |cx: &mut Context<'_>| {
loop {
if let Err(e) = match stdin.try_poll_next_unpin(cx)? {
Poll::Ready(Some(line)) => swarm
.gossipsub
.publish(gossipsub_topic.clone(), line.as_bytes()),
Poll::Ready(None) => panic!("Stdin closed"),
Poll::Pending => break,
} {
println!("Publish error: {:?}", e);
}
}
loop {
match swarm.poll_next_unpin(cx) {
Poll::Ready(Some(event)) => println!("{:?}", event),
Poll::Ready(None) => return Poll::Ready(Ok(())),
Poll::Pending => {
if !listening {
for addr in Swarm::listeners(&swarm) {
println!("Address {}/ipfs/{}", addr, local_peer_id);
listening = true;
}
}
break;
}
}
}
Poll::Pending
}))
}