pikvm/pikvm#1420: VNC: Ignore CUT event 3 seconds after connection

This commit is contained in:
Maxim Devaev 2024-11-04 18:59:50 +02:00
parent 7ef2e16b51
commit 0010dd1d11
4 changed files with 21 additions and 4 deletions

View File

@ -667,9 +667,10 @@ def _get_config_scheme() -> dict:
}, },
"vnc": { "vnc": {
"desired_fps": Option(30, type=valid_stream_fps), "desired_fps": Option(30, type=valid_stream_fps),
"mouse_output": Option("usb", type=valid_hid_mouse_output), "mouse_output": Option("usb", type=valid_hid_mouse_output),
"keymap": Option("/usr/share/kvmd/keymaps/en-us", type=valid_abs_file), "keymap": Option("/usr/share/kvmd/keymaps/en-us", type=valid_abs_file),
"allow_cut_after": Option(3.0, type=valid_float_f0),
"server": { "server": {
"host": Option("", type=valid_ip_or_host, if_empty=""), "host": Option("", type=valid_ip_or_host, if_empty=""),

View File

@ -71,6 +71,7 @@ def main(argv: (list[str] | None)=None) -> None:
desired_fps=config.desired_fps, desired_fps=config.desired_fps,
mouse_output=config.mouse_output, mouse_output=config.mouse_output,
keymap_path=config.keymap, keymap_path=config.keymap,
allow_cut_after=config.allow_cut_after,
kvmd=KvmdClient(user_agent=user_agent, **config.kvmd._unpack()), kvmd=KvmdClient(user_agent=user_agent, **config.kvmd._unpack()),
streamers=streamers, streamers=streamers,

View File

@ -22,6 +22,7 @@
import asyncio import asyncio
import ssl import ssl
import time
from typing import Callable from typing import Callable
from typing import Coroutine from typing import Coroutine
@ -64,6 +65,7 @@ class RfbClient(RfbClientStream): # pylint: disable=too-many-instance-attribute
width: int, width: int,
height: int, height: int,
name: str, name: str,
allow_cut_after: float,
vnc_passwds: list[str], vnc_passwds: list[str],
vencrypt: bool, vencrypt: bool,
none_auth_only: bool, none_auth_only: bool,
@ -79,6 +81,7 @@ class RfbClient(RfbClientStream): # pylint: disable=too-many-instance-attribute
self._width = width self._width = width
self._height = height self._height = height
self.__name = name self.__name = name
self.__allow_cut_after = allow_cut_after
self.__vnc_passwds = vnc_passwds self.__vnc_passwds = vnc_passwds
self.__vencrypt = vencrypt self.__vencrypt = vencrypt
self.__none_auth_only = none_auth_only self.__none_auth_only = none_auth_only
@ -90,6 +93,8 @@ class RfbClient(RfbClientStream): # pylint: disable=too-many-instance-attribute
self.__fb_cont_updates = False self.__fb_cont_updates = False
self.__fb_reset_h264 = False self.__fb_reset_h264 = False
self.__allow_cut_since_ts = 0.0
self.__lock = asyncio.Lock() self.__lock = asyncio.Lock()
# ===== # =====
@ -414,6 +419,7 @@ class RfbClient(RfbClientStream): # pylint: disable=too-many-instance-attribute
# ===== # =====
async def __main_loop(self) -> None: async def __main_loop(self) -> None:
self.__allow_cut_since_ts = time.monotonic() + self.__allow_cut_after
handlers = { handlers = {
0: self.__handle_set_pixel_format, 0: self.__handle_set_pixel_format,
2: self.__handle_set_encodings, 2: self.__handle_set_encodings,
@ -499,7 +505,12 @@ class RfbClient(RfbClientStream): # pylint: disable=too-many-instance-attribute
async def __handle_client_cut_text(self) -> None: async def __handle_client_cut_text(self) -> None:
length = (await self._read_struct("cut text length", "xxx L"))[0] length = (await self._read_struct("cut text length", "xxx L"))[0]
text = await self._read_text("cut text data", length) text = await self._read_text("cut text data", length)
await self._on_cut_event(text) if self.__allow_cut_since_ts > 0 and time.monotonic() >= self.__allow_cut_since_ts:
# We should ignore cut event a few seconds after handshake
# because bVNC, AVNC and maybe some other clients perform
# it right after the connection automatically.
# - https://github.com/pikvm/pikvm/issues/1420
await self._on_cut_event(text)
async def __handle_enable_cont_updates(self) -> None: async def __handle_enable_cont_updates(self) -> None:
enabled = bool((await self._read_struct("enabled ContUpdates", "B HH HH"))[0]) enabled = bool((await self._read_struct("enabled ContUpdates", "B HH HH"))[0])

View File

@ -81,6 +81,7 @@ class _Client(RfbClient): # pylint: disable=too-many-instance-attributes
mouse_output: str, mouse_output: str,
keymap_name: str, keymap_name: str,
symmap: dict[int, dict[int, str]], symmap: dict[int, dict[int, str]],
allow_cut_after: float,
kvmd: KvmdClient, kvmd: KvmdClient,
streamers: list[BaseStreamerClient], streamers: list[BaseStreamerClient],
@ -100,6 +101,7 @@ class _Client(RfbClient): # pylint: disable=too-many-instance-attributes
tls_timeout=tls_timeout, tls_timeout=tls_timeout,
x509_cert_path=x509_cert_path, x509_cert_path=x509_cert_path,
x509_key_path=x509_key_path, x509_key_path=x509_key_path,
allow_cut_after=allow_cut_after,
vnc_passwds=list(vnc_credentials), vnc_passwds=list(vnc_credentials),
vencrypt=vencrypt, vencrypt=vencrypt,
none_auth_only=none_auth_only, none_auth_only=none_auth_only,
@ -444,6 +446,7 @@ class VncServer: # pylint: disable=too-many-instance-attributes
desired_fps: int, desired_fps: int,
mouse_output: str, mouse_output: str,
keymap_path: str, keymap_path: str,
allow_cut_after: float,
kvmd: KvmdClient, kvmd: KvmdClient,
streamers: list[BaseStreamerClient], streamers: list[BaseStreamerClient],
@ -501,6 +504,7 @@ class VncServer: # pylint: disable=too-many-instance-attributes
mouse_output=mouse_output, mouse_output=mouse_output,
keymap_name=keymap_name, keymap_name=keymap_name,
symmap=symmap, symmap=symmap,
allow_cut_after=allow_cut_after,
kvmd=kvmd, kvmd=kvmd,
streamers=streamers, streamers=streamers,
vnc_credentials=(await self.__vnc_auth_manager.read_credentials())[0], vnc_credentials=(await self.__vnc_auth_manager.read_credentials())[0],