(
wss_proxy_options: WssproxyOptions,
wss_address: SocketAddr,
tls_config: ServerConfig,
)
| 93 | } |
| 94 | |
| 95 | async fn start_proxy( |
| 96 | wss_proxy_options: WssproxyOptions, |
| 97 | wss_address: SocketAddr, |
| 98 | tls_config: ServerConfig, |
| 99 | ) -> Result<(), anyhow::Error> { |
| 100 | let listener = TcpListener::bind(wss_address).await?; |
| 101 | log::info!("Websocket Secure Server Started at {}", wss_address); |
| 102 | |
| 103 | loop { |
| 104 | if let Ok((stream, _)) = listener.accept().await { |
| 105 | let tls_acceptor = TlsAcceptor::from(Arc::new(tls_config.clone())); |
| 106 | let tls_stream = match tls_acceptor.accept(stream).await { |
| 107 | Ok(o) => o, |
| 108 | Err(e) => { |
| 109 | log::debug!("Error upgrading to tls: {}", e); |
| 110 | continue; |
| 111 | } |
| 112 | }; |
| 113 | let wss_stream = match accept_async(tls_stream).await { |
| 114 | Ok(o) => o, |
| 115 | Err(e) => { |
| 116 | log::debug!("Error upgrading to websocket: {}", e); |
| 117 | continue; |
| 118 | } |
| 119 | }; |
| 120 | tokio::spawn(async move { |
| 121 | match relay_messages(wss_stream, wss_proxy_options.ws_address).await { |
| 122 | Ok(_) => (), |
| 123 | Err(e) => log::info!("Error relaying messages: {}", e), |
| 124 | } |
| 125 | }); |
| 126 | } else { |
| 127 | return Err(anyhow!("TCP Listener closed!")); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | async fn relay_messages( |
| 133 | wss_stream: WebSocketStream<TlsStream<TcpStream>>, |
no test coverage detected