ZճcLFdZddlZddlZddlZddlZeeddeddZdZdZ dZ da ejZ Gd d e ZGd d eZGd deZGddeZGddeZGddeZdZdZdZdZ ddZdZGddejZy)aSocksiPy - 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. Nsocketz-socket.socket missing, proxy support unusablec eZdZy) ProxyErrorN__name__ __module__ __qualname__0/usr/lib/python3/dist-packages/httplib2/socks.pyr r 7rr c eZdZy)GeneralProxyErrorNr rrrrr;rrrc eZdZy)Socks5AuthErrorNr rrrrr?rrrc eZdZy) Socks5ErrorNr rrrrrCrrrc eZdZy) Socks4ErrorNr rrrrrGrrrc eZdZy) HTTPErrorNr rrrrrKrrr)successz invalid dataz not connectedz not availablezbad proxy typez bad input) succeededzgeneral SOCKS server failurez!connection not allowed by rulesetzNetwork unreachablezHost unreachablezConnection refusedz TTL expiredzCommand not supportedzAddress type not supportedz Unknown error)rzauthentication is requiredz0all offered authentication methods were rejectedz$unknown username or invalid password unknown error)zrequest grantedzrequest rejected or failedzLrequest rejected because SOCKS server cannot connect to identd on the clientzPrequest rejected because the client program and identd report different user-idsrc||||||fay)zsetdefaultproxy(proxytype, addr[, port[, rdns[, username[, password]]]]) Sets a default proxy which all further socksocket objects will use, unless explicitly changed. N) _defaultproxy) proxytypeaddrportrdnsusernamepasswords rsetdefaultproxyr'wsdD(HEMrcVtdk7rt|j_ytd)a7wrapmodule(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)rzno proxy specified)r socksocketrr)modules r wrapmoduler+s$)  9::rceZdZdZej ej ddfdZdZfdZ dZ dZ dd Z d Z d Zd Zd ZdZdZdZxZS)r)asocksocket([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. rNctj|||||tdk7r t|_nd|_d|_d|_d|_y)N)NNNNNNT) _orgsocket__init__r _socksocket__proxy_socksocket__proxysockname_socksocket__proxypeername_socksocket__httptunnel)selffamilytypeproto_socks rr/zsocksocket.__init__sH D&$u= D (DL?DL## rc|j|}t||kr>|j|t|z }|s td||z}t||kr>|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. )rzconnection closed unexpectedly)recvlenr)r4countdatads r __recvallzsocksocket.__recvallsb yy$i% %#d)+,A'(MNN!8D $i%  rcf|js|j|}tt||g|S)zq override socket.socket.sendall method to rewrite the header for non-tunneling proxies if needed )r3_socksocket__rewriteproxysuperr)sendall)r4contentargs __class__s rrCzsocksocket.sendalls5  ))'2GZ.w>>>rc d\}}|jd}|D]e}|jjdr|}%|jjds |jjdsd|}g|r|r|j||j||jdd}|jd}|jdd k7r2|jd d k7r |j d |j |j d d |z|j d |d d ||dd|ddj|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 zhost:getpost rrNrzHost: %sz http://r)splitlower startswithremover0insert_socksocket__getauthheaderjoin)r4headerhostendpthdrshdrs r__rewriteproxyzsocksocket.__rewriteproxys* ! e||F# Cyy{%%g.''.#))+2H2H2P   E KK  KK ::c?1%DKK$E||A$&4<<?d+B At3356 KK:, - KK%(D%(ERSHU V{{4  rc|jddz|jdz}dtj|jzS)Nr:rLzProxy-Authorization: Basic )r0base64 b64encodedecode)r4auths r__getauthheaderzsocksocket.__getauthheaders@||A% Q7,v/?/?/E/L/L/NNNrch|||||r|jnd|r|jnd|f|_y)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)encoder0)r4r!r"r#r$r%r&headerss rsetproxyzsocksocket.setproxys8<    !)HOO t!)HOO t   rc ~ |jddk7r;|jddk7r)|jtjdddddn'|jtjdddd|j d}|ddt dj k7r$|jtdtdf|ddt dj k(rn|ddt dj k(rMt}|jd|jt|jd|j|jd|jt|jd|j|jd|j||j d}|ddt dj k7r$|jtdtdf|ddt dj k7r{|jtd td f|j|dt d j k(rtdtdftdtdftjdddd} t!j"|}|t dj z|z}|tjd |z}|j||j d}|ddt dj k7r$|jtdtdf|ddt dj k7ra|jt)|ddd kr,t+t)|ddt,t)|ddft+d t,d f|d dt dj k(r|j d} nu|d dt d j k(r2||j/dz}|j t)|dd} n$|jtdtdftj0d |j dd} | | f|_|dk7rt!j4||f|_y||f|_y#t j$$r|jd rSd}|t d j zt t|j z|j z}nGt!j"t!j&|}|t dj z|z}YwxYw)zk__negotiatesocks5(self,destaddr,destport) Negotiates a connection through a SOCKS5 server. rNrLBBBBrrBBBrr>H )r0rCstructpack_socksocket__recvallchrrbcloser_generalerrors bytearrayappendr;extendr_socks5autherrorsr inet_atonerror gethostbynameordr _socks5errorsr:unpackr1 inet_ntoar2) r4destaddrdestport chosenauthpacketauthstatreqipaddrresp boundaddr boundports r__negotiatesocks5zsocksocket.__negotiatesocks5s LLOt #$,,q/T*A LLVT4tD E LLUD$= >^^A& a?c$i..0 0 JJL#Qq(9$:; ; a?c$i..0 0  !_D 0 0 2 2[F MM$  MM#dll1o. / MM$,,q/ * MM#dll1o. / MM$,,q/ * LL ~~a(H!}D 0 0 22 'N1,=(>??!}D 0 0 22 %q*;A*>&?@@ JJL!}D 0 0 22%q*;A*>&?@@'N1,=(>??kk%tT2 8%%h/FD ((**V3C FKKh// S~~a  !9D ((* * JJL#Qq(9$:; ; !AY#d)**, , JJL4!9~"!3tAay>=T!AY3P"QRR!1mA&6"788 !AY#d)**, ,q)I !AY#d)**, ,$))A,&Ds4!9~6I JJL#Qq(9$:; ;MM$q(9:1= )95 T>$*$4$4V$*>x*HICI,,..7 8s4S< address info Returns the bound IP address and port number at the proxy. )r1r4s rgetproxysocknamezsocksocket.getproxysocknamebs###rc,tj|S)z`getproxypeername() -> address info Returns the IP and port number of the proxy. )r. getpeernamers rgetproxypeernamezsocksocket.getproxypeernamehs%%d++rc|jS)zgetpeername() -> address info Returns the IP address and port number of the destination machine (note: getproxypeername returns the proxy) )r2rs rrzsocksocket.getpeernamens ###rc d} tj|}t j ddd||z}|jdd k7r||jdz}|tdjz}|r||ztdjz}|j||jd }|ddtdjk7r$|jtdtdf|dd td jk7rs|jt|dd d vr?|jtt|dd t t|dd d z ftdt dftj"|dd t j$d|d ddf|_|d k7rtj"||f|_y ||f|_y #tj$rW|jdrt j ddddd}d}n(tjtj |}YAwxYw)zk__negotiatesocks4(self,destaddr,destport) Negotiates a connection through a SOCKS4 server. FrrfrrTz>BBHrNrjrZ)[\]^ri)rrvrwr0rlrmrxrorbrCrnrprrqryr _socks4errorsr|r{r1r2)r4r}r~rmtrslvrrrs r__negotiatesocks4zsocksocket.__negotiatesocks4us*  J%%h/Fkk&$h7&@ <<?d " Q'CCI$$&& .3t9#3#3#55C S~~a  !9D ((* * JJL#Qq(9$:; ; !9D ((* * JJL4!9~- !3tAay>=T!AYRTAT3U"VWW!2}Q'7"899   T!"X & MM$Qq *1 -  d?$*$4$4V$*>x*HI  JsG11A&IIc|jdstj|}n|}d|dt|dg}d}d}|jddk7rT|jdj D]4\}}||d|d gz }|j d k(}|j d k(}6|s|d |d gz }|s9|jd dk7r'|jddk7r||j d gz }|jd |jdj|j|jd} | jdjdk(r7| |jdz} | jdjdk(r7| jdjdjd} | ddjdjfvr$|jt!dt"df t%| d} | dk7r |jt)| | dfd|_||f|_y#t&$r%|jt!dt"dfwxYw)zh__negotiatehttp(self,destaddr,destport) Negotiates a connection through an HTTP server. rzCONNECT :z HTTP/1.1 FNz: rHrUzproxy-authorizationzHost: rrLrz rrKrzHTTP/1.0zHTTP/1.1)z0.0.0.0r)r0rrxstr iteritemsrNrRrsrCrSrbr:find splitlinesrMrprrqint ValueErrorrr1r2) r4r}r~r"rcwrote_host_headerwrote_auth_headerkeyvalr statusline statuscodes r__negotiatehttpzsocksocket.__negotiatehttpsd ||A''1DDtS#h-I!! <<?d " LLO557 ISCsF33$'IIK6$9!$'IIK3H$H! I! (F3 3G ||A$&4<<?d+BD002F;;v RWWW%,,./yy|ii ))+,2$))A,&Dii ))+,2__&q)// a@ a=!2!2!4j6G6G6I J J JJL#Qq(9$:; ; <Z]+J   JJLZA78 8- $h/ < JJL#Qq(9$:; ; $)! \\!_ $   thqk8A;%? @#Qq(9$:; ;r)NNNTNNN)r r r __doc__rAF_INET SOCK_STREAMr/rnrCrArRrdrrrrrrr __classcell__)rFs@rr)r)s{^^&*<*rs#J  68T"* E FF ]]      j  *  *      NRF ;u<u