AlkantarClanX12

Your IP : 13.58.200.16


Current Path : /lib/python3.6/site-packages/__pycache__/
Upload File :
Current File : //lib/python3.6/site-packages/__pycache__/socks.cpython-36.pyc

3

�2;Z~�@sXdZddlmZddlmZddlmZmZmZddl	Z	ddl
mZddlZddl
Z
ddl
mZddlZddlZddlZdZe
jd	kr�ejdCkr�yddlZWnek
r�ed��YnXeje�ZdZZd
ZZd
ZZeeed�Z e!e"e j#�e j$���Z%ejZ&Z'dd�Z(Gdd�de)�Z*Gdd�de*�Z+Gdd�de*�Z,Gdd�de*�Z-Gdd�de*�Z.Gdd�de*�Z/Gdd�de*�Z0dd d!d"�Z1d#d$d%d&d'd(d)d*d+�Z2ed,ed,ed-iZ3dDd/d0�Z4d1d2�Z5d3d4�Z6e6Z7d5d6�Z8e8Z9dEd7d8�Z:Gd9d:�d:ej�Z;d;d<�Z<xBdFD]:Ze=e;ed�Z>e?e>e��se;j@jAe�eBe;ee<e���qWGdAdB�dBe;�ZCdS)Ga�
SocksiPy - Python SOCKS module.

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 DAMAGE.


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
�)�	b64encode)�Callable)�
EOPNOTSUPP�EINVAL�EAGAINN)�BytesIO)�SEEK_CURz1.6.7�nt�z8To run PySocks on Windows you must install win_inet_pton��)�SOCKS4�SOCKS5�HTTPcstj���fdd��}|S)Ncsp|d}zNy$|j�}|dkr&|jd��||�Stk
rR}z�WYdd}~XnXWd|dkrj|jd�XdS)NrTF)�
gettimeout�setblocking�	Exception)�args�kwargs�selfZ_is_blocking�e)�function��/usr/lib/python3.6/socks.py�wrapper]s

z"set_self_blocking.<locals>.wrapper)�	functools�wraps)rrr)rr�set_self_blocking[src@s"eZdZdZddd�Zdd�ZdS)�
ProxyErrorz4Socket_err contains original socket.error exception.NcCs(||_||_|r$|jdj|�7_dS)Nz: {0})�msg�
socket_err�format)rrr rrr�__init__pszProxyError.__init__cCs|jS)N)r)rrrr�__str__wszProxyError.__str__)N)�__name__�
__module__�__qualname__�__doc__r"r#rrrrrns
rc@seZdZdS)�GeneralProxyErrorN)r$r%r&rrrrr({sr(c@seZdZdS)�ProxyConnectionErrorN)r$r%r&rrrrr)sr)c@seZdZdS)�SOCKS5AuthErrorN)r$r%r&rrrrr*�sr*c@seZdZdS)�SOCKS5ErrorN)r$r%r&rrrrr+�sr+c@seZdZdS)�SOCKS4ErrorN)r$r%r&rrrrr,�sr,c@seZdZdS)�	HTTPErrorN)r$r%r&rrrrr-�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)rrr
�����i8i�TcCs.|||||r|j�nd|r"|j�ndft_dS)z�Sets a default proxy.

    All further socksocket objects will use the default 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_proxy�sr?cOs d|kr|jd�|d<t||�S)N�	proxytyper9)�popr?)rrrrr�setdefaultproxy�srBcCstjS)z4Returns the default proxy, set by set_default_proxy.)r7r8rrrr�get_default_proxy�srCcCstjrt|j_ntd��dS)aAttempts 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)r7r8�socketr()�modulerrr�wrap_module�s
rFc
Cs8|\}
}|
jd�r|
jd�}
|r4|jd�r4|jd�}d}x�tj||dtj�D]�}
|
\}}}}}d}yxt|||�}|	r�x|	D]}|j|�qzWt|tt	f�r�|j
|�|r�|j||||||�|r�|j|�|j
|
|f�|Stjtfk
�r}z|}|�r
|j�d}WYdd}~XqLXqLW|�r*|�tjd��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�striprD�getaddrinfo�SOCK_STREAMr7Z
setsockopt�
isinstance�int�float�
settimeout�	set_proxy�bind�connect�errorr)�close)�	dest_pair�timeoutZsource_addressr9�
proxy_addr�
proxy_portZ
proxy_rdnsZproxy_usernameZproxy_passwordZsocket_optionsZremote_hostZremote_port�err�r�familyZsocket_type�protoZ	canonnameZsaZsock�optrrrr�create_connection�s>





r^c@seZdZdZdd�Ze�ZdS)�_BaseSocketzBAllows Python 2 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)r$r%r&r'r"�listrcrrrrr_sr_cs�fdd�S)Ncs|j�||�S)N)rb)rrfrg)rhrr�<lambda>sz_makemethod.<locals>.<lambda>r)rhr)rhr�_makemethodsrk�sendto�send�recvfrom�recvcs.eZdZdZdZejejdf�fdd�	Zdd�Z	�fdd	�Z
d
d�Zdd
�Zd5dd�Z
dd�Z�fdd�Z�fdd�Zd6�fdd�	Zd7�fdd�	Zdd�Z�fdd�Zdd �ZeZd!d"�ZeZd#d$�ZeZd%d&�Z�fd'd(�Zd)d*�Zd+d,�Zd-d.�Zd/d0�Z e!ee"ee#e iZ$e%�fd1d2��Z&d3d4�Z'�Z(S)8r7a2socksocket([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.
    Nrcsp|tjtjfkr"d}t|j|���tt|�j|||f|�|�d|_|j	rT|j	|_
nd|_
d|_d|_d|_
dS)Nz0Socket type must be stream or datagram, not {!r})NNNNNN)rDrK�
SOCK_DGRAM�
ValueErrorr!�superr7r"�
_proxyconnr8�proxy�proxy_sockname�proxy_peername�_timeout)rr[�typer\rrr)�	__class__rrr"1s
zsocksocket.__init__cCs@d}x6t|�|kr:|j|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.�zConnection closed unexpectedly)�len�readr()r�file�count�data�drrr�_readallCszsocksocket._readallcs@||_y|j�}tt|�j|j�Wntjk
r:YnXdS)N)rw�get_proxy_peernamerrr7rOrDrS)rrVZpeer)ryrrrOOszsocksocket.settimeoutcCs|jS)N)rw)rrrrrXszsocksocket.gettimeoutcCs|r|jd�n
|jd�dS)Ng)rO)r�vrrrr[szsocksocket.setblockingTcCs.|||||r|j�nd|r"|j�ndf|_dS)a� 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)r6rt)rr9r:r;r<r=r>rrrrPaszsocksocket.set_proxycOs"d|kr|jd�|d<|j||�S)Nr@r9)rArP)rrrrrr�setproxyvszsocksocket.setproxycs�|j\}}}}}}|s$|jtjkr6tj|f|�|�S|jrHtjtd��|t	kr`d}	tjt
|	��tt|�j||�|j
�\}
}d|f}t�|_|j�}
|jj|
�d}|j|j||�\}
}|
\}}
|\}
}tt|�j||f�tt|�j|j�d|_dS)	zVImplements proxy connection for UDP sockets.

        Happens during the bind() phase.z"Socket already bound to an addressz'UDP only supported by SOCKS5 proxy type�0��0.0.0.0rN)r�r)rtrxrDrpr`rQrsrSrrrrrr7Zgetsockname�_proxy_addrrR�_SOCKS5_requestrOrwru)rrfrgr9rWrXr<r=r>r�_r;�dstrtZ
UDP_ASSOCIATEZrelay�host)ryrrrQ{s*zsocksocket.bindc
s�|jtjkr$tt|�j|f|�|�S|js4|jd�|d}|dd�}t�}d}|j	|�d}|j	|�|j
||�tt|�j|j�|f|�|�}	|	|j
�S)	N�rrs�)r�r���r�)rxrDrprrr7rlrsrQr�write�_write_SOCKS5_addressrm�getvalue�tell)
r�bytesrrZaddress�flags�headerZRSVZ
STANDALONEZsent)ryrrrl�s


zsocksocket.sendtocs:|jtjkr |j|||jf|�Stt|�j||f|�SdS)N)rxrDrprlrvrrr7rm)rr�r�r)ryrrrm�szsocksocket.sendc	s�|jtjkrtt|�j||�S|js.|jd�ttt|�j	|d|��}|j
dt�|jd�}t
|�rntd��|j|�\}}|jr�|j\}}||ks�|d|fkr�tjtd��|j|�||ffS)	Nr�rirrzReceived UDP packet fragmentzPacket filtered)r�r)rxrDrprrr7rnrsrQrro�seekrr|�ord�NotImplementedError�_read_SOCKS5_addressrvrSr)	r�bufsizer�ZbufZfragZfromhostZfromportZpeerhostZpeerport)ryrrrn�s


zsocksocket.recvfromcOs|j||�\}}|S)N)rn)rrfrgr�r�rrrro�szsocksocket.recvcs|jr|jj�tt|�j�S)N)rsrTrrr7)r)ryrrrT�s
zsocksocket.closecCs|jS)z:Returns the bound IP address and port number at the proxy.)ru)rrrr�get_proxy_sockname�szsocksocket.get_proxy_socknamecCs|j�S)z>
        Returns the IP and port number of the proxy.
        )�getpeername)rrrrr��szsocksocket.get_proxy_peernamecCs|jS)zwReturns the IP address and port number of the destination machine.

        Note: get_proxy_peername returns the proxy.)rv)rrrr�get_peername�szsocksocket.get_peernamecGsd}|j|||�\|_|_dS)z7Negotiates a stream connection through a SOCKS5 server.�N)r�rvru)r�	dest_addrZCONNECTrrr�_negotiate_SOCKS5�szsocksocket._negotiate_SOCKS5cs�|j\}}}}}}	|jd�}
|jdd�}�z�|r@|	r@|
jd�n
|
jd�|
j�|j|d�}|dd�dkrvtd	��|dd�d
kr�|
jdtt|��j�|tt|	��j�|	�|
j�|j|d�}
|
dd�dkr�td	��|
dd�dkr�t	d
��n6|dd�dk�r4|dd�dk�r,t	d��ntd	��|
jd|d�|j
||
�}|
j�|j|d�}|dd�dk�r�td	��t|dd��}|dk�r�tj
|d�}tdj||���|j|�}tt|�j|j�||fS|j�|
j�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 rejectedr
z
Unknown errorz
{0:#04x}: {1}N)rt�makefiler��flushr�r(�chrr{r6r*r�r��
SOCKS5_ERRORS�getr+r!r�rrr7rOrwrT)rZconn�cmdr�r9r:r;r<r=r>�writer�readerZchosen_authZauth_statusZresolved�resp�statusrSZbnd)ryrrr��sX

.


zsocksocket._SOCKS5_requestcCsH|\}}|j\}}}}}}	tjdtjdi}
xrtjtjfD]b}yDtj||�}|j|
||�tj||�}|jtjd|��||fStj	k
r�w8Yq8Xq8W|r�|j
d�}
|jdtt|
��j
�|
�nbtj
||tjtjtjtj�}|d}|d}|dd}tj||�}|j|
||�tj||�}|jtjd|��||fS)z~
        Return the host and port packed for the SOCKS5 protocol,
        and the resolved address as a tuple object.
        r��z>H�idnar�rr1)rtrD�AF_INET�AF_INET6Z	inet_ptonr��	inet_ntop�struct�packrSr6r�r{rJZ	AF_UNSPECrKZIPPROTO_TCPZ
AI_ADDRCONFIG)rr:r}r�r;r9r�r<r=r>Zfamily_to_byter[�
addr_bytesZ
host_bytesZ	addressesZtarget_addrrrrr�Ms6
 z socksocket._write_SOCKS5_addresscCs�|j|d�}|dkr(tj|j|d��}nN|dkrN|j|d�}|j|t|��}n(|dkrntjtj|j|d��}ntd��tjd|j|d	��d
}||fS)Nrr�r1r�r��z%SOCKS5 proxy server sent invalid dataz>Hrr)	r�rD�	inet_ntoar�r�r�r(r��unpack)rr}Zatypr:Zlengthr;rrrr�zszsocksocket._read_SOCKS5_addresscCs�|j\}}}}}}|jd�}	|jdd�}
�zLd}ytj|�}Wn4tjk
rr|r^d}d}ntjtj|��}YnX|	jtjddd	|��|	j|�|r�|	j|�|	jd
�|r�|	j|j	d�d
�|	j
�|j|
d�}
|
dd	�d
kr�td
��t
|
d	d��}|dk�r&tj|d�}tdj||���tj|
dd��tjd|
dd��df|_|�rjtj|�|f|_n
||f|_Wd|
j�|	j�XdS)z0Negotiates a connection through a SOCKS4 server.r�r�rFsTz>BBHr1rr�r�r5z%SOCKS4 proxy server sent invalid datar�Zz
Unknown errorz
{0:#04x}: {1}Nz>H)rtr�rDZ	inet_atonrS�
gethostbynamer�r�r�r6r�r�r(r��
SOCKS4_ERRORSr�r,r!r�r�rurvrT)rr��	dest_portr9r:r;r<r=r>r�r�Zremote_resolver�r�r�rSrrr�_negotiate_SOCKS4�sH




zsocksocket._negotiate_SOCKS4cCsj|j\}}}}}}|r|ntj|�}d|jd�dt|�j�dd|jd�g}	|rv|rv|	jdt|d|��|	jd�|jdj|	��|j	�}
|
j
�}|
j�|s�td��y|j
d	d
�\}}
}Wntk
r�td��YnX|jd�s�td
��yt|
�}
Wntk
�r$td��YnX|
dk�rVdj|
|�}|
dk�rN|d7}t|��d|_||f|_dS)zwNegotiates 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}���za
[*] Note: The HTTP proxy server may not be supported by PySocks (must be a CONNECT tunnel proxy)�0.0.0.0rN)r�r�r�)r�r)rtrDr�r6�str�appendrZsendall�joinr��readlinerTr(�splitrqrHrMr-r!rurv)rr�r�r9r:r;r<r=r>Zhttp_headersZfobjZstatus_liner\Zstatus_codeZ
status_msgrSrrr�_negotiate_HTTP�sB 



zsocksocket._negotiate_HTTPcst|�dks|djd�r,tjdt|���|\}}|jtjkr~|jsP|jd
�tj	|�}|dkrp|rpd|_
n
||f|_
dS|j\}}}}}}	t|t
tf�s�t|�dks�|s�t|t�r�td��tt|�j|j�|dk�r||_
tt|�j|j�tt|�j||f�dS|j�}ytt|�j|�Wnntjk
�r�}
zN|j�|\}}d	j||�}t|}d
j||�}
tjd|
|
�t|
|
��WYdd}
~
XntXy|j|}||||�WnVtjk
�r�}
z|j�td|
��WYdd}
~
Xn tk
�r|j��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rrGz PySocks doesn't support IPv6: %sr�z0.0.0.0Nz0Invalid destination-connection (host, port) pairz{0}:{1}z!Error connecting to {0} proxy {1}z
%s due to: %szSocket error)r�r)r{rHrDrSr�rxrprsrQr�rvrtrLri�tuplerMr(rrr7rOrwrRr�rTr!�PRINTABLE_PROXY_TYPES�log�debugr)�_proxy_negotiatorsr)rrUr�r�r9rWrXr<r=r>rSZproxy_serverZprintable_typerZ	negotiate)ryrrrRs\	




zsocksocket.connectcCs4|j\}}}}}}|ptj|�}|s,td��||fS)zD
        Return proxy address to connect to as tuple object
        zInvalid proxy type)rt�
DEFAULT_PORTSr�r()rr9rWrXr<r=r>rrrr�]s
zsocksocket._proxy_addr)NNNTNN)r)r))r$r%r&r'r8rDr�rKr"r�rOrrrPr�rQrlrmrnrorTr�Zgetproxysocknamer�Zgetproxypeernamer�r�r�r�r�r�r�r�r
rrr�rrRr��
__classcell__rr)ryrr7&sD	
%Z-<<Wr7)r
r)NNNTNN)	NNNNNTNNN)rlrmrnro)Dr'�base64r�collectionsr�errnorrrr�iorZlogging�osrrDr��sys�__version__rh�version_infoZ
win_inet_pton�ImportErrorZ	getLoggerr$r�ZPROXY_TYPE_SOCKS4r
ZPROXY_TYPE_SOCKS5rZPROXY_TYPE_HTTPrZPROXY_TYPESra�zip�values�keysr�Z
_orgsocketr`r�IOErrorrr(r)r*r+r,r-r�r�r�r?rBrCZgetdefaultproxyrFZ
wrapmoduler^r_rkrd�methodrLrcr��setattrr7rrrr�<module>7s�





8