-
Notifications
You must be signed in to change notification settings - Fork 5
/
hyperchat.rs
283 lines (252 loc) · 9.2 KB
/
hyperchat.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
use async_std::channel::{Receiver, Sender};
use async_std::io::{self, ReadExt, WriteExt};
use async_std::stream::StreamExt;
use async_std::sync::Mutex;
use async_std::task::{self, JoinHandle};
use clap::Parser;
use futures::future::{select, Either};
use std::net::SocketAddr;
use std::sync::Arc;
use hyperswarm::{
hash_topic, BootstrapNode, Config, DhtConfig, Hyperswarm, HyperswarmStream, TopicConfig,
};
const DISCOVERY_NS_BUF: &[u8] = b"hyperchat:v0";
#[derive(Parser, Debug)]
struct Opts {
/// Set bootstrap addresses
#[clap(short, long)]
pub bootstrap: Vec<SocketAddr>,
/// Address to bind DHT node to (default: 0.0.0.0:49737).
#[clap(long)]
pub bind: Option<SocketAddr>,
/// Command to run
#[clap(subcommand)]
pub command: Command,
}
#[derive(Parser, Debug)]
enum Command {
/// Run a DHT bootstrap node.
Bootstrap,
/// Join the swarm and connect to peers.
Join(JoinOpts),
}
#[derive(Parser, Debug)]
pub struct JoinOpts {
/// Set topics
#[clap(short, long)]
pub topics: Vec<String>,
/// Set name to send to peers in hello message
#[clap(short, long)]
pub name: Option<String>,
}
#[async_std::main]
async fn main() -> anyhow::Result<()> {
env_logger::init();
let opts: Opts = Opts::parse();
match opts.command {
Command::Bootstrap => {
let config = DhtConfig::default();
let config = if !opts.bootstrap.is_empty() {
config.set_bootstrap_nodes(&opts.bootstrap)
} else {
config.empty_bootstrap_nodes()
};
let node = BootstrapNode::new(config, opts.bind);
let (addr, task) = node.run().await?;
eprintln!("Running bootstrap node on {:?}", addr);
task.await?;
}
Command::Join(join_opts) => {
let config = Config::default();
let config = if !opts.bootstrap.is_empty() {
config.set_bootstrap_nodes(&opts.bootstrap)
} else {
config.set_bootstrap_nodes(&["127.0.0.1:49737"])
};
let name = join_opts.name.unwrap_or_else(random_name);
eprintln!("your name: {}", name);
// Bind and open swarm.
let swarm = Hyperswarm::bind(config).await?;
// Configure swarm and listen on topics.
let handle = swarm.handle();
let config = TopicConfig::announce_and_lookup();
for topic in join_opts.topics {
let hash = hash_topic(DISCOVERY_NS_BUF, topic.as_bytes());
handle.configure(hash, config.clone());
eprintln!("join topic \"{}\": {}", topic, hex::encode(hash));
}
// Open broadcaster.
let initial_message = name.as_bytes().to_vec();
let broadcaster = HyperBroadcast::new(initial_message);
// Start the broadcast loops.
let (task, mut incoming_rx) = broadcaster.run(swarm).await;
// Print incoming messages.
task::spawn(async move {
let mut name = None;
while let Some(message) = incoming_rx.next().await {
let content = String::from_utf8(message.content)
.unwrap_or_else(|_| "<invalid utf8>".to_string());
match name.as_ref() {
None => {
println!("[{}] is now known as `{}`", message.from, content);
name = Some(content);
}
Some(name) => {
println!("[{}] <{}> {}", message.from, name, content);
}
}
}
});
// Read outgoing messages from stdin.
{
let broadcaster = broadcaster.clone();
task::spawn(async move {
let stdin = io::stdin();
loop {
let mut line = String::new();
stdin.read_line(&mut line).await.unwrap();
broadcaster.broadcast(line).await;
}
});
}
// Wait for the broadcast task until error or forever.
task.await?;
}
};
Ok(())
}
#[derive(Clone)]
struct HyperBroadcast {
initial_message: Vec<u8>,
peers: Arc<Mutex<Vec<Peer>>>,
}
impl HyperBroadcast {
pub fn new(initial_message: Vec<u8>) -> Self {
Self {
initial_message,
peers: Default::default(),
}
}
pub async fn broadcast(&self, message: String) {
let mut peers = self.peers.lock().await;
for peer in peers.iter_mut() {
match peer.send_message(message.clone()).await {
Ok(_) => log::debug!("Message sent to {}", peer.id()),
Err(err) => log::error!("Error sending message to {}: {}", peer.id(), err),
}
}
}
pub async fn run(
&self,
mut swarm: Hyperswarm,
) -> (JoinHandle<anyhow::Result<()>>, Receiver<IncomingMessage>) {
let (incoming_tx, incoming_rx) = async_std::channel::unbounded();
let peers = self.peers.clone();
let initial_message = self.initial_message.clone();
let task = task::spawn(async move {
while let Some(stream) = swarm.next().await {
let stream = stream?;
// Open channels.
let (outbound_tx, outbound_rx) = async_std::channel::unbounded();
let (inbound_tx, mut inbound_rx) = async_std::channel::unbounded();
// Queue initial message.
outbound_tx.send(initial_message.clone()).await?;
// Construct peer info.
let peer = Peer {
outbound_tx,
addr: stream.peer_addr(),
protocol: stream.protocol().to_string(),
};
let peer_id = peer.id();
println!("[{}] connected", peer_id);
// Spawn loop to send and receive messages.
{
let peers = peers.clone();
task::spawn(async move {
if let Err(err) = connection_loop(stream, inbound_tx, outbound_rx).await {
// On error in peer connection, remove from active peers.
println!("[{}] disconnected", peer_id);
log::debug!("[{}] error: {}", peer_id, err);
peers.lock().await.retain(|peer| peer.id() != peer_id);
}
});
}
// Spawn loop to forward incoming messages.
{
let peer_id = peer.id();
let incoming_tx = incoming_tx.clone();
task::spawn(async move {
while let Some(message) = inbound_rx.next().await {
let message = IncomingMessage {
from: peer_id.clone(),
content: message,
};
incoming_tx.send(message).await.unwrap();
}
});
}
// Save peer for broadcasting.
peers.lock().await.push(peer);
}
Ok(())
});
(task, incoming_rx)
}
}
type PeerId = String;
struct IncomingMessage {
from: PeerId,
content: Vec<u8>,
}
struct Peer {
outbound_tx: Sender<Vec<u8>>,
addr: SocketAddr,
protocol: String,
}
impl Peer {
pub fn id(&self) -> PeerId {
format!("{}:{}", self.protocol, self.addr)
}
pub async fn send_message(&self, message: String) -> anyhow::Result<()> {
let message = message.as_bytes().to_vec();
self.outbound_tx.send(message).await?;
Ok(())
}
}
async fn connection_loop(
mut stream: HyperswarmStream,
inbound_tx: Sender<Vec<u8>>,
mut outbound_rx: Receiver<Vec<u8>>,
) -> anyhow::Result<()> {
let mut len_buf = [0u8; 4];
loop {
// Incoming message.
let left = stream.read_exact(&mut len_buf);
// Outgoing message.
let right = outbound_rx.next();
match select(left, right).await {
Either::Left((res, _)) => match res {
Ok(_) => {
let len = u32::from_be_bytes(len_buf);
let mut buf = vec![0u8; len as usize];
stream.read_exact(&mut buf).await?;
inbound_tx.send(buf).await?;
}
Err(err) => return Err(err.into()),
},
Either::Right((message, _)) => match message {
Some(message) => {
let mut buf = Vec::new();
buf.extend((message.len() as u32).to_be_bytes());
buf.extend(&message[..]);
stream.write_all(&buf).await?;
}
None => return Err(anyhow::anyhow!("Remote connection closed?")),
},
}
}
}
pub fn random_name() -> String {
let bytes = rand::random::<[u8; 4]>();
hex::encode(&bytes)
}