ó ì2;Zc @s‹dZddlmZddlmZddlmZmZmZddl Z ddl m Z ddl Z ddl Z ddl mZddlZddlZddlZdZe jd krejdDkryddlZWqek rýed ƒ‚qXne jeƒZd ZZdZZd ZZied6ed6ed6Z e!e"e j#ƒe j$ƒƒƒZ%ejZ&Z'd„Z(de)fd„ƒYZ*de*fd„ƒYZ+de*fd„ƒYZ,de*fd„ƒYZ-de*fd„ƒYZ.de*fd„ƒYZ/de*fd „ƒYZ0id!d"6d#d$6d%d&6Z1id'd 6d(d6d)d 6d*d+6d,d-6d.d/6d0d16d2d36Z2id4e6d4e6d5e6Z3ddde5ddd6„Z6d7„Z7d8„Z8e8Z9d9„Z:e:Z;ddddde5dddd:„ Z<d;ejfd<„ƒYZ=d=„Z>xXdED]PZe?e=edƒZ@eAe@eƒse=jBjCeƒeDe=ee>eƒƒqqWdBe=fdC„ƒYZEdS(FsŠ SocksiPy - Python SOCKS module. 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 DAMAGE. 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 iÿÿÿÿ(t b64encode(tCallable(t EOPNOTSUPPtEINVALtEAGAINN(tBytesIO(tSEEK_CURs1.6.7tntiis8To run PySocks on Windows you must install win_inet_ptoniitSOCKS4tSOCKS5tHTTPcs"tjˆƒ‡fd†ƒ}|S(Ncs„|d}zVy9|jƒ}|dkr8|jtƒnˆ||ŽSWntk r^}‚nXWd|dkr|jtƒnXdS(Ni(t gettimeoutt setblockingtTruet ExceptiontFalse(targstkwargstselft _is_blockingte(tfunction(s)/usr/lib/python2.7/site-packages/socks.pytwrapper]s     (t functoolstwraps(RR((Rs)/usr/lib/python2.7/site-packages/socks.pytset_self_blocking[st ProxyErrorcBs#eZdZdd„Zd„ZRS(s4Socket_err contains original socket.error exception.cCs7||_||_|r3|jdj|ƒ7_ndS(Ns: {0}(tmsgt socket_errtformat(RRR((s)/usr/lib/python2.7/site-packages/socks.pyt__init__ps  cCs|jS(N(R(R((s)/usr/lib/python2.7/site-packages/socks.pyt__str__wsN(t__name__t __module__t__doc__tNoneRR(((s)/usr/lib/python2.7/site-packages/socks.pyRns tGeneralProxyErrorcBseZRS((R R!(((s)/usr/lib/python2.7/site-packages/socks.pyR${stProxyConnectionErrorcBseZRS((R R!(((s)/usr/lib/python2.7/site-packages/socks.pyR%stSOCKS5AuthErrorcBseZRS((R R!(((s)/usr/lib/python2.7/site-packages/socks.pyR&ƒst SOCKS5ErrorcBseZRS((R R!(((s)/usr/lib/python2.7/site-packages/socks.pyR'‡st SOCKS4ErrorcBseZRS((R R!(((s)/usr/lib/python2.7/site-packages/socks.pyR(‹st HTTPErrorcBseZRS((R R!(((s)/usr/lib/python2.7/site-packages/socks.pyR)ssRequest rejected or failedi[sLRequest rejected because SOCKS server cannot connect to identd on the clienti\sPRequest rejected because the client program and identd report different user-idsi]sGeneral SOCKS server failures!Connection not allowed by rulesetsNetwork unreachablesHost unreachableisConnection refusedis TTL expiredis(Command not supported, or protocol errorisAddress type not supportedii8icCsC|||||r|jƒnd|r3|jƒndft_dS(s›Sets a default proxy. All further socksocket objects will use the default unless explicitly changed. All parameters are as for socket.set_proxy().N(tencodeR#t socksockett default_proxy(t proxy_typetaddrtporttrdnstusernametpassword((s)/usr/lib/python2.7/site-packages/socks.pytset_default_proxy¨s cOs/d|kr"|jdƒ|d 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) t[s[]isgai returned empty list.N(t startswithtstripR#R8t getaddrinfot SOCK_STREAMR+t setsockoptt isinstancetinttfloatt settimeoutt set_proxytbindtconnectterrorR%tclose(t dest_pairttimeouttsource_addressR-t proxy_addrt proxy_portt proxy_rdnstproxy_usernametproxy_passwordtsocket_optionst remote_hostt remote_portterrtrtfamilyt socket_typetprotot canonnametsatsocktoptR((s)/usr/lib/python2.7/site-packages/socks.pytcreate_connectionÎs> "    t _BaseSocketcBs eZdZd„ZeƒZRS(sBAllows Python 2 delegated methods such as send() to be overridden.cOsZtj|||Žtƒ|_x4|jD])}t||ƒ|j|t((Rh((Rhs)/usr/lib/python2.7/site-packages/socks.pyt _makemethodstsendtotsendtrecvfromtrecvR+cBsFeZdZdZejejdd„Zd„Z d„Z d„Z d„Z ddde ddd„Zd„Zd „Zd „Zdd „Zdd „Zd „Zd„Zd„ZeZd„ZeZd„ZeZd„Zd„Zd„Zd„Zd„Z d„Z!ie e"6ee#6e!e$6Z%e&d„ƒZ'd„Z(RS(s2socksocket([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. icOs¡|tjtjfkr6d}t|j|ƒƒ‚ntt|ƒj|||||Žd|_ |j ry|j |_ n d|_ d|_ d|_ d|_dS(Ns0Socket type must be stream or datagram, not {!r}(NNNNNN(R8R?t SOCK_DGRAMt ValueErrorRtsuperR+RR#t _proxyconnR,tproxytproxy_socknametproxy_peernamet_timeout(RRWttypeRYRRR((s)/usr/lib/python2.7/site-packages/socks.pyR1s"     cCs[d}xNt|ƒ|krV|j|t|ƒƒ}|sItdƒ‚n||7}q W|S(sŠReceive EXACTLY the number of bytes requested from the file object. Blocks until the required number of bytes have been received.RksConnection closed unexpectedly(tlentreadR$(Rtfiletcounttdatatd((s)/usr/lib/python2.7/site-packages/socks.pyt_readallCscCsM||_y)|jƒ}tt|ƒj|jƒWntjk rHnXdS(N(Rxtget_proxy_peernameRsR+RDR8RH(RRKtpeer((s)/usr/lib/python2.7/site-packages/socks.pyRDOs   cCs|jS(N(Rx(R((s)/usr/lib/python2.7/site-packages/socks.pyR XscCs'|r|jdƒn |jdƒdS(Ng(RDR#(Rtv((s)/usr/lib/python2.7/site-packages/socks.pyR [scCsC|||||r|jƒnd|r3|jƒndf|_dS(s 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*R#Ru(RR-R.R/R0R1R2((s)/usr/lib/python2.7/site-packages/socks.pyREas cOs2d|kr"|jdƒ|d Returns the IP and port number of the proxy. (t getpeername(R((s)/usr/lib/python2.7/site-packages/socks.pyRÝscCs|jS(swReturns the IP address and port number of the destination machine. Note: get_proxy_peername returns the proxy.(Rw(R((s)/usr/lib/python2.7/site-packages/socks.pyt get_peernameåscGs+d}|j|||ƒ\|_|_dS(s7Negotiates a stream connection through a SOCKS5 server.sN(RˆRwRv(Rt dest_addrtCONNECT((s)/usr/lib/python2.7/site-packages/socks.pyt_negotiate_SOCKS5íscCs¡|j\}}}}}} |jdƒ} |jddƒ} zI|r[| r[| jdƒn | jdƒ| jƒ|j| dƒ} | dd!dkr¦td ƒ‚n| dd!d kr]| jd tt|ƒƒjƒ|tt| ƒƒjƒ| ƒ| jƒ|j| dƒ} | dd!d kr8td ƒ‚n| dd!d kr¡t d ƒ‚q¡nD| dd!d kr¡| dd!dkr’t dƒ‚q¡td ƒ‚n| jd|d ƒ|j || ƒ}| jƒ|j| dƒ}|dd!dkrtd ƒ‚nt |dd!ƒ}|dkrRt j |dƒ}tdj||ƒƒ‚n|j| ƒ}tt|ƒj|jƒ||fSWd| jƒ| jƒXdS(s‘ Send SOCKS5 request with given command (CMD field) and address (DST field). Returns resolved DST address that was used. twbtrbissiiss%SOCKS5 proxy server sent invalid datassssSOCKS5 authentication failedsÿs7All offered SOCKS5 authentication methods were rejectedis Unknown errors {0:#04x}: {1}N(RutmakefileRŽtflushR€R$tchrRzR*R&RRšt SOCKS5_ERRORStgetR'RRœRsR+RDRxRI(RtconntcmdRŠR-R.R/R0R1R2twritertreadert chosen_autht auth_statustresolvedtresptstatusRHtbnd((s)/usr/lib/python2.7/site-packages/socks.pyRˆósX    ;        cCs¿|\}}|j\}}}}}} idtj6dtj6} x”tjtjfD]€} y`tj| |ƒ} |j| | | ƒtj| | ƒ}|jtjd|ƒƒ||fSWqTtj k rÓqTqTXqTW|r|j dƒ} |jdt t | ƒƒj ƒ| ƒn…tj ||tjtjtjtjƒ}|d}|d} |dd}tj| |ƒ} |j| | | ƒtj| | ƒ}|jtjd|ƒƒ||fS(s~ Return the host and port packed for the SOCKS5 protocol, and the resolved address as a tuple object. sss>Htidnasii(RuR8tAF_INETtAF_INET6t inet_ptonRŽt inet_ntoptstructtpackRHR*R®RzR>t AF_UNSPECR?t IPPROTO_TCPt AI_ADDRCONFIG(RR.R|RR/R-R‰R0R1R2tfamily_to_byteRWt addr_bytest host_bytest addressest target_addr((s)/usr/lib/python2.7/site-packages/socks.pyRMs6  *   cCsÝ|j|dƒ}|dkr<tj|j|dƒƒ}nu|dkru|j|dƒ}|j|t|ƒƒ}n<|dkr¥tjtj|j|dƒƒ}n tdƒ‚tjd|j|d ƒƒd }||fS( Nisissis%SOCKS5 proxy server sent invalid datas>Hii( R€R8t inet_ntoaRšR¿R½R$RÀtunpack(RR|tatypR.tlengthR/((s)/usr/lib/python2.7/site-packages/socks.pyRœzs   $ "cCs|j\}}}}}}|jdƒ} |jddƒ} zÀt} ytj|ƒ} WnAtjk r›|r€d} t} qœtjtj|ƒƒ} nX| jt j ddd|ƒƒ| j| ƒ|rÞ| j|ƒn| jdƒ| r| j|j d ƒdƒn| j ƒ|j | d ƒ} | dd!dkrLtd ƒ‚nt| dd !ƒ}|d kr˜tj|dƒ}tdj||ƒƒ‚ntj| dƒt jd| d d!ƒdf|_| rìtj| ƒ|f|_n||f|_Wd| jƒ| jƒXdS(s0Negotiates a connection through a SOCKS4 server.RªR«iss>BBHiisR»is%SOCKS4 proxy server sent invalid dataiiZs Unknown errors {0:#04x}: {1}s>HN(RuR¬RR8t inet_atonRHR t gethostbynameRŽRÀRÁR*R­R€R$Ršt SOCKS4_ERRORSR°R(RRÊRËRvRwRI(RR§t dest_portR-R.R/R0R1R2R³R´tremote_resolveRÆR¸R¹RH((s)/usr/lib/python2.7/site-packages/socks.pyt_negotiate_SOCKS4‰sH      # cCsë|j\}}}}}}|r'|n tj|ƒ}d|jdƒdt|ƒjƒdd|jdƒg} |r¢|r¢| jdt|d|ƒƒn| jdƒ|jdj| ƒƒ|j ƒ} | j ƒ} | j ƒ| süt dƒ‚ny| j d d ƒ\} } }Wntk r:t d ƒ‚nX| jd ƒsYt d ƒ‚nyt| ƒ} Wntk rˆtdƒ‚nX| dkrÏdj| |ƒ}| dkrÀ|d7}nt|ƒ‚nd|_||f|_dS(swNegotiates a connection through an HTTP server. NOTE: This currently only supports HTTP CONNECT-style proxies.sCONNECT R»t:s HTTP/1.1sHost: sProxy-Authorization: basic s sConnection closed unexpectedlyt is'HTTP proxy server sent invalid responsesHTTP/s0Proxy server does not appear to be an HTTP proxys4HTTP proxy server did not return a valid HTTP statusiÈs{0}: {1}ii“i•sa [*] Note: The HTTP proxy server may not be supported by PySocks (must be a CONNECT tunnel proxy)s0.0.0.0iN(ii“i•(s0.0.0.0i(RuR8RÏR*tstrtappendRtsendalltjoinR¬treadlineRIR$tsplitRrR<RBR)RRvRw(RR§RÑR-R.R/R0R1R2t http_headerstfobjt status_lineRYt status_codet status_msgRH((s)/usr/lib/python2.7/site-packages/socks.pyt_negotiate_HTTPÅsB(              cCs¢t|ƒdks%|djdƒrAtjdt|ƒƒ‚n|\}}|jtjkr¹|jsx|jd ƒntj |ƒ}|dkr¦| r¦d|_ n||f|_ dS|j \}}}}}} t |ttfƒ st|ƒdks| st |tƒ r"tdƒ‚ntt|ƒj|jƒ|dkr‰||_ tt|ƒj|jƒtt|ƒj||fƒdS|jƒ}ytt|ƒj|ƒWn|tjk r-} |jƒ|\}}d j||ƒ} t|} d j| | ƒ} tjd | | ƒt| | ƒ‚nqXy!|j|}||||ƒWnLtjk r€} |jƒtd | ƒ‚ntk r|jƒ‚nXdS(sã 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). iiR;s PySocks doesn't support IPv6: %sRks0.0.0.0Ns0Invalid destination-connection (host, port) pairs{0}:{1}s!Error connecting to {0} proxy {1}s %s due to: %ss Socket error(Rki( RzR<R8RHRÖRyRqRtRFRÏR#RwRuRARittupleRBR$RsR+RDRxRGR‡RIRtPRINTABLE_PROXY_TYPEStlogtdebugR%t_proxy_negotiatorsR(RRJR§RÑR-RMRNR0R1R2RHt proxy_servertprintable_typeRt negotiate((s)/usr/lib/python2.7/site-packages/socks.pyRGs\ %                 cCsO|j\}}}}}}|p-tj|ƒ}|sEtdƒ‚n||fS(sD Return proxy address to connect to as tuple object sInvalid proxy type(Rut DEFAULT_PORTSR°R$(RR-RMRNR0R1R2((s)/usr/lib/python2.7/site-packages/socks.pyR‡]s N()R R!R"R#R,R8R¼R?RR€RDR R R RER„RFRmRnRoRpRIR¤tgetproxysocknameRtgetproxypeernameR¦R¥R©RˆRRœRÓRáRR R RæRRGR‡(((s)/usr/lib/python2.7/site-packages/socks.pyR+&sF       %          Z -  < ; W(ii(RmRnRoRp(FR"tbase64Rt collectionsRterrnoRRRRtioRtloggingtosRR8RÀtsyst __version__Rht version_infot win_inet_ptont ImportErrort getLoggerR RätPROXY_TYPE_SOCKS4RtPROXY_TYPE_SOCKS5R tPROXY_TYPE_HTTPR t PROXY_TYPESRatziptvaluestkeysRãt _orgsocketR`RtIOErrorRR$R%R&R'R(R)RÐR¯RêR#R R3R6R7tgetdefaultproxyR:t wrapmoduleR^R_RlRdtmethodRARcR×tsetattrR+(((s)/usr/lib/python2.7/site-packages/socks.pyt7s†          !          8