| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #![doc = include_str!("../README.md")] |
|
|
| use either::Either; |
| use futures::prelude::*; |
| use libp2p::{ |
| core::transport::upgrade::Version, |
| gossipsub, identify, |
| multiaddr::Protocol, |
| noise, ping, |
| pnet::{PnetConfig, PreSharedKey}, |
| swarm::{NetworkBehaviour, SwarmEvent}, |
| tcp, yamux, Multiaddr, Transport, |
| }; |
| use std::{env, error::Error, fs, path::Path, str::FromStr, time::Duration}; |
| use tokio::{io, io::AsyncBufReadExt, select}; |
| use tracing_subscriber::EnvFilter; |
|
|
| |
| |
| 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: &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 {addr} so this address can be dialed by rust-libp2p"); |
| } |
| 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) |
| } |
|
|
| #[tokio::main] |
| async fn main() -> Result<(), Box<dyn Error>> { |
| let _ = tracing_subscriber::fmt() |
| .with_env_filter(EnvFilter::from_default_env()) |
| .try_init(); |
|
|
| let ipfs_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()?; |
|
|
| if let Some(psk) = psk { |
| println!("using swarm key with fingerprint: {}", psk.fingerprint()); |
| } |
|
|
| |
| let gossipsub_topic = gossipsub::IdentTopic::new("chat"); |
|
|
| |
| #[derive(NetworkBehaviour)] |
| struct MyBehaviour { |
| gossipsub: gossipsub::Behaviour, |
| identify: identify::Behaviour, |
| ping: ping::Behaviour, |
| } |
|
|
| 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(); |
|
|
| 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))?; |
| 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()), |
| }) |
| })? |
| .with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(60))) |
| .build(); |
|
|
| println!("Subscribing to {gossipsub_topic:?}"); |
| swarm |
| .behaviour_mut() |
| .gossipsub |
| .subscribe(&gossipsub_topic) |
| .unwrap(); |
|
|
| |
| for to_dial in std::env::args().skip(1) { |
| let addr: Multiaddr = parse_legacy_multiaddr(&to_dial)?; |
| swarm.dial(addr)?; |
| println!("Dialed {to_dial:?}") |
| } |
|
|
| |
| let mut stdin = io::BufReader::new(io::stdin()).lines(); |
|
|
| |
| swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?; |
|
|
| |
| loop { |
| select! { |
| Ok(Some(line)) = stdin.next_line() => { |
| if let Err(e) = swarm |
| .behaviour_mut() |
| .gossipsub |
| .publish(gossipsub_topic.clone(), line.as_bytes()) |
| { |
| println!("Publish error: {e:?}"); |
| } |
| }, |
| event = swarm.select_next_some() => { |
| match event { |
| SwarmEvent::NewListenAddr { address, .. } => { |
| println!("Listening on {address:?}"); |
| } |
| SwarmEvent::Behaviour(MyBehaviourEvent::Identify(event)) => { |
| println!("identify: {event:?}"); |
| } |
| SwarmEvent::Behaviour(MyBehaviourEvent::Gossipsub(gossipsub::Event::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 |
| ) |
| } |
| SwarmEvent::Behaviour(MyBehaviourEvent::Ping(event)) => { |
| match event { |
| ping::Event { |
| peer, |
| result: Result::Ok(rtt), |
| .. |
| } => { |
| println!( |
| "ping: rtt to {} is {} ms", |
| peer.to_base58(), |
| rtt.as_millis() |
| ); |
| } |
| ping::Event { |
| peer, |
| result: Result::Err(ping::Failure::Timeout), |
| .. |
| } => { |
| println!("ping: timeout to {}", peer.to_base58()); |
| } |
| ping::Event { |
| peer, |
| result: Result::Err(ping::Failure::Unsupported), |
| .. |
| } => { |
| println!("ping: {} does not support ping protocol", peer.to_base58()); |
| } |
| ping::Event { |
| peer, |
| result: Result::Err(ping::Failure::Other { error }), |
| .. |
| } => { |
| println!("ping: ping::Failure with {}: {error}", peer.to_base58()); |
| } |
| } |
| } |
| _ => {} |
| } |
| } |
| } |
| } |
| } |
|
|