x^c @sadZddlZddlZddlZddlZeedddkr]edndZdZ dZ dZ da ejZ d efd YZd efd YZd efdYZdefdYZdefdYZdefdYZd1Zd2Zd3Zd4Zdddeddd-Zd.Zd/ejfd0YZdS(5sSocksiPy - 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. iNtsockets-socket.socket missing, proxy support unusableiiiit ProxyErrorcBseZRS((t__name__t __module__(((s2/usr/lib/python2.7/site-packages/httplib2/socks.pyR7stGeneralProxyErrorcBseZRS((RR(((s2/usr/lib/python2.7/site-packages/httplib2/socks.pyR;stSocks5AuthErrorcBseZRS((RR(((s2/usr/lib/python2.7/site-packages/httplib2/socks.pyR?st Socks5ErrorcBseZRS((RR(((s2/usr/lib/python2.7/site-packages/httplib2/socks.pyRCst Socks4ErrorcBseZRS((RR(((s2/usr/lib/python2.7/site-packages/httplib2/socks.pyRGst HTTPErrorcBseZRS((RR(((s2/usr/lib/python2.7/site-packages/httplib2/socks.pyRKstsuccesss invalid datas not connecteds not availablesbad proxy types bad inputt succeededsgeneral SOCKS server failures!connection not allowed by rulesetsNetwork unreachablesHost unreachablesConnection refuseds TTL expiredsCommand not supportedsAddress type not supporteds Unknown errorsauthentication is requireds0all offered authentication methods were rejecteds$unknown username or invalid passwords unknown errorsrequest grantedsrequest rejected or failedsLrequest rejected because SOCKS server cannot connect to identd on the clientsPrequest rejected because the client program and identd report different user-idscCs||||||fadS(ssetdefaultproxy(proxytype, addr[, port[, rdns[, username[, password]]]]) Sets a default proxy which all further socksocket objects will use, unless explicitly changed. N(t _defaultproxy(t proxytypetaddrtporttrdnstusernametpassword((s2/usr/lib/python2.7/site-packages/httplib2/socks.pytsetdefaultproxywscCs+tdkrt|j_n tddS(s7wrapmodule(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. isno proxy specifiedN(isno proxy specified(R tNonet socksocketRR(tmodule((s2/usr/lib/python2.7/site-packages/httplib2/socks.pyt wrapmodules RcBseZdZejejdddZdZdZ dZ dZ ddde ddddZ dZd Zd Zd Zd Zd ZdZRS(ssocksocket([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. icCsYtj|||||tdkr1t|_n d|_d|_d|_t|_dS(N(NNNNNN( t _orgsockett__init__R Rt_socksocket__proxyt_socksocket__proxysocknamet_socksocket__proxypeernametTruet_socksocket__httptunnel(tselftfamilyttypetprotot_sock((s2/usr/lib/python2.7/site-packages/httplib2/socks.pyRs     cCsd|j|}xNt||kr_|j|t|}|sRtdn||}qW|S(s__recvall(count) -> data Receive EXACTLY the number of bytes requested from the socket. Blocks until the required number of bytes have been received. isconnection closed unexpectedly(isconnection closed unexpectedly(trecvtlenR(Rtcounttdatatd((s2/usr/lib/python2.7/site-packages/httplib2/socks.pyt __recvallscGs4|js|j|}ntt|j||S(sq override socket.socket.sendall method to rewrite the header for non-tunneling proxies if needed (Rt_socksocket__rewriteproxytsuperRtsendall(Rtcontenttargs((s2/usr/lib/python2.7/site-packages/httplib2/socks.pyR+s c CsTd\}}|jd}x_|D]W}|jjdrF|}q"|jjdsp|jjdr"|}q"q"W|rG|rG|j||j||jdd}|jd}|jdd kr|jdd kr|jd |jn|jd d ||jd d |d ||d|d fndj|S(s 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. s shost:tgettpostt iiiisHost: %ss%s http://%s%s %siN(NN( Rtsplittlowert startswithtremoveRtinsertt_socksocket__getauthheadertjoin(Rtheaderthosttendptthdrsthdr((s2/usr/lib/python2.7/site-packages/httplib2/socks.pyt__rewriteproxys"   *    &/cCs-|jdd|jd}dtj|S(Nit:isProxy-Authorization: Basic (Rtbase64t b64encode(Rtauth((s2/usr/lib/python2.7/site-packages/httplib2/socks.pyt__getauthheaderscCsF|||||r|jnd|r3|jnd|f|_dS(s 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(tencodeRR(RR R RRRRtheaders((s2/usr/lib/python2.7/site-packages/httplib2/socks.pytsetproxysc Cs*|jdd krK|jdd krK|jtjdddddn|jtjdddd|jd}|dd!tdjkr|jt dt dfn|dd!tdjkrng|dd!tdjkr|jtdjtt |jd|jdtt |jd|jd|jd}|dd!tdjkr|jt dt dfn|dd!tdjkrD|jt dt dfqDnU|j|dtd jkr.t dt dfnt dt dftjdddd}y-tj|}|tdj|}Wntjk r|jdrd }|tdjtt |j|j}qtjtj|}|tdj|}nX|tjd |}|j||jd}|dd!tdjkr|jt dt dfn0|dd!tdjkr |jt|dd!d krtt|dd!tt|dd!fqtd td fn|dd!tdjkrQ|jd}nq|dd!tdjkr||jd}|jt|dd!}n |jt dt dftjd |jdd} || f|_|d krtj||f|_n||f|_d S(sk__negotiatesocks5(self,destaddr,destport) Negotiates a connection through a SOCKS5 server. iitBBBBiitBBBiiis>Hii N(RRR+tstructtpackt_socksocket__recvalltchrRCtcloseRt_generalerrorsR$Rt_socks5autherrorsRt inet_atonterrort gethostbynametordRt _socks5errorsR#tunpackRt inet_ntoaR( Rtdestaddrtdestportt chosenauthtauthstattreqtipaddrtrespt boundaddrt boundport((s2/usr/lib/python2.7/site-packages/httplib2/socks.pyt__negotiatesocks5sp&% W    9   3  cCs|jS(sigetsockname() -> address info Returns the bound IP address and port number at the proxy. (R(R((s2/usr/lib/python2.7/site-packages/httplib2/socks.pytgetproxysocknamebscCs tj|S(s`getproxypeername() -> address info Returns the IP and port number of the proxy. (Rt getpeername(R((s2/usr/lib/python2.7/site-packages/httplib2/socks.pytgetproxypeernamehscCs|jS(sgetpeername() -> address info Returns the IP address and port number of the destination machine (note: getproxypeername returns the proxy) (R(R((s2/usr/lib/python2.7/site-packages/httplib2/socks.pyRanscCs\t}ytj|}Wn]tjk rx|jdr]tjddddd}t}qytjtj|}nXtjddd||}|jddkr||jd}n|t dj }|r||t dj }n|j ||j d}|dd!t dj krS|jtdtdfn|dd!t d j kr|jt|dd!dkr|jtt|dd!tt|dd!d fqtd tdfntj|dtjd|dd!df|_|dkrItj||f|_n||f|_dS(sk__negotiatesocks4(self,destaddr,destport) Negotiates a connection through a SOCKS4 server. iRFiis>BBHiiiiZi[i\i]i^s>HN(i[i\i](tFalseRRORPRRHRIRRQRRKRCR+RJRLRRMRRRt _socks4errorsRURTRR(RRVRWtrmtrslvR[RZR\((s2/usr/lib/python2.7/site-packages/httplib2/socks.pyt__negotiatesocks4us<      7# c Cs|jdstj|}n|}d|dt|dg}t}t}|jddkrx^|jdjD]F\}}||d|dg7}|jdk}|jd k}qsWn|s|d |dg7}n|s$|jd dkr$|jd dkr$||jdg7}q$n|j d|j d j |j |j d} x2| jdj dkr| |j d} q_W| jdjdj d} | ddj dj fkr|jtdtdfnyt| d} Wn1tk r?|jtdtdfnX| dkro|jt| | dfnd|_||f|_dS(sh__negotiatehttp(self,destaddr,destport) Negotiates a connection through an HTTP server. isCONNECT R>s HTTP/1.1 is: s R9sproxy-authorizationsHost: iitis iiR0isHTTP/1.0sHTTP/1.1is0.0.0.0N(s0.0.0.0i(RRRQtstrRcRt iteritemsR2R6tappendR+R7RCR#tfindt splitlinesR1RLRRMtintt ValueErrorRRR( RRVRWR RDtwrote_host_headertwrote_auth_headertkeytvalR\t statuslinet statuscode((s2/usr/lib/python2.7/site-packages/httplib2/socks.pyt__negotiatehttpsF  & ""      cCst|ttfksTt|dksTt|dt sTt|dtkrmtdtdfn|j dt kr|j dd kr|j d}nd}t j ||j d|f|j|d|dn|j dtkrU|j dd kr|j d}nd}t j ||j d|f|j|d|dnN|j dtkr|j dd kr|j d}nd}t j ||j d|f|j|d|dn|j dtkrY|j dd kr|j d}nd}t j ||j d|f|ddkrM|j|d|dqt|_nJ|j dd krt j ||d|dfntdtdfd S( sconnect(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(). iiiii8iiiN(R tlistttupleR$t isinstancet basestringRmRRMRtPROXY_TYPE_SOCKS5RRtconnectt_socksocket__negotiatesocks5tPROXY_TYPE_SOCKS4t_socksocket__negotiatesocks4tPROXY_TYPE_HTTPt_socksocket__negotiatehttptPROXY_TYPE_HTTP_NO_TUNNELRcR(Rtdestpairtportnum((s2/usr/lib/python2.7/site-packages/httplib2/socks.pyR{sD  !N(RRt__doc__RtAF_INETt SOCK_STREAMRRRJR+R)R6RRER|R`RbRaR~RR{(((s2/usr/lib/python2.7/site-packages/httplib2/socks.pyRs(     g    3 -(ssuccesss invalid datas not connecteds not availablesbad proxy types bad input( R sgeneral SOCKS server failures!connection not allowed by rulesetsNetwork unreachablesHost unreachablesConnection refuseds TTL expiredsCommand not supportedsAddress type not supporteds Unknown error(R sauthentication is requireds0all offered authentication methods were rejecteds$unknown username or invalid passwords unknown error(srequest grantedsrequest rejected or failedsLrequest rejected because SOCKS server cannot connect to identd on the clientsPrequest rejected because the client program and identd report different user-idss unknown error(RR?RRHtsystgetattrRt ImportErrorR}RzRRR Rt ExceptionRRRRRRRMRSRNRdRRRR(((s2/usr/lib/python2.7/site-packages/httplib2/socks.pyt$s^