Viewing File: /opt/alt/python38/lib/python3.8/site-packages/__pycache__/socks.cpython-38.opt-1.pyc

U

"Ù@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„Z1d7D]:Z2e3e0e2dƒZ4e5e4e
ƒse0j6 7e2¡e8e0e2e1e2ƒƒqGd8d9„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ú	/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)ÚProxyConnectionErrorNrrrrrr Ysr c@seZdZdS)ÚSOCKS5AuthErrorNrrrrrr!Zsr!c@seZdZdS)ÚSOCKS5ErrorNrrrrrr"[sr"c@seZdZdS)ÚSOCKS4ErrorNrrrrrr#\sr#c@seZdZdS)Ú	HTTPErrorNrrrrrr$]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éééééi8iTcCs.|||||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_proxyssþr6cCstjS)z>
    Returns the default proxy, set by set_default_proxy.
    )r.r/rrrrÚget_default_proxy€sr7cCstjrt|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
r:c
CsF|\}
}|
 d¡r|
 d¡}
|r4| d¡r4| d¡}d}t ||dtj¡D]Þ}
|
\}}}}}d}z†t|||ƒ}|	dk	rŠ|	D]}|j|Žqzt|tt	fƒr¢| 
|¡|dk	r¾| ||||||¡|dk	rÐ| |¡| 
|
|f¡|WStjk
r&}z|}|dk	r| ¡d}W5d}~XYqJXqJ|dk	r8|‚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Ústripr8ÚgetaddrinfoÚSOCK_STREAMr.Z
setsockoptÚ
isinstanceÚintÚfloatZ
settimeoutÚ	set_proxyÚbindÚconnectÚerrorÚclose)Ú	dest_pairr0Ú
proxy_addrÚ
proxy_portZ
proxy_rdnsZproxy_usernameZproxy_passwordZtimeoutZsource_addressZsocket_optionsZremote_hostZremote_portÚerrÚrÚfamilyZsocket_typeÚprotoZ	canonnameZsaZsockÚoptÚerrrÚcreate_connection–sB



ÿ



rQc@seZdZdZdd„ZeƒZdS)Ú_BaseSocketzJAllows Python 2's "delegated" methods such as send() to be overridden
    cOsDtj|f|ž|Žtƒ|_|jD]}t||ƒ|j|<t||ƒq dSr)Ú_orig_socketrÚdictÚ
_savedmethodsÚ
_savenamesÚgetattrÚdelattr)rÚposÚkwÚnamerrrrÔs

z_BaseSocket.__init__N)rrrrrÚlistrVrrrrrRÑsrRcs‡fdd„S)Ncs|jˆ||ŽSr)rU)rrYrZ©r[rrÚ<lambda>ßóz_makemethod.<locals>.<lambda>rr]rr]rÚ_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)r8r?Ú
SOCK_DGRAMÚ
ValueErrorrrRrÚ
_proxyconnr/ÚproxyÚproxy_socknameÚproxy_peername)rrMÚtyperNÚargsÚkwargsrrrrrös
zsocksocket.__init__cCs<d}t|ƒ|kr8| |t|ƒ¡}|s.tdƒ‚||7}q|S)z›
        Receive EXACTLY the number of bytes requested from the file object.
        Blocks until the required number of bytes have been received.
        r_úConnection 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-rh)rr0r1r2r3r4r5rrrrCsþzsocksocket.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ó)ú0.0.0.0rN)rhrkr8rerSrDrgrFrr
rrRZgetsocknameÚ_proxy_addrrEÚ_SOCKS5_requestri)rrYrZr0rIrJr3r4r5rÚ_r2ÚdstrhZ
UDP_ASSOCIATEZrelayÚhostrrrrD*s(zsocksocket.bindc
Osš|jtjkr tj||f|ž|ŽS|js0| d¡|d}|dd…}tƒ}d}| |¡d}| |¡| 	||¡tj
|| ¡|f|ž|Ž}	|	| ¡S)N©Úréÿÿÿÿsó)
rkr8rerRrargrDrÚwriteÚ_write_SOCKS5_addressrbÚgetvalueÚtell)
rÚbytesrlrmZaddressÚflagsÚheaderZRSVZ
STANDALONEZsentrrrraNs


zsocksocket.sendtocKs6|jtjkr |j|||jf|ŽStj|||f|ŽSdSr)rkr8rerarjrRrb)rr†r‡rmrrrrbaszsocksocket.sendc	Cs°|jtjkrt |||¡S|js*| d¡tt |||¡ƒ}| 	dt
¡| d¡}t|ƒrbt
dƒ‚| |¡\}}|jr |j\}}||ks”|d|fkr t td¡‚| ¡||ffS)Nr~r
r	zReceived UDP packet fragmentrzPacket filtered)rkr8rerRrcrgrDrrdÚseekrrpÚordÚNotImplementedErrorÚ_read_SOCKS5_addressrjrFr)	rÚbufsizer‡ZbufZfragZfromhostZfromportZpeerhostZpeerportrrrrcgs


zsocksocket.recvfromcOs|j||Ž\}}|Sr)rc)rrYrZr†r{rrrrd{szsocksocket.recvcCs|jr|j ¡t |¡Sr)rgrGrRrrrrrGs
zsocksocket.closecCs|jS)zL
        Returns the bound IP address and port number at the proxy.
        )rirrrrÚget_proxy_sockname„szsocksocket.get_proxy_socknamecCs
t |¡S)z>
        Returns the IP and port number of the proxy.
        )rRÚgetpeernamerrrrÚ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)
        )rjrrrrÚget_peername”szsocksocket.get_peernamecGsd}| |||¡\|_|_dS)zI
        Negotiates a stream connection through a SOCKS5 server.
        óN)rzrjri)rÚ	dest_addrZCONNECTrrrÚ_negotiate_SOCKS5s
ÿzsocksocket._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kr4|dd…dkr,t
dƒ‚ntd	ƒ‚|
 d|d¡| ||
¡}|
 ¡| |d¡}|dd…dkr€td	ƒ‚t|dd…ƒ}|dkr¶t
 |d¡}td ||¡ƒ‚| |¡}||fW¢S| ¡|
 ¡XdS)z‘
        Send SOCKS5 request with given command (CMD field) and
        address (DST field). Returns resolved DST address that was used.
        ÚwbÚrbrssr
r	óú%SOCKS5 proxy server sent invalid dataór’rzSOCKS5 authentication failedóÿz7All offered SOCKS5 authentication methods were rejectedrú
Unknown errorú
{0:#04x}: {1}N)rhÚmakefilerGr‚ÚflushrurÚchrror-r!rƒrŠÚ
SOCKS5_ERRORSÚgetr"rrŒ)rZconnÚcmdr|r0r1r2r3r4r5ÚwriterÚreaderZchosen_authZauth_statusZresolvedÚrespÚstatusrFZbndrrrrz¥sV

ÿþý



zsocksocket._SOCKS5_requestc	CsL|\}}|j\}}}}}}	tjdtjdi}
tjtjfD]j}zJt ||¡}| |
||¡t ||¡}| t d|¡¡||fWStj	k
ržYq6Yq6Xq6|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’óú>HÚidnarwrr()rhr8ÚAF_INETÚAF_INET6Z	inet_ptonr‚Ú	inet_ntopÚstructÚpackrFr-rŸror>Z	AF_UNSPECr?ZIPPROTO_TCPZ
AI_ADDRCONFIG)rr1rqr}r2r0r{r3r4r5Zfamily_to_byterMÚ
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(rwr§ér˜r¨r
r)	rur8Ú	inet_ntoarŠr¬r«rr­Úunpack)rrqZatypr1Zlengthr2rrrrŒ!szsocksocket._read_SOCKS5_addresscCsŽ|j\}}}}}}| d¡}	| dd¡}
zLd}zt |¡}Wn4tjk
rr|r^d}d}nt t |¡¡}YnX|	 t 	ddd	|¡¡|	 |¡|r¢|	 |¡|	 d
¡|rÄ|	 | 
d¡d
¡|	 ¡| |
d¡}
|
dd	…d
krðt
d
ƒ‚t|
d	d…ƒ}|dkr&t |d¡}td ||¡ƒ‚t |
dd…¡t d|
dd…¡df|_|rjt |¡|f|_n
||f|_W5|
 ¡|	 ¡XdS)zB
        Negotiates a connection through a SOCKS4 server.
        r•r–rFsTz>BBHr(r	rr©r,z%SOCKS4 proxy server sent invalid datar
éZr›rœNr¨)rhrrGr8Z	inet_atonrFÚ
gethostbynamer‚r­r®r-ržrurrŠÚ
SOCKS4_ERRORSr¡r#rr±r²rirj)rr“Ú	dest_portr0r1r2r3r4r5r£r¤Zremote_resolver¯r¥r¦rFrrrÚ_negotiate_SOCKS40sB




,zsocksocket._negotiate_SOCKS4cCsj|j\}}}}}}|r|nt |¡}d| d¡dt|ƒ ¡dd| d¡g}	|rv|rv|	 dt|d|ƒ¡|	 d¡| d |	¡¡| 	¡}
|
 
¡}|
 ¡|s´tdƒ‚z| 
d	d
¡\}}
}Wntk
rætdƒ‚YnX| d¡sútd
ƒ‚zt|
ƒ}
Wntk
r$tdƒ‚YnX|
dkrVd |
|¡}|
dkrN|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
rnú 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})ii“i•za
[*] Note: The HTTP proxy server may not be supported by PySocks (must be a CONNECT tunnel proxy))s0.0.0.0rN)rhr8r´r-ÚstrÚappendrZsendallÚjoinrÚreadlinerGrÚsplitrfr<rAr$rrirj)rr“r¶r0r1r2r3r4r5Zhttp_headersZfobjZstatus_linerNZstatus_codeZ
status_msgrFrrrÚ_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| ¡}zt ||¡Wn`tjk
rP}
z>| ¡|\}}d	 ||¡}t|}d
 ||¡}
t|
|
ƒ‚W5d}
~
XYntXz|j|}||||ƒWnVtjk
r¤}
z| ¡td|
ƒ‚W5d}
~
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
rr;zPySocks doesn't support IPv6r~rxNz0Invalid destination-connection (host, port) pairz{0}:{1}z!Error connecting to {0} proxy {1}zSocket error)ror<r8rFrkrergrDr´rjrhr@r\ÚtuplerArrRrEryrGrÚPRINTABLE_PROXY_TYPESr Ú_proxy_negotiatorsr)rrHr“r¶r0rIrJr3r4r5rFZproxy_serverZprintable_typerZ	negotiaterrrrE¨sZ




ÿþýÿ
zsocksocket.connectcCs4|j\}}}}}}|pt |¡}|s,tdƒ‚||fS)zD
        Return proxy address to connect to as tuple object
        zInvalid proxy type)rhÚ
DEFAULT_PORTSr¡r)rr0rIrJr3r4r5rrrryõs
zsocksocket._proxy_addr)NNNTNN)r)r)$rrrrr/r8rªr?rrurCZsetproxyrDrarbrcrdrGrŽZgetproxysocknamerZgetproxypeernamer‘rr”rzrƒrŒr·rÀrr
rrÃrEryrrrrr.ësB
$

R*;7ýMr.)NNNTNN)	NNNTNNNNN):rÚ__version__r8r­ÚerrnorrrÚiorÚosrÚcollectionsrÚbase64rZPROXY_TYPE_SOCKS4rZPROXY_TYPE_SOCKS5r
ZPROXY_TYPE_HTTPrZPROXY_TYPESrTÚzipÚvaluesÚkeysrÂZ
_orgsocketrSÚIOErrorrrr r!r"r#r$rµr rÄr6Zsetdefaultproxyr7Zgetdefaultproxyr:Z
wrapmodulerQrRr`r[rWÚmethodr@rVr¼Úsetattrr.rrrrÚ<module>s~6
þù
þ
ü
;

Back to Directory File Manager