Your IP : 18.119.112.208


Current Path : /opt/alt/python37/lib/python3.7/site-packages/__pycache__/
Upload File :
Current File : //opt/alt/python37/lib/python3.7/site-packages/__pycache__/socks.cpython-37.pyc

B

"�@Wu�	@s�dZdZddlZddlZddlmZmZmZddlm	Z	ddl
mZddlm
Z
ddlmZd	ZZd
ZZdZZeeed�Zeee��e����ZejZZGd
d�de�ZGdd�de�Z Gdd�de�Z!Gdd�de�Z"Gdd�de�Z#Gdd�de�Z$Gdd�de�Z%dddd�Z&dd d!d"d#d$d%d&d'�Z'ed(ed(ed)iZ(d:d+d,�Z)e)Z*d-d.�Z+e+Z,d/d0�Z-e-Z.d;d1d2�Z/Gd3d4�d4ej�Z0d5d6�Z1xBd7D]:Z2e3e0e2d�Z4e5e4e
��s�e0j6�7e2�e8e0e2e1e2���q�WGd8d9�d9e0�Z9dS)<a�

SocksiPy - Python SOCKS module.
Version 1.5.7

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

Modifications made by Anorov (https://github.com/Anorov)
-Forked and renamed to PySocks
-Fixed issue with HTTP proxy failure checking (same bug that was in the old ___recvall() method)
-Included SocksiPyHandler (sockshandler.py), to be used as a urllib2 handler,
 courtesy of e000 (https://github.com/e000): https://gist.github.com/869791#file_socksipyhandler.py
-Re-styled code to make it readable
    -Aliased PROXY_TYPE_SOCKS5 -> SOCKS5 etc.
    -Improved exception handling and output
    -Removed irritating use of sequence indexes, replaced with tuple unpacked variables
    -Fixed up Python 3 bytestring handling - chr(0x03).encode() -> b""
    -Other general fixes
-Added clarification that the HTTP proxy connection method only supports CONNECT-style tunneling HTTP proxies
-Various small bug fixes
z1.5.7�N)�
EOPNOTSUPP�EINVAL�EAGAIN)�BytesIO)�SEEK_CUR)�Callable)�	b64encode���)�SOCKS4�SOCKS5�HTTPc@s"eZdZdZddd�Zdd�ZdS)�
ProxyErrorz>
    socket_err contains original socket.error exception.
    NcCs(||_||_|r$|jd�|�7_dS)Nz: {0})�msg�
socket_err�format)�selfrr�r�6/opt/alt/python37/lib/python3.7/site-packages/socks.py�__init__NszProxyError.__init__cCs|jS)N)r)rrrr�__str__UszProxyError.__str__)N)�__name__�
__module__�__qualname__�__doc__rrrrrrrJs
rc@seZdZdS)�GeneralProxyErrorN)rrrrrrrrXsrc@seZdZdS)�ProxyConnectionErrorN)rrrrrrrrYsrc@seZdZdS)�SOCKS5AuthErrorN)rrrrrrrrZsrc@seZdZdS)�SOCKS5ErrorN)rrrrrrrr[src@seZdZdS)�SOCKS4ErrorN)rrrrrrrr \sr c@seZdZdS)�	HTTPErrorN)rrrrrrrr!]sr!zRequest rejected or failedzLRequest rejected because SOCKS server cannot connect to identd on the clientzPRequest rejected because the client program and identd report different user-ids)�[�\�]zGeneral SOCKS server failurez!Connection not allowed by rulesetzNetwork unreachablezHost unreachablezConnection refusedzTTL expiredz(Command not supported, or protocol errorzAddress type not supported)r	r
r�����i8i�TcCs.|||||r|��nd|r"|��ndft_dS)z�
    set_default_proxy(proxy_type, addr[, port[, rdns[, username, password]]])

    Sets a default proxy which all further socksocket objects will use,
    unless explicitly changed. All parameters are as for socket.set_proxy().
    N)�encode�
socksocket�
default_proxy)�
proxy_type�addr�port�rdns�username�passwordrrr�set_default_proxyssr3cCstjS)z>
    Returns the default proxy, set by set_default_proxy.
    )r+r,rrrr�get_default_proxy�sr4cCstjrt|j_ntd��dS)a"
    Attempts to replace a module's socket library with a SOCKS socket. Must set
    a default proxy using set_default_proxy(...) first.
    This will only work on modules that import socket directly into the namespace;
    most of the Python Standard Library falls into this category.
    zNo default proxy specifiedN)r+r,�socketr)�modulerrr�wrap_module�s
r7c
CsH|\}
}|
�d�r|
�d�}
|r4|�d�r4|�d�}d}x�t�||dtj�D]�}
|
\}}}}}d}y�t|||�}|	dk	r�x|	D]}|j|�q~Wt|tt	f�r�|�
|�|dk	r�|�||||||�|dk	r�|�|�|�
|
|f�|Stjk
�r&}z|}|dk	�r|��d}Wdd}~XYqLXqLW|dk	�r:|�t�d��dS)a�create_connection(dest_pair, *[, timeout], **proxy_args) -> socket object

    Like socket.create_connection(), but connects to proxy
    before returning the socket object.

    dest_pair - 2-tuple of (IP/hostname, port).
    **proxy_args - Same args passed to socksocket.set_proxy() if present.
    timeout - Optional socket timeout value, in seconds.
    source_address - tuple (host, port) for the socket to bind to as its source
    address before connecting (only for compatibility)
    �[z[]Nrzgai returned empty list.)�
startswith�stripr5�getaddrinfo�SOCK_STREAMr+Z
setsockopt�
isinstance�int�floatZ
settimeout�	set_proxy�bind�connect�error�close)�	dest_pairr-�
proxy_addr�
proxy_portZ
proxy_rdnsZproxy_usernameZproxy_password�timeoutZsource_addressZsocket_optionsZremote_hostZremote_port�err�r�familyZsocket_type�protoZ	canonnameZsaZsock�opt�errr�create_connection�s>







rOc@seZdZdZdd�Ze�ZdS)�_BaseSocketzJAllows Python 2's "delegated" methods such as send() to be overridden
    cOsHtj|f|�|�t�|_x(|jD]}t||�|j|<t||�q"WdS)N)�_orig_socketr�dict�
_savedmethods�
_savenames�getattr�delattr)r�pos�kw�namerrrr�s
z_BaseSocket.__init__N)rrrrr�listrTrrrrrP�srPcs�fdd�S)Ncs|j�||�S)N)rS)rrWrX)rYrr�<lambda>��z_makemethod.<locals>.<lambda>r)rYr)rYr�_makemethod�sr])�sendto�send�recvfrom�recvc@s�eZdZdZdZejejdfdd�Zdd�Z	d-d	d
�Z
e
Zdd�Zd
d�Z
d.dd�Zd/dd�Zdd�Zdd�Zdd�ZeZdd�ZeZdd�ZeZdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zeeeee eiZ!d)d*�Z"d+d,�Z#dS)0r+a2socksocket([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 and proto=0.
    The "type" argument must be either SOCK_STREAM or SOCK_DGRAM.
    NrcOsf|tjtjfkr"d}t|�|���tj||||f|�|�d|_|jrP|j|_	nd|_	d|_
d|_dS)Nz0Socket type must be stream or datagram, not {!r})NNNNNN)r5r<�
SOCK_DGRAM�
ValueErrorrrPr�
_proxyconnr,�proxy�proxy_sockname�proxy_peername)rrK�typerL�args�kwargsrrrrr�s
zsocksocket.__init__cCs@d}x6t|�|kr:|�|t|��}|s0td��||7}qW|S)z�
        Receive EXACTLY the number of bytes requested from the file object.
        Blocks until the required number of bytes have been received.
        r\zConnection closed unexpectedly)�len�readr)r�file�count�data�drrr�_readallszsocksocket._readallTcCs.|||||r|��nd|r"|��ndf|_dS)a�set_proxy(proxy_type, addr[, port[, rdns[, username[, password]]]])
        Sets the proxy to be used.

        proxy_type -    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 performed 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.
        N)r*re)rr-r.r/r0r1r2rrrr@szsocksocket.set_proxycOs�|j\}}}}}}|r"|jtjkr4tj|f|�|�S|jrFt�td��|t	kr^d}	t�t
|	��tj|f|�|�|��\}
}d|f}t�|_|�
�}
|j�|
�d}|�|j||�\}
}|
\}}
|\}
}t�|||f�d|_dS)zm
        Implements proxy connection for UDP sockets,
        which happens during the bind() phase.
        z"Socket already bound to an addressz'UDP only supported by SOCKS5 proxy type�0�)z0.0.0.0rN)rerhr5rbrQrArdrCrr
rrPZgetsockname�_proxy_addrrB�_SOCKS5_requestrf)rrWrXr-rFrGr0r1r2r�_r/�dstreZ
UDP_ASSOCIATEZrelay�hostrrrrA*s(zsocksocket.bindc
Os�|jtjkr tj||f|�|�S|js0|�d�|d}|dd�}t�}d}|�|�d}|�|�|�	||�tj
||��|f|�|�}	|	|��S)N)�r���s�)
rhr5rbrPr^rdrAr�write�_write_SOCKS5_addressr_�getvalue�tell)
r�bytesrirjZaddress�flags�headerZRSVZ
STANDALONEZsentrrrr^Ns


zsocksocket.sendtocKs6|jtjkr |j|||jf|�Stj|||f|�SdS)N)rhr5rbr^rgrPr_)rr�r�rjrrrr_aszsocksocket.sendc	Cs�|jtjkrt�|||�S|js*|�d�tt�|||��}|�	dt
�|�d�}t|�rbt
d��|�|�\}}|jr�|j\}}||ks�|d|fkr�t�td��|��||ffS)N)ryrr
r	zReceived UDP packet fragmentrzPacket filtered)rhr5rbrPr`rdrArra�seekrrl�ord�NotImplementedError�_read_SOCKS5_addressrgrCr)	r�bufsizer��bufZfragZfromhostZfromportZpeerhostZpeerportrrrr`gs


zsocksocket.recvfromcOs|j||�\}}|S)N)r`)rrWrXr�rvrrrra{szsocksocket.recvcCs|jr|j��t�|�S)N)rdrDrP)rrrrrDs
zsocksocket.closecCs|jS)zL
        Returns the bound IP address and port number at the proxy.
        )rf)rrrr�get_proxy_sockname�szsocksocket.get_proxy_socknamecCs
t�|�S)z>
        Returns the IP and port number of the proxy.
        )rP�getpeername)rrrr�get_proxy_peername�szsocksocket.get_proxy_peernamecCs|jS)z�
        Returns the IP address and port number of the destination
        machine (note: get_proxy_peername returns the proxy)
        )rg)rrrr�get_peername�szsocksocket.get_peernamecGsd}|�|||�\|_|_dS)zI
        Negotiates a stream connection through a SOCKS5 server.
        �N)rurgrf)r�	dest_addrZCONNECTrrr�_negotiate_SOCKS5�szsocksocket._negotiate_SOCKS5cCs�|j\}}}}}}	|�d�}
|�dd�}�z�|r@|	r@|
�d�n
|
�d�|
��|�|d�}|dd�dkrvtd	��|dd�d
kr�|
�dtt|����|tt|	����|	�|
��|�|d�}
|
dd�dkr�td	��|
dd�dkr�t	d
��n6|dd�dk�r4|dd�dk�r,t	d��ntd	��|
�d|d�|�
||
�}|
��|�|d�}|dd�dk�r�td	��t|dd��}|dk�r�t�
|d�}td�||���|�|�}||fS|��|
��XdS)z�
        Send SOCKS5 request with given command (CMD field) and
        address (DST field). Returns resolved DST address that was used.
        �wb�rbrssr
r	�z%SOCKS5 proxy server sent invalid data�r�r{zSOCKS5 authentication failed��z7All offered SOCKS5 authentication methods were rejectedrz
Unknown errorz
{0:#04x}: {1}N)re�makefiler|�flushrqr�chrrkr*rr}r��
SOCKS5_ERRORS�getrrr�rD)rZconn�cmdrwr-r.r/r0r1r2�writer�readerZchosen_authZauth_statusZresolved�resp�statusrCZbndrrrru�sL

.



zsocksocket._SOCKS5_requestc	CsH|\}}|j\}}}}}}	tjdtjdi}
xrtjtjfD]b}yDt�||�}|�|
||�t�||�}|�t�d|��||fStj	k
r�w8Yq8Xq8W|r�|�
d�}
|�dtt|
���
�|
�nbt�
||tjtjtjtj�}|d}|d}|dd}t�||�}|�|
||�t�||�}|�t�d|��||fS)z~
        Return the host and port packed for the SOCKS5 protocol,
        and the resolved address as a tuple object.
        r��z>H�idnarsrr%)rer5�AF_INET�AF_INET6Z	inet_ptonr|�	inet_ntop�struct�packrCr*r�rkr;Z	AF_UNSPECr<ZIPPROTO_TCPZ
AI_ADDRCONFIG)rr.rmrxr/r-rvr0r1r2Zfamily_to_byterK�
addr_bytesZ
host_bytesZ	addressesZtarget_addrrrrr}�s0
 z socksocket._write_SOCKS5_addresscCs�|�|d�}|dkr(t�|�|d��}nN|dkrN|�|d�}|�|t|��}n(|dkrnt�tj|�|d��}ntd��t�d|�|d	��d
}||fS)Nr	r�r%rsr��z%SOCKS5 proxy server sent invalid dataz>Hr
r)	rqr5�	inet_ntoar�r�r�rr��unpack)rrmZatypr.�lengthr/rrrr�!szsocksocket._read_SOCKS5_addresscCs�|j\}}}}}}|�d�}	|�dd�}
�zLd}yt�|�}Wn4tjk
rr|r^d}d}nt�t�|��}YnX|	�t�ddd	|��|	�|�|r�|	�|�|	�d
�|r�|	�|�	d�d
�|	�
�|�|
d�}
|
dd	�d
kr�td
��t
|
d	d��}|dk�r&t�|d�}td�||���t�|
dd��t�d|
dd��df|_|�rjt�|�|f|_n
||f|_Wd|
��|	��XdS)zB
        Negotiates a connection through a SOCKS4 server.
        r�r�rFsTz>BBHr%r	r{r�r)z%SOCKS4 proxy server sent invalid datar
�Zz
Unknown errorz
{0:#04x}: {1}Nz>H)rer�r5Z	inet_atonrC�
gethostbynamer|r�r�r*r�rqrr��
SOCKS4_ERRORSr�r rr�r�rfrgrD)rr��	dest_portr-r.r/r0r1r2r�r�Zremote_resolver�r�r�rCrrr�_negotiate_SOCKS40sB




,zsocksocket._negotiate_SOCKS4cCsj|j\}}}}}}|r|nt�|�}d|�d�dt|���dd|�d�g}	|rv|rv|	�dt|d|��|	�d�|�d�|	��|�	�}
|
�
�}|
��|s�td��y|�
d	d
�\}}
}Wntk
r�td��YnX|�d�s�td
��yt|
�}
Wntk
�r$td��YnX|
dk�rVd�|
|�}|
dk�rN|d7}t|��d|_||f|_dS)z�
        Negotiates a connection through an HTTP server.
        NOTE: This currently only supports HTTP CONNECT-style proxies.
        sCONNECT r��:s	 HTTP/1.1sHost: sProxy-Authorization: basic s
zConnection closed unexpectedly� r
z'HTTP proxy server sent invalid responsezHTTP/z0Proxy server does not appear to be an HTTP proxyz4HTTP proxy server did not return a valid HTTP status��z{0}: {1})i�i�i�za
[*] Note: The HTTP proxy server may not be supported by PySocks (must be a CONNECT tunnel proxy))s0.0.0.0rN)rer5r�r*�str�appendrZsendall�joinr��readlinerDr�splitrcr9r>r!rrfrg)rr�r�r-r.r/r0r1r2Zhttp_headersZfobjZstatus_linerLZstatus_codeZ
status_msgrCrrr�_negotiate_HTTPks< 



zsocksocket._negotiate_HTTPc
Cs�t|�dks|d�d�r$t�d��|\}}|jtjkrt|jsH|�d�t�|�}|dkrf|sfd|_	n
||f|_	dS|j
\}}}}}}	t|tt
f�r�t|�dks�|r�t|t�s�td��|dkr�||_	t�|||f�dS|��}yt�||�Wn`tjk
�rP}
z>|��|\}}d	�||�}t|}d
�||�}
t|
|
��Wdd}
~
XYntXy|j|}||||�WnVtjk
�r�}
z|��td|
��Wdd}
~
XYn tk
�r�|���YnXdS)z�
        Connects to the specified destination through a proxy.
        Uses the same API as socket's connect().
        To select the proxy server, use set_proxy().

        dest_pair - 2-tuple of (IP/hostname, port).
        r
rr8zPySocks doesn't support IPv6)ryrz0.0.0.0Nz0Invalid destination-connection (host, port) pairz{0}:{1}z!Error connecting to {0} proxy {1}zSocket error)rkr9r5rCrhrbrdrAr�rgrer=rZ�tupler>rrPrBrtrDr�PRINTABLE_PROXY_TYPESr�_proxy_negotiatorsr)rrEr�r�r-rFrGr0r1r2rCZproxy_serverZprintable_typerZ	negotiaterrrrB�sR





zsocksocket.connectcCs4|j\}}}}}}|pt�|�}|s,td��||fS)zD
        Return proxy address to connect to as tuple object
        zInvalid proxy type)re�
DEFAULT_PORTSr�r)rr-rFrGr0r1r2rrrrt�s
zsocksocket._proxy_addr)NNNTNN)r)r)$rrrrr,r5r�r<rrqr@ZsetproxyrAr^r_r`rarDr�Zgetproxysocknamer�Zgetproxypeernamer�r�r�rur}r�r�r�rr
rr�rBrtrrrrr+�s:
$

R*;7Mr+)NNNTNN)	NNNTNNNNN):r�__version__r5r��errnorrr�ior�osr�collectionsr�base64rZPROXY_TYPE_SOCKS4rZPROXY_TYPE_SOCKS5r
ZPROXY_TYPE_HTTPrZPROXY_TYPESrR�zip�values�keysr�Z
_orgsocketrQ�IOErrorrrrrrr r!r�r�r�r3Zsetdefaultproxyr4Zgetdefaultproxyr7Z
wrapmodulerOrPr]rYrU�methodr=rTr��setattrr+rrrr�<module>5sl


7



?>