o
    }j6hõL  ã                   @   sð   d Z ddlZddlZddlZddlZeeddƒdu redƒ‚dZdZdZ	dZ
daejZG d	d
„ d
eƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZdZdZdZdZ	d dd„Zdd„ ZG dd„ dejƒZdS )!a  SocksiPy - Python SOCKS module.

Version 1.00

Copyright 2006 Dan-Haim. All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.
3. Neither the name of Dan Haim nor the names of his contributors may be used
   to endorse or promote products derived from this software without specific
   prior written permission.

THIS SOFTWARE IS PROVIDED BY DAN HAIM "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
EVENT SHALL DAN HAIM OR HIS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMANGE.

This module provides a standard socket-like interface for Python
for tunneling connections through SOCKS proxies.

Minor modifications made by Christopher Gilbert (http://motomastyle.com/) for
use in PyLoris (http://pyloris.sourceforge.net/).

Minor modifications made by Mario Vilas (http://breakingcode.wordpress.com/)
mainly to merge bug fixes found in Sourceforge.
é    NÚsocketz-socket.socket missing, proxy support unusableé   é   é   é   c                   @   ó   e Zd ZdS )Ú
ProxyErrorN©Ú__name__Ú
__module__Ú__qualname__© r   r   ú]/var/www/html/chefvision.cloud.itp360.com/venv/lib/python3.10/site-packages/httplib2/socks.pyr   7   ó    r   c                   @   r   )ÚGeneralProxyErrorNr	   r   r   r   r   r   ;   r   r   c                   @   r   )ÚSocks5AuthErrorNr	   r   r   r   r   r   ?   r   r   c                   @   r   )ÚSocks5ErrorNr	   r   r   r   r   r   C   r   r   c                   @   r   )ÚSocks4ErrorNr	   r   r   r   r   r   G   r   r   c                   @   r   )Ú	HTTPErrorNr	   r   r   r   r   r   K   r   r   )Úsuccesszinvalid dataznot connectedznot availablezbad proxy typez	bad input)
Ú	succeededzgeneral SOCKS server failurez!connection not allowed by rulesetzNetwork unreachablezHost unreachablezConnection refusedzTTL expiredzCommand not supportedzAddress type not supportedzUnknown error)r   zauthentication is requiredz0all offered authentication methods were rejectedz$unknown username or invalid passwordúunknown error)zrequest grantedzrequest rejected or failedzLrequest rejected because SOCKS server cannot connect to identd on the clientzPrequest rejected because the client program and identd report different user-idsr   Tc                 C   s   | |||||fa dS )z´setdefaultproxy(proxytype, addr[, port[, rdns[, username[, password]]]])
    Sets a default proxy which all further socksocket objects will use,
    unless explicitly changed.
    N)Ú_defaultproxy)Ú	proxytypeÚaddrÚportÚrdnsÚusernameÚpasswordr   r   r   Úsetdefaultproxyw   s   r   c                 C   s   t dkr
t| j_dS tdƒ‚)a7  wrapmodule(module)

    Attempts to replace a module's socket library with a SOCKS socket. Must set
    a default proxy using setdefaultproxy(...) first.
    This will only work on modules that import socket directly into the
    namespace;
    most of the Python Standard Library falls into this category.
    N)r   zno proxy specified)r   Ú
socksocketr   r   )Úmoduler   r   r   Ú
wrapmodule‚   s   	r"   c                       sž   e Zd ZdZejejddfdd„Zdd„ Z‡ fdd	„Z	d
d„ Z
dd„ Z							ddd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Z‡  ZS ) r    a  socksocket([family[, type[, proto]]]) -> socket object
    Open a SOCKS enabled socket. The parameters are the same as
    those of the standard socket init. In order for SOCKS to work,
    you must specify family=AF_INET, type=SOCK_STREAM and proto=0.
    r   Nc                 C   s>   t  | ||||¡ td krt| _nd| _d | _d | _d| _d S )N)NNNNNNT)Ú
_orgsocketÚ__init__r   Ú_socksocket__proxyÚ_socksocket__proxysocknameÚ_socksocket__proxypeernameÚ_socksocket__httptunnel)ÚselfÚfamilyÚtypeÚprotoÚ_sockr   r   r   r$   ˜   s   
zsocksocket.__init__c                 C   sL   |   |¡}t|ƒ|k r$|   |t|ƒ ¡}|stdƒ‚|| }t|ƒ|k s|S )z®__recvall(count) -> data
        Receive EXACTLY the number of bytes requested from the socket.
        Blocks until the required number of bytes have been received.
        )r   zconnection closed unexpectedly)ÚrecvÚlenr   )r)   ÚcountÚdataÚdr   r   r   Ú	__recvall¤   s   
üzsocksocket.__recvallc                    s(   | j s|  |¡}tt| ƒj|g|¢R Ž S )zq override socket.socket.sendall method to rewrite the header
        for non-tunneling proxies if needed
        )r(   Ú_socksocket__rewriteproxyÚsuperr    Úsendall)r)   ÚcontentÚargs©Ú	__class__r   r   r6   ±   s   
zsocksocket.sendallc              	   C   sî   d\}}|  d¡}|D ]}| ¡  d¡r|}q| ¡  d¡s%| ¡  d¡r'|}q|rr|rr| |¡ | |¡ |  d¡d }|  d¡}| jd d	krX| jd
 d	krX| d|  ¡ ¡ | dd| ¡ | dd|d ||d |d f ¡ d |¡S )zÒ rewrite HTTP request headers to support non-tunneling proxies
        (i.e. those which do not support the CONNECT method).
        This only works for HTTP (not HTTPS) since HTTPS requires tunneling.
        )NNú
zhost:ÚgetÚpostú r   r   Né   r   zHost: %sz%s http://%s%s %sr   )ÚsplitÚlowerÚ
startswithÚremover%   ÚinsertÚ_socksocket__getauthheaderÚjoin)r)   ÚheaderÚhostÚendptÚhdrsÚhdrr   r   r   Ú__rewriteproxy¹   s$   
€


$
zsocksocket.__rewriteproxyc                 C   s*   | j d d | j d  }dt |¡ ¡  S )Nr   ó   :r?   zProxy-Authorization: Basic )r%   Úbase64Ú	b64encodeÚdecode)r)   Úauthr   r   r   Ú__getauthheaderÐ   s   zsocksocket.__getauthheaderTc                 C   s0   |||||r
|  ¡ nd|r|  ¡ nd|f| _dS )a
  setproxy(proxytype, addr[, port[, rdns[, username[, password]]]])

        Sets the proxy to be used.
        proxytype -    The type of the proxy to be used. Three types
                are supported: PROXY_TYPE_SOCKS4 (including socks4a),
                PROXY_TYPE_SOCKS5 and PROXY_TYPE_HTTP
        addr -        The address of the server (IP or DNS).
        port -        The port of the server. Defaults to 1080 for SOCKS
                servers and 8080 for HTTP proxy servers.
        rdns -        Should DNS queries be preformed on the remote side
                (rather than the local side). The default is True.
                Note: This has no effect with SOCKS4 servers.
        username -    Username to authenticate with to the server.
                The default is no authentication.
        password -    Password to authenticate with to the server.
                Only relevant when username is also provided.
        headers -     Additional or modified headers for the proxy connect
        request.
        N)Úencoder%   )r)   r   r   r   r   r   r   Úheadersr   r   r   ÚsetproxyÔ   s   
ùzsocksocket.setproxyc              	   C   sú  | j d dkr| j d dkr|  t ddddd¡¡ n|  t dddd¡¡ |  d¡}|dd… tdƒ ¡ krC|  ¡  tdt	d fƒ‚|dd… tdƒ ¡ krPn‘|dd… tdƒ ¡ krÃt
ƒ }| d¡ | t| j d ƒ¡ | | j d ¡ | t| j d ƒ¡ | | j d ¡ |  |¡ |  d¡}|dd… tdƒ ¡ krª|  ¡  tdt	d fƒ‚|dd… tdƒ ¡ krÂ|  ¡  td	td	 fƒ‚n|  ¡  |d td
ƒ ¡ krÙtdtd fƒ‚tdt	d fƒ‚t dddd¡}zt |¡}|tdƒ ¡  | }W n: tjy4   | j d	 r d}|td	ƒ ¡  tt|ƒƒ ¡  | ¡  }nt t |¡¡}|tdƒ ¡  | }Y nw |t d|¡ }|  |¡ |  d¡}|dd… tdƒ ¡ kr`|  ¡  tdt	d fƒ‚|dd… tdƒ ¡ kr˜|  ¡  t|dd… ƒdkrtt|dd… ƒtt|dd… ƒ fƒ‚tdtd fƒ‚|d	d… tdƒ ¡ kr«|  d¡}	n,|d	d… td	ƒ ¡ krË||  d¡ }|  t|dd… ƒ¡}	n|  ¡  tdt	d fƒ‚t d|  d¡¡d }
|	|
f| _|dkröt |¡|f| _dS ||f| _dS )zk__negotiatesocks5(self,destaddr,destport)
        Negotiates a connection through a SOCKS5 server.
        r   Nr?   ÚBBBBr   r   ÚBBBr   r   éÿ   ú>Hé   é	   )r%   r6   ÚstructÚpackÚ_socksocket__recvallÚchrrS   Úcloser   Ú_generalerrorsÚ	bytearrayÚappendr/   Úextendr   Ú_socks5autherrorsr   Ú	inet_atonÚerrorÚgethostbynameÚordr   Ú_socks5errorsr.   Úunpackr&   Ú	inet_ntoar'   )r)   ÚdestaddrÚdestportÚ
chosenauthÚpacketÚauthstatÚreqÚipaddrÚrespÚ	boundaddrÚ	boundportr   r   r   Ú__negotiatesocks5û   sŽ   



ý

ÿþýÿ€ò

(

zsocksocket.__negotiatesocks5c                 C   ó   | j S )zigetsockname() -> address info
        Returns the bound IP address and port number at the proxy.
        )r&   ©r)   r   r   r   Úgetproxysocknameb  s   zsocksocket.getproxysocknamec                 C   s
   t  | ¡S )z`getproxypeername() -> address info
        Returns the IP and port number of the proxy.
        )r#   Úgetpeernamery   r   r   r   Úgetproxypeernameh  s   
zsocksocket.getproxypeernamec                 C   rx   )z£getpeername() -> address info
        Returns the IP address and port number of the destination
        machine (note: getproxypeername returns the proxy)
        )r'   ry   r   r   r   r{   n  s   zsocksocket.getpeernamec              
   C   sÄ  d}zt  |¡}W n# t jy,   | jd r"t ddddd¡}d}nt  t  |¡¡}Y nw t ddd|¡| }| jd d	krE|| jd  }|tdƒ ¡  }|rY|| tdƒ ¡  }|  	|¡ |  
d
¡}|dd… tdƒ ¡ kr{|  ¡  tdtd fƒ‚|dd… tdƒ ¡ kr·|  ¡  t|dd… ƒdv r¯|  ¡  tt|dd… ƒtt|dd… ƒd  fƒ‚tdtd fƒ‚t  |dd	… ¡t d|dd… ¡d f| _|d	krÛt  |¡|f| _d	S ||f| _d	S )zk__negotiatesocks4(self,destaddr,destport)
        Negotiates a connection through a SOCKS4 server.
        Fr   rV   r   r   Tz>BBHr   NrZ   r   éZ   )é[   é\   é]   é^   rY   )r   rf   rg   r%   r\   r]   rh   r_   rS   r6   r^   r`   r   ra   ri   r   Ú_socks4errorsrl   rk   r&   r'   )r)   rm   rn   Úrmtrslvrs   rr   rt   r   r   r   Ú__negotiatesocks4u  sB   
€ú

,þzsocksocket.__negotiatesocks4c                 C   sÒ  | j d st |¡}n|}d|dt|ƒdg}d}d}| j d dkrA| j d  ¡ D ]\}}||d|d	g7 }| ¡ d
k}| ¡ dk}q(|sJ|d|d	g7 }|sb| j d dkrb| j d dkrb||  ¡ d	g7 }| d	¡ |  d 	|¡ 
¡ ¡ |  d¡}	|	 d 
¡ ¡dkr|	|  d¡ }	|	 d 
¡ ¡dks|	 ¡ d  d 
¡ d¡}
|
d d 
¡ d 
¡ fvr³|  ¡  tdtd fƒ‚zt|
d ƒ}W n tyÎ   |  ¡  tdtd fƒ‚w |dkrß|  ¡  t||
d fƒ‚d| _||f| _dS )zh__negotiatehttp(self,destaddr,destport)
        Negotiates a connection through an HTTP server.
        r   zCONNECT ú:z HTTP/1.1
Fé   Nz: r;   rH   zproxy-authorizationzHost: r   r?   Ú r   z

éÿÿÿÿr   r>   r   zHTTP/1.0zHTTP/1.1éÈ   )z0.0.0.0r   )r%   r   rh   ÚstrÚ	iteritemsrA   rE   rc   r6   rF   rS   r.   ÚfindÚ
splitlinesr@   r`   r   ra   ÚintÚ
ValueErrorr   r&   r'   )r)   rm   rn   r   rT   Úwrote_host_headerÚwrote_auth_headerÚkeyÚvalrt   Ú
statuslineÚ
statuscoder   r   r   Ú__negotiatehttp¨  sJ   


ÿþzsocksocket.__negotiatehttpc                 C   s  t |ƒttfvst|ƒdk st|d ttfƒrt |d ƒtkr'tdt	d fƒ‚| j
d tkrT| j
d dkr;| j
d }nd}t | | j
d |f¡ |  |d |d ¡ dS | j
d tkr| j
d dkrh| j
d }nd}t | | j
d |f¡ |  |d |d ¡ dS | j
d tkr®| j
d dkr•| j
d }nd}t | | j
d |f¡ |  |d |d ¡ dS | j
d tkræ| j
d dkrÂ| j
d }nd}t | | j
d |f¡ |d dkrá|  |d |d ¡ dS d	| _dS | j
d dkrût | |d |d f¡ dS td
t	d
 fƒ‚)zÿconnect(self, despair)
        Connects to the specified destination through a proxy.
        destpar - A tuple of the IP/DNS address and the port number.
        (identical to socket's connect).
        To select the proxy server use setproxy().
        r   r   r   r?   Ni8  i  i»  Fr   )r+   ÚlistÚtupler/   Ú
isinstancerŠ   ÚbytesrŽ   r   ra   r%   ÚPROXY_TYPE_SOCKS5r#   ÚconnectÚ_socksocket__negotiatesocks5ÚPROXY_TYPE_SOCKS4Ú_socksocket__negotiatesocks4ÚPROXY_TYPE_HTTPÚ_socksocket__negotiatehttpÚPROXY_TYPE_HTTP_NO_TUNNELr(   )r)   ÚdestpairÚportnumr   r   r   rœ   Õ  sF   	ÿ
zsocksocket.connect)NNNTNNN)r
   r   r   Ú__doc__r   ÚAF_INETÚSOCK_STREAMr$   r^   r6   r4   rE   rU   r   rz   r|   r{   rŸ   r¡   rœ   Ú__classcell__r   r   r9   r   r    ‘   s.    
ÿ
ø'g3-r    )NNNTNN)r¥   rN   r   r\   ÚsysÚgetattrÚImportErrorrž   r›   r    r¢   r   r#   Ú	Exceptionr   r   r   r   r   r   ra   rj   re   r‚   r   r"   r    r   r   r   r   Ú<module>   s6    %	
ÿ