ÿØÿàJFIFÿþ ÿÛC       ÿÛC ÿÀÿÄÿÄ"#QrÿÄÿÄ&1!A"2qQaáÿÚ ?Øy,æ/3JæÝ¹È߲؋5êXw²±ÉyˆR”¾I0ó2—PI¾IÌÚiMö¯–þrìN&"KgX:Šíµ•nTJnLK„…@!‰-ý ùúmë;ºgµŒ&ó±hw’¯Õ@”Ü— 9ñ-ë.²1<yà‚¹ïQÐU„ہ?.’¦èûbß±©Ö«Âw*VŒ) `$‰bØÔŸ’ëXÖ-ËTÜíGÚ3ð«g Ÿ§¯—Jx„–’U/ÂÅv_s(Hÿ@TñJÑãõçn­‚!ÈgfbÓc­:él[ðQe 9ÀPLbÃãCµm[5¿ç'ªjglå‡Ûí_§Úõl-;"PkÞÞÁQâ¼_Ñ^¢SŸx?"¸¦ùY騐ÒOÈ q’`~~ÚtËU¹CڒêV  I1Áß_ÿÙ 2;Zc @sdZddlmZddlmZddlmZmZmZddl Z ddl m Z ddl Z ddl Z ddl mZddlZddlZddlZdZe jd krejdDkryddlZWqek red qXne jeZd ZZdZZd ZZied6ed6ed6Z e!e"e j#e j$Z%ejZ&Z'dZ(de)fdYZ*de*fdYZ+de*fdYZ,de*fdYZ-de*fdYZ.de*fdYZ/de*fd YZ0id!d"6d#d$6d%d&6Z1id'd 6d(d6d)d 6d*d+6d,d-6d.d/6d0d16d2d36Z2id4e6d4e6d5e6Z3ddde5ddd6Z6d7Z7d8Z8e8Z9d9Z:e:Z;ddddde5dddd: Z<d;ejfd<YZ=d=Z>xXdED]PZe?e=edZ@eAe@ese=jBjCeeDe=ee>eqqWdBe=fdCYZEdS(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"tjfd}|S(Ncs|d}zVy9|j}|dkr8|jtn||SWntk r^}nXWd|dkr|jtnXdS(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#eZdZddZdZRS(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|jnd|r3|jndft_dS(sSets 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_proxys 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_connections> "    t _BaseSocketcBs eZdZdZeZRS(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+cBsFeZdZdZejejddZdZ dZ dZ dZ ddde dddZdZd Zd Zdd Zdd Zd ZdZdZeZdZeZdZeZdZdZdZdZdZ dZ!ie e"6ee#6e!e$6Z%e&dZ'dZ(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|}|sItdn||7}q W|S(sReceive 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|jWntjk 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|jdn |jddS(Ng(RDR#(Rtv((s)/usr/lib/python2.7/site-packages/socks.pyR [scCsC|||||r|jnd|r3|jndf|_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.pyRscCs|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_peernamescGs+d}|j|||\|_|_dS(s7Negotiates a stream connection through a SOCKS5 server.sN(RRwRv(Rt dest_addrtCONNECT((s)/usr/lib/python2.7/site-packages/socks.pyt_negotiate_SOCKS5scCs|j\}}}}}} |jd} |jdd} zI|r[| r[| jdn | jd| j|j| d} | dd!dkrtd n| dd!d kr]| jd tt|j|tt| j| | j|j| d} | dd!d kr8td n| dd!d krt d qnD| dd!d kr| dd!dkrt dqtd n| jd|d |j || }| j|j| d}|dd!dkrtd nt |dd!}|dkrRt j |d}tdj||n|j| }tt|j|j||fSWd| j| jXdS(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 failedss7All offered SOCKS5 authentication methods were rejectedis Unknown errors {0:#04x}: {1}N(RutmakefileRtflushRR$tchrRzR*R&RRt SOCKS5_ERRORStgetR'RRRsR+RDRxRI(RtconntcmdRR-R.R/R0R1R2twritertreadert chosen_autht auth_statustresolvedtresptstatusRHtbnd((s)/usr/lib/python2.7/site-packages/socks.pyRsX    ;        cCs|\}}|j\}}}}}} idtj6dtj6} xtjtjfD]} y`tj| |} |j| | | tj| | }|jtjd|||fSWqTtj k rqTqTXqTW|r|j d} |jdt t | j | ntj ||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_ptonRt inet_ntoptstructtpackRHR*RRzR>t AF_UNSPECR?t IPPROTO_TCPt AI_ADDRCONFIG(RR.R|RR/R-RR0R1R2tfamily_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<|dkrtjtj|j|d}n tdtjd|j|d d }||fS( Nisissis%SOCKS5 proxy server sent invalid datas>Hii( RR8t inet_ntoaRRRR$Rtunpack(RR|tatypR.tlengthR/((s)/usr/lib/python2.7/site-packages/socks.pyRzs   $ "cCs|j\}}}}}}|jd} |jdd} zt} ytj|} WnAtjk r|rd} t} qtjtj|} nX| jt j ddd|| j| |r| j|n| jd| r| j|j d dn| j |j | d } | dd!dkrLtd nt| dd !}|d krtj|d}tdj||ntj| dt jd| d d!df|_| rtj| |f|_n||f|_Wd| j| jXdS(s0Negotiates a connection through a SOCKS4 server.RRiss>BBHiisRis%SOCKS4 proxy server sent invalid dataiiZs Unknown errors {0:#04x}: {1}s>HN(RuRRR8t inet_atonRHR t gethostbynameRRRR*RRR$Rt SOCKS4_ERRORSRR(RRRRvRwRI(RRt dest_portR-R.R/R0R1R2RRtremote_resolveRRRRH((s)/usr/lib/python2.7/site-packages/socks.pyt_negotiate_SOCKS4sH      # cCs|j\}}}}}}|r'|n tj|}d|jddt|jdd|jdg} |r|r| jdt|d|n| jd|jdj| |j } | j } | j | st dny| j d d \} } }Wntk r:t d nX| jd sYt d nyt| } Wntk rtdnX| dkrdj| |}| dkr|d7}nt|nd|_||f|_dS(swNegotiates a connection through an HTTP server. NOTE: This currently only supports HTTP CONNECT-style proxies.sCONNECT Rt: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 statusis{0}: {1}iiisa [*] Note: The HTTP proxy server may not be supported by PySocks (must be a CONNECT tunnel proxy)s0.0.0.0iN(iii(s0.0.0.0i(RuR8RR*tstrtappendRtsendalltjoinRtreadlineRIR$tsplitRrR<RBR)RRvRw(RRRR-R.R/R0R1R2t http_headerstfobjt status_lineRYt status_codet status_msgRH((s)/usr/lib/python2.7/site-packages/socks.pyt_negotiate_HTTPsB(              cCst|dks%|djdrAtjdt|n|\}}|jtjkr|jsx|jd ntj |}|dkr| rd|_ n||f|_ dS|j \}}}}}} t |ttf st|dks| st |t r"tdntt|j|j|dkr||_ tt|j|jtt|j||fdS|j}ytt|j|Wn|tjk r-} |j|\}}d j||} t|} d j| | } tjd | | t| | nqXy!|j|}||||WnLtjk r} |jtd | ntk r|jnXdS(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<R8RHRRyRqRtRFRR#RwRuRARittupleRBR$RsR+RDRxRGRRIRtPRINTABLE_PROXY_TYPEStlogtdebugR%t_proxy_negotiatorsR(RRJRRR-RMRNR0R1R2RHt proxy_servertprintable_typeRt negotiate((s)/usr/lib/python2.7/site-packages/socks.pyRGs\ %                 cCsO|j\}}}}}}|p-tj|}|sEtdn||fS(sD Return proxy address to connect to as tuple object sInvalid proxy type(Rut DEFAULT_PORTSRR$(RR-RMRNR0R1R2((s)/usr/lib/python2.7/site-packages/socks.pyR]s N()R R!R"R#R,R8RR?RRRDR R R RERRFRmRnRoRpRIRtgetproxysocknameRtgetproxypeernameRRRRRRRRRR R RRRGR(((s)/usr/lib/python2.7/site-packages/socks.pyR+&sF       %          Z -  < ; W(ii(RmRnRoRp(FR"tbase64Rt collectionsRterrnoRRRRtioRtloggingtosRR8Rtsyst __version__Rht version_infot win_inet_ptont ImportErrort getLoggerR RtPROXY_TYPE_SOCKS4RtPROXY_TYPE_SOCKS5R tPROXY_TYPE_HTTPR t PROXY_TYPESRatziptvaluestkeysRt _orgsocketR`RtIOErrorRR$R%R&R'R(R)RRRR#R R3R6R7tgetdefaultproxyR:t wrapmoduleR^R_RlRdtmethodRARcRtsetattrR+(((s)/usr/lib/python2.7/site-packages/socks.pyt7s          !          8