
    EgF'                         d dl Z d dlZd dlZd dlmZ d dlmZmZ d dlmZ d dl	m
Z
mZmZmZmZ d dlmZ d dlmZ d dlmZ d d	lmZmZ d d
lmZ d dlmZ d dlmZmZ  G d dej                  ZdS )    N)SSL)WSConnectionevents)ConnectionType)AcceptConnectionCloseConnectionMessagePingRequest)PerMessageDeflate)
exceptions)flow)
websocketstcp)base)strutils)WebSocketFlowWebSocketMessagec                   L     e Zd ZdZ fdZd Zd Zd Zd Zd Z	d Z
d	 Z xZS )
WebSocketLayera)  
        WebSocket layer to intercept, modify, and forward WebSocket messages.

        Only version 13 is supported (as specified in RFC6455).
        Only HTTP/1.1-initiated connections are supported.

        The client starts by sending an Upgrade-request.
        In order to determine the handshake and negotiate the correct protocol
        and extensions, the Upgrade-request is forwarded to the server.
        The response from the server is then parsed and negotiated settings are extracted.
        Finally the handshake is completed by forwarding the server-response to the client.
        After that, only WebSocket frames are exchanged.

        PING/PONG frames pass through and must be answered by the other endpoint.

        CLOSE frames are forwarded before this WebSocketLayer terminates.

        This layer is transparent to any negotiated extensions.
        This layer is transparent to any negotiated subprotocols.
        Only raw frames are forwarded to the other endpoint.

        WebSocket messages are stored in a WebSocketFlow.
    c                 8   t                                          |           || _        d | _        g | _        g | _        i | _        g }g }d|j        j        v r<t          j
        |j        j        d         v rt                      g}t                      g}t          t          j                  | j        | j        <   t          t          j                  | j        | j        <   |r+|d                             |j        j        d                    |r+|d                             |j        j        d                    t%          ||j        j        |j        j                  }| j        | j                                     |          }| j        | j                                     |           t1          | j        | j                                                           }t5          |t2          j                  sJ | j        | j                                     t7          |                    }| j        | j                                     |           t5          t1          | j        | j                                                           t2          j                  sJ d S )NzSec-WebSocket-Extensionsr   )
extensionshosttarget)r   )super__init__handshake_flowr   client_frame_bufferserver_frame_bufferconnectionsresponseheadersr   namer   r   SERVERclient_connCLIENTserver_connfinalizer   requestr   pathsendreceive_datanextr   
isinstancer   )	selfctxr   client_extensionsserver_extensionsr)   dataevent	__class__s	           p/var/www/sysmax/venv/lib/python3.11/site-packages/seleniumwire/thirdparty/mitmproxy/server/protocol/websocket.pyr   zWebSocketLayer.__init__,   sL   ,#'	#% #% 79%)@)HHH %)@)HIc)ddd%6%8%8$9!%6%8%8$9!-9.:O-P-P)*-9.:O-P-P)* 	ga )).*A*IJd*efff 	ga )).*A*IJd*efff%6^=S=Xaoawa|}}} 0166w??)*77===T%d&67>>@@AA%00000 01667GSd7e7e7eff)*77===$t/0@AHHJJKKVMdeeeeeee    c                    t          |t          j                  r|                     ||||          S t          |t          j                  r|                     ||||          S t          |t          j                  r|                     ||||          S t          |t          j                  r| 	                    ||||          S dS )NT)
r.   r   r	   _handle_messager
   _handle_pingPong_handle_pongr   _handle_close_connectionr/   r4   source_conn
other_conn	is_servers        r6   _handle_eventzWebSocketLayer._handle_eventO   s    eV^,, 	\''{J	RRRv{++ 	\$$UKYOOOv{++ 	\$$UKYOOOv566 	\00ZQZ[[[ tr7   c                    r| j         n| j        }|                    |j                   |j        rjd |D             t          |t          j                  r,t          j	        j
        j        }d                    |          }n+t          j	        j
        j        }d                    |          }|                                 t          | |          }t!          |j                  | j        j                            |           | j                            d| j                   | j        j        sh|j        safd}	 |	|j                  D ]I\  }
}| j        |                             t5          |
|                    }|                    |           J| j        j        rN| j        |                             t5          |j        |j                            }|                    |           dS )Nc                 ,    g | ]}t          |          S  )len).0fs     r6   
<listcomp>z2WebSocketLayer._handle_message.<locals>.<listcomp>a   s    #7#7#7qCFF#7#7#7r7    r7   websocket_messagec              3   *  K   t          |           k    r*d}D ]#}| |||z            ||z   k    rdndfV  ||z  }$d S rdnd}t          dt          |           |          }|D ]+}| |||z            ||z   t          |           k    rdndfV  ,d S )Nr   TFi  i  )rF   range)	payloadposs
chunk_sizechunksirA   lengthoriginal_chunk_sizess	         r6   	get_chunkz1WebSocketLayer._handle_message.<locals>.get_chunkr   s      7||v--!5 % %A#*3sQw;#7qFARARX]"^^^^1HCC% % .7%@TTD
!&q#g,,
!C!C!' q qA#*1Q^+;#<a*nX[\cXdXdFdFdddjo"pppppq qr7   r3   message_finishedT)r   r   appendr3   rX   r.   r   TextMessagewsprotoframe_protocolOpcodeTEXTjoinBINARYclearr   rF   contentr   messageschannelaskstreamkilledr    r+   r	   )r/   r4   r?   r@   rA   fbmessage_typerN   rK   rV   chunkfinalr3   rT   rU   s       `        @@r6   r9   zWebSocketLayer._handle_message\   s   )2PT%%8P
		%*! #	*#7#7B#7#7#7 %!344 '&5<A''"++&5<C((2,,HHJJJ 09}g V V*233FI%%&7888L0$)<<<9# *,=,D *q q q q q q q %.I.?.G$H$H * *LE5+J7<<W%bg=h=h=hiiDOOD))))9 	"#J/44W%*_d_u5v5v5vwwDOOD!!!tr7   c           	         | j         |                             t                                }|                    |           | j         |                             |                                          }|                    |           |                     d                    |rdnd          dt          j        t          |j	                            g           dS )NzPing Received from {}serverclientinfoT)
r    r+   r
   r!   logformatr   bytes_to_escaped_strbytesrN   r/   r4   r?   r@   rA   r3   s         r6   r:   zWebSocketLayer._handle_ping   s    
+0088,11%..2B2BCC#**y+N88hOO*5+?+?@@A	
 	
 	

 tr7   c           	          |                      d                    |rdnd          dt          j        t	          |j                            g           dS )NzPong Received from {}rm   rn   ro   T)rp   rq   r   rr   rs   rN   r>   s        r6   r<   zWebSocketLayer._handle_pong   sW    #**y+N88hOO*5+?+?@@A	
 	
 	

 tr7   c                    |rdnd| j         _        |j        | j         _        |j        | j         _        | j        |                             t          |j        |j                            }|                    |           | j        |                             |	                                          }|                    |           dS )Nrm   rn   )codereasonF)
r   close_senderrw   
close_coderx   close_reasonr    r+   r   r!   rt   s         r6   r=   z'WebSocketLayer._handle_close_connection   s    -6!DH	$z	!&	
+00ejY^Ye1f1f1fgg,11%..2B2BCCur7   c                     	 	 |                                 }| j        |                             t          |d                    }|                    |           n# t          j        $ r Y d S w xY wq)NTrW   )
get_nowaitr    r+   r	   queueEmpty)r/   endpointmessage_queuerN   r3   s        r6   _inject_messageszWebSocketLayer._inject_messages   s    	'2244'166wG^b7c7c7cddd####;   	s   AA A/.A/c           	         t          | j        | j        | j                  | _        | j        j        | j        j        d<   | j        j        | j        j        d<   | j                            d| j                   d | j	        
                                D             }d}	 | j        j                                        s|                     | j        | j        j                   |                     | j        | j        j                   t!          j        |d          }|D ]}|| j        j        k    r| j        n| j        }|| j        j        k    r| j        n| j        }|| j        k    }t&          j                            |j                  }| j	        |                             t1          |                    }	|                    |	           |r/ d| j        _        | j                            d| j                   d S | j	        |                                         D ]}
|                     |
|||          s|sd}!| j        j                                        n# t<          j        t@          j!        tD          j#        f$ ro}|rd	nd
}t	          j#        d$                    |tK          |                              | j        _        | j                            d| j                   Y d }~nd }~ww xY wd| j        _        | j                            d| j                   d S # d| j        _        | j                            d| j                   w xY w)Nwebsocket_handshakewebsocket_flowwebsocket_startc                     g | ]	}|j         
S rE   )
connection)rG   cs     r6   rI   z+WebSocketLayer.__call__.<locals>.<listcomp>   s    ???!???r7   Fg?Twebsocket_endrm   rn   z2WebSocket connection closed unexpectedly by {}: {}websocket_error)&r   r%   r'   r   r   idmetadatard   re   r    keysshould_exitis_setr   _inject_messages_client_inject_messages_serverr   ssl_read_selectr   r   Frame	from_filerfiler,   rs   r+   endedtellr   rB   socketerrorr   TcpExceptionr   Errorrq   repr)r/   connsclose_receivedrconnr?   r@   rA   framer3   r4   erP   s                r6   __call__zWebSocketLayer.__call__   s7   !$"2D4DdFYZZ	484G4J	019=$%56*DI666??t'7'<'<'>'>???	:l.5577 6%%d&6	8YZZZ%%d&6	8YZZZ's33 6 6D6:d>N>Y6Y6Y$"2"2_c_oK59T=M=X5X5X!1!1^b^nJ!,0@!@I&,66{7HIIE+K8EEeEllSSD$$T***%  #DIOLoty99999 "&!1+!>!E!E!G!G 6 6#11%jR[\\ 6#1 6156# l.5577 6* j5syA 	< 	< 	<%38A"j)])d)defhlmnhoho)p)pqqDIOL/;;;;;;;;	<
 #DIOLoty99999 #DIOLoty9999s9   D3I ?AI L% &K4A%K/*L% /K44L% %.M)__name__
__module____qualname____doc__r   rB   r9   r:   r<   r=   r   r   __classcell__)r5   s   @r6   r   r      s         0!f !f !f !f !fF  , , ,\    
 
 
  %: %: %: %: %: %: %:r7   r   ) r~   r   r[   OpenSSLr   r   r   wsproto.connectionr   wsproto.eventsr   r   r	   r
   r   wsproto.extensionsr   !seleniumwire.thirdparty.mitmproxyr   r   %seleniumwire.thirdparty.mitmproxy.netr   r   1seleniumwire.thirdparty.mitmproxy.server.protocolr   'seleniumwire.thirdparty.mitmproxy.utilsr   +seleniumwire.thirdparty.mitmproxy.websocketr   r   Layerr   rE   r7   r6   <module>r      sP            ( ( ( ( ( ( ( ( - - - - - - T T T T T T T T T T T T T T 0 0 0 0 0 0 8 8 8 8 8 8 2 2 2 2 2 2 A A A A A A A A B B B B B B < < < < < < W W W W W W W WF: F: F: F: F:TZ F: F: F: F: F:r7   