a
   ߘ[=D                   @   s  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rhddlmZ nddlmZ g dZzddlZejZdZW n    G d	d
 d
eZdZY n0 G dd deZdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZdS ) aL  
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-1335  USA

    N   )*)encodebytes)encodestring)
proxy_infoconnectread_headersTc                   @   s   e Zd ZdS )ProxyConnectionErrorN)__name__
__module____qualname__ r   r   3/usr/lib/python3.9/site-packages/websocket/_http.pyr	   /   s   r	   Fc                   @   s   e Zd Zdd ZdS )r   c                 K   sx   | dpd| _| jdvr"td| dd | _| jrb| dd| _| dd | _| d	d | _nd| _d | _d | _d S )
N
proxy_typehttp)r   socks4Zsocks5Zsocks5hz:proxy_type must be 'http', 'socks4', 'socks5' or 'socks5h'Zhttp_proxy_hostZhttp_proxy_portr   Zhttp_proxy_authZhttp_no_proxy)gettype
ValueErrorhostportauthno_proxy)selfoptionsr   r   r   __init__5   s    
zproxy_info.__init__N)r
   r   r   r   r   r   r   r   r   3   s   r   c           
      C   s   t | \}}}}tstdtj}d}|jdkr6tj}|jdkrFtj}|jd dkrXd}tj||f||j	|j
||jr~|jd nd |jr|jd	 nd |jt|j d
	}	|rtrt|	|j|}	ntd|	|||ffS )NzPySocks module not found.Fr   r   hTr   r   )r   Z
proxy_addrZ
proxy_portZ
proxy_rdnsZproxy_usernameZproxy_passwordtimeoutZsocket_optionsSSL not available.)	parse_urlHAS_PYSOCKSWebSocketExceptionsocksZSOCKS5r   ZSOCKS4ZHTTPZcreate_connectionr   r   r   r   DEFAULT_SOCKET_OPTIONsockoptHAVE_SSL_ssl_socketsslopt)
urlr   proxyhostnamer   resource	is_secureZptypeZrdnssockr   r   r   _open_proxied_socketD   s6    

r/   c                 C   s   |j r |s |jdks t| ||S t| \}}}}|rB||||ffS t||||\}}	}
|srtd| d t| d }zRt||j|j	}|	rt
||||
}|rtrt||j|}ntd||||ffW S    |r|   Y n0 d S )Nr   zHost not found.: :r   )r   r   r/   r    _get_addrinfo_listr"   str_open_socketr%   r   _tunnelr&   r'   r(   close)r)   r   r*   socketr+   r   r,   r-   addrinfo_listZneed_tunnelr   r.   r   r   r   r   h   s4    
r   c           	   
   C   s   t | ||j|j|j|j\}}}zX|sHt| |dtjtj}|dd fW S |rP|pRd}t||dtjtj}|d|fW S W n. tj	y } zt
|W Y d }~n
d }~0 0 d S )Nr   FP   T)Zget_proxy_infor   r   r   r   r6   ZgetaddrinfoZSOCK_STREAMZSOL_TCPZgaierrorZWebSocketAddressException)	r+   r   r-   r*   ZphostZpportZpauthr7   er   r   r   r1      s    
r1   c                 C   s  d }| D ]f}|d d \}}}t  |||}|| tD ]}	|j|	  q<|D ]}	|j|	  qP|d }
d }|sz||
 W n ty } z2tt|}t|
d |_W Y d }~qlW Y d }~qld }~0  t j	y` } z~t|
d |_zt
jt
jf}W n   t
jf}Y n0 |j
t
jkr*W Y d }~qln"|j
|v rH|}W Y d }~qln|W Y d }~qld }~0 0 qjqlq q|q|r|||S )N      r   )r6   Z
settimeoutr$   Z
setsockoptr   r	   WebSocketProxyExceptionr2   Z	remote_iperrorerrnoZECONNREFUSEDZWSAECONNREFUSEDZEINTR)r7   r%   r   errZaddrinfoZfamilyZsocktypeprotor.   ZoptsZaddressr=   ZeConnRefusedr   r   r   r3      sH    

 r3   c                   C   s   t jrtjdkptjdkS )N)      	   )r:   rA   )sixZPY2sysversion_infor   r   r   r   _can_use_sni   s    rG   c           
      C   s&  t |dt j}|dt jt jkrp|dd }|dd }|sH|rX|j||d nt|drp|t jj	 |dd r|
|d |dd |d	d  |d |_tr||_d
|v r||d
  d|v r|d \}}}	|
|||	 d|v r||d  |j| |dd|dd|dS )NZssl_version	cert_reqsca_certsca_cert_path)cafilecapathload_default_certscertfilekeyfilepasswordZciphersZ
cert_chainZ
ecdh_curvedo_handshake_on_connectTsuppress_ragged_eofs)rQ   rR   Zserver_hostname)sslZ
SSLContextr   ZPROTOCOL_SSLv23	CERT_NONEZload_verify_locationshasattrrM   ZPurposeZSERVER_AUTHZload_cert_chainZverify_modeHAVE_CONTEXT_CHECK_HOSTNAMEcheck_hostnameZset_ciphersZset_ecdh_curvewrap_socket)
r.   r(   r+   rW   contextrK   rL   rN   rO   rP   r   r   r   _wrap_sni_socket   s<    






rZ   c                 C   s   t tjd}|| tjd}|r\tj|r\|dd d u r\|dd d u r\||d< n(|rtj	|r|dd d u r||d< |d tj
ko|dd}t rt| |||} n|dd tj| fi |} ts|rt|  | | S )	N)rH   ZWEBSOCKET_CLIENT_CA_BUNDLErI   Zca_certrJ   rH   rW   T)dictrS   ZCERT_REQUIREDupdateosenvironr   pathisfileisdirrT   poprG   rZ   rX   rV   Zmatch_hostnameZgetpeercert)r.   Zuser_ssloptr+   r(   ZcertPathrW   r   r   r   r'      s.    

r'   c              
   C   s   t d d||f }|rh|d rh|d }|d r@|d|d  7 }t|   dd}|d| 7 }|d	7 }td
| t| | zt| \}}}	W n0 t	y }
 zt
t|
W Y d }
~
n
d }
~
0 0 |dkrt
d| | S )NzConnecting proxy...zCONNECT %s:%d HTTP/1.0
r   r   r0   
 zProxy-Authorization: Basic %s
z
zrequest header   z#failed CONNECT via proxy status: %r)debugbase64encodeencodestripdecodereplacedumpsendr   	Exceptionr<   r2   )r.   r   r   r   Zconnect_headerZauth_strZencoded_strstatusZresp_headersstatus_messager9   r   r   r   r4     s(    

"r4   c           	      C   s   d }d }i }t d t| }|d }|s0qt | |sj|dd}t|d }t|dkr|d }q|dd}t|dkr|\}}| || < qtdqt d |||fS )	Nz--- response header ---zutf-8 rA   r   r0   zInvalid headerz-----------------------)	ZtraceZ	recv_linerj   ri   splitintlenlowerr"   )	r.   ro   rp   ZheaderslineZstatus_infoZkvkeyvaluer   r   r   r   .  s*    

r   ) __doc__r>   r]   r6   rE   rD   _exceptionsZ_loggingZ_socketZ_ssl_compatZ_urlZPY3base64r   rg   r   __all__r#   r	   r!   BaseExceptionobjectr   r/   r   r1   r3   rG   rZ   r'   r4   r   r   r   r   r   <module>   s>   
$"-%