Skip to content

Commit

Permalink
Proxy to the proper WebSocket handler path
Browse files Browse the repository at this point in the history
  • Loading branch information
ekzhang committed Oct 29, 2023
1 parent a945d7a commit acfc1e0
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions crates/sshx-server/src/web/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub async fn get_session_ws(
}
}
Ok(Err(Some(host))) => {
if let Err(err) = proxy_redirect(&mut socket, &host).await {
if let Err(err) = proxy_redirect(&mut socket, &host, &name).await {
error!(%err, "failed to proxy websocket");
let frame = CloseFrame {
code: 4500,
Expand Down Expand Up @@ -208,13 +208,13 @@ async fn handle_socket(socket: &mut WebSocket, session: Arc<Session>) -> Result<
}

/// Transparently reverse-proxy a WebSocket connection to a different host.
async fn proxy_redirect(socket: &mut WebSocket, host: &str) -> Result<()> {
async fn proxy_redirect(socket: &mut WebSocket, host: &str, name: &str) -> Result<()> {
use tokio_tungstenite::{
connect_async,
tungstenite::protocol::{CloseFrame as TCloseFrame, Message as TMessage},
};

let (mut upstream, _) = connect_async(format!("ws://{host}")).await?;
let (mut upstream, _) = connect_async(format!("ws://{host}/s/{name}")).await?;
loop {
// Due to axum having its own WebSocket API types, we need to manually translate
// between it and tungstenite's message type.
Expand Down

0 comments on commit acfc1e0

Please sign in to comment.