F Vc @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. iNtsockets-socket.socket missing, proxy support unusableiiiit ProxyErrorcBseZRS((t__name__t __module__(((s2/usr/lib/python2.7/site-packages/httplib2/socks.pyR;stGeneralProxyErrorcBseZRS((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.pyR>st Socks4ErrorcBseZRS((RR(((s2/usr/lib/python2.7/site-packages/httplib2/socks.pyR?st HTTPErrorcBseZRS((RR(((s2/usr/lib/python2.7/site-packages/httplib2/socks.pyR@stsuccesss 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.pytsetdefaultproxy`scCs+tdkrt|j_n tddS(s2wrapmodule(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 wrapmodulehs RcBseZdZejejdddZdZdZ dZ dZ ddde dddZ 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.pyR{s     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__getauthheaderscCs||||||f|_dS(ssetproxy(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. N(R(RR R RRRR((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|}qtjtj|}|tdj|}nX|tjd |}|j||jd}|dd!tdjkr|jt dt dfn0|dd!tdjkr|jt|dd!d krtt|dd!tt|dd!fqtd td fn|dd!tdjkrK|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__recvalltchrtencodetcloseRt_generalerrorsR$Rt_socks5autherrorsRt inet_atonterrort gethostbynametordRt _socks5errorsR#tunpackRt inet_ntoaR( Rtdestaddrtdestportt chosenauthtauthstattreqtipaddrtrespt boundaddrt boundport((s2/usr/lib/python2.7/site-packages/httplib2/socks.pyt__negotiatesocks5sn&% ]    3   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.pytgetproxysockname#scCs 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.pytgetproxypeername)scCs|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.pyR`/scCs\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. iRDiis>BBHiiiiZi[i\i]i^s>HN(i[i\i](tFalseRRNRORRFRGRRPRRIRJR+RHRKRRLRQRt _socks4errorsRTRSRR(RRURVtrmtrslvRZRYR[((s2/usr/lib/python2.7/site-packages/httplib2/socks.pyt__negotiatesocks46s:      73 cCs|jdstj|}n|}d|dt|dg}|d|dg7}|jddkr|jddkr||jdg7}n|jd|jd j|j |j d }x2|j d j d kr||j d }qW|j d j dj d}|d dj dj fkrf|jtd td fnyt|d }Wn1tk r|jtd td fnX|dkr|jt||dfnd|_||f|_dS(sh__negotiatehttp(self,destaddr,destport) Negotiates a connection through an HTTP server. isCONNECT R>s HTTP/1.1 sHost: s iitis iiR0isHTTP/1.0sHTTP/1.1is0.0.0.0N(s0.0.0.0i(RRRPtstrRR6tappendR+R7RJR#tfindt splitlinesR1RKRRLtintt ValueErrorRRR(RRURVR theadersR[t statuslinet statuscode((s2/usr/lib/python2.7/site-packages/httplib2/socks.pyt__negotiatehttpfs4 & ""      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 basestringRkRRLRtPROXY_TYPE_SOCKS5RRtconnectt_socksocket__negotiatesocks5tPROXY_TYPE_SOCKS4t_socksocket__negotiatesocks4tPROXY_TYPE_HTTPt_socksocket__negotiatehttptPROXY_TYPE_HTTP_NO_TUNNELRbR(Rtdestpairtportnum((s2/usr/lib/python2.7/site-packages/httplib2/socks.pyRvs>T !N(RRt__doc__RtAF_INETt SOCK_STREAMRRRHR+R)R6RRCRwR_RaR`RyR{Rv(((s2/usr/lib/python2.7/site-packages/httplib2/socks.pyRts    \    0 $(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?RRFtsystgetattrRt ImportErrorRxRuRzR|R Rt ExceptionRRRRRRRLRRRMRcRRRR(((s2/usr/lib/python2.7/site-packages/httplib2/socks.pyts^