HEX
Server: LiteSpeed
System: Linux php-prod-1.spaceapp.ru 5.15.0-157-generic #167-Ubuntu SMP Wed Sep 17 21:35:53 UTC 2025 x86_64
User: xnsbb3110 (1041)
PHP: 8.1.33
Disabled: NONE
Upload Files
File: //usr/local/CyberPanel/lib64/python3.10/site-packages/asyncssh/__pycache__/x11.cpython-310.pyc
o

�hmB�
@sddZddlZddlZddlmZddlZddlZddlmZm	Z	m
Z
mZddlmZm
Z
mZmZmZddlmZddlmZmZdd	lmZmZdd
lmZddlmZddlmZerhdd
lm Z ddl!m"Z"e
e	e#gdfZ$dZ%dZ&dZ'dZ(dZ)dZ*dZ+dZ,dZ-dZ.dZ/dZ0dZ1dZ2dZ3de4dee4e4e5ffdd�Z6dej7d e4d!e5dee4fd"d#�Z8Gd$d%�d%e�Z9Gd&d'�d'e�Z:Gd(d)�d)�Z;Gd*d+�d+�Z<d,e
e4de4fd-d.�Z=d,e4dee9fd/d0�Z>dej7d,e
e4d e4d1e4dee#e#ff
d2d3�Z?d,e
e4d e4d1e4d4e#d5e#ddfd6d7�Z@dej7de4d,e
e4de;fd8d9�ZAd:d;dej7d,e
e4d4e#d5e#de
e<fd<d=�ZBdS)>zX11 forwarding support�N)�Path)�
TYPE_CHECKING�Callable�Dict�Iterable)�
NamedTuple�Optional�Sequence�Set�Tuple�)�OPEN_CONNECT_FAILED)�SSHForwarder�SSHForwarderCoro)�SSHListener�create_tcp_forward_listener)�logger)�ChannelOpenError)�DataType)�
SSHChannel)�SSHServerConnection��i��sMIT-MAGIC-COOKIE-1�z-c�g�������?ip�
�@�	localhost�display�returnc	Cs�z6|�dd�\}}|�d�r|�d�r|dd�}|�d�}|dkr3t||dd��}|d|�}nd}WnttfyDtd	�d�w|||fS)
zParse an X11 display value�:r�[�]����.rNzInvalid X11 display)�rsplit�
startswith�endswith�find�int�
ValueError�UnicodeEncodeError)r�host�dpynum�idx�screen�r0�B/usr/local/CyberPanel/lib/python3.10/site-packages/asyncssh/x11.py�_parse_displayJs
�
�
r2�loopr,�familyc�sH�z|j|d|tjd�IdH}WntjygYSwdd�|D�S)z-Look up IPv4 or IPv6 addresses of a host namer)r4�typeNcSsg|]}|dd�qS)�rr0)�.0�air0r0r1�
<listcomp>hsz _lookup_host.<locals>.<listcomp>)�getaddrinfo�socket�SOCK_STREAM�gaierror)r3r,r4�addrinfor0r0r1�_lookup_host^s�
��r?c@sHeZdZUdZeed<eed<eed<eed<eed<defdd	�Zd
S)�SSHXAuthorityEntryzAn entry in an Xauthority filer4�addrr-�proto�datarcsZdtdtfdd��dtdtf�fdd�}d��|j�||j�||j�||j�||j�f�S)	zConstruct an Xauthority entry�valuercSs|�dd�S)z.Construct a big-endian 16-bit unsigned integer��big)�to_bytes)rDr0r0r1�_uint16wsz-SSHXAuthorityEntry.__bytes__.<locals>._uint16rCcs�t|��|S)z.Construct a binary string with a 16-bit length��len)rC�rHr0r1�_string|�z-SSHXAuthorityEntry.__bytes__.<locals>._string�)r)�bytes�joinr4rAr-rBrC)�selfrLr0rKr1�	__bytes__ts�zSSHXAuthorityEntry.__bytes__N)�__name__�
__module__�__qualname__�__doc__r)�__annotations__rOrRr0r0r0r1r@ks
r@cs�eZdZdZdddef�fdd�Zdedefd	d
�Zdedefdd�Z	e
d
edefdd��Ze
dedefdd��Zdeddfdd�Z
deddfdd�Zdeddfdd�Zddededdf�fdd�
Z�ZS)�SSHX11ClientForwarderz!X11 forwarding connection handler�listener�SSHX11ClientListener�peercsZt��|�||_d|_d|_|j|_d|_d|_d|_	d|_
d|_d|_d|_
d|_dS)NrN�r)�super�__init__�	_listener�_inpbuf�
_bytes_needed�_recv_prefix�
_recv_handler�_endian�_prefix�_auth_proto_len�_auth_data_len�_auth_proto�_auth_proto_pad�
_auth_data�_auth_data_pad)rQrYr[��	__class__r0r1r^�s
zSSHX11ClientForwarder.__init__rDrcCs2|jdkrt|d?|d@f�St|d@|d?f�S)z Encode a 16-bit unsigned integer�B��)rdrO�rQrDr0r0r1�_encode_uint16��
z$SSHX11ClientForwarder._encode_uint16cCs2|jdkr|dd>|dS|dd>|dS)z Decode a 16-bit unsigned integerrnrror)rdrqr0r0r1�_decode_uint16�rsz$SSHX11ClientForwarder._decode_uint16�lengthcCs|dddS)z8Return length rounded up to the next multiple of 4 bytes�r6r0)rur0r0r1�_padded_len�sz!SSHX11ClientForwarder._padded_lenrCcCs&t|�d}||rd|dSdS)z%Pad a string to a multiple of 4 bytesr6�rNrI)rCrur0r0r1�_pad�szSSHX11ClientForwarder._padNcCsV|dd�|_||_|�|dd��|_|�|dd��|_|j|_|�|j�|_dS)zParse X11 client prefixNrrror)	rdrertrfrg�_recv_auth_protorcrwra�rQrCr0r0r1rb�sz"SSHX11ClientForwarder._recv_prefixcCs:|d|j�|_||jd�|_|j|_|�|j�|_dS)zExtract X11 auth protocolN)rfrhri�_recv_auth_datarcrwrgrar{r0r0r1rz�sz&SSHX11ClientForwarder._recv_auth_protocCs�|d|j�|_||jd�|_z
|j�|j�|_WnEty_d}d�tdt|�f�|�	d�|�	d�|�	t|�dd�|�
|�f�}z|�|�|��Wn	t
yYYnwd|_Ynw|j|j|j|j|j|_d|_d|_dS)zExtract X11 auth dataNsInvalid authentication key
rNr�rvr6)rgrjrkr_�
validate_auth�KeyErrorrPrOrJrrry�write�	write_eof�OSErrorr`rerhrircra)rQrC�reason�responser0r0r1r|�s:�
�
�
���
z%SSHX11ClientForwarder._recv_auth_data�datatypecs�|jr7|j|7_|jr1t|j�|jkr,|jd|j�}|j|jd�|_|�|�ndS|js
|j}d|_|rBt��||�dSdS)z(Handle incoming data from the X11 clientNrN)rcr`rJrar]�
data_received)rQrCr�rlr0r1r��s��z#SSHX11ClientForwarder.data_received�N)rSrTrUrVrr^r)rOrrrt�staticmethodrwryrbrzr|rr��
__classcell__r0r0rlr1rX�s	$ rXc@s�eZdZdZdejdedededef
dd�Zd	ed
dde	d
e
eeeffdd�Zd
dd
e	fdd�Z
d
efdd�Zded
efdd�ZdS)rZz8Client listener used to accept forwarded X11 connectionsr3r,r-�
auth_proto�	auth_datacCs�||_||_||_||_|�d�r|j|_|d|f|_n|dvr-|j|_d|f|_n
|j|_|t	t
|�f|_i|_i|_dS)N�/r )��unixz/tmp/.X11-unix/X)
�_host�_dpynumrh�_local_authr&�create_unix_connection�
_connect_coro�
_connect_args�create_connection�
X11_BASE_PORTr)�_remote_auth�_channel)rQr3r,r-r�r�r0r0r1r^s

zSSHX11ClientListener.__init__r�chanr�single_connectionrcCs^t|�\}}}|j|ks|j|krtd��t�t|j��}||j|<||f|j	|<|j
||fS)z!Attach a channel to this listenerz)Already forwarding to another X11 display)r2r�r�r*�os�urandomrJr�r�r�rh)rQrr�r�r,r-r/�remote_authr0r0r1�attachs
zSSHX11ClientListener.attachcCs8z|j�|�}|j|=Wn	tyYnwt|j�S)�#Detach a channel from this listener)r��popr�r�bool)rQr�r�r0r0r1�detach+s�zSSHX11ClientListener.detachc
�sX�z|jtg|j�R�IdH\}}Wnty&}zttt|��d�d}~wwt||�S)z4Forward an incoming connection to the local X serverN)r�rr�r�rr
�strrX)rQ�_r[�excr0r0r1�forward_connection6s����
z'SSHX11ClientListener.forward_connectionr�cCs(|j|\}}|r|j|=|j|=|jS)z7Validate client auth and enforce single connection flag)r�r�r�)rQr�r�r�r0r0r1r~Cs
z"SSHX11ClientListener.validate_authN)rSrTrUrV�asyncio�AbstractEventLoopr�rOr^r�rr)r�r�rXr�r~r0r0r0r1rZs�
�
�
�
rZc@sJeZdZdZdedefdd�Zddded	efd
d�Zddd	e	fdd
�Z
dS)�SSHX11ServerListenerz/Server listener used to forward X11 connections�tcp_listenerrcCs||_||_t�|_dSr�)�
_tcp_listener�_display�set�	_channels)rQr�rr0r0r1r^RszSSHX11ServerListener.__init__r�rr/rcCs|j�|�|j�d|��S)z8Attach a channel to this listener and return its displayr$)r��addr�)rQr�r/r0r0r1r�WszSSHX11ServerListener.attachcCs<z|j�|�Wn	tyYnw|js|j��dSdS)r�TF)r��removerr��close)rQr�r0r0r1r�^s�
zSSHX11ServerListener.detachN)rSrTrUrVrr�r^r)r�r�r�r0r0r0r1r�Os
r��	auth_pathcCs*|stj�d�}|sttdd����}|S)z'Compute the path to the Xauthority file�
XAUTHORITY�~z.Xauthority)r��environ�getr�r�
expanduser)r�r0r0r1�get_xauth_pathms
r�c	#s��dtdtf�fdd��dtf�fdd��dtf��fdd�}zGt|d	��7�	z��}Wn	ty5Ynwzt||�|�|�|��VWntyQtd�d�wq'Wd�WdS1s_wYWdStypYdSw)
z&Walk the entries in an Xauthority file�nrcs��|�}t|�|kr
t�|S)zRead exactly n bytes)�readrJ�EOFError)r�rC)�	auth_filer0r1�_read_bytes|s
zwalk_xauth.<locals>._read_bytescst��d�d�S)zRead a 16-bit unsigned integerrErF)r)�
from_bytesr0)r�r0r1�_read_uint16�rMz walk_xauth.<locals>._read_uint16cs
����S)z
Read a stringr0r0)r�r�r0r1�_read_string�s
z walk_xauth.<locals>._read_string�rbTzIncomplete Xauthority entryN)r)rO�openr�r@r*r�)r�r�r4r0)r�r�r�r1�
walk_xauthys4�

�
�
��&�
�r�r-c	�s*�t|�}|�d�s|dvrt��}|�d�}g}g}t|�D]i}|jr*|j|kr*q|jtkrH|s;t	||tj
�IdH}t�tj
|j�}||v}n6|jt
krf|sYt	||tj�IdH}t�tj|j�}||v}n|jtkrt|j|�d�k}n
|jtkr|d}nd}|r�|j|jfSqt�d�tt�t�fS)	z1Look up Xauthority data for the specified displayr��r�r�r�asciiN�idnaTFz3No xauth entry found for display: using random auth)r�r&r;�gethostname�encoder�r-r4�XAUTH_FAMILY_IPV4r?�AF_INET�	inet_ntoprA�XAUTH_FAMILY_IPV6�AF_INET6�XAUTH_FAMILY_HOSTNAME�XAUTH_FAMILY_WILDrBrCr�debug1�XAUTH_PROTO_COOKIEr�r��XAUTH_COOKIE_LEN)	r3r�r,r-�
ipv4_addrs�
ipv6_addrs�entryrA�matchr0r0r1�lookup_xauth�s<�






�
r�r�r�c
	�sD�|�d�s
|dvrt��}|�d�}t|��d�}t|�}|t}d}zt��t�	|�j
tkr6t�|�Wn	t
y@Ynwtt�D]}zt|d�}Wnty_t�t�IdHYqEw|sgtd��tt||||�}|�t|��t|�D]}	|	j|jks�|	j|jks�|	j|jkr�|�t|	��qz|��t�||�dS)z0Update Xauthority data for the specified displayr�r�r�r�N�xbz!Unable to acquire Xauthority lock) r&r;r�r�r�r��XAUTH_LOCK_SUFFIX�timer��stat�st_ctime�XAUTH_LOCK_DEAD�unlink�FileNotFoundError�range�XAUTH_LOCK_TRIESr��FileExistsErrorr��sleep�XAUTH_LOCK_DELAYr*r@r�r�rOr�r4rAr-r��replace)
r�r,r-r�r��
new_auth_path�new_filer��	new_entryr�r0r0r1�update_xauth�sF�

�����r�c�s8�t|�\}}}t||||�IdH\}}t|||||�S)z>Create a listener to accept X11 connections forwarded over SSHN)r2r�rZ)r3rr�r,r-r�r�r�r0r0r1�create_x11_client_listener�s�r��connrc	�s��ttt�D]H}zt|||jtt|�IdH}Wn	ty!Yqwt�d|��}zt|tt	|�||�IdHWnt
yG|��YdSwt||�SdS)z5Create a listener to forward X11 connections over SSHNr )
r��X11_DISPLAY_START�X11_MAX_DISPLAYSr�create_x11_connection�X11_LISTEN_HOSTr�r�r�r�r*r�r�)r�r3r�r�r�r-r�rr0r0r1�create_x11_server_listener
s,�����r�)CrVr�r��pathlibrr;r��typingrrrrrrr	r
r�	constantsr
�forwardrrrYrr�loggingr�miscr�sessionr�channelr�
connectionrrO�_RecvHandlerr��XAUTH_FAMILY_DECNETr�r�r�r�r�r�r�r�r�r�r�r�r�r�r)r2r�r?r@rXrZr�r�r�r�r�r�r�r0r0r0r1�<module>s��
�
~K)���

�-��
�.��
�
�����