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
use std::{collections::HashMap, future::Future};
use bytes::{Buf, Bytes};
use futures::{
channel::{mpsc, oneshot},
future, FutureExt, SinkExt, StreamExt,
};
use thiserror::Error;
use tokio::io::{AsyncRead, AsyncWrite, AsyncWriteExt, DuplexStream};
use tokio_tungstenite::{tungstenite as ws, WebSocketStream};
use tokio_util::io::ReaderStream;
#[derive(Debug, Error)]
pub enum Error {
#[error("received invalid channel {0}")]
InvalidChannel(usize),
#[error("received initial frame with invalid size")]
InvalidInitialFrameSize,
#[error("invalid port mapping in initial frame, got {actual}, expected {expected}")]
InvalidPortMapping { actual: u16, expected: u16 },
#[error("failed to forward bytes from Pod: {0}")]
ForwardFromPod(#[source] futures::channel::mpsc::SendError),
#[error("failed to forward bytes to Pod: {0}")]
ForwardToPod(#[source] futures::channel::mpsc::SendError),
#[error("failed to write bytes from Pod: {0}")]
WriteBytesFromPod(#[source] std::io::Error),
#[error("failed to read bytes to send to Pod: {0}")]
ReadBytesToSend(#[source] std::io::Error),
#[error("received invalid error message from Pod: {0}")]
InvalidErrorMessage(#[source] std::string::FromUtf8Error),
#[error("failed to forward an error message {0:?}")]
ForwardErrorMessage(String),
#[error("failed to send a WebSocket message: {0}")]
SendWebSocketMessage(#[source] ws::Error),
#[error("failed to receive a WebSocket message: {0}")]
ReceiveWebSocketMessage(#[source] ws::Error),
#[error("failed to complete the background task: {0}")]
Spawn(#[source] tokio::task::JoinError),
}
type ErrorReceiver = oneshot::Receiver<String>;
type ErrorSender = oneshot::Sender<String>;
enum Message {
FromPod(u8, Bytes),
ToPod(u8, Bytes),
}
pub struct Portforwarder {
ports: HashMap<u16, DuplexStream>,
errors: HashMap<u16, ErrorReceiver>,
task: tokio::task::JoinHandle<Result<(), Error>>,
}
impl Portforwarder {
pub(crate) fn new<S>(stream: WebSocketStream<S>, port_nums: &[u16]) -> Self
where
S: AsyncRead + AsyncWrite + Unpin + Sized + Send + 'static,
{
let mut ports = HashMap::with_capacity(port_nums.len());
let mut error_rxs = HashMap::with_capacity(port_nums.len());
let mut error_txs = Vec::with_capacity(port_nums.len());
let mut task_ios = Vec::with_capacity(port_nums.len());
for port in port_nums.iter() {
let (a, b) = tokio::io::duplex(1024 * 1024);
ports.insert(*port, a);
task_ios.push(b);
let (tx, rx) = oneshot::channel();
error_rxs.insert(*port, rx);
error_txs.push(Some(tx));
}
let task = tokio::spawn(start_message_loop(
stream,
port_nums.to_vec(),
task_ios,
error_txs,
));
Portforwarder {
ports,
errors: error_rxs,
task,
}
}
#[inline]
pub fn take_stream(&mut self, port: u16) -> Option<impl AsyncRead + AsyncWrite + Unpin> {
self.ports.remove(&port)
}
#[inline]
pub fn take_error(&mut self, port: u16) -> Option<impl Future<Output = Option<String>>> {
self.errors.remove(&port).map(|recv| recv.map(|res| res.ok()))
}
#[inline]
pub fn abort(&self) {
self.task.abort();
}
pub async fn join(self) -> Result<(), Error> {
self.task.await.unwrap_or_else(|e| Err(Error::Spawn(e)))
}
}
async fn start_message_loop<S>(
stream: WebSocketStream<S>,
ports: Vec<u16>,
duplexes: Vec<DuplexStream>,
error_senders: Vec<Option<ErrorSender>>,
) -> Result<(), Error>
where
S: AsyncRead + AsyncWrite + Unpin + Sized + Send + 'static,
{
let mut writers = Vec::new();
let mut loops = Vec::with_capacity(ports.len() + 2);
let (sender, receiver) = mpsc::channel::<Message>(1);
for (i, (r, w)) in duplexes.into_iter().map(tokio::io::split).enumerate() {
writers.push(w);
let ch = 2 * (i as u8);
loops.push(to_pod_loop(ch, r, sender.clone()).boxed());
}
let (ws_sink, ws_stream) = stream.split();
loops.push(from_pod_loop(ws_stream, sender).boxed());
loops.push(forwarder_loop(&ports, receiver, ws_sink, writers, error_senders).boxed());
future::try_join_all(loops).await.map(|_| ())
}
async fn to_pod_loop(
ch: u8,
reader: tokio::io::ReadHalf<DuplexStream>,
mut sender: mpsc::Sender<Message>,
) -> Result<(), Error> {
let mut read_stream = ReaderStream::new(reader);
while let Some(bytes) = read_stream
.next()
.await
.transpose()
.map_err(Error::ReadBytesToSend)?
{
if !bytes.is_empty() {
sender
.send(Message::ToPod(ch, bytes))
.await
.map_err(Error::ForwardToPod)?;
}
}
Ok(())
}
async fn from_pod_loop<S>(
mut ws_stream: futures::stream::SplitStream<WebSocketStream<S>>,
mut sender: mpsc::Sender<Message>,
) -> Result<(), Error>
where
S: AsyncRead + AsyncWrite + Unpin + Sized + Send + 'static,
{
while let Some(msg) = ws_stream
.next()
.await
.transpose()
.map_err(Error::ReceiveWebSocketMessage)?
{
match msg {
ws::Message::Binary(bin) if bin.len() > 1 => {
let mut bytes = Bytes::from(bin);
let ch = bytes.split_to(1)[0];
sender
.send(Message::FromPod(ch, bytes))
.await
.map_err(Error::ForwardFromPod)?;
}
_ => {}
}
}
Ok(())
}
async fn forwarder_loop<S>(
ports: &[u16],
mut receiver: mpsc::Receiver<Message>,
mut ws_sink: futures::stream::SplitSink<WebSocketStream<S>, ws::Message>,
mut writers: Vec<tokio::io::WriteHalf<DuplexStream>>,
mut error_senders: Vec<Option<ErrorSender>>,
) -> Result<(), Error>
where
S: AsyncRead + AsyncWrite + Unpin + Sized + Send + 'static,
{
let mut initialized = vec![false; 2 * ports.len()];
while let Some(msg) = receiver.next().await {
match msg {
Message::FromPod(ch, mut bytes) => {
let ch = ch as usize;
if ch >= initialized.len() {
return Err(Error::InvalidChannel(ch));
}
let port_index = ch / 2;
if !initialized[ch] {
if bytes.len() != 2 {
return Err(Error::InvalidInitialFrameSize);
}
let port = bytes.get_u16_le();
if port != ports[port_index] {
return Err(Error::InvalidPortMapping {
actual: port,
expected: ports[port_index],
});
}
initialized[ch] = true;
continue;
}
if ch % 2 != 0 {
if let Some(sender) = error_senders[port_index].take() {
let s = String::from_utf8(bytes.into_iter().collect())
.map_err(Error::InvalidErrorMessage)?;
sender.send(s).map_err(Error::ForwardErrorMessage)?;
}
} else {
writers[port_index]
.write_all(&bytes)
.await
.map_err(Error::WriteBytesFromPod)?;
}
}
Message::ToPod(ch, bytes) => {
let mut bin = Vec::with_capacity(bytes.len() + 1);
bin.push(ch);
bin.extend(bytes.into_iter());
ws_sink
.send(ws::Message::binary(bin))
.await
.map_err(Error::SendWebSocketMessage)?;
}
}
}
Ok(())
}