
    DQc*                     (   d Z ddlZddlZddlZddlZddlZddlT ddlT ddlT ddl	T ddl
T ej        rddlmZ nddlmZ g dZ	 ddlZej        ZdZn#   G d	 d
e          ZdZY nxY w G d de          Zd Zd Zd Zd Zd Zd Zd Zd Zd ZdS )aH  
websocket - WebSocket client library for Python

Copyright (C) 2010 Hiroki Ohtani(liris)

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

    N   )*)encodebytes)encodestring)
proxy_infoconnectread_headersTc                       e Zd ZdS )ProxyConnectionErrorN)__name__
__module____qualname__     /lib/python3.11/site-packages/websocket/_http.pyr   r   .   s        r   r   Fc                       e Zd Zd ZdS )r   c                    |                     d          pd| _        | j        dvrt          d          |                     dd           | _        | j        rS|                     dd          | _        |                     dd           | _        |                     d	d           | _        d S d| _        d | _        d | _        d S )
N
proxy_typehttp)r   socks4socks5socks5hz:proxy_type must be 'http', 'socks4', 'socks5' or 'socks5h'http_proxy_hosthttp_proxy_portr   http_proxy_authhttp_no_proxy)gettype
ValueErrorhostportauthno_proxy)selfoptionss     r   __init__zproxy_info.__init__4   s    KK--7	yCC 	[YZZZKK 1488	9 	!$5q99DI$5t<<DI#KK>>DMMMDIDI DMMMr   N)r   r   r   r&   r   r   r   r   r   2   s#        ! ! ! ! !r   r   c                 &   t          |           \  }}}}t          st          d          t          j        }d}|j        dk    rt          j        }|j        dk    rt          j        }|j        d         dk    rd}t          j        ||f||j	        |j
        ||j        r|j        d         nd |j        r|j        d	         nd |j        t          |j        z   
	  	        }	|r-t          rt!          |	|j        |          }	nt          d          |	|||ffS )NzPySocks module not found.Fr   r   hTr   r   )r   
proxy_addr
proxy_port
proxy_rdnsproxy_usernameproxy_passwordtimeoutsocket_optionsSSL not available.)	parse_urlHAS_PYSOCKSWebSocketExceptionsocksSOCKS5r   SOCKS4HTTPcreate_connectionr    r!   r"   r/   DEFAULT_SOCKET_OPTIONsockoptHAVE_SSL_ssl_socketsslopt)
urlr%   proxyhostnamer!   resource	is_secureptyperdnssocks
             r   _open_proxied_socketrG   C   s+   *3C..'HdHi > !<===LEDzX zV 
z"~ "t.3jBUZ]]d.3jBUZ]]do2W_D
 
 
D  ; 	;tW^X>>DD$%9:::(D(+++r   c                    |j         r|s|j        dk    st          | ||          S t          |           \  }}}}|r||||ffS t	          ||||          \  }}	}
|s%t          d|z   dz   t          |          z             d }	 t          ||j        |j	                  }|	rt          ||||
          }|r-t          rt          ||j        |          }nt          d          ||||ffS #  |r|                                  xY w)Nr   zHost not found.: :r1   )r    r   rG   r2   _get_addrinfo_listr4   str_open_socketr;   r/   _tunnelr<   r=   r>   close)r?   r%   r@   socketrA   r!   rB   rC   addrinfo_listneed_tunnelr"   rF   s               r   r   r   g   sU   z 9& 9%**> 9#C%888*3C..'HdHi 2$111'9$	5(* (*$M; > (*S03t99<> > 	> DM7?GOLL 	74466D 	? ?"4BB()=>>>hh/// 	JJLLLs   A$C( (Dc                 |   t          | ||j        |j        |j        |j                  \  }}}	 |s1t          j        | |dt
          j        t
          j                  }|dd fS |r|pd}t          j        ||dt
          j        t
          j                  }|d|fS # t
          j	        $ r}t          |          d }~ww xY w)Nr   FP   T)get_proxy_infor    r!   r"   r#   rO   getaddrinfoSOCK_STREAMSOL_TCPgaierrorWebSocketAddressException)	rA   r!   rC   r@   phostpportpauthrP   es	            r   rJ   rJ      s    ()UZUZQ QE5%+  	.".$6#5v~G GM %--Oe)rE
 #.ueQ@RTZTbccM $--? + + +'***+s   2B !6B B;'B66B;c                    d }| D ]W}|d d         \  }}}t          j         |||          }|                    |           t          D ]}	 |j        |	  |D ]}	 |j        |	  |d         }
d }|s	 |                    |
           n# t
          $ r@}t          t          |                    }t          |
d                   |_        Y d }~^d }~wt           j	        $ ry}t          |
d                   |_        	 t          j        t          j        f}n#  t          j        f}Y nxY w|j
        t          j        k    rY d }~|j
        |v r|}Y d }~|d }~ww xY wW n|r||S )N      r   )rO   
settimeoutr:   
setsockoptr   r   WebSocketProxyExceptionrK   	remote_iperrorerrnoECONNREFUSEDWSAECONNREFUSEDEINTR)rP   r;   r/   erraddrinfofamilysocktypeprotorF   optsaddressre   eConnRefuseds                r   rL   rL      s   
C! & &"*2A2,%}VXu55   ) 	# 	#DDOT""" 	# 	#DDOT"""1+ 	W%%%& % (   -c%jj99 #GAJ<      "%gaj//:$)$68M#NLL:$)$6#9LLL;%+-  HHHH[L0  CHHHHK    	IKsH   1B
E6CEE9DED#!E EEEc                  Z    t           j        rt          j        dk    pt          j        dk    S )N)      	   )r_   rs   )sixPY2sysversion_infor   r   r   _can_use_snirz      s$    74s'94R8HF8RRr   c                    t                               |                    dt           j                            }|                    dt           j                  t           j        k    r||                    dd           }|                    dd           }|s|r|                    ||           n4t          |d          r$|                    t           j        j	                   |                    dd           rE|
                    |d         |                    dd           |                    d	d                      |d         |_        t          r||_        d
|v r|                    |d
                    d|v r#|d         \  }}}	|
                    |||	           d|v r|                    |d                    |                    | |                    dd          |                    dd          |          S )Nssl_version	cert_reqsca_certsca_cert_path)cafilecapathload_default_certscertfilekeyfilepasswordciphers
cert_chain
ecdh_curvedo_handshake_on_connectTsuppress_ragged_eofs)r   r   server_hostname)ssl
SSLContextr   PROTOCOL_SSLv23	CERT_NONEload_verify_locationshasattrr   PurposeSERVER_AUTHload_cert_chainverify_modeHAVE_CONTEXT_CHECK_HOSTNAMEcheck_hostnameset_ciphersset_ecdh_curvewrap_socket)
rF   r>   rA   r   contextr   r   r   r   r   s
             r   _wrap_sni_socketr      s   nnVZZs7JKKLLGzz+s}--> @J--ND11 	@V 	@)))GGGGW233 	@&&s{'>???zz*d## 
:JJy$''JJz4((	
 	
 	
 !-G" 0!/F /F9-...v =&,\&:#'8'8<<<v 5vl3444 &

+Dd K K#ZZ(>EE 	    r   c                     t          t          j                  }|                    |           t          j                            d          }|rQt          j                            |          r2|                    dd           |                    dd           ||d<   n<|r:t          j        	                    |          r|                    dd           ||d<   |d         t          j
        k    o|                    dd          }t                      rt          | |||          } n(|                    dd           t          j        | fi |} t          s$|r"t!          |                                 |           | S )	N)r}   WEBSOCKET_CLIENT_CA_BUNDLEr~   ca_certr   r}   r   T)dictr   CERT_REQUIREDupdateosenvironr   pathisfileisdirr   poprz   r   r   r   match_hostnamegetpeercert)rF   user_ssloptrA   r>   certPathr   s         r   r=   r=      sz   C-...F
MM+z~~:;;H *BGNN8,, *
D11*	400* &z	 *bgmmH-- *55*!)~K(CM9  fjj$?  ? N ~~ /fhGG

#T***t..v..& 5> 5t''))8444Kr   c                 D   t          d           d||fz  }|r|d         r|d         }|d         r|d|d         z   z  }t          |                                                                                                                              dd          }|d|z  z  }|d	z  }t          d
|           t          | |           	 t          |           \  }}}	n.# t          $ r!}
t          t          |
                    d }
~
ww xY w|dk    rt          d|z            | S )NzConnecting proxy...zCONNECT %s:%d HTTP/1.0
r   r   rI   
 zProxy-Authorization: Basic %s
z
zrequest header   z#failed CONNECT via proxy status: %r)debugbase64encodeencodestripdecodereplacedumpsendr	   	Exceptionrc   rK   )rF   r    r!   r"   connect_headerauth_strencoded_strstatusresp_headersstatus_messager]   s              r   rM   rM     sS   	
   1T4L@N LQ L77 	&d1g%H"8??#4#455;;==DDFFNNtUWXX=KKfN	>***~./;D/A/A,nn . . .%c!ff---. } <%1F:< < 	< Ks   C 
D$D  Dc                 H   d }d }i }t          d           	 t          |           }|                    d                                          }|snt          |           |sG|                    dd          }t          |d                   }t          |          dk    r|d         }ng|                    dd          }t          |          dk    r/|\  }}|                                ||                                <   nt          d          t          d	           |||fS )
Nz--- response header ---Tzutf-8 rs   r   rI   zInvalid headerz-----------------------)	trace	recv_liner   r   splitintlenlowerr4   )	rF   r   r   headerslinestatus_infokvkeyvalues	            r   r	   r	   -  s+   FNG	
#$$$;{{7##))++ 	d 	;**S!,,KQ((F;!# 0!,QC##B2ww!| ;
U',{{}}		$$()9:::%;( 

#$$$7N**r   ) __doc__rf   r   rO   rx   rv   _exceptions_logging_socket_ssl_compat_urlPY3base64r   r   r   __all__r5   r   r3   BaseExceptionobjectr   rG   r   rJ   rL   rz   r   r=   rM   r	   r   r   r   <module>r      s   (  				  



 



                    7 42222222333333
3
3
3LLL 5KK    }   KKK! ! ! ! ! ! ! !"!, !, !,H  D+ + +.* * *ZS S S" " "J  8  6+ + + + +s   A A&