Bridge a single local TCP connection through a WebSocket to the tunnel.
(
tcp_stream: TcpStream,
ws_url: &str,
)
| 204 | |
| 205 | /// Bridge a single local TCP connection through a WebSocket to the tunnel. |
| 206 | async fn proxy_connection( |
| 207 | tcp_stream: TcpStream, |
| 208 | ws_url: &str, |
| 209 | ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> { |
| 210 | let (ws_stream, _response) = tokio_tungstenite::connect_async(ws_url).await?; |
| 211 | let (ws_sink, ws_source) = ws_stream.split(); |
| 212 | let (tcp_read, tcp_write) = tokio::io::split(tcp_stream); |
| 213 | |
| 214 | let mut tcp_to_ws = tokio::spawn(proxy_tcp_to_ws(tcp_read, ws_sink)); |
| 215 | let mut ws_to_tcp = tokio::spawn(proxy_ws_to_tcp(ws_source, tcp_write)); |
| 216 | |
| 217 | tokio::select! { |
| 218 | _ = &mut tcp_to_ws => { |
| 219 | ws_to_tcp.abort(); |
| 220 | } |
| 221 | _ = &mut ws_to_tcp => { |
| 222 | tcp_to_ws.abort(); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | Ok(()) |
| 227 | } |
| 228 | |
| 229 | async fn proxy_tcp_to_ws( |
| 230 | mut tcp_read: tokio::io::ReadHalf<TcpStream>, |
no test coverage detected