Skip to content

Commit

Permalink
Add more logs
Browse files Browse the repository at this point in the history
  • Loading branch information
erebe committed Jan 16, 2024
1 parent 81fb524 commit 27df033
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
9 changes: 6 additions & 3 deletions src/tunnel/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{tunnel, WsClientConfig};
use futures_util::pin_mut;
use hyper::header::COOKIE;
use jsonwebtoken::TokenData;
use log::debug;
use std::future::Future;
use std::ops::Deref;
use std::sync::Arc;
Expand All @@ -25,19 +26,20 @@ where
W: AsyncWrite + Send + 'static,
{
// Connect to server with the correct protocol
let (ws_rx, ws_tx) = match client_cfg.remote_addr.scheme() {
let (ws_rx, ws_tx, response) = match client_cfg.remote_addr.scheme() {
TransportScheme::Ws | TransportScheme::Wss => {
tunnel::transport::websocket::connect(request_id, client_cfg, remote_cfg)
.await
.map(|(r, w, _response)| (TunnelReader::Websocket(r), TunnelWriter::Websocket(w)))?
.map(|(r, w, response)| (TunnelReader::Websocket(r), TunnelWriter::Websocket(w), response))?
}
TransportScheme::Http | TransportScheme::Https => {
tunnel::transport::http2::connect(request_id, client_cfg, remote_cfg)
.await
.map(|(r, w, _response)| (TunnelReader::Http2(r), TunnelWriter::Http2(w)))?
.map(|(r, w, response)| (TunnelReader::Http2(r), TunnelWriter::Http2(w), response))?
}
};

debug!("Server response: {:?}", response);
let (local_rx, local_tx) = duplex_stream;
let (close_tx, close_rx) = oneshot::channel::<()>();

Expand Down Expand Up @@ -121,6 +123,7 @@ where
};

// Connect to endpoint
debug!("Server response: {:?}", response);
let remote = response
.headers
.get(COOKIE)
Expand Down
16 changes: 11 additions & 5 deletions src/tunnel/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use hyper::header::{COOKIE, SEC_WEBSOCKET_PROTOCOL};
use hyper::http::HeaderValue;
use hyper::server::conn::{http1, http2};
use hyper::service::service_fn;
use hyper::{http, Request, Response, StatusCode};
use hyper::{http, Request, Response, StatusCode, Version};
use hyper_util::rt::TokioExecutor;
use jsonwebtoken::TokenData;
use once_cell::sync::Lazy;
Expand Down Expand Up @@ -569,14 +569,20 @@ pub async fn run_server(server_config: Arc<WsServerConfig>) -> anyhow::Result<()
move |req: Request<Incoming>| {
let server_config = server_config.clone();
async move {
if !fastwebsockets::upgrade::is_upgrade_request(&req) {
if fastwebsockets::upgrade::is_upgrade_request(&req) {
ws_server_upgrade(server_config.clone(), client_addr, req)
.map(|response| Ok::<_, anyhow::Error>(response.map(Either::Left)))
.await
} else if req.version() == Version::HTTP_2 {
http_server_upgrade(server_config.clone(), client_addr, req)
.map::<anyhow::Result<_>, _>(Ok)
.await
} else {
ws_server_upgrade(server_config.clone(), client_addr, req)
.map(|response| Ok::<_, anyhow::Error>(response.map(Either::Left)))
.await
error!("Invalid protocol version request, got {:?} while expecting either websocket http1 upgrade or http2", req.version());
Ok(http::Response::builder()
.status(StatusCode::BAD_REQUEST)
.body(Either::Left("Invalid protocol request".to_string()))
.unwrap())
}
}
}
Expand Down

0 comments on commit 27df033

Please sign in to comment.