HEX
Server: Apache
System: Linux insrv.serversdaddy.com 5.14.0-362.24.2.el9_3.x86_64 #1 SMP PREEMPT_DYNAMIC Sat Mar 30 14:11:54 EDT 2024 x86_64
User: jsaha (1066)
PHP: 8.3.6
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //lib/python3.9/site-packages/cockpit/__pycache__/protocol.cpython-39.pyc
a

���e~*�@s�ddlZddlZddlZddlZddlmZmZmZddlm	Z	ddl
mZmZm
Z
mZe�e�ZGdd�de�ZGdd	�d	e�ZGd
d�dej�ZGdd
�d
e�ZdS)�N)�ClassVar�Dict�Optional)�systemd_ctypes�)�	JsonError�
JsonObject�get_str�typecheckedcs&eZdZdZed��fdd�Z�ZS)�CockpitProblema�A type of exception that carries a problem code and a message.

    Depending on the scope, this is used to handle shutting down:

      - an individual channel (sends problem code in the close message)
      - peer connections (sends problem code in close message for each open channel)
      - the main stdio interaction with the bridge

    It is usually thrown in response to some violation of expected protocol
    when parsing messages, connecting to a peer, or opening a channel.
    )�problemcs&t��|�d�p|�||_||_dS)N�message)�super�__init__�getr�kwargs)�selfrr��	__class__��4/usr/lib/python3.9/site-packages/cockpit/protocol.pyr+szCockpitProblem.__init__)�__name__�
__module__�__qualname__�__doc__�strr�
__classcell__rrrrrsrcseZdZd�fdd�	Z�ZS)�CockpitProtocolError�protocol-errorcst�j||d�dS)N)r
)rr)rr
rrrrr2szCockpitProtocolError.__init__)r)rrrrrrrrrr1src@sFeZdZUdZejdd�Zeeje	d<dZ
eej
e	d<dZdZee	d	<dZeeje	d
<dd�dd
�Zeedd�dd�Zeedd�dd�Zeeedd�dd�Zeedd�dd�Zedd�dd�Zed�dd�Zd d!�Zd"d#�Zd$d%�Z d4eedd�d&d'�Z!d(d)�Z"d*d+�Z#d,d-�Z$d.d/�Z%eed�d0d1�Z&dd�d2d3�Z'dS)5�CockpitProtocolz�A naive implementation of the Cockpit frame protocol

    We need to use this because Python's SelectorEventLoop doesn't supported
    buffered protocols.
    �)�indent�json_encoderN�	transport�F�_closed�_communication_done)�returncCsdS�Nr�rrrr�do_readyBszCockpitProtocol.do_ready)�excr'cCsdSr(r�rr+rrr�	do_closedEszCockpitProtocol.do_closed)�commandr
r'cCst�dSr(��NotImplementedError)rr.r
rrr�transport_control_receivedHsz*CockpitProtocol.transport_control_received)�channelr.r
r'cCst�dSr(r/)rr2r.r
rrr�channel_control_receivedKsz(CockpitProtocol.channel_control_received)r2�datar'cCst�dSr(r/)rr2r4rrr�channel_data_receivedNsz%CockpitProtocol.channel_data_received)�framer'cCsP|�d�\}}}|dkrB|�d�}t�dt|�|�|�||�n
|�|�dS)N�
r$�asciiz.data received: %d bytes of data for channel %s)�	partition�decode�logger�debug�lenr5�control_received)rr6�header�_r4r2rrr�frame_receivedQs
zCockpitProtocol.frame_received)r4c
Cs�zftt�|�t�}t|d�}t|dd�}|durLt�d|�|�|||�nt�d|�|�||�Wn:tj	t
fy�}ztd|���|�WYd}~n
d}~00dS)Nr.r2zchannel control received %sztransport control received %szcontrol message: )r
�json�loads�dictr	r;r<r3r1ZJSONDecodeErrorrr)rr4r
r.r2r+rrrr>\s
z CockpitProtocol.control_receivedc
Cs|sdSt|�}t|dd��}z|�d�}WnRty�}z:t|�dkrbt|�dWYd}~Std�|�WYd}~n
d}~00zt|d|��}Wn.ty�}ztd�|�WYd}~n
d}~00|d}||}|t|�kr�t|�|S|�|||��|S)z�Consumes a single frame from view.

        Returns positive if a number of bytes were consumed, or negative if no
        work can be done because of a given number of bytes missing.
        rN�
r7zsize line is too longzframe size is not an integerr)�bytes�index�
ValueErrorr=r�intrA)r�viewr?�newliner+�length�start�endrrr�consume_one_framels(  z!CockpitProtocol.consume_one_framecCs6t�d|�||_|��|jr2t�d�|��dS)Nzconnection_made(%s)z;  but the protocol already was closed, so closing transport)r;r<r#r*r%�close)rr#rrr�connection_made�s
zCockpitProtocol.connection_madecCs,t�d�|jdusJ�d|_|�|�dS)N�connection_lost)r;r<r#rPr,rrrrR�s
zCockpitProtocol.connection_lostcCsZ|jr
dSd|_|jr |j��|�|�|jdurV|durJ|j�d�n|j�|�dS)NT)r%r#rPr-r&�
set_resultZ
set_exceptionr,rrrrP�s


zCockpitProtocol.closecCsbt|d�t|�}|�d|�d��d�}|jdurTt�d|j�|j�||�n
t�d�dS)z0Send a given payload (bytes) on channel (string)�
r8Nzwriting to transport %sz cannot write to closed transport)r=�encoder#r;r<�write)rr2ZpayloadZframe_lengthr?rrr�write_channel_data�s
z"CockpitProtocol.write_channel_datacKsdt|�D]&}d|vr||||�dd�<||=qt�d||�tj�|�d}|�||�d��dS)z�Format kwargs as a JSON blob and send as a message
           Any kwargs with '_' in their names will be converted to '-'
        r@�-zsending message %s %srTzutf-8N)�list�replacer;r<rr"rUrW)rZ_channelr�nameZprettyrrr�
write_message�szCockpitProtocol.write_messagecKs|jdi|��dS)N�)r])r\)rrrrr�
write_control�szCockpitProtocol.write_controlc
Csrz>|j|7_|�|j�}|dkr*WdS|j|d�|_qWn.tyl}z|�|�WYd}~n
d}~00dS)Nr)�bufferrOrrP)rr4�resultr+rrr�
data_received�szCockpitProtocol.data_receivedcCsdS)NFrr)rrr�eof_received�szCockpitProtocol.eof_receivedc�s2|jdusJ�t����|_|jIdHd|_dS)z6Wait until communication is complete on this protocol.N)r&�asyncio�get_running_loop�
create_futurer)rrr�communicate�szCockpitProtocol.communicate)N)(rrrrrZJSONEncoderr"rrB�__annotations__r#rrcZ	Transportr_r%�boolr&�Futurer*�	Exceptionr-rrr1r3rFr5rAr>rOrQrRrPrWr\r^rarbrfrrrrr6s.
,	
rc@s�eZdZUdZeeed<dZeeee	j
fed<dd�Zdd�Zeeeedd�d	d
�Z
dd�Zd
d�Zdeeeeed�dd�Zedd�dd�ZdS)�CockpitProtocolServerN�	init_host�authorizationscCst�dSr(r/r)rrr�do_send_init�sz"CockpitProtocolServer.do_send_initcCsdSr(r)rr
rrr�do_init�szCockpitProtocolServer.do_init)�host�groupr'cCst�dSr(r/)rrprqrrr�do_kill�szCockpitProtocolServer.do_killc
Cs|dkr�zt|d�dkr$tdd��Wn^tyV}ztdd�|�WYd}~n8d}~0ty�}ztdd�|�WYd}~n
d}~00z|d|_Wn0ty�}ztd	d�|�WYd}~n
d}~00|�|�nH|d
kr�|�|�d�|�d��n&|dk�r
|�|�ntd
|�d���dS)N�init�versionrzincorrect version numberrzversion field is missingzversion field is not an intrpzmissing host field�killrq�	authorizezunexpected control message z	 received)	rIr�KeyErrorrHrlrorrr�do_authorize)rr.r
r+rrrr1�s$ ""
z0CockpitProtocolServer.transport_control_receivedcCs|��dSr()rnr)rrrr*szCockpitProtocolServer.do_ready)�	challenge�timeoutrr'c	�s~|jduri|_tt���}t����}zB||j|<|jfd||d�|��t�||�IdHW|j�	|�S|j�	|�0dS)Nrv)r.ry�cookie)
rmr�uuidZuuid4rcrdrer^�wait_for�pop)rryrzrr{Zfuturerrr�request_authorizations

�z+CockpitProtocolServer.request_authorization)r
r'cCsf|�d�}|�d�}t|t�r(t|t�s0td��|jdusD||jvrRt�d�dS|j|�|�dS)Nr{�responsezinvalid authorize responsezno matching authorize request)r�
isinstancerrrmr;ZwarningrS)rr
r{r�rrrrxs


z"CockpitProtocolServer.do_authorize)N)rrrrlrrrgrmrrcrirnrorrr1r*rI�objectrrrxrrrrrk�s
rk)rcrBZloggingr|�typingrrrZcockpit._vendorrZjsonutilrrr	r
Z	getLoggerrr;rjrr�Protocolrrkrrrr�<module>s
4