From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <d.csapak@proxmox.com>
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 0B1FF6C7ED
 for <pbs-devel@lists.proxmox.com>; Mon,  1 Feb 2021 08:55:22 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id E2C5421DDB
 for <pbs-devel@lists.proxmox.com>; Mon,  1 Feb 2021 08:55:21 +0100 (CET)
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 6DB5D21DA9
 for <pbs-devel@lists.proxmox.com>; Mon,  1 Feb 2021 08:55:20 +0100 (CET)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 3194B4250E
 for <pbs-devel@lists.proxmox.com>; Mon,  1 Feb 2021 08:55:20 +0100 (CET)
From: Dominik Csapak <d.csapak@proxmox.com>
To: pbs-devel@lists.proxmox.com
Date: Mon,  1 Feb 2021 08:55:18 +0100
Message-Id: <20210201075518.21727-4-d.csapak@proxmox.com>
X-Mailer: git-send-email 2.20.1
In-Reply-To: <20210201075518.21727-1-d.csapak@proxmox.com>
References: <20210201075518.21727-1-d.csapak@proxmox.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.241 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
Subject: [pbs-devel] [PATCH xtermjs 3/3] termproxy: rewrite read_ticket_line
X-BeenThere: pbs-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox Backup Server development discussion
 <pbs-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pbs-devel/>
List-Post: <mailto:pbs-devel@lists.proxmox.com>
List-Help: <mailto:pbs-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Mon, 01 Feb 2021 07:55:22 -0000

since we cannot accept a std TcpStream from a mio::net::TcpListener
anymore, we cannot use set_read_timeout here

instead implement the readloop as a mio poll loop similar
to listen_and_accept, otherwise termproxy will busy loop and
consume 100% of a single core during authentication

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 src/main.rs | 43 ++++++++++++++++++++++++++++++++-----------
 1 file changed, 32 insertions(+), 11 deletions(-)

diff --git a/src/main.rs b/src/main.rs
index 07fa1a8..21bd066 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -92,19 +92,40 @@ fn read_ticket_line(
     buf: &mut ByteBuffer,
     timeout: Duration,
 ) -> TicketResult {
-    let now = Instant::now();
-    while !&buf[..].contains(&b'\n') {
-        if buf.is_full() || now.elapsed() >= timeout {
-            io_bail!("authentication data is incomplete: {:?}", &buf[..]);
-        }
-        match buf.read_from(stream) {
-            Ok(n) => {
-                if n == 0 {
-                    io_bail!("connection closed before authentication");
+
+    let mut poll = Poll::new()?;
+    poll.registry().register(stream, Token(0), Interest::READABLE)?;
+    let mut events = Events::with_capacity(1);
+    let mut timeout = timeout;
+
+    loop {
+        let now = Instant::now();
+        poll.poll(&mut events, Some(timeout))?;
+        let elapsed = now.elapsed();
+        if !events.is_empty() {
+            match buf.read_from(stream) {
+                Ok(n) => {
+                    if n == 0 {
+                        io_bail!("connection closed before authentication");
+                    }
                 }
+                Err(err) if err.kind() == ErrorKind::WouldBlock => {}
+                Err(err) => return Err(err),
             }
-            Err(err) if err.kind() == ErrorKind::WouldBlock => {}
-            Err(err) => return Err(err),
+
+            if buf[..].contains(&b'\n') {
+                break;
+            }
+
+            if buf.is_full() {
+                io_bail!("authentication data is incomplete: {:?}", &buf[..]);
+            }
+        }
+
+        if timeout >= elapsed {
+            timeout -= elapsed;
+        } else {
+            io_bail!("timed out");
         }
     }
 
-- 
2.20.1