a h1@sddlZddlZddlZddlmZmZmZmZddlm Z m Z ddl m Z ddl mZmZmZddlmZmZmZddlmZmZeeZGd d d eZGd d d eZGd ddeeeZGdddeZGdddeZ GdddeZ!dS)N)CallableListOptionalSequence) JsonObject JsonValue) BridgeConfig)CockpitProblemCockpitProtocolCockpitProtocolError)EndpointRouter RoutingRule)SubprocessProtocolSubprocessTransportc@s eZdZdS) PeerErrorN)__name__ __module__ __qualname__rr0/usr/lib/python3.9/site-packages/cockpit/peer.pyr src@seZdZedddZdS) PeerExited exit_codecCs ||_dSNr)selfrrrr__init__%szPeerExited.__init__N)rrrintrrrrrr$srcseZdZUeegdfed<eejed<e dfdd Z dddd Z e e e e ejd d d Zd4ee eed ddZd5ee edd ddZegdfddddZddddZeddddZe eddddZedddZeeddd d!Zddd"d#Ze e edd$d%d&Ze edd'd(d)Ze e edd$d*d+Z e edd'd,d-Z!d.d.edd/d0d1Z"ddd2d3Z#Z$S)6PeerNdone_callbacks init_future)routercs,t||t|_g|_dSr)superrZfreeze_endpointasyncioget_running_loopZ create_futurer!r )rr" __class__rrr-s z Peer.__init__returncstdSr)NotImplementedErrorrrrrdo_connect_transport7szPeer.do_connect_transport)argvenvr)cs@t}tdd|D}t|||fdttjfi|i|S)Ncss|]}|ddVqdS)=rN)split).0errr =zPeer.spawn..r.)r$r%dictrosenviron)rr-r.kwargsZloopZuser_envrrrspawn:sz Peer.spawn) init_hostr8r)c sjdusJtjddfdd }t|zNzjIdH}Wn"ttfyrIdHYn0Wd_s nd_s 0|durt d|j d dd|d| |S) aRequest that the Peer is started and connected to the router. Creates the transport, connects it to the protocol, and participates in exchanging of init messages. If anything goes wrong, the connection will be closed and an exception will be raised. The Peer starts out in a frozen state (ie: attempts to send messages to it will initially be queued). If init_host is not None then an init message is sent with the given 'host' field, plus any extra kwargs, and the queue is thawed. Otherwise, the caller is responsible for sending the init message and thawing the peer. In any case, the return value is the init message from the peer. Ntaskr)c s^|us Jz |Wn@tjy,Yn.tyX}z|WYd}~n d}~00dSr)resultr$CancelledError Exceptionclose)r<excZ connect_taskrrr_connect_task_doneQs  z&Peer.start.._connect_task_donez$ sending init message back, host %sinitr)commandversionhost)N)r!r$Task create_taskr,add_done_callbackrBrokenPipeErrordonecancelloggerdebug write_controlZ thaw_endpoint)rr:r8rCZ init_messagerrBrstart@s(       z Peer.startc s<tjddfdd }t|j|fi||dS)Nr;c s:|us Jz |Wnttttjfy4Yn0dSr)r=OSErrorrr r$r>)r<Z start_taskrr_start_task_dones   z2Peer.start_in_background.._start_task_done)r$rHrIrQrJ)rr:r8rTrrSrstart_in_backgroundszPeer.start_in_background)callbackr)cCs|j|dSr)r append)rrVrrrrJszPeer.add_done_callbackcCsdSrrr+rrrdo_superuser_init_doneszPeer.do_superuser_init_done)messager)cCsdSrr)rrYrrr do_authorizeszPeer.do_authorize)rErYr)cCsb|dkr*|jdur*td|j|n4|dkr>||n |dkrP|ntd|dS)NrDz:Got init message with active init_future. Setting result.Z authorizezsuperuser-init-donez$Received unexpected control message )r!rNrOZ set_resultrZrXr )rrErYrrrtransport_control_receiveds   zPeer.transport_control_receivedcCstd|jjdS)NzPeer %s received unexpected EOFT)rNrOr'rr+rrr eof_receivedszPeer.eof_received)rAr)cCstd|jjt|||dur.|jddnpt|trh|jdkrP|jddq|jdd|jdn6t|t r||j n|jdd |jjd |d|j dur|j s|dur|j |n |j |jD] }|qdS) NzPeer %s connection lost %s %sZ terminated)problemz no-cockpitzPeer exited with status )r]rYzinternal-error[z] )rNrOr'rtypeZshutdown_endpoint isinstancerrr attrsr!rLZ set_exceptionrMr )rrArVrrr do_closeds$     zPeer.do_closedcCsRt|jtsJtd|jj|j|j}t|ts@J| t |dS)NzPeer %s exited, status %d) raZ transportrrNrOr'rZget_returncoderr@r)r returncoderrrprocess_exiteds  zPeer.process_exited)channelrErYr)cCs$|jdurtd||||dS)Nz7Received unexpected channel control message before init)r!r Zsend_channel_controlrrfrErYrrrchannel_control_receiveds zPeer.channel_control_received)rfdatar)cCs"|jdurtd|||dS)Nz,Received unexpected channel data before init)r!r Zsend_channel_datarrfrirrrchannel_data_receiveds zPeer.channel_data_receivedcCs|jdusJ||dSrr!rPrgrrrdo_channel_controlszPeer.do_channel_controlcCs|jdusJ|||dSr)r!Zwrite_channel_datarjrrrdo_channel_dataszPeer.do_channel_dataz str | None)rGgrouprYr)cCs|jdusJ||dSrrl)rrGrorYrrrdo_killsz Peer.do_killcCs |dSr)r@r+rrrdo_closesz Peer.do_close)N)N)%rrrrr__annotations__rr$ZFuturerrr,rstrZ Transportr9rrrQrUrJrXrZr[boolr\r?rcrerhbytesrkrmrnrprq __classcell__rrr&rr)s(  A   rcsTeZdZUeed<eeed<eeed<eedfdd Zddd d Z Z S) ConfiguredPeerconfigargsr.r"rxcs&||_|j|_|j|_t|dSr)rxr9ryr7r.r#rrr"rxr&rrrszConfiguredPeer.__init__Nr(cs||j|jIdHdSr)r9ryr.r+rrrr,sz#ConfiguredPeer.do_connect_transport) rrrr rrrrsrrr,rvrrr&rrws   rwcsreZdZUeed<eed<eeed<eedfdd Z eeeddd Z d d d d Z d d ddZ Z S)PeerRoutingRulerxmatchpeerrzcs$t|||_|j|_d|_dSr)r#rrxr}r~r{r&rrrs zPeerRoutingRule.__init__optionsr)cCs|jD]P\}}||vr,td|dS|dur |||kr td||||dSq |jdurt|j|j|_|j|j |jj sJ|jj |jj d|jS)Nz+ rejecting because key %s is missingz; rejecting because key %s has wrong value %s (vs %s))r:) r}itemsrNrOr~rwr"rxrJ peer_closedr:rU)rrkeyvaluerrr apply_rules   zPeerRoutingRule.apply_ruleNr(cCs d|_dSr)r~r+rrrrszPeerRoutingRule.peer_closedcCs|jdur|jdSr)r~r@r+rrrshutdowns zPeerRoutingRule.shutdown)rrrr rrrrrrrrrrrvrrr&rr|s  r|c@sTeZdZUgZeeed<eee dddZ e e ddddZ dd d d ZdS) PeersRoutingRulerulesrcCs`tdt|j|jD]8}td|jj||}|durtd|SqtddS)Nz considering %d rulesz considering %sz selectedz no peer rules matched)rNrOlenrrxnamer)rrruleZendpointrrrr%s     zPeersRoutingRule.apply_ruleN)bridge_configsr)cCs||j}g|_|D]T}|jsd|jvr&qt|D]}|j|kr.||qXq.t|j|}|j|q|D] }| qjdS)NrG) rZ privilegedr}listrxremover|r"rWr)rrZ old_rulesrxrrrr set_configs0s    zPeersRoutingRule.set_configsr(cCs|jD] }|qdSr)rr)rrrrrrHs zPeersRoutingRule.shutdown)rrrrrr|rrrrr rrr rrrrrrr"s  r)"r$Zloggingr6typingrrrrZjsonutilrrZpackagesr Zprotocolr r r r"r rrZ transportsrrZ getLoggerrrNrr?rrrwr|rrrrrs   D'