grthtrhthjhtyjytjytkergtrhtrjytjerhrfh4:24 29/09/2026a q©j¤#ã@s˜ddlZddlZddlZddlZddlmZmZmZmZm Z m Z m Z m Z e  e¡ZGdd„deƒZGdd„deƒZGdd „d ejƒZGd d „d eƒZdS) éNé)Ú JsonErrorÚ JsonObjectÚ JsonValueÚ create_objectÚget_intÚget_strÚget_str_or_noneÚ typecheckedcsFeZdZUdZeed<d ededdœ‡fdd„ Zedœd d „Z ‡Z S) Ú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. ÚattrsNúJsonObject | None)ÚproblemÚmsgÚkwargsÚreturnc s.||d<t||ƒ|_tƒ t|jd|ƒ¡dS)NrÚmessage)rr ÚsuperÚ__init__r)Úselfrrr©Ú __class__©ú4/usr/lib/python3.9/site-packages/cockpit/protocol.pyrs zCockpitProblem.__init__©rcCsD|jddkr:|jdur:t|jt |jj|j|jj¡d�S|jSdS)Nrzinternal-error)Úcause)r Ú __cause__ÚdictÚ tracebackÚformat_exceptionrÚ __traceback__©rrrrÚ get_attrs#s  ÿzCockpitProblem.get_attrs)N) Ú__name__Ú __module__Ú __qualname__Ú__doc__rÚ__annotations__Ústrrrr"Ú __classcell__rrrrr s  r cs&eZdZdeedœ‡fdd„ Z‡ZS)ÚCockpitProtocolErrorúprotocol-error)rrcstƒj||d�dS)N)r)rr)rrrrrrr-szCockpitProtocolError.__init__)r+)r#r$r%r(rr)rrrrr*,sr*c@s8eZdZUdZdZded<dZdZeed<dZ ded <dd œd d „Z d ddœdd„Z e e ddœdd„Ze e e ddœdd„Ze eddœdd„Zeddœdd„Zeddœdd„Zeedœd d!„Zejdd"œd#d$„Zd ddœd%d&„Zd4d ddœd'd(„Ze edd)œd*d+„Zd5d,edd-œd.d/„Zeddœd0d1„Zed œd2d3„ZdS)6ÚCockpitProtocolzŸA naive implementation of the Cockpit frame protocol We need to use this because Python's SelectorEventLoop doesn't supported buffered protocols. Nzasyncio.Transport | NoneÚ transportóFÚ_closedzasyncio.Future[None] | NoneÚ_communication_donercCsdS©Nrr!rrrÚdo_ready<szCockpitProtocol.do_readyzException | None)ÚexcrcCsdSr1r©rr3rrrÚ do_closed?szCockpitProtocol.do_closed©ÚcommandrrcCst‚dSr1©ÚNotImplementedError©rr7rrrrÚtransport_control_receivedBsz*CockpitProtocol.transport_control_received)Úchannelr7rrcCst‚dSr1r8)rr<r7rrrrÚchannel_control_receivedEsz(CockpitProtocol.channel_control_received)r<ÚdatarcCst‚dSr1r8)rr<r>rrrÚchannel_data_receivedHsz%CockpitProtocol.channel_data_received)ÚframercCsP| d¡\}}}|dkrB| d¡}t dt|ƒ|¡| ||¡n | |¡dS)Nó r.Úasciiz.data received: %d bytes of data for channel %s)Ú partitionÚdecodeÚloggerÚdebugÚlenr?Úcontrol_received)rr@ÚheaderÚ_r>r<rrrÚframe_receivedKs  zCockpitProtocol.frame_received)r>rc Cs¦zftt |¡tƒ}t|dƒ}t|ddƒ}|durLt d|¡| |||¡nt d|¡| ||¡Wn:tj t fy }zt d|›�ƒ|‚WYd}~n d}~00dS)Nr7r<zchannel control received %sztransport control received %szcontrol message: ) r ÚjsonÚloadsrrrErFr=r;ZJSONDecodeErrorrr*)rr>rr7r<r3rrrrHVs    z CockpitProtocol.control_receivedc Csä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. rAé Nzsize line is too longzframe size is not an integerr)ÚindexÚ ValueErrorrGr*ÚintrK)rr>Únewliner3ÚlengthÚstartÚendrrrÚconsume_one_framefs      z!CockpitProtocol.consume_one_frame)r-rcCsFt d|¡t|tjƒsJ‚||_| ¡|jrBt d¡| ¡dS)Nzconnection_made(%s)z; but the protocol already was closed, so closing transport) rErFÚ isinstanceÚasyncioZ Transportr-r2r/Úclose)rr-rrrÚconnection_made…s  zCockpitProtocol.connection_madecCs,t d¡|jdusJ‚d|_| |¡dS)NÚconnection_lost)rErFr-rYr4rrrr[�s zCockpitProtocol.connection_lostcCs.|jr dSd|_|jr |j ¡| |¡dS)NT)r/r-rYr5r4rrrrY•s  zCockpitProtocol.close)r<ÚpayloadrcCsbt|dƒt|ƒ}|›d|›d� d¡}|jdurTt d|j¡|j ||¡n t d¡dS)z0Send a given payload (bytes) on channel (string)Ú rBNzwriting to transport %sz cannot write to closed transport)rGÚencoder-rErFÚwrite)rr<r\Z frame_lengthrIrrrÚwrite_channel_dataŸs  z"CockpitProtocol.write_channel_datar )rrrcKs:t d||¡tjt||ƒdd�d}| d| ¡¡dS)zCWrite a control message. See jsonutil.create_object() for details.zsending control message %r %ré)Úindentr]ÚN)rErFrLÚdumpsrr`r^)rrrZprettyrrrÚ write_controlªszCockpitProtocol.write_controlc CsxzD|j|7_|jrB| |j¡}|dkr0WdS|j|d…|_qWn.tyr}z| |¡WYd}~n d}~00dS)Nr)ÚbufferrVr*rY)rr>Úresultr3rrrÚ data_received°s zCockpitProtocol.data_receivedcCsdS)NFrr!rrrÚ eof_received»szCockpitProtocol.eof_received)N)N) r#r$r%r&r-r'rfr/Úboolr0r2r5r(rr;r=Úbytesr?rKrHrQrVrXZ BaseTransportrZr[rYr`rrerhrirrrrr,1s(         r,c@s¸eZdZUdZded<dZded<dZddœdd „Zedd œd d „Z ddedd œdd„Z e eddœdd„Z ddœdd„Z de deedœdd„Zde dee dœdd„Zedd œdd„ZdS)ÚCockpitProtocolServerNz str | NoneÚ init_hostz,dict[str, asyncio.Future[JsonObject]] | NoneÚauthorizationsrrcCst‚dSr1r8r!rrrÚ do_send_initÅsz"CockpitProtocolServer.do_send_init)rrcCsdSr1r)rrrrrÚdo_initÈszCockpitProtocolServer.do_init)ÚhostÚgrouprrcCst‚dSr1r8)rrqrrrrrrÚdo_killËszCockpitProtocolServer.do_killr6cCs†|dkr6t|dƒdkrtdƒ‚t|dƒ|_| |¡nL|dkr^| t|ddƒt|ddƒ|¡n$|dkrr| |¡ntd |›d �ƒ‚dS) NÚinitÚversionrzincorrect version numberrqÚkillrrÚ authorizezunexpected control message z received)rr*rrmrprsr Ú do_authorizer:rrrr;Îs    z0CockpitProtocolServer.transport_control_receivedcCs | ¡dSr1)ror!rrrr2ÛszCockpitProtocolServer.do_readyz int | None)Ú challengeÚtimeoutrrc ËsŠ|jduri|_t|jƒ}|jd7_t ¡ ¡}zB||j|<|jdd||dœ|¤Žt ||¡IdHW|j |¡S|j |¡0dS)Nrrw)r7ryÚcookie)N) rnr(Ú next_auth_idrXZget_running_loopZ create_futurereÚwait_forÚpop)rryrzrr{ZfuturerrrÚrequest_authorization_objectßs     þz2CockpitProtocolServer.request_authorization_objectcËs t|j||fi|¤ŽIdHdƒS)NZresponse)rr)rryrzrrrrÚrequest_authorizationîsz+CockpitProtocolServer.request_authorizationcCs@t|dƒ}|jdus||jvr,t d¡dS|j| |¡dS)Nr{zno matching authorize request)rrnrEZwarningZ set_result)rrr{rrrrxós   z"CockpitProtocolServer.do_authorize)N)N)r#r$r%rmr'rnr|rorrprsr(r;r2rrr€rxrrrrrlÀs&    ÿþ ÿþ rl)rXrLZloggingrZjsonutilrrrrrrr r Z getLoggerr#rEÚ Exceptionr r*ÚProtocolr,rlrrrrÚs(