3 2;Z~@sXdZddlmZddlmZddlmZmZmZddl Z ddl m Z ddl Z ddl Z ddl mZddlZddlZddlZdZe jd krejdCkry ddlZWnek red YnXe jeZd ZZd ZZd ZZeeedZ e!e"e j#e j$Z%ejZ&Z'ddZ(Gddde)Z*Gddde*Z+Gddde*Z,Gddde*Z-Gddde*Z.Gddde*Z/Gddde*Z0dd d!d"Z1d#d$d%d&d'd(d)d*d+Z2ed,ed,ed-iZ3dDd/d0Z4d1d2Z5d3d4Z6e6Z7d5d6Z8e8Z9dEd7d8Z:Gd9d:d:ejZ;d;d<Ze?e>ese;j@jAeeBe;ee 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 ) b64encode)Callable) EOPNOTSUPPEINVALEAGAINN)BytesIO)SEEK_CURz1.6.7ntz8To run PySocks on Windows you must install win_inet_pton)SOCKS4SOCKS5HTTPcstjfdd}|S)Ncsp|d}zNy$|j}|dkr&|jd||Stk rR}zWYdd}~XnXWd|dkrj|jdXdS)NrTF) gettimeout setblocking Exception)argskwargsselfZ _is_blockinge)function/usr/lib/python3.6/socks.pywrapper]s  z"set_self_blocking..wrapper) functoolswraps)rrr)rrset_self_blocking[src@s"eZdZdZdddZddZdS) ProxyErrorz4Socket_err contains original socket.error exception.NcCs(||_||_|r$|jdj|7_dS)Nz: {0})msg socket_errformat)rrr rrr__init__pszProxyError.__init__cCs|jS)N)r)rrrr__str__wszProxyError.__str__)N)__name__ __module__ __qualname____doc__r"r#rrrrrns rc@s eZdZdS)GeneralProxyErrorN)r$r%r&rrrrr({sr(c@s eZdZdS)ProxyConnectionErrorN)r$r%r&rrrrr)sr)c@s eZdZdS)SOCKS5AuthErrorN)r$r%r&rrrrr*sr*c@s eZdZdS) SOCKS5ErrorN)r$r%r&rrrrr+sr+c@s eZdZdS) SOCKS4ErrorN)r$r%r&rrrrr,sr,c@s eZdZdS) HTTPErrorN)r$r%r&rrrrr-sr-zRequest rejected or failedzLRequest rejected because SOCKS server cannot connect to identd on the clientzPRequest rejected because the client program and identd report different user-ids)[\]zGeneral SOCKS server failurez!Connection not allowed by rulesetzNetwork unreachablezHost unreachablezConnection refusedz TTL expiredz(Command not supported, or protocol errorzAddress type not supported)r r r i8iTcCs.|||||r|jnd|r"|jndft_dS)zSets a default proxy. All further socksocket objects will use the default unless explicitly changed. All parameters are as for socket.set_proxy().N)encode socksocket default_proxy) proxy_typeaddrportrdnsusernamepasswordrrrset_default_proxysr?cOs d|kr|jd|d<t||S)N proxytyper9)popr?)rrrrrsetdefaultproxysrBcCstjS)z4Returns the default proxy, set by set_default_proxy.)r7r8rrrrget_default_proxysrCcCstjrt|j_ntddS)aAttempts to replace a module's socket library with a SOCKS socket. Must set a default proxy using set_default_proxy(...) first. This will only work on modules that import socket directly into the namespace; most of the Python Standard Library falls into this category.zNo default proxy specifiedN)r7r8socketr()modulerrr wrap_modules rFc Cs8|\} } | jdr| jd} |r4|jdr4|jd}d} xtj||dtjD]} | \}}}}}d}yxt|||}| rx| D]}|j|qzWt|tt fr|j ||r|j |||||||r|j ||j | | f|Stjtfk r}z|} |r |jd}WYdd}~XqLXqLW| r*| tjddS)acreate_connection(dest_pair, *[, timeout], **proxy_args) -> 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) [z[]Nrzgai returned empty list.) startswithstriprD getaddrinfo SOCK_STREAMr7Z setsockopt isinstanceintfloat settimeout set_proxybindconnecterrorr)close) dest_pairtimeoutZsource_addressr9 proxy_addr proxy_portZ proxy_rdnsZproxy_usernameZproxy_passwordZsocket_optionsZ remote_hostZ remote_porterrrfamilyZ socket_typeprotoZ canonnameZsaZsockoptrrrrcreate_connections>        r^c@seZdZdZddZeZdS) _BaseSocketzBAllows Python 2 delegated methods such as send() to be overridden.cOsHtj|f||t|_x(|jD]}t|||j|<t||q"WdS)N) _orig_socketr"dict _savedmethods _savenamesgetattrdelattr)rposkwnamerrrr" s  z_BaseSocket.__init__N)r$r%r&r'r"listrcrrrrr_ sr_cs fddS)Ncs|j||S)N)rb)rrfrg)rhrrsz_makemethod..r)rhr)rhr _makemethodsrksendtosendrecvfromrecvcs.eZdZdZdZejejdffdd ZddZ fdd Z d d Z d d Z d5ddZ ddZfddZfddZd6fdd Zd7fdd ZddZfddZdd ZeZd!d"ZeZd#d$ZeZd%d&Zfd'd(Zd)d*Zd+d,Zd-d.Zd/d0Z e!ee"ee#e iZ$e%fd1d2Z&d3d4Z'Z(S)8r7a2socksocket([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. Nrcsp|tjtjfkr"d}t|j|tt|j|||f||d|_|j rT|j |_ nd|_ d|_ d|_ d|_ dS)Nz0Socket type must be stream or datagram, not {!r})NNNNNN)rDrK SOCK_DGRAM ValueErrorr!superr7r" _proxyconnr8proxyproxy_socknameproxy_peername_timeout)rr[typer\rrr) __class__rrr"1s zsocksocket.__init__cCs@d}x6t||kr:|j|t|}|s0td||7}qW|S)zReceive EXACTLY the number of bytes requested from the file object. Blocks until the required number of bytes have been received.zConnection closed unexpectedly)lenreadr()rfilecountdatadrrr_readallCs zsocksocket._readallc s@||_y|j}tt|j|jWntjk r:YnXdS)N)rwget_proxy_peernamerrr7rOrDrS)rrVZpeer)ryrrrOOs zsocksocket.settimeoutcCs|jS)N)rw)rrrrrXszsocksocket.gettimeoutcCs|r|jdn |jddS)Ng)rO)rvrrrr[s zsocksocket.setblockingTcCs.|||||r|jnd|r"|jndf|_dS)a 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)r6rt)rr9r:r;r<r=r>rrrrPaszsocksocket.set_proxycOs"d|kr|jd|d<|j||S)Nr@r9)rArP)rrrrrrsetproxyvszsocksocket.setproxycs|j\}}}}}}| s$|jtjkr6tj|f||S|jrHtjtd|t kr`d} tjt | t t |j|||j \} } d| f} t|_|j} |jj| d}|j|j|| \} }| \}} |\} } t t |j|| ft t |j|jd|_dS) zVImplements proxy connection for UDP sockets. Happens during the bind() phase.z"Socket already bound to an addressz'UDP only supported by SOCKS5 proxy type00.0.0.0rN)rr)rtrxrDrpr`rQrsrSrrrrrr7Z getsockname _proxy_addrrR_SOCKS5_requestrOrwru)rrfrgr9rWrXr<r=r>r_r;dstrtZ UDP_ASSOCIATEZrelayhost)ryrrrQ{s*    zsocksocket.bindc s|jtjkr$tt|j|f||S|js4|jd|d}|dd}t}d}|j |d}|j ||j ||tt|j |j |f||} | |j S) Nrr s)rrr)rxrDrprrr7rlrsrQrwrite_write_SOCKS5_addressrmgetvaluetell) rbytesrrZaddressflagsheaderZRSVZ STANDALONEZsent)ryrrrls      zsocksocket.sendtoc s:|jtjkr |j|||jf|Stt|j||f|SdS)N)rxrDrprlrvrrr7rm)rrrr)ryrrrms zsocksocket.sendc s|jtjkrtt|j||S|js.|jdttt|j |d|}|j dt |j d}t |rntd|j|\}}|jr|j\}}||ks|d|fkrtjtd|j |||ffS) Nrrir r zReceived UDP packet fragmentzPacket filtered)rr)rxrDrprrr7rnrsrQrroseekrr|ordNotImplementedError_read_SOCKS5_addressrvrSr) rbufsizerZbufZfragZfromhostZfromportZpeerhostZpeerport)ryrrrns      zsocksocket.recvfromcOs|j||\}}|S)N)rn)rrfrgrrrrrroszsocksocket.recvcs|jr|jjtt|jS)N)rsrTrrr7)r)ryrrrTs zsocksocket.closecCs|jS)z:Returns the bound IP address and port number at the proxy.)ru)rrrrget_proxy_socknameszsocksocket.get_proxy_socknamecCs|jS)z> Returns the IP and port number of the proxy. ) getpeername)rrrrrszsocksocket.get_proxy_peernamecCs|jS)zwReturns the IP address and port number of the destination machine. Note: get_proxy_peername returns the proxy.)rv)rrrr get_peernameszsocksocket.get_peernamecGsd}|j|||\|_|_dS)z7Negotiates a stream connection through a SOCKS5 server.N)rrvru)r dest_addrZCONNECTrrr_negotiate_SOCKS5szsocksocket._negotiate_SOCKS5c s|j\}}}}}} |jd} |jdd} z|r@| r@| jdn | jd| j|j| d} | dddkrvtd | ddd kr| jd tt|j|tt| j| | j|j| d} | ddd krtd | ddd krt d n6| ddd kr4| dddkr,t dntd | jd|d |j || }| j|j| d}|dddkrtd t |dd}|dkrt j |d}tdj|||j| }tt|j|j||fS| j| jXdS)z Send SOCKS5 request with given command (CMD field) and address (DST field). Returns resolved DST address that was used. wbrbrssr r z%SOCKS5 proxy server sent invalid datarrzSOCKS5 authentication failedz7All offered SOCKS5 authentication methods were rejectedr z Unknown errorz {0:#04x}: {1}N)rtmakefilerflushrr(chrr{r6r*rr SOCKS5_ERRORSgetr+r!rrrr7rOrwrT)rZconncmdrr9r:r;r<r=r>writerreaderZ chosen_authZ auth_statusZresolvedrespstatusrSZbnd)ryrrrsX     .       zsocksocket._SOCKS5_requestc CsH|\}}|j\}}}}}} tjdtjdi} xrtjtjfD]b} yDtj| |} |j| | | tj| | }|jtjd|||fStj k rw8Yq8Xq8W|r|j d} |jdt t | j | nbtj ||tjtjtjtj}|d}|d} |dd}tj| |} |j| | | tj| | }|jtjd|||fS)z~ Return the host and port packed for the SOCKS5 protocol, and the resolved address as a tuple object. rz>Hidnarrr1)rtrDAF_INETAF_INET6Z inet_ptonr inet_ntopstructpackrSr6rr{rJZ AF_UNSPECrKZ IPPROTO_TCPZ AI_ADDRCONFIG)rr:r}rr;r9rr<r=r>Zfamily_to_byter[ addr_bytesZ host_bytesZ addressesZ target_addrrrrrMs6         z socksocket._write_SOCKS5_addresscCs|j|d}|dkr(tj|j|d}nN|dkrN|j|d}|j|t|}n(|dkrntjtj|j|d}ntdtjd|j|d d }||fS) Nr rr1rrz%SOCKS5 proxy server sent invalid dataz>Hr r) rrD inet_ntoarrrr(runpack)rr}Zatypr:Zlengthr;rrrrzs  zsocksocket._read_SOCKS5_addresscCs|j\}}}}}}|jd} |jdd} zLd} ytj|} Wn4tjk rr|r^d} d} ntjtj|} YnX| jtjddd || j| |r| j|| jd | r| j|j d d | j |j | d } | dd d krt d t | d d}|dkr&tj|d}tdj||tj| ddtjd| dddf|_| rjtj| |f|_n ||f|_Wd| j| jXdS)z0Negotiates a connection through a SOCKS4 server.rrrFsTz>BBHr1r rrr5z%SOCKS4 proxy server sent invalid datar Zz Unknown errorz {0:#04x}: {1}Nz>H)rtrrDZ inet_atonrS gethostbynamerrrr6rrr(r SOCKS4_ERRORSrr,r!rrrurvrT)rr dest_portr9r:r;r<r=r>rrZremote_resolverrrrSrrr_negotiate_SOCKS4sH        zsocksocket._negotiate_SOCKS4cCsj|j\}}}}}}|r|ntj|}d|jddt|jdd|jdg} |rv|rv| jdt|d|| jd|jdj| |j } | j } | j | st dy| j d d \} } }Wntk rt d YnX| jd st d y t| } Wntk r$tdYnX| dkrVdj| |}| dkrN|d7}t|d|_||f|_dS)zwNegotiates a connection through an HTTP server. NOTE: This currently only supports HTTP CONNECT-style proxies.sCONNECT r:s HTTP/1.1sHost: sProxy-Authorization: basic s zConnection closed unexpectedly r z'HTTP proxy server sent invalid responsezHTTP/z0Proxy server does not appear to be an HTTP proxyz4HTTP proxy server did not return a valid HTTP statusz{0}: {1}za [*] Note: The HTTP proxy server may not be supported by PySocks (must be a CONNECT tunnel proxy)0.0.0.0rN)rrr)rr)rtrDrr6strappendrZsendalljoinrreadlinerTr(splitrqrHrMr-r!rurv)rrrr9r:r;r<r=r>Z http_headersZfobjZ status_liner\Z status_codeZ status_msgrSrrr_negotiate_HTTPsB        zsocksocket._negotiate_HTTPcst|dks|djdr,tjdt||\}}|jtjkr~|jsP|jd tj |}|dkrp| rpd|_ n ||f|_ dS|j \}}}}}} t |t tf st|dks| st |t rtdtt|j|j|dkr||_ tt|j|jtt|j||fdS|j}ytt|j|Wnntjk r} zN|j|\}}d j||} t|} d j| | } tjd | | t| | WYdd} ~ XntXy|j|}||||WnVtjk r} z|jtd | WYdd} ~ Xn tk r|jYnXdS)z 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). r rrGz PySocks doesn't support IPv6: %srz0.0.0.0Nz0Invalid destination-connection (host, port) pairz{0}:{1}z!Error connecting to {0} proxy {1}z %s due to: %sz Socket error)rr)r{rHrDrSrrxrprsrQrrvrtrLrituplerMr(rrr7rOrwrRrrTr!PRINTABLE_PROXY_TYPESlogdebugr)_proxy_negotiatorsr)rrUrrr9rWrXr<r=r>rSZ proxy_serverZprintable_typerZ negotiate)ryrrrRs\           zsocksocket.connectcCs4|j\}}}}}}|ptj|}|s,td||fS)zD Return proxy address to connect to as tuple object zInvalid proxy type)rt DEFAULT_PORTSrr()rr9rWrXr<r=r>rrrr]s zsocksocket._proxy_addr)NNNTNN)r)r))r$r%r&r'r8rDrrKr"rrOrrrPrrQrlrmrnrorTrZgetproxysocknamerZgetproxypeernamerrrrrrrrr rrrrrRr __classcell__rr)ryrr7&sD   %   Z-<<Wr7)r r)NNNTNN) NNNNNTNNN)rlrmrnro)Dr'base64r collectionsrerrnorrrriorZloggingosrrDrsys __version__rh version_infoZ win_inet_pton ImportErrorZ getLoggerr$rZPROXY_TYPE_SOCKS4r ZPROXY_TYPE_SOCKS5rZPROXY_TYPE_HTTPrZ PROXY_TYPESrazipvalueskeysrZ _orgsocketr`rIOErrorrr(r)r*r+r,r-rrrr?rBrCZgetdefaultproxyrFZ wrapmoduler^r_rkrdmethodrLrcrsetattrr7rrrr7s             8