
    iW@                         d dl Z d dlmZmZ d dlZd dlZd dlZd dlZd dlZd dlm	Z	 ddl
mZ ddlmZ  ej                    Z ej                    Z G d d          ZdS )	    N)datetimetimezone)parse_qs   )EventBuffer)ConnectionRefusedErrorc                       e Zd Z	 	 ddZd Zd Zd	 Zd
 ZddZddZ	ddZ
d Zd Zd ZddZd Z	 	 ddZd Zd ZddZd Zd Zd Zd ZddZdS )InstrumentedAsyncServerN/adminFdevelopment   c                 f   |t          d          || _        || _        || _        || _        |p1t          | j        j        d          r| j        j        j        nt          | _	        || _
        || _        g | _        t                      | _        d| _        d| _        |                                  dS )zInstrument the Socket.IO server for monitoring with the `Socket.IO
        Admin UI <https://socket.io/docs/v4/admin-ui/>`_.
        Nzauth must be specifiedhost_id)
ValueErrorsioauthadmin_namespace	read_onlyhasattrmanagerr   HOSTNAME	server_idmodeserver_stats_intervaladmin_queuer   event_bufferstop_stats_event
stats_task
instrument)selfr   r   	namespacer   r   r   r   s           ?/usr/local/lib/python3.11/dist-packages/socketio/async_admin.py__init__z InstrumentedAsyncServer.__init__   s    
 <5666	("" 
(/0@)(L(L DH$$ 	 	%:"'MM !% 	    c                    | j                             d| j        | j                   | j        dk    r}| j        s| j                             d| j        | j                   | j                             d| j        | j                   | j                             d| j        | j                   | j                             d| j	        | j                   i | j         j
        _        | j         j        | j         _        | j        | j         _        | j         j
        j        | j         j
        _        | j        | j         j
        _        | j         j
        j        | j         j
        _        | j        | j         j
        _        | j         j
        j        | j         j
        _        | j        | j         j
        _        | j         j                            d| j                   | j         j                            d| j                   d	d
lm} | j         j        j        | j         j        _        | j        | j         j        _        |j        |_         tC          j"        | j#        j$        |           |_        |j%        |_&        tC          j"        | j#        j'        |           |_%        | j        dk    r2|j(        |_)        tC          j"        | j#        j*        |           |_(        d S d S )Nconnectr!   r   emitjoinleave_disconnect
disconnectr   AsyncSocket)+r   onadmin_connectr   r   r   
admin_emitadmin_enter_roomadmin_leave_roomadmin_disconnectr   _timestamps_trigger_event'_InstrumentedAsyncServer__trigger_eventbasic_enter_room*_InstrumentedAsyncServer__basic_enter_room_basic_enter_roombasic_leave_room*_InstrumentedAsyncServer__basic_leave_room_basic_leave_roomr(   _InstrumentedAsyncServer__emit_emiteio_handle_eio_connect_handle_eio_disconnectengineio.async_socketr.   _ok_InstrumentedAsyncServer__ok_eio_http_responsehandle_post_request-_InstrumentedAsyncServer__handle_post_request	functoolspartialmethod	__class___eio_handle_post_request_websocket_handler+_InstrumentedAsyncServer__websocket_handler_eio_websocket_handler
_send_ping#_InstrumentedAsyncServer__send_ping_eio_send_pingr    r.   s     r"   r   z"InstrumentedAsyncServer.instrument,   s   It1"2 	 	4 	4 	4 9%%> <FDO&*&:  < < <FD$9&*&:  < < <GT%:&*&:  < < <M4+@&*&:  < < < ,.DH( (,x'>DH$&*&9DH#  1 H/040FDH-  1 H/040FDH- '+h&6&;DH#$(JDH! 		4#;<<<d&ABBB 	655555 HL,2,7,K)*3*AN3T+; +;' +6*H')2)@N14*9 *9& 9%%&1&<K#%.%<-t&5 &5K""" &%r$   c                    | j         dk    rv| j        j        | j        _        | j        j        j        | j        j        _        | j        j        j        | j        j        _        | j        j        j	        | j        j        _
        | j        j        j        | j        j        _        ddlm} |j        |_        |j        |_        | j         dk    r|j        |_        d S d S )Nr   r   r-   )r   r   r7   r6   r   r9   r8   r<   r;   r>   r(   r@   rE   rD   rC   r.   rH   rG   rN   rM   rQ   rP   rS   s     r"   uninstrumentz$InstrumentedAsyncServer.uninstrumentg   s    9%%&*h&>DH# 3 H-  3 H-$(H$4$;DH!8<,555555*5*K')4)H&9%%%0%<K""" &%r$   c                    K   d} j         rd}t           j         t                    r| j         k    }nnt           j         t                    r
| j         v }nJt	          j         j                   r                      |           d {V }n                      |          }|st          d           fd} j                            ||           d S )NTFzauthentication failedc           	      V  K   j                             d           d {V  dg}j        s|g dz  }j        dk    r|                    d           j                             dd|i| j                   d {V  j        dk    rg }j         j                                        D ]R}j         j        	                    |d           D ]/\  } }|                    
                    | ||                     0Sj                             d	|| j                   d {V  d S d S )
Ng?AGGREGATED_EVENTS)EMITJOINLEAVE
DISCONNECTMJOINMLEAVEMDISCONNECTr   
ALL_EVENTSconfigsupportedFeaturestor!   all_sockets)r   sleepr   r   appendr(   r   r   get_namespacesget_participantsserialize_socket)sidfeaturesre   nspeio_sidr    s        r"   ra   z5InstrumentedAsyncServer.admin_connect.<locals>.config   s     (..%%%%%%%%% ,,H> 6 6 6 6 6yM))---(--+>*I#&$2F   H H H H H H H H H yM)) 8+::<< F FC(,(8(I(I)' )' F FW#** 11#sGDDF F F FF hmmM;3.2.B $ D D D D D D D D D D D *)r$   )	r   
isinstancedictlistinspectiscoroutinefunctionr   r   start_background_task)r    rk   environclient_authauthenticatedra   s   `     r"   r0   z%InstrumentedAsyncServer.admin_connectw   s     9 	F!M$)T** ; +ty 8DIt,, ; +ty 8.ty99 ;*.))K*@*@$@$@$@$@$@$@MM$(IIk$:$:M  F,-DEEE	D 	D 	D 	D 	D0 	&&vs33333r$   c                 R   K   | j                             ||||           d {V  d S )Nrc   )r   r(   )r    _r!   room_filtereventdatas         r"   r1   z"InstrumentedAsyncServer.admin_emit   s:      hmmE4K9mMMMMMMMMMMMr$   c                    K   | j         j                            ||          D ](\  }}| j                             |||           d {V  )d S Nr'   )r   r   ri   
enter_roomr    ry   r!   roomrz   rk   s         r"   r2   z(InstrumentedAsyncServer.admin_enter_room   s      h&77;( ( 	F 	FFC(%%c49%EEEEEEEEEE	F 	Fr$   c                    K   | j         j                            ||          D ](\  }}| j                             |||           d {V  )d S r~   )r   r   ri   
leave_roomr   s         r"   r3   z(InstrumentedAsyncServer.admin_leave_room   r   r$   c                    K   | j         j                            ||          D ]'\  }}| j                             ||           d {V  (d S r~   )r   r   ri   r,   )r    ry   r!   closerz   rk   s         r"   r4   z(InstrumentedAsyncServer.admin_disconnect   sq      h&77;( ( 	@ 	@FC(%%cY%??????????	@ 	@r$   c                    K   | j         rZ| j                                         t          j        | j                    d {V  d | _         | j                                         d S d S N)r   r   setasynciogatherclear)r    s    r"   shutdownz InstrumentedAsyncServer.shutdown   su      ? 	*!%%'''.111111111"DO!'')))))		* 	*r$   c           
        K   t          j                     }|d         }|dk    r| j        j                            ||          }|| j        j        j        |<   |                     |||          }| j                            d|t          j        |t          j
                                                  f| j                   d {V  | j        j                            |          j        s"| j                            | j        |||           n|dk    rv| j        j        j        |= |d         }| j                            d|||t          j        |t          j
                                                  f| j                   d {V  ng| j                            d|||g|dd          R t          j        |t          j
                                                  f| j                   d {V   | j        j        ||g|R   d {V S )	Nr   r&   socket_connectedr'   r,   r   socket_disconnectedevent_received)timer   r   eio_sid_from_sidr5   rj   r(   r   fromtimestampr   utc	isoformatr   r@   _get_socketupgradedrt   _check_for_upgrader7   )	r    r{   r!   argstrk   rn   serialized_socketreasons	            r"   r6   z&InstrumentedAsyncServer._trigger_event   st     IKK1gIh&77YGGG01DH(- $ 5 5c9g N N(-- 2!&q(,77AACC5 -   / / / / / / / / / 8<++G44= F..+Wc9F F Fl"" ,S1!WF(-- 5&q(,77AACC	8
 -   / / / / / / / / / / (-- 0"abb""&q(,77AACC	3
 -   / / / / / / / / / .TX-eYFFFFFFFFFFFr$   c                 6  K   t          d          D ]}| j                            d           d {V  	 | j        j                            |          j        r/| j                            d||dd| j                   d {V   d S w# t          $ r Y w xY wd S )N   socket_updated	websocket)idrm   	transportr'   )	ranger   rf   r@   r   r   r(   r   KeyError)r    rn   rk   r!   ry   s        r"   r   z*InstrumentedAsyncServer._check_for_upgrade   s      q 	 	A(..#########	8<++G44= (--(8!(%0; ; "&!5	 ( 7 7 7 7 7 7 7 7 7
 EE    	 	s   AB		
BBc           	          | j         j                            ||||          }|rN| j                            d|||t          j        t          j                  	                                ff           |S )Nroom_joined)
r   r   r9   r   rg   r   nowr   r   r   )r    rk   r!   r   rn   rets         r"   r:   z)InstrumentedAsyncServer._basic_enter_room   s    h11#y$29; ; 	##]X\**4466	5 %    
r$   c           	          |rN| j                             d|||t          j        t          j                                                  ff           | j        j        	                    |||          S )N	room_left)
r   rg   r   r   r   r   r   r   r   r<   )r    rk   r!   r   s       r"   r=   z)InstrumentedAsyncServer._basic_leave_room   st     	##[X\**4466	3 %    x223	4HHHr$   c           
        K    | j         j        j        |||f|||d| d {V }|| j        k    rt	          |t
                    r|gt          |          z   n||g}	t	          |t                    s|g}| j         j                            ||          D ]c\  }
}|
|vrZ| j                             d||
|	t          j
        t          j                                                  f| j                   d {V  d|S )N)r   skip_sidcallback
event_sentr'   )r   r   r>   r   ro   tuplerq   ri   r(   r   r   r   r   r   )r    r{   r|   r!   r   r   r   kwargsr   
event_datark   ry   s               r"   r?   zInstrumentedAsyncServer._emit   s`     +DH$+4))-) )!') ) ) ) ) ) ) ) ,,,1;D%1H1H #%4::--T] h-- &$:(*;;ItLL 7 7Qh&&(--!" X\22<<>>	7
 "&!5 ( 7 7 7 7 7 7 7 7 7 
r$   c                 &  K   | j         #| j        j                                        | _         | j        $| j                            | j                  | _        | j                            d           | j        	                    ||           d {V S )NrawConnection)
r   r   r@   create_eventr   rt   _emit_server_statsr   pushrA   )r    rn   ru   s      r"   rA   z+InstrumentedAsyncServer._handle_eio_connect  s       ($(HL$=$=$?$?D!?""h<<') )DO 	///X11'7CCCCCCCCCr$   c                 |   K   | j                             d           | j                            ||           d {V S )NrawDisconnection)r   r   r   rB   )r    rn   r   s      r"   rB   z.InstrumentedAsyncServer._handle_eio_disconnect  sH      1222X44WfEEEEEEEEEr$   c                     | j         j                            |||          }| j                            d           | j                            dt          |d                              |S )N)packetsheadersjsonp_index
packetsOutbytesOutresponse)r   r@   rE   r   r   len)r    r   r   r   r   s        r"   rF   z*InstrumentedAsyncServer._eio_http_response!  sh    hl,7   9 9|,,,z3s:+?+?@@@
r$   c           	         K    | j         |           d {V }|j                            d           |j                            dt          |                    dd                               |S )N	packetsInbytesInCONTENT_LENGTHr   )rH   r   r   intget)socketr    ru   r   s       r"   rL   z0InstrumentedAsyncServer._eio_handle_post_request(  s}      0F099999999{+++s7;;'7;;<<	> 	> 	>
r$   c                    K   fd}fd}|j         |_        t          j        ||          |_         |j        |_        t          j        ||          |_         | j        |           d {V S )Nc                    K   j                             d           j                             dt          |                     |                     |           d {V S )Nr   r   )r   r   r   _InstrumentedAsyncServer__send)wsr|   r    s     r"   _sendz=InstrumentedAsyncServer._eio_websocket_handler.<locals>._send0  s_      ""<000"":s4yy9994(((((((r$   c                    K   |                                   d {V }j                            d           j                            dt          |pd                     |S )Nr   r    )_InstrumentedAsyncServer__waitr   r   r   )r   r   r    s     r"   _waitz=InstrumentedAsyncServer._eio_websocket_handler.<locals>._wait5  se      		######C"";///""9c#)nn===Jr$   )sendr   rI   partialwaitr   rN   )r   r    r   r   r   s    `   r"   rO   z.InstrumentedAsyncServer._eio_websocket_handler/  s      	) 	) 	) 	) 	)
	 	 	 	 	 G	#E2..G	#E2../V/333333333r$   c           	        K   | j         }t          j                    }|j        j                                        D ]}|j        j                            ||          }|rp|                    |||          }|j                            d|t          j	        |t          j                                                  f|j                   d {V   | j                     d {V S )Nr   r'   )rk   r   r   r   rh   sid_from_eio_sidrj   r(   r   r   r   r   r   r   rQ   )r   r    rn   r   r!   rk   r   s          r"   rR   z&InstrumentedAsyncServer._eio_send_pingA  s     *IKK)88:: 	3 	3I("33GYGGC 3$($9$9#y:A%C %C!hmm$6%*1hl;;EEGG9 "1 $ 3 3 3 3 3 3 3 3 3 (V')))))))))r$   c                    K   t          j                     }t           j        j                                                  }|                                  j                                        s_ j                             j	                   d {V   j        
                    d j        t          t          t          j                     |z
  t           j        j        j                  t          d  j        j        j                                        D                        j                                         fd|D             d j                   d {V   j        rL j                            d          \  }} j        
                    || j                   d {V   j        L j                                        ]d S d S )Nserver_statsc                      g | ]}|j         	|S  )r   ).0ss     r"   
<listcomp>z>InstrumentedAsyncServer._emit_server_stats.<locals>.<listcomp>\  s.     ( ( (1J(Q ( ( (r$   c           
          g | ]M}|t          j        j        j                            |d g i                              d g                     dNS )N)namesocketsCount)r   r   r   roomsr   )r   rm   r    s     r"   r   z>InstrumentedAsyncServer._emit_server_stats.<locals>.<listcomp>_  sr     ) ) )   $'(8(>(B(BdBZ)) ))),T2%8 %8    ) ) )r$   )serverIdhostnamepiduptimeclientsCountpollingClientsCountaggregatedEvents
namespacesr'   r   )r   rq   r   handlerskeyssortr   is_setrf   r   r(   r   r   PIDr   r@   socketsvaluesr   get_and_clearr   r   pop)r    
start_timer   r{   r   s   `    r"   r   z*InstrumentedAsyncServer._emit_server_statsO  s(     Y[[
$(+002233
'..00 	D(..!;<<<<<<<<<(-- N$)++
2 #DHL$8 9 9'*( ( 4 ; ; = = ( ( (() () %)$5$C$C$E$E) ) ) ) (	) ) )1 1 -   / / / / / / / / /  " D".22155thmmE4.2.B $ D D D D D D D D D " D% '..00 	D 	D 	D 	D 	Dr$   c                 *   || j         j                            |          }| j         j                            |          }| j         j                            |i           }|| j         j        j        v r| j         j        j        |         nd}|||j        rdnd|i |                    dd          d |	                                D             d t          |                    dd                    	                                D             |                    d	d          d
k    |                    dd          |dz  |r1t          j        |t          j                                                  ndd| j         j                            ||          dS )Nr   r   pollingREMOTE_ADDRr   c                 x    i | ]7\  }}|                     d           |dd                                         |8S )HTTP_r   N)
startswithlowerr   kvs      r"   
<dictcomp>z<InstrumentedAsyncServer.serialize_socket.<locals>.<dictcomp>y  sN     6 6 6A ||G446AabbEKKMM1 6 6 6r$   c                 P    i | ]#\  }}|t          |          d k    r|d         n|$S )r   r   )r   r   s      r"   r   z<InstrumentedAsyncServer.serialize_socket.<locals>.<dictcomp>{  s@     > > >DAq!SVVq[[QqTTa > > >r$   QUERY_STRINGzwsgi.url_schemehttps	PATH_INFOi  )addressr   querysecureurlissuedr   )r   clientIdr   rm   r|   	handshaker   )r   r   r   r@   r   ru   r   r5   r   itemsr   r   r   r   r   r   	get_rooms)r    rk   r!   rn   r   ru   tms          r"   rj   z(InstrumentedAsyncServer.serialize_socketj  s   ?h&77<<G))'22("&&w3325H(3) 3)TX)#.../ 	 (.FY";;}b996 6 6 6 6> >HKK33E5 E55:UWW> > >!++&7<<G{{;33t).r8<@@JJLLL  X%//Y??%
 
 	
r$   )Nr   FNr   r   r   )NNN)__name__
__module____qualname__r#   r   rU   r0   r1   r2   r3   r4   r   r6   r   r:   r=   r?   rA   rB   rF   rL   rO   rR   r   rj   r   r$   r"   r
   r
      s       EJKL   695 95 95v= = = (4 (4 (4TN N NF F F F
F F F F
@ @ @ @
* * *G G G@  
 
 
 
I I I GK!   (D D DF F F     4 4 4$* * *D D D6
 
 
 
 
 
r$   r
   )r   r   r   rI   rr   osr   r   urllib.parser   adminr   
exceptionsr   gethostnamer   getpidr   r
   r   r$   r"   <module>r     s     ' ' ' ' ' ' ' '      				   ! ! ! ! ! !       . . . . . .6bikkt
 t
 t
 t
 t
 t
 t
 t
 t
 t
r$   