ZճcLjdZddlZddlZddlZddlZeedd eddZdZdZ dZ da ejZ Gd d e ZGd d eZGd deZGddeZGddeZGddeZdZdZdZdZ ddZdZGddejZdS)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 unusableceZdZdS) ProxyErrorN__name__ __module__ __qualname__0/usr/lib/python3/dist-packages/httplib2/socks.pyr r 7Drr ceZdZdS)GeneralProxyErrorNr rrrrr;rrrceZdZdS)Socks5AuthErrorNr rrrrr?rrrceZdZdS) Socks5ErrorNr rrrrrCrrrceZdZdS) Socks4ErrorNr rrrrrGrrrceZdZdS) 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-idsrTc||||||fadS)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(HEMMMrc\tdkrt|j_dStd)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:::rceZdZdZejejddfdZdZfdZ dZ dZ dd Z d Z d Zd ZdZdZdZdZxZS)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. rNct|||||tdkr t|_nd|_d|_d|_d|_dS)N)NNNNNNT) _orgsocket__init__r _socksocket__proxy_socksocket__proxysockname_socksocket__proxypeername_socksocket__httptunnel)selffamilytypeproto_socks rr/zsocksocket.__init__sY D&$u=== D (DLL?DL## rc||}t||krN||t|z }|std||z}t||kN|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.__recvalls} yy$ii% %#d))+,,A O'(MNNN!8D $ii%  rc~|js||}tt|j|g|RS)zq override socket.socket.sendall method to rewrite the header for non-tunneling proxies if needed )r3_socksocket__rewriteproxysuperr)sendall)r4contentargs __class__s rrCzsocksocket.sendallsH  3))'22G.uZ&&.w>>>>>rc Dd\}}|d}|D]|}|dr|},|ds'|dr|}}|r|r|||||dd}|d}|jdd kr9|jd d kr(|d ||d d |z|d |d d ||dd|dd|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.. ''.. #))++2H2H2P2P   WE W KK    KK   ::c??1%DKK$$E|A$&&4<?d+B+B At3355666 KK:, - - - KK%(((DD%(((ERSHHU V V V{{4   rc|jddz|jdz}dtj|zS)Nr:rLzProxy-Authorization: Basic )r0base64 b64encodedecode)r4auths r__getauthheaderzsocksocket.__getauthheaders?|A% Q7,v/?/E/E/L/L/N/NNNrTcz|||||r|nd|r|nd|f|_dS)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.setproxysN<    !) 3HOO   t!) 3HOO   t   rc h|jddkr=|jddkr,|tjdddddn*|tjdddd|d}|ddt dkr0|tdtdf|ddt dkrnG|ddt dkrt}| d| t|jd| |jd| t|jd| |jd|||d}|ddt dkr0|tdtdf|ddt dkr0|td td fnw||dt d krtdtdftdtdftjdddd} t!j|}|t dz|z}n#t j$r|jd rkd}|t d zt t|z|z}nMt!jt!j|}|t dz|z}YnwxYw|tjd |z}|||d}|ddt dkr0|tdtdf|ddt dkr|t)|ddd krFt+t)|ddt,t)|ddft+d t,d f|d dt dkr|d} n|d dt d krC||dz}|t)|dd} n0|tdtdftjd |dd} | | f|_|dkrt!j||f|_dS||f|_dS)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 LOt # #$,q/T*A*A LLVT4tDD E E E E LLUD$== > > >^^A&& ac?c$ii..00 0 0 JJLLL#Qq(9$:;; ; ac?c$ii..00 0 0  !_D 0 0 2 2 2 2[[F MM$    MM#dl1o.. / / / MM$,q/ * * * MM#dl1o.. / / / MM$,q/ * * * LL ~~a((H!}D 0 0 2 222 'N1,=(>???!}D 0 0 2 222 %q*;A*>&?@@@3 JJLLL!}D 0 0 2 222%q*;A*>&?@@@'N1,=(>???k%tT22 8%h//FD ((***V3CC| 8 8 8|A 8$ii&&(()#h--((//112oo''( )&*>x*H*HIICII,,...7 8FKh/// S~~a   !9D ((** * * JJLLL#Qq(9$:;; ; !A#Y#d))**,, , , JJLLL4!9~~""!3tAaCy>>=T!A#Y3P"QRRR!1mA&6"7888 !A#Y#d))**,, , ,q))II !A#Y#d))**,, , ,$))A,,&Ds4!9~~66II JJLLL#Qq(9$:;; ;M$q(9(9::1= )95 T>>$*$4V$<$ address info Returns the bound IP address and port number at the proxy. )r1r4s rgetproxysocknamezsocksocket.getproxysocknamebs ##rc6t|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|}nc#tj$rQ|jdrt jddddd}d}n&tjtj|}YnwxYwt jddd||z}|jdd kr||jdz}|tdz}|r'||ztdz}| || d }|ddtdkr0| tdtdf|dd td kr| t|dd d vr]| tt|dd t t|dd d z ftdt dftj|dd t jd|d ddf|_|d krtj||f|_d S||f|_d S)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//FF| J J J|A JVT4tDD)&*>x*H*HII  Jk&$h77&@ <?d " " Q'CCII$$&&&  6.3t99#3#3#5#55C S~~a   !9D ((** * * JJLLL#Qq(9$:;; ; !9D ((** * * JJLLL4!9~~-- !3tAaCy>>=T!A#YRTAT3U"VWWW!2}Q'7"8999  T!""X & & M$QqS * *1 -  d??$*$4V$<$\}}||d|d gz }|d k}|d k}?|s|d |d gz }|s;|jd dkr*|jddkr||d gz }|d |d | | d} | d dkrC| | dz} | d dkC| dd d} | dd d fvr0|t!dt"df t%| d} n>#t&$r1|t!dt"dfwxYw| dkr+|t)| | dfd|_||f|_dS)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.__negotiatehttps |A '11DDDtS#h--I!! <?d " " LO5577 I ISCsF33$'IIKK6$9!$'IIKK3H$H!!  4 (F3 3G  <|A$&&4<?d+B+BD0022F;;v RWWW%%,,..///yy||ii ))++,,22$))A,,&Dii ))++,,22__&&q)// a@@ a=!2!2!4!4j6G6G6I6I J J J JJLLL#Qq(9$:;; ; <Z]++JJ < < < JJLLL#Qq(9$:;; ; <    JJLLLZA788 8- $h/s &I<<;J7c t|ttfvsSt|dks@t |dt t frt|dtkrtdtdf|j dtkrm|j ddkr|j d}nd}t ||j d|f||d|ddS|j dtkrm|j ddkr|j d}nd}t ||j d|f||d|ddS|j dt"krm|j ddkr|j d}nd}t ||j d|f||d|ddS|j dt&kr|j ddkr|j d}nd}t ||j d|f|ddkr$||d|ddSd |_dS|j ddkr+t ||d|dfdStd td f) zconnect(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(). rrrrLNi8iiFr)r6listtupler; isinstancerbytesrrrqr0PROXY_TYPE_SOCKS5r.connect_socksocket__negotiatesocks5PROXY_TYPE_SOCKS4_socksocket__negotiatesocks4PROXY_TYPE_HTTP_socksocket__negotiatehttpPROXY_TYPE_HTTP_NO_TUNNELr3)r4destpairportnums rrzsocksocket.connectshD%=00H !!x{S%L99"Xa[!!S((#Qq(9$:;; ; <?/ / /|A$&&,q/   tdl1ow%? @ @ @  " "8A; < < < < < \!_ 1 1 1|A$&&,q/   tdl1ow%? @ @ @  " "8A; < < < < < \!_ / /|A$&&,q/   tdl1ow%? @ @ @  !hqk : : : : : \!_ 9 9 9|A$&&,q/   tdl1ow%? @ @ @{c!!$$Xa[(1+>>>>>$)!!! \!_ $ $   thqk8A;%? @ @ @ @ @#Qq(9$:;; ;r)NNNTNNN)r r r __doc__rAF_INET SOCK_STREAMr/rnrCrArRrdrrrrrrr __classcell__)rFs@rr)r)s1^&*rs##J  768T""* +E F FF ]                      j        *        *              NRFFFF ; ; ;u<u<u<u<u<u<u<u<u<u