-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
302 lines (253 loc) · 10.3 KB
/
main.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
291
292
293
294
295
296
297
298
299
300
301
use certs::{load_keys, load_certs};
use std::io::{ErrorKind, Error as IoError};
use std::net::SocketAddr;
use std::sync::Arc;
use std::path::Path;
use std::error::Error;
use std::collections::HashMap;
use bytes::Bytes;
use tokio::net::{TcpListener, TcpStream};
use tokio::sync::{mpsc, Mutex};
use tokio_stream::StreamExt;
use tokio_util::codec::{Framed, BytesCodec};
use tokio_rustls::{TlsAcceptor};
use tokio_rustls::server::TlsStream;
use tokio_rustls::rustls::{self};
use futures::SinkExt;
use tokio_tungstenite::WebSocketStream;
//use tungstenite::protocol::Message;
/// Shorthand for the transmit half of the message channel.
type Tx = mpsc::UnboundedSender<Bytes>;
/// Shorthand for the receive half of the message channel.
type Rx = mpsc::UnboundedReceiver<Bytes>;
struct Shared {
peers: HashMap<SocketAddr, Tx>,
}
/// The state for each connected client.
struct Peer {
/// The TCP socket wrapped with the `Lines` codec, defined below.
///
/// This handles sending and receiving data on the socket. When using
/// `Lines`, we can work at the line level instead of having to manage the
/// raw byte operations.
lines: Framed<TlsStream<TcpStream>, BytesCodec>,
/// Receive half of the message channel.
///
/// This is used to receive messages from peers. When a message is received
/// off of this `Rx`, it will be written to the socket.
rx: Rx,
}
impl Peer {
/// Create a new instance of `Peer`.
async fn new(
state: Arc<Mutex<Shared>>,
lines: Framed<TlsStream<TcpStream>, BytesCodec>,
) -> std::io::Result<Self> {
// Get the client socket address
let addr = lines.get_ref().get_ref().0.peer_addr()?;
// Create a channel for this peer
let (tx, rx) = mpsc::unbounded_channel();
// Add an entry for this `Peer` in the shared state map.
state.lock().await.peers.insert(addr, tx);
Ok(Peer { lines, rx })
}
}
struct PeerWs {
ws: WebSocketStream<TlsStream<TcpStream>>,
rx: Rx,
}
impl PeerWs {
/// Create a new instance of `Peer`.
async fn new(
state: Arc<Mutex<Shared>>,
ws: WebSocketStream<TlsStream<TcpStream>>,
) -> std::io::Result<Self> {
// Get the client socket address
let addr = ws.get_ref().get_ref().0.peer_addr()?;
// Create a channel for this peer
let (tx, rx) = mpsc::unbounded_channel();
// Add an entry for this `Peer` in the shared state map.
state.lock().await.peers.insert(addr, tx);
Ok(PeerWs { ws, rx })
}
}
impl Shared {
/// Create a new, empty, instance of `Shared`.
fn new() -> Self {
Shared {
peers: HashMap::new(),
}
}
fn get_clients_number(&self) -> usize
{
self.peers.len()
}
async fn send_to_peer(&mut self, target: SocketAddr, bytes: &Bytes)
{
for peer in self.peers.iter_mut() {
if *peer.0 == target {
tracing::warn!("send_to_peer to: {:?}, msg: {:?}", peer.0, bytes);
let _ = peer.1.send(bytes.clone());
}
}
}
/// Send a `LineCodec` encoded message to every peer, except
/// for the sender.
async fn broadcast(&mut self, sender: SocketAddr, bytes: &Bytes) {
for peer in self.peers.iter_mut() {
if *peer.0 != sender {
tracing::warn!("send brodcast to: {:?}, msg: {:?}", peer.0, bytes);
let _ = peer.1.send(bytes.clone());
}
}
}
}
async fn process_ws_stream(state: Arc<Mutex<Shared>>, mut peer: PeerWs, addr: SocketAddr) -> Result<(), Box<dyn Error>>
{
loop
{
tokio::select! {
// A message was received from a peer. Send it to the current user.
Some(msg) = peer.rx.recv() => {
//tracing::warn!("send to: {:?}, msg: {:?}", peer.ws, msg);
let ws_msg = tungstenite::protocol::Message::Binary(msg.to_vec());
peer.ws.send(ws_msg).await?;
}
result = peer.ws.next() =>
{
match result {
// A message was received from the current user, we should
// broadcast this message to the other users.
Some(Ok(msg)) => {
//tracing::warn!("send to: {:?}, Send msg: {:?}", peer.ws.get_ref().get_ref().0.peer_addr().unwrap(), msg);
//peer.ws.send(msg).await?;
tracing::warn!("msg: {:?}", msg);
if let tungstenite::protocol::Message::Binary(v) = msg {
{
state.lock().await.broadcast(addr, &Bytes::from(v)).await;
}
}
}
// An error occurred.
Some(Err(e)) => {
tracing::error!("error = {:?}", e);
}
// The stream has been exhausted.
None => {
{
let mut state = state.lock().await;
state.peers.remove(&addr);
let msg = format!("{} has left", addr);
tracing::info!("{}", msg);
tracing::info!("clients: {}", state.get_clients_number());
state.broadcast(addr, &Bytes::from(msg)).await;
}
return Ok(());
},
};
}
}
}
}
async fn process_tls_stream(state: Arc<Mutex<Shared>>, mut peer: Peer, addr: SocketAddr) -> Result<(), Box<dyn Error>>
{
loop
{
tokio::select! {
// A message was received from a peer. Send it to the current user.
Some(msg) = peer.rx.recv() => {
//tracing::warn!("send to: {:?}, msg: {:?}", peer.lines, msg);
peer.lines.send(msg).await?; //NOTE: send to peer
}
result = peer.lines.next() =>
{
match result {
// A message was received from the current user, we should
// broadcast this message to the other users.
Some(Ok(msg)) => {
{
state.lock().await.broadcast(addr, &Bytes::from(msg)).await;
}
//tracing::warn!("send to: {:?}, msg: {:?}", peer.lines, msg);
//peer.lines.send(msg).await?;
}
// An error occurred.
Some(Err(e)) => {
tracing::error!("error = {:?}", e);
}
// The stream has been exhausted.
None => {
{
let mut state = state.lock().await;
state.peers.remove(&addr);
let msg = format!("{} has left", addr);
tracing::info!("{}", msg);
tracing::info!("clients: {}", state.get_clients_number());
state.broadcast(addr, &Bytes::from(msg)).await;
}
return Ok(());
},
};
}
}
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>>
{
tracing_subscriber::fmt().init();
let certs = load_certs(Path::new("../keys/eddsa-ca.cert"))?;
let keys = load_keys(Path::new("../keys/Ed25519_private_key.pem"))?;
let private_key = keys.get(0).unwrap().clone();
let _cert = certs.get(0).unwrap().clone();
let config = rustls::ServerConfig::builder()
.with_safe_defaults()
.with_no_client_auth()
.with_single_cert(certs, private_key)
.map_err(|err| IoError::new(ErrorKind::InvalidInput, err))?;
let cfg = Arc::new(config);
let plain_listener = TcpListener::bind("0.0.0.0:9900").await?;
let websocket_listener = TcpListener::bind("0.0.0.0:9999").await?;
let state = Arc::new(Mutex::new(Shared::new()));
loop {
tokio::select! {
plain_accept_result = plain_listener.accept() => {
let (stream, addr) = plain_accept_result.unwrap();
tracing::info!("accepted connection");
let acceptor = TlsAcceptor::from(cfg.clone());
let tls_stream = acceptor.accept(stream).await.unwrap();
let peer = Peer::new(state.clone(), Framed::new(tls_stream, BytesCodec::new())).await?;
let state = Arc::clone(&state);
{
let mut state = state.lock().await;
state.broadcast(addr, &Bytes::from("new tls client")).await;
tracing::info!("clients: {}", state.get_clients_number());
}
tokio::spawn(async move {
if let Err(e) = process_tls_stream(state, peer, addr).await {
tracing::info!("an error occurred; error = {:?}", e);
}
});
}
websocket_accept_result = websocket_listener.accept() => {
let (stream, addr) = websocket_accept_result.unwrap();
tracing::info!("accepted connection");
let acceptor = TlsAcceptor::from(cfg.clone());
let tls_stream = acceptor.accept(stream).await.unwrap();
let ws_stream = tokio_tungstenite::accept_async(tls_stream).await.unwrap();
let peer = PeerWs::new(state.clone(), ws_stream).await?;
let state = Arc::clone(&state);
{
let mut state = state.lock().await;
state.broadcast(addr, &Bytes::from("new websocket client")).await;
tracing::info!("clients: {}", state.get_clients_number());
}
tokio::spawn(async move {
if let Err(e) = process_ws_stream(state, peer, addr).await {
tracing::info!("an error occurred; error = {:?}", e);
}
});
},
};
}
}