
    wg?                         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mZ ddl	m
Z
 ddlmZ  ej                         Z ej                         Z G d d      Zy)	    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y)InstrumentedAsyncServerNc                 x   |t        d      || _        || _        || _        || _        |xsF t        | j                  j                  d      r | j                  j                  j                  nt        | _	        || _
        || _        g | _        t               | _        d| _        d| _        | j#                          y)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           Y/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/socketio/async_admin.py__init__z InstrumentedAsyncServer.__init__   s    
 <566	("" 
(/0@0@)(LDHH$$ 	 	%:"'M !% 	    c                 B   | j                   j                  d| j                  | j                         | j                  dk(  rI| j
                  s| j                   j                  d| j                  | j                         | j                   j                  d| j                  | j                         | j                   j                  d| j                  | 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.                  j                  d| j0                         | j                   j.                  j                  d| j2                         d	d
lm} | j                   j.                  j8                  | j                   j.                  _        | j<                  | j                   j.                  _        |j>                  |_         tC        jD                  | jF                  jH                  |       |_        |jJ                  |_&        tC        jD                  | jF                  jN                  |       |_%        | j                  dk(  rA|jP                  |_)        tC        jD                  | jF                  jT                  |       |_(        y y )Nconnectr   development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1"22 	 	4 99%>>FDOO&*&:&:  <FD$9$9&*&:&:  <GT%:%:&*&:&:  <M4+@+@&*&:&:  < ,.DHH( (,xx'>'>DHH$&*&9&9DHH#   11 HH/040F0FDHH-   11 HH/040F0FDHH- '+hh&6&6&;&;DHH#$(JJDHH! 		4#;#;<d&A&AB 	6 HHLL,,22,7,K,K)*3*A*ANN33T+;' +6*H*H')2)@)@NN114*9& 99%&1&<&<K#%.%<%<--t&5K" &r!   c                    | j                   dk(  r| 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(                  |_        y y )Nr%   r   r+   )r   r   r5   r4   r   r7   r6   r:   r9   r<   r&   r>   rC   rB   rA   r,   rF   rE   rL   rK   rO   rN   rQ   s     r   uninstrumentz$InstrumentedAsyncServer.uninstrumentf   s    99%&*hh&>&>DHH#  33 HH-   33 HH-$(HH$4$4$;$;DHH!88<<,,5*5*K*K')4)H)H&99%%0%<%<K" &r!   c                 n   K   d} j                   rd}t         j                   t              r| j                   k(  }nst         j                   t              r| j                   v }nJt	        j
                   j                         r j                  |       d {   }n j                  |      }|st        d       fd} j                  j                  ||        j                  j                  j                          _         j                  j                   j                         _        y 7 w)NTFzauthentication failedc           	        K   j                   j                  d       d {    dg}j                  s|g dz  }j                  dk(  r|j	                  d       j                   j                  dd|i| j                         d {    j                  dk(  rg }j                   j                  j                         D ]R  }j                   j                  j                  |d       D ]'  \  } }|j	                  j                  | ||             ) T j                   j                  d	|| j                         d {    y y 7 +7 7 w)
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featuresrc   nspeio_sidr   s        r   r_   z5InstrumentedAsyncServer.admin_connect.<locals>.config   s^    ((..%%% ,,H>> 6 6yyM)-((--+>*I#&$2F2F   H H H yyM) 88++::< FC(,(8(8(I(I)' FW#** 11#sGDFFF
 hhmmM;3.2.B.B $ D D D * &HDs5   EEA(EEB:EEEEE)r   
isinstancedictlistasyncioiscoroutinefunctionr   r   start_background_taskr>   create_eventr   _emit_server_statsr   )r   ri   environclient_authauthenticatedr_   s   `     r   r.   z%InstrumentedAsyncServer.admin_connectv   s     99!M$))T* +tyy 8DIIt, +tyy 8..tyy9*.))K*@$@M$(IIk$:M ,-DEE	D0 	&&vs3 $ 9 9 ;((88##%A %As   BD5D3BD5c                 ^   K   | j                   j                  ||||       d {    y 7 w)Nra   )r   r&   )r   _r   room_filtereventdatas         r   r/   z"InstrumentedAsyncServer.admin_emit   s$     hhmmE4K9mMMMs   #-+-c                    K   | j                   j                  j                  ||      D ]+  \  }}| j                   j                  |||       d {    - y 7 wNr$   )r   r   rg   
enter_roomr   ry   r   roomrz   ri   s         r   r0   z(InstrumentedAsyncServer.admin_enter_room   W     hh&&77;( 	FFC((%%c49%EEE	FE   AAAAc                    K   | j                   j                  j                  ||      D ]+  \  }}| j                   j                  |||       d {    - y 7 wr~   )r   r   rg   
leave_roomr   s         r   r1   z(InstrumentedAsyncServer.admin_leave_room   r   r   c                    K   | j                   j                  j                  ||      D ]*  \  }}| j                   j                  ||       d {    , y 7 wr~   )r   r   rg   r*   )r   ry   r   closerz   ri   s         r   r2   z(InstrumentedAsyncServer.admin_disconnect   sU     hh&&77;( 	@FC((%%cY%???	@?s   AAAAc                    K   | j                   rB| j                  j                          t        j                  | j                          d {    y y 7 wN)r   r   setrp   gather)r   s    r   shutdownz InstrumentedAsyncServer.shutdown   s=     ??!!%%'..111 1s   A	AAAc           
        K   t        j                          }|d   }|dk(  r| j                  j                  j                  ||      }|| j                  j                  j                  |<   | j                  |||      }| j                  j                  d|t        j                  |t        j                        j                         f| j                         d {    n|dk(  r| j                  j                  j                  |= |d   }| j                  j                  d|||t        j                  |t        j                        j                         f| j                         d {    nk| j                  j                  d|||g|dd  t        j                  |t        j                        j                         f| j                         d {     | j                  j                  ||g|  d {   S 7 '7 7 /7 w)	Nr   r#   socket_connectedr$   r*   r   socket_disconnectedevent_received)timer   r   eio_sid_from_sidr3   rh   r&   r   fromtimestampr   utc	isoformatr   r5   )	r   r{   r   argstri   rl   serialized_socketreasons	            r   r4   z&InstrumentedAsyncServer._trigger_event   s    IIK1gIhh&&77YGG01DHH((- $ 5 5c9g N((-- 2!&&q(,,7AAC5 --   / / / l"  ,,S1!WF((-- 5&&q(,,7AAC	8
 --   / / / ((-- 0"ab"&&q(,,7AAC	3
 --   / / / .TXX--eYFFFF)/// GsJ   CH
HBH
*H+A+H
H%H
<H=H
H
H
H
c                 d  K   t        d      D ]  }| j                  j                  d       d {    	 | j                  j                  j	                  |      j
                  r6| j                  j                  d||dd| j                         d {     y  y 7 m7 # t        $ r Y w xY ww)N   socket_updated	websocket)idrk   	transportr$   )	ranger   rd   r>   _get_socketupgradedr&   r   KeyError)r   rl   ri   r   ry   s        r   _check_for_upgradez*InstrumentedAsyncServer._check_for_upgrade   s     q 	A((..###	88<<++G4==((--(8!(%0; "&!5!5	 ( 7 7 7
  >	#7  sE   -B0BB0AB!BB!B0B!!	B-*B0,B--B0c           	          | j                   j                  j                  ||||      }|rP| j                  j	                  d|||t        j                  t        j                        j                         ff       |S )Nroom_joined)
r   r   r7   r   re   r   nowr   r   r   )r   ri   r   r   rl   rets         r   r8   z)InstrumentedAsyncServer._basic_enter_room   sq    hh11#y$29;##]X\\*446	5 %  
r!   c           	          |rP| j                   j                  d|||t        j                  t        j
                        j                         ff       | j                  j                  j                  |||      S )N	room_left)
r   re   r   r   r   r   r   r   r   r:   )r   ri   r   r   s       r   r;   z)InstrumentedAsyncServer._basic_leave_room   sh    ##[X\\*446	3 %  xx223	4HHr!   c           
      6  K    | j                   j                  j                  |||f|||d| d {   }|| j                  k7  rt	        |t
              r|gt        |      z   n||g}	t	        |t              s|g}| j                   j                  j                  ||      D ]m  \  }
}|
|vs| j                   j                  d||
|	t        j                  t        j                        j                         f| j                         d {    o |S 7 7 
w)N)r   skip_sidcallback
event_sentr$   )r   r   r<   r   rm   tuplero   rg   r&   r   r   r   r   r   )r   r{   r|   r   r   r   r   kwargsr   
event_datari   ry   s               r   r=   zInstrumentedAsyncServer._emit   s    +DHH$$++4))-)!') ) ,,,1;D%1H%4:-T] h-$:((**;;ItL 7Qh&((--!" X\\2<<>	7
 "&!5!5 ( 7 7 77 
!)7s)   1DDA9D.ADD	DDc                 J  K   | j                   S| j                  j                  j                         | _         | j                  j	                  | j
                        | _        | j                  j                  d       | j                  j                  ||       d {   S 7 w)NrawConnection)
r   r   r>   rs   rr   rt   r   r   pushr?   )r   rl   ru   s      r   r?   z+InstrumentedAsyncServer._handle_eio_connect  s}       ($(HHLL$=$=$?D!"hh<<'')DO 	/XX11'7CCCCs   BB#B!B#c                    K   | j                   j                  d       | j                  j                  ||       d {   S 7 w)NrawDisconnection)r   r   r   r@   )r   rl   r   s      r   r@   z.InstrumentedAsyncServer._handle_eio_disconnect  s8     12XX44WfEEEEs   ;AAAc                     | j                   j                  j                  |||      }| j                  j	                  d       | j                  j	                  dt        |d                |S )N)packetsheadersjsonp_index
packetsOutbytesOutresponse)r   r>   rC   r   r   len)r   r   r   r   r   s        r   rD   z*InstrumentedAsyncServer._eio_http_response  s^    hhll,7   9|,z3s:+?@
r!   c           	         K    | j                   |       d {   }|j                  j                  d       |j                  j                  dt        |j	                  dd                   |S 7 Vw)N	packetsInbytesInCONTENT_LENGTHr   )rF   r   r   intget)socketr   ru   r   s       r   rJ   z0InstrumentedAsyncServer._eio_handle_post_request$  sc     0F0099{+s7;;'7;<	>
	 :s   A1A/AA1c                   K   fd}fd}|j                   |_        t        j                  ||      |_         |j                  |_        t        j                  ||      |_         | j                  |       d {   S 7 w)Nc                    K   j                   j                  d       j                   j                  dt        |             | j                  |       d {   S 7 w)Nr   r   )r   r   r   _InstrumentedAsyncServer__send)wsr|   r   s     r   _sendz=InstrumentedAsyncServer._eio_websocket_handler.<locals>._send,  sI     ""<0"":s4y94(((s   AAAAc                    K   | j                          d {   }j                  j                  d       j                  j                  dt        |xs d             |S 7 Jw)Nr   r    )_InstrumentedAsyncServer__waitr   r   r   )r   r   r   s     r   _waitz=InstrumentedAsyncServer._eio_websocket_handler.<locals>._wait1  sQ     		#C"";/""9c#)n=J $s   A$A"AA$)sendr   rG   partialwaitr   rL   )r   r   r   r   r   s    `   r   rM   z.InstrumentedAsyncServer._eio_websocket_handler+  sj     	)
	 GG	##E2.GG	##E2./V//3333s   A8B;B <Bc           	        K   | j                   }t        j                         }|j                  j                  j	                         D ]  }|j                  j                  j                  ||      }|s,|j                  |||      }|j                  j                  d|t        j                  |t        j                        j                         f|j                         d {      | j                          d {   S 7 7 w)Nr   r$   )ri   r   r   r   rf   sid_from_eio_sidrh   r&   r   r   r   r   r   r   rO   )r   r   rl   r   r   ri   r   s          r   rP   z&InstrumentedAsyncServer._eio_send_ping=  s     **IIK))88: 	3I((""33GYGC$($9$9#y:A%C!hhmm$6%**1hll;EEG9 "11 $ 3 3 3	3 (V'')))	3 *s+   A0D3A0D#D$D>D?DDc                   K   t        j                          }t        | j                  j                  j	                               }|j                          | j                  j                         s| j                  j                  | j                         d {    | j                  j                  d| j                  t        t        t        j                          |z
  t        | j                  j                  j                         t        | j                  j                  j                   j#                         D cg c]  }|j$                  s| c}      | j&                  j)                         |D cg c]P  }|t        | j                  j*                  j,                  j/                  |d g i      j/                  d g             dR c}d| j0                         d {    | j2                  r[| j2                  j5                  d      \  }}| j                  j                  ||| j0                         d {    | j2                  r[| j                  j                         sy y 7 c c}w c c}w 7 7 >w)Nserver_stats)namesocketsCount)serverIdhostnamepiduptimeclientsCountpollingClientsCountaggregatedEvents
namespacesr$   r   )r   ro   r   handlerskeyssortr   is_setrd   r   r&   r   r   PIDr   r>   socketsvaluesr   r   get_and_clearr   roomsr   r   r   pop)r   
start_timer   srk   r{   r   s          r   rt   z*InstrumentedAsyncServer._emit_server_statsK  s    YY[
$((++0023
''..0((..!;!;<<<((-- NN$))+
2 #DHHLL$8$8 9'* $ 4 4 ; ; = (1JJ  (() %)$5$5$C$C$E
 (	)   $'(8(8(>(>(B(BdBZ))),T2%8  )1 --   / / /  """..2215thhmmE4.2.B.B $ D D D ""% ''..00<()/$Ds]   BI:I)B"I::I,#I:2AI1I:I6 AI:9I8:I:I:'I:,I:8I:c                    |%| j                   j                  j                  |      }| j                   j                  j	                  |      }| j                   j
                  j                  |i       }|| j                   j                  j                  v r#| j                   j                  j                  |   nd}|||j                  rdnd|i |j                  dd      |j                         D ci c]*  \  }}|j                  d      r|dd  j                         |, c}}t        |j                  dd            j                         D ci c]  \  }}|t        |      d	k(  r|d   n| c}}|j                  d
d      dk(  |j                  dd      |dz  |r2t        j                  |t         j"                        j%                         ndd| j                   j                  j'                  ||      dS c c}}w c c}}w )Nr   r   pollingREMOTE_ADDRr   HTTP_r   QUERY_STRINGr   zwsgi.url_schemehttps	PATH_INFOi  )addressr   querysecureurlissuedr   )r   clientIdr   rk   r|   	handshaker   )r   r   r   r>   r   ru   r   r3   r   items
startswithlowerr   r   r   r   r   r   r   	get_rooms)	r   ri   r   rl   r   ru   tmkvs	            r   rh   z(InstrumentedAsyncServer.serialize_socketf  s   ?hh&&77<G))'2((""&&w325HH((3)TXX))#../ 	 (.Y";;}b99@ 6A ||G4 abEKKM1, 6DLKK3E55:UW>DAq!SVq[QqTa7 >!++&7<G{{;3t) !..r8<<@JJL XX%%//Y?%
 	
6>s   1/G: H )Nz/adminFNr%      r   )NNN)__name__
__module____qualname__r    r   rS   r.   r/   r0   r1   r2   r   r4   r   r8   r;   r=   r?   r@   rD   rJ   rM   rP   rt   rh    r!   r   r
   r
      s    EJKL695v= +%ZNF
F
@
2
G:
I GK!(DF4$*D6
r!   r
   )rp   r   r   rG   osr   r   urllib.parser   adminr   
exceptionsr   gethostnamer   getpidr   r
   r  r!   r   <module>r     sI     '  	   !  .6biikq
 q
r!   