U
    Zh.5                  
   @  s(  d dl mZ 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mZ ddlmZ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 ddlmZ ddlmZ e dZ!dddddZ"dddddddddd	Z#ddd d!d"d#d$d%d&d'Z$G d(d) d)eZ%G d*d+ d+eZ&dS ),    )annotationsN   )SyncBackend)NetworkBackendNetworkStream)ConnectionNotAvailable
ProxyError)URLOriginRequestResponseenforce_bytesenforce_url)default_ssl_context)Lock)Trace   )ConnectionPool)HTTP11Connection)ConnectionInterfacezhttpcore.sockszNO AUTHENTICATION REQUIREDZGSSAPIzUSERNAME/PASSWORDzNO ACCEPTABLE METHODS)             Z	SucceededzGeneral SOCKS server failurez!Connection not allowed by rulesetzNetwork unreachablezHost unreachablezConnection refusedzTTL expiredzCommand not supportedzAddress type not supported)	r   r   r                     )authr   bytesinttuple[bytes, bytes] | NoneNone)streamhostportr    returnc                C  s  t j }|d krt jjjnt jjj}|t j|g | }| 	| | j
dd}||}t|t jjstt|j|krt|d}	t|jd}
td|	 d|
 d|jt jjjkr4|d k	st|\}}|t j|| | }| 	| | j
dd}||}t|t jjs$t|js4td|t jjt jjj||f | }| 	| | j
dd}||}t|t jjst|jt jjjkrt|jd}td	| dd S )
Ni   )Z	max_bytesUNKNOWNz
Requested z from proxy server, but got .zInvalid username/passwordZUNKOWNz Proxy Server could not connect: )socksioZsocks5ZSOCKS5ConnectionZSOCKS5AuthMethodZNO_AUTH_REQUIREDZUSERNAME_PASSWORDsendZSOCKS5AuthMethodsRequestZdata_to_sendwritereadZreceive_data
isinstanceZSOCKS5AuthReplyAssertionErrormethodAUTH_METHODSgetr   ZSOCKS5UsernamePasswordRequestZSOCKS5UsernamePasswordReplysuccessZSOCKS5CommandRequestfrom_addressZSOCKS5CommandZCONNECTZSOCKS5Reply
reply_codeZSOCKS5ReplyCodeZ	SUCCEEDEDREPLY_CODES)r%   r&   r'   r    connZauth_methodZoutgoing_bytesZincoming_bytesresponse	requestedZ	respondedusernamepasswordr6    r=   >/tmp/pip-unpacked-wheel-s37ty00c/httpcore/_sync/socks_proxy.py_init_socks5_connection*   sT    





 

r?   c                      sL   e Zd ZdZdddd	d
d
ddddddd fddZdddddZ  ZS )
SOCKSProxyzB
    A connection pool that sends requests via an HTTP proxy.
    N
   TFr   zURL | bytes | strz&tuple[bytes | str, bytes | str] | Nonessl.SSLContext | Nonez
int | Nonefloat | Noneboolr"   NetworkBackend | Noner$   )	proxy_url
proxy_authssl_contextmax_connectionsmax_keepalive_connectionskeepalive_expiryhttp1http2retriesnetwork_backendr(   c              
     sn   t  j|||||||
|	d || _t|dd| _|dk	rd|\}}t|dd}t|dd}||f| _nd| _dS )a&  
        A connection pool for making HTTP requests.

        Parameters:
            proxy_url: The URL to use when connecting to the proxy server.
                For example `"http://127.0.0.1:8080/"`.
            ssl_context: An SSL context to use for verifying connections.
                If not specified, the default `httpcore.default_ssl_context()`
                will be used.
            max_connections: The maximum number of concurrent HTTP connections that
                the pool should allow. Any attempt to send a request on a pool that
                would exceed this amount will block until a connection is available.
            max_keepalive_connections: The maximum number of idle HTTP connections
                that will be maintained in the pool.
            keepalive_expiry: The duration in seconds that an idle HTTP connection
                may be maintained for before being expired from the pool.
            http1: A boolean indicating if HTTP/1.1 requests should be supported
                by the connection pool. Defaults to True.
            http2: A boolean indicating if HTTP/2 requests should be supported by
                the connection pool. Defaults to False.
            retries: The maximum number of retries when trying to establish
                a connection.
            local_address: Local address to connect from. Can also be used to
                connect using a particular address family. Using
                `local_address="0.0.0.0"` will connect using an `AF_INET` address
                (IPv4), while using `local_address="::"` will connect using an
                `AF_INET6` address (IPv6).
            uds: Path to a Unix Domain Socket to use instead of TCP sockets.
            network_backend: A backend instance to use for handling network I/O.
        )rH   rI   rJ   rK   rL   rM   rO   rN   rF   )nameNrG   )super__init___ssl_contextr   
_proxy_urlr   _proxy_auth)selfrF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   r;   r<   Zusername_bytesZpassword_bytes	__class__r=   r>   rR   n   s(    +
zSOCKSProxy.__init__r
   r   originr(   c              
   C  s(   t | jj|| j| j| j| j| j| jdS )N)proxy_originremote_originrG   rH   rK   rL   rM   rO   )	Socks5ConnectionrT   rZ   rU   rS   _keepalive_expiry_http1_http2_network_backendrV   rZ   r=   r=   r>   create_connection   s    zSOCKSProxy.create_connection)	NNrA   NNTFr   N)__name__
__module____qualname____doc__rR   rc   __classcell__r=   r=   rW   r>   r@   i   s            (Br@   c                   @  s   e Zd Zd&dddddddd	d
d	ddZdddddZdddddZd
dddZddddZddddZddddZ	dddd Z
d!dd"d#Zd!dd$d%ZdS )'r]   NTFr
   r#   rB   rC   rD   rE   r$   )	r[   r\   rG   rH   rK   rL   rM   rO   r(   c	           	      C  sV   || _ || _|| _|| _|| _|| _|| _|d kr8t n|| _t	 | _
d | _d| _d S )NF)_proxy_origin_remote_originrU   rS   r^   r_   r`   r   ra   r   _connect_lock_connection_connect_failed)	rV   r[   r\   rG   rH   rK   rL   rM   rO   r=   r=   r>   rR      s    zSocks5Connection.__init__r   r   )requestr(   c                 C  s&  |j di }|j dd }|dd }| j | jd krz| jjd| jj|d}tdt	||}| j
jf |}||_W 5 Q R X || jjd| jj| jd}tdt	||}tf | ||_W 5 Q R X | jjd	kr^| jd krt n| j}| jrd
dgnd
g}	||	 ||p*| jjd|d}tdt	||}|jf |}||_W 5 Q R X |d}
|
d k	o||
 dk}|s| jr| jsddlm} || j|| jd| _nt| j|| jd| _W n. tk
r } zd| _|W 5 d }~X Y nX n| j st  W 5 Q R X | j!|S )Ntimeoutsni_hostnameconnectascii)r&   r'   ro   connect_tcp)r%   r&   r'   r    Zsetup_socks5_connection   httpszhttp/1.1h2)rH   server_hostnamero   	start_tls
ssl_objectr   )HTTP2Connection)rZ   r%   rK   T)"
extensionsr3   rk   rl   ri   r&   decoder'   r   loggerra   rs   Zreturn_valuerj   rU   r?   schemerS   r   r`   set_alpn_protocolsrw   Zget_extra_infoselected_alpn_protocolr_   rM   ry   r^   r   	Exceptionrm   is_availabler   handle_request)rV   rn   Ztimeoutsrp   ro   kwargstracer%   rH   alpn_protocolsrx   Zhttp2_negotiatedry   excr=   r=   r>   r      s    
   





zSocks5Connection.handle_requestrY   c                 C  s
   || j kS N)rj   rb   r=   r=   r>   can_handle_request-  s    z#Socks5Connection.can_handle_request)r(   c                 C  s   | j d k	r| j   d S r   )rl   closerV   r=   r=   r>   r   0  s    
zSocks5Connection.closec                 C  s6   | j d kr,| jo*| jjdks$| j o*| j S | j  S )Nrt   )rl   r`   rj   r}   r_   rm   r   r   r=   r=   r>   r   4  s    
zSocks5Connection.is_availablec                 C  s   | j d kr| jS | j  S r   )rl   rm   has_expiredr   r=   r=   r>   r   @  s    
zSocks5Connection.has_expiredc                 C  s   | j d kr| jS | j  S r   )rl   rm   is_idler   r=   r=   r>   r   E  s    
zSocks5Connection.is_idlec                 C  s   | j d kr| jS | j  S r   )rl   rm   	is_closedr   r=   r=   r>   r   J  s    
zSocks5Connection.is_closedstrc                 C  s"   | j d kr| jrdS dS | j  S )NzCONNECTION FAILEDZ
CONNECTING)rl   rm   infor   r=   r=   r>   r   O  s    
zSocks5Connection.infoc                 C  s   d| j j d|   dS )N<z [z]>)rX   rd   r   r   r=   r=   r>   __repr__T  s    zSocks5Connection.__repr__)NNNTFN)rd   re   rf   rR   r   r   r   r   r   r   r   r   r   r=   r=   r=   r>   r]      s           Ur]   )'
__future__r   loggingsslr+   Z_backends.syncr   Z_backends.baser   r   _exceptionsr   r   Z_modelsr	   r
   r   r   r   r   _sslr   Z_synchronizationr   Z_tracer   Zconnection_poolr   Zhttp11r   
interfacesr   	getLoggerr|   r2   r7   r?   r@   r]   r=   r=   r=   r>   <module>   sB    
?T