From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 86FCC729F2 for ; Tue, 13 Apr 2021 14:16:44 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 849D52A5FA for ; Tue, 13 Apr 2021 14:16:44 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [212.186.127.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id EE7CD2A5F0 for ; Tue, 13 Apr 2021 14:16:43 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id BAE4F45A71 for ; Tue, 13 Apr 2021 14:16:43 +0200 (CEST) From: =?UTF-8?q?Fabian=20Gr=C3=BCnbichler?= To: pve-devel@lists.proxmox.com Date: Tue, 13 Apr 2021 14:16:20 +0200 Message-Id: <20210413121640.3602975-3-f.gruenbichler@proxmox.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210413121640.3602975-1-f.gruenbichler@proxmox.com> References: <20210413121640.3602975-1-f.gruenbichler@proxmox.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.027 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [websocket.rs] Subject: [pve-devel] [PATCH proxmox 2/2] websocket: adapt for client connection X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 13 Apr 2021 12:16:44 -0000 previously, this was only used for the server side handling of web sockets. by making the mask part of the WebSocket struct and making some of the fns associated, we can re-use this for client-side connections such as in proxmox-websocket-tunnel. Signed-off-by: Fabian Grünbichler --- proxmox/src/tools/websocket.rs | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/proxmox/src/tools/websocket.rs b/proxmox/src/tools/websocket.rs index 8685aab..e3a559b 100644 --- a/proxmox/src/tools/websocket.rs +++ b/proxmox/src/tools/websocket.rs @@ -659,6 +659,7 @@ pub const MAGIC_WEBSOCKET_GUID: &str = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; /// Provides methods for connecting a WebSocket endpoint with another pub struct WebSocket { pub text: bool, + pub mask: Option<[u8; 4]>, } impl WebSocket { @@ -710,10 +711,13 @@ impl WebSocket { .header(SEC_WEBSOCKET_PROTOCOL, ws_proto) .body(Body::empty())?; - Ok((Self { text }, response)) + let mask = None; + + Ok((Self { text, mask }, response)) } - async fn handle_channel_message( + pub async fn handle_channel_message( + &self, result: WebSocketReadResult, writer: &mut WebSocketWriter, ) -> Result @@ -722,11 +726,11 @@ impl WebSocket { { match result { Ok((OpCode::Ping, msg)) => { - writer.send_control_frame(None, OpCode::Pong, &msg).await?; + writer.send_control_frame(self.mask, OpCode::Pong, &msg).await?; Ok(OpCode::Pong) } Ok((OpCode::Close, msg)) => { - writer.send_control_frame(None, OpCode::Close, &msg).await?; + writer.send_control_frame(self.mask, OpCode::Close, &msg).await?; Ok(OpCode::Close) } Ok((opcode, _)) => { @@ -735,7 +739,7 @@ impl WebSocket { } Err(err) => { writer - .send_control_frame(None, OpCode::Close, &err.generate_frame_payload()) + .send_control_frame(self.mask, OpCode::Close, &err.generate_frame_payload()) .await?; Err(Error::from(err)) } @@ -743,6 +747,7 @@ impl WebSocket { } async fn copy_to_websocket( + &self, mut reader: &mut R, mut writer: &mut WebSocketWriter, receiver: &mut mpsc::UnboundedReceiver, @@ -759,7 +764,7 @@ impl WebSocket { res = buf.read_from_async(&mut reader).fuse() => res?, res = receiver.recv().fuse() => { let res = res.ok_or_else(|| format_err!("control channel closed"))?; - match Self::handle_channel_message(res, &mut writer).await? { + match self.handle_channel_message(res, &mut writer).await? { OpCode::Close => return Ok(true), _ => { continue; }, } @@ -799,10 +804,10 @@ impl WebSocket { let (tx, mut rx) = mpsc::unbounded_channel(); let mut wsreader = WebSocketReader::new(usreader, tx); - let mut wswriter = WebSocketWriter::new(None, self.text, uswriter); + let mut wswriter = WebSocketWriter::new(self.mask, self.text, uswriter); let ws_future = tokio::io::copy(&mut wsreader, &mut dswriter); - let term_future = Self::copy_to_websocket(&mut dsreader, &mut wswriter, &mut rx); + let term_future = self.copy_to_websocket(&mut dsreader, &mut wswriter, &mut rx); let res = select! { res = ws_future.fuse() => match res { @@ -812,7 +817,7 @@ impl WebSocket { res = term_future.fuse() => match res { Ok(sent_close) if !sent_close => { // status code 1000 => 0x03E8 - wswriter.send_control_frame(None, OpCode::Close, &WebSocketErrorKind::Normal.to_be_bytes()).await?; + wswriter.send_control_frame(self.mask, OpCode::Close, &WebSocketErrorKind::Normal.to_be_bytes()).await?; Ok(()) } Ok(_) => Ok(()), -- 2.20.1