o ™­³_õLã@sðdZddlZddlZddlZddlZeeddƒduredƒ‚dZdZdZ dZ da ejZ Gd d „d e ƒZGd d „d eƒZGd d„deƒZGdd„deƒZGdd„deƒZGdd„deƒZdZdZdZdZ d dd„Zdd„ZGdd„dejƒZdS)!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. éNÚsocketz-socket.socket missing, proxy support unusableééééc@ó eZdZdS)Ú ProxyErrorN©Ú__name__Ú __module__Ú __qualname__©r r ú0/usr/lib/python3/dist-packages/httplib2/socks.pyr7órc@r)ÚGeneralProxyErrorNr r r r rr;rrc@r)ÚSocks5AuthErrorNr r r r rr?rrc@r)Ú Socks5ErrorNr r r r rrCrrc@r)Ú Socks4ErrorNr r r r rrGrrc@r)Ú HTTPErrorNr r r r rrKrr)Ú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-idsrTcCs||||||fadS)z´setdefaultproxy(proxytype, addr[, port[, rdns[, username[, password]]]]) Sets a default proxy which all further socksocket objects will use, unless explicitly changed. N)Ú _defaultproxy)Ú proxytypeÚaddrÚportÚrdnsÚusernameÚpasswordr r rÚsetdefaultproxywsrcCstdkr t|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)Úmoduler r rÚ wrapmodule‚s r"csžeZdZdZejejddfdd„Zdd„Z‡fdd „Z d d „Z d d „Z       ddd„Z dd„Z dd„Zdd„Zdd„Zdd„Zdd„Zdd„Z‡ZS) 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. rNcCs>t |||||¡tdkrt|_nd|_d|_d|_d|_dS)N)NNNNNNT)Ú _orgsocketÚ__init__rÚ_socksocket__proxyÚ_socksocket__proxysocknameÚ_socksocket__proxypeernameÚ_socksocket__httptunnel)ÚselfÚfamilyÚtypeÚprotoÚ_sockr r rr$˜s zsocksocket.__init__cCsL| |¡}t|ƒ|kr$| |t|ƒ¡}|stdƒ‚||}t|ƒ|ks |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)ÚrecvÚlenr)r)ÚcountÚdataÚdr r rÚ __recvall¤s   üzsocksocket.__recvallcs(|js| |¡}tt|ƒj|g|¢RŽS)zq override socket.socket.sendall method to rewrite the header for non-tunneling proxies if needed )r(Ú_socksocket__rewriteproxyÚsuperr Úsendall)r)ÚcontentÚargs©Ú __class__r rr6±s zsocksocket.sendallc Csîd\}}| d¡}|D]}| ¡ d¡r|}q | ¡ d¡s%| ¡ d¡r'|}q |rr|rr| |¡| |¡| d¡d}| d¡}|jdd krX|jd d krX| d | ¡¡| d d |¡| d d |d ||d|df¡d |¡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:ÚgetÚpostú rrNérzHost: %sz%s http://%s%s %sr)ÚsplitÚlowerÚ startswithÚremover%ÚinsertÚ_socksocket__getauthheaderÚjoin)r)ÚheaderÚhostÚendptÚhdrsÚhdrr r rÚ__rewriteproxy¹s$ €   $ zsocksocket.__rewriteproxycCs*|jdd|jd}dt |¡ ¡S)Nró:r?zProxy-Authorization: Basic )r%Úbase64Ú b64encodeÚdecode)r)Úauthr r rÚ__getauthheaderÐszsocksocket.__getauthheaderTcCs0|||||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)Úencoder%)r)rrrrrrÚheadersr r rÚsetproxyÔs ùzsocksocket.setproxyc Csú|jddkr|jddkr| t ddddd¡¡n | t dddd¡¡| d¡}|dd…tdƒ ¡krC| ¡tdt dfƒ‚|dd…tdƒ ¡krPn‘|dd…tdƒ ¡krÃt ƒ}|  d¡|  t |jdƒ¡|  |jd¡|  t |jdƒ¡|  |jd¡| |¡| d¡}|dd…tdƒ ¡krª| ¡tdt dfƒ‚|dd…tdƒ ¡krÂ| ¡td td fƒ‚n| ¡|dtd ƒ ¡krÙtdtdfƒ‚tdt dfƒ‚t dddd¡}zt |¡}|tdƒ ¡|}Wn:tjy4|jd r d}|td ƒ ¡tt |ƒƒ ¡| ¡}nt t |¡¡}|tdƒ ¡|}Ynw|t d |¡}| |¡| d¡}|dd…tdƒ ¡kr`| ¡tdt dfƒ‚|dd…tdƒ ¡kr˜| ¡t|dd…ƒd krtt|dd…ƒtt|dd…ƒfƒ‚td td fƒ‚|d d…tdƒ ¡kr«| d¡} n,|d d…td ƒ ¡krË|| d¡}| t|dd…ƒ¡} n | ¡tdt dfƒ‚t d | d¡¡d} | | f|_|dkröt |¡|f|_dS||f|_dS)zk__negotiatesocks5(self,destaddr,destport) Negotiates a connection through a SOCKS5 server. rNr?ÚBBBBrrÚBBBrréÿú>Héé )r%r6ÚstructÚpackÚ_socksocket__recvallÚchrrSÚcloserÚ_generalerrorsÚ bytearrayÚappendr/ÚextendrÚ_socks5autherrorsrÚ inet_atonÚerrorÚ gethostbynameÚordrÚ _socks5errorsr.Úunpackr&Ú inet_ntoar') r)ÚdestaddrÚdestportÚ chosenauthÚpacketÚauthstatÚreqÚipaddrÚrespÚ boundaddrÚ boundportr r rÚ__negotiatesocks5ûsŽ    ý   ÿþýÿ€ò  (   zsocksocket.__negotiatesocks5cCó|jS)zigetsockname() -> address info Returns the bound IP address and port number at the proxy. )r&©r)r r rÚgetproxysocknamebszsocksocket.getproxysocknamecCs t |¡S)z`getproxypeername() -> address info Returns the IP and port number of the proxy. )r#Ú getpeernameryr r rÚgetproxypeernamehs zsocksocket.getproxypeernamecCrx)z£getpeername() -> address info Returns the IP address and port number of the destination machine (note: getproxypeername returns the proxy) )r'ryr r rr{nszsocksocket.getpeernamec CsÄd}zt |¡}Wn#tjy,|jdr"t ddddd¡}d}nt t |¡¡}Ynwt ddd|¡|}|jdd krE||jd}|tdƒ ¡}|rY||tdƒ ¡}|  |¡|  d ¡}|dd…tdƒ ¡kr{|  ¡t dt dfƒ‚|dd …td ƒ ¡kr·|  ¡t|dd …ƒd vr¯|  ¡tt|dd …ƒtt|dd …ƒd fƒ‚tdtdfƒ‚t |dd …¡t d|d d…¡df|_|d krÛt |¡|f|_d S||f|_d S)zk__negotiatesocks4(self,destaddr,destport) Negotiates a connection through a SOCKS4 server. FrrVrrTz>BBHrNrZréZ)é[é\é]é^rY)rrfrgr%r\r]rhr_rSr6r^r`rrarirÚ _socks4errorsrlrkr&r')r)rmrnÚrmtrslvrsrrrtr r rÚ__negotiatesocks4usB €ú  ,þzsocksocket.__negotiatesocks4c CsÒ|jds t |¡}n|}d|dt|ƒdg}d}d}|jddkrA|jd ¡D]\}}||d|d g7}| ¡d k}| ¡d k}q(|sJ|d |d g7}|sb|jd dkrb|jddkrb|| ¡d g7}| d ¡| d  |¡  ¡¡|  d¡} |   d  ¡¡dkr| |  d¡} |   d  ¡¡dks|   ¡d d  ¡d¡} | dd  ¡d  ¡fvr³| ¡tdtdfƒ‚zt| dƒ} WntyÎ| ¡tdtdfƒ‚w| dkrß| ¡t| | dfƒ‚d|_||f|_dS)zh__negotiatehttp(self,destaddr,destport) Negotiates a connection through an HTTP server. rzCONNECT ú:z HTTP/1.1 FéNz: r;rHzproxy-authorizationzHost: rr?Úrz éÿÿÿÿrr>rzHTTP/1.0zHTTP/1.1éÈ)z0.0.0.0r)r%rrhÚstrÚ iteritemsrArErcr6rFrSr.ÚfindÚ splitlinesr@r`rraÚintÚ ValueErrorrr&r') r)rmrnrrTÚwrote_host_headerÚwrote_auth_headerÚkeyÚvalrtÚ statuslineÚ statuscoder r rÚ__negotiatehttp¨sJ     ÿ þzsocksocket.__negotiatehttpcCst|ƒttfvst|ƒdkst|dttfƒrt|dƒtkr'tdt dfƒ‚|j dt krT|j ddkr;|j d}nd}t   ||j d|f¡| |d|d¡dS|j dtkr|j ddkrh|j d}nd}t   ||j d|f¡| |d|d¡dS|j dtkr®|j ddkr•|j d}nd}t   ||j d|f¡| |d|d¡dS|j dtkræ|j ddkrÂ|j d}nd}t   ||j d|f¡|ddkrá| |d|d¡dSd |_dS|j ddkrût   ||d|df¡dStd t d fƒ‚) zÿconnect(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(). rrrr?Ni8ii»Fr)r+ÚlistÚtupler/Ú isinstancerŠÚbytesrŽrrar%ÚPROXY_TYPE_SOCKS5r#ÚconnectÚ_socksocket__negotiatesocks5ÚPROXY_TYPE_SOCKS4Ú_socksocket__negotiatesocks4ÚPROXY_TYPE_HTTPÚ_socksocket__negotiatehttpÚPROXY_TYPE_HTTP_NO_TUNNELr()r)ÚdestpairÚportnumr r rrœÕsF ÿ      zsocksocket.connect)NNNTNNN)r r r Ú__doc__rÚAF_INETÚ SOCK_STREAMr$r^r6r4rErUrrzr|r{rŸr¡rœÚ __classcell__r r r9rr ‘s.  ÿ  ø'g3-r )NNNTNN)r¥rNrr\ÚsysÚgetattrÚ ImportErrorržr›r r¢rr#Ú Exceptionrrrrrrrarjrer‚rr"r r r r rÚs6%   ÿ