forked from libp2p/rust-libp2p
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipfs-private.rs
290 lines (268 loc) · 10.5 KB
/
ipfs-private.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// Copyright 2020 Parity Technologies (UK) Ltd.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//! A minimal node that can interact with ipfs
//!
//! This node implements the gossipsub, ping and identify protocols. It supports
//! the ipfs private swarms feature by reading the pre shared key file `swarm.key`
//! from the IPFS_PATH environment variable or from the default location.
//!
//! You can pass any number of nodes to be dialed.
//!
//! On startup, this example will show a list of addresses that you can dial
//! from a go-ipfs or js-ipfs node.
//!
//! You can ping this node, or use pubsub (gossipsub) on the topic "chat". For this
//! to work, the ipfs node needs to be configured to use gossipsub.
use async_std::io;
use futures::{prelude::*, select};
use libp2p::{
core::{
either::EitherTransport, muxing::StreamMuxerBox, transport, transport::upgrade::Version,
},
gossipsub::{self, Gossipsub, GossipsubConfigBuilder, GossipsubEvent, MessageAuthenticity},
identify::{Identify, IdentifyConfig, IdentifyEvent},
identity,
multiaddr::Protocol,
noise, ping,
pnet::{PnetConfig, PreSharedKey},
swarm::{NetworkBehaviourEventProcess, SwarmEvent},
tcp::TcpConfig,
yamux::YamuxConfig,
Multiaddr, NetworkBehaviour, PeerId, Swarm, Transport,
};
use std::{env, error::Error, fs, path::Path, str::FromStr, time::Duration};
/// 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_keys = noise::Keypair::<noise::X25519Spec>::new()
.into_authentic(&key_pair)
.unwrap();
let noise_config = noise::NoiseConfig::xx(noise_keys).into_authenticated();
let yamux_config = YamuxConfig::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(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> {
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()
})
}
/// Read the pre shared key file from the given ipfs directory
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),
}
}
/// for a multiaddr that ends with a peer id, this strips this suffix. Rust-libp2p
/// only supports dialing to an address without providing the peer id.
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),
_ => {}
}
}
/// parse a legacy multiaddr (replace ipfs with p2p), and strip the peer id
/// so it can be dialed by rust-libp2p
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)
}
#[async_std::main]
async 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()?;
// 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);
for psk in 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(event_process = true)]
struct MyBehaviour {
gossipsub: Gossipsub,
identify: Identify,
ping: ping::Behaviour,
}
impl NetworkBehaviourEventProcess<IdentifyEvent> for MyBehaviour {
// Called when `identify` produces an event.
fn inject_event(&mut self, event: IdentifyEvent) {
println!("identify: {:?}", event);
}
}
impl NetworkBehaviourEventProcess<GossipsubEvent> for MyBehaviour {
// Called when `gossipsub` produces an event.
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<ping::Event> for MyBehaviour {
// Called when `ping` produces an event.
fn inject_event(&mut self, event: ping::Event) {
match event {
ping::Event {
peer,
result: Result::Ok(ping::Success::Ping { rtt }),
} => {
println!(
"ping: rtt to {} is {} ms",
peer.to_base58(),
rtt.as_millis()
);
}
ping::Event {
peer,
result: Result::Ok(ping::Success::Pong),
} => {
println!("ping: pong from {}", peer.to_base58());
}
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 {}: {}", peer.to_base58(), error);
}
}
}
}
// Create a Swarm to manage peers and events
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(IdentifyConfig::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();
Swarm::new(transport, behaviour, local_peer_id)
};
// Reach out to other nodes if specified
for to_dial in std::env::args().skip(1) {
let addr: Multiaddr = parse_legacy_multiaddr(&to_dial)?;
swarm.dial(addr)?;
println!("Dialed {:?}", to_dial)
}
// Read full lines from stdin
let mut stdin = io::BufReader::new(io::stdin()).lines().fuse();
// Listen on all interfaces and whatever port the OS assigns
swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?;
// Kick it off
loop {
select! {
line = stdin.select_next_some() => {
if let Err(e) = swarm
.behaviour_mut()
.gossipsub
.publish(gossipsub_topic.clone(), line.expect("Stdin not to close").as_bytes())
{
println!("Publish error: {:?}", e);
}
},
event = swarm.select_next_some() => {
if let SwarmEvent::NewListenAddr { address, .. } = event {
println!("Listening on {:?}", address);
}
}
}
}
}