B "Ù@Wuã @sädZdZddlZddlZddlmZmZmZddlm Z ddl m Z ddl m Z ddlmZd ZZd ZZd ZZeeed œZeee ¡e ¡ƒƒZejZZGd d„deƒZGdd„deƒZ Gdd„deƒZ!Gdd„deƒZ"Gdd„deƒZ#Gdd„deƒZ$Gdd„deƒZ%ddddœZ&dd d!d"d#d$d%d&d'œZ'ed(ed(ed)iZ(d:d+d,„Z)e)Z*d-d.„Z+e+Z,d/d0„Z-e-Z.d;d1d2„Z/Gd3d4„d4ejƒZ0d5d6„Z1xBd7D]:Z2e3e0e2dƒZ4e5e4e ƒs’e0j6 7e2¡e8e0e2e1e2ƒƒq’WGd8d9„d9e0ƒZ9dS) 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 z1.5.7éN)Ú EOPNOTSUPPÚEINVALÚEAGAIN)ÚBytesIO)ÚSEEK_CUR)ÚCallable)Ú b64encodeééé)ÚSOCKS4ÚSOCKS5ÚHTTPc@s"eZdZdZddd„Zdd„ZdS)Ú ProxyErrorz> socket_err contains original socket.error exception. NcCs(||_||_|r$|jd |¡7_dS)Nz: {0})ÚmsgÚ socket_errÚformat)Úselfrr©rú6/opt/alt/python37/lib/python3.7/site-packages/socks.pyÚ__init__NszProxyError.__init__cCs|jS)N)r)rrrrÚ__str__UszProxyError.__str__)N)Ú__name__Ú __module__Ú __qualname__Ú__doc__rrrrrrrJs rc@s eZdZdS)ÚGeneralProxyErrorN)rrrrrrrrXsrc@s eZdZdS)ÚProxyConnectionErrorN)rrrrrrrrYsrc@s eZdZdS)ÚSOCKS5AuthErrorN)rrrrrrrrZsrc@s eZdZdS)Ú SOCKS5ErrorN)rrrrrrrr[src@s eZdZdS)Ú SOCKS4ErrorN)rrrrrrrr \sr c@s eZdZdS)Ú HTTPErrorN)rrrrrrrr!]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| ¡nd|r"| ¡ndft_dS)zé set_default_proxy(proxy_type, addr[, port[, rdns[, username, password]]]) Sets a default proxy which all further socksocket objects will use, unless explicitly changed. All parameters are as for socket.set_proxy(). N)ÚencodeÚ socksocketÚ default_proxy)Ú proxy_typeÚaddrÚportÚrdnsÚusernameÚpasswordrrrÚset_default_proxyssr3cCstjS)z> Returns the default proxy, set by set_default_proxy. )r+r,rrrrÚget_default_proxy€sr4cCstjrt|j_ntdƒ‚dS)a" Attempts 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)r+r,Úsocketr)ÚmodulerrrÚ wrap_moduleˆs r7c  CsH|\} } |  d¡r|  d¡} |r4| d¡r4| d¡}d} xòt ||dtj¡D]Ü} | \}}}}}d}y„t|||ƒ}| dk rx| D]}|j|Žq~Wt|tt fƒr¨|  |¡|dk rÄ|  ||||||¡|dk rÖ|  |¡|  | | f¡|Stjk r&}z|} |dk r| ¡d}Wdd}~XYqLXqLW| dk r:| ‚t d¡‚dS)aícreate_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.)Ú startswithÚstripr5Ú getaddrinfoÚ SOCK_STREAMr+Z setsockoptÚ isinstanceÚintÚfloatZ settimeoutÚ set_proxyÚbindÚconnectÚerrorÚclose)Ú dest_pairr-Ú proxy_addrÚ proxy_portZ proxy_rdnsZproxy_usernameZproxy_passwordÚtimeoutZsource_addressZsocket_optionsZ remote_hostZ remote_portÚerrÚrÚfamilyZ socket_typeÚprotoZ canonnameZsaZsockÚoptÚerrrÚcreate_connection–s>          rOc@seZdZdZdd„ZeƒZdS)Ú _BaseSocketzJAllows Python 2's "delegated" methods such as send() to be overridden cOsHtj|f|ž|Žtƒ|_x(|jD]}t||ƒ|j|<t||ƒq"WdS)N)Ú _orig_socketrÚdictÚ _savedmethodsÚ _savenamesÚgetattrÚdelattr)rÚposÚkwÚnamerrrrÔs  z_BaseSocket.__init__N)rrrrrÚlistrTrrrrrPÑsrPcs ‡fdd„S)Ncs|jˆ||ŽS)N)rS)rrWrX)rYrrÚßóz_makemethod..r)rYr)rYrÚ _makemethodÞsr])ÚsendtoÚsendÚrecvfromÚrecvc@sæeZdZdZdZejejdfdd„Zdd„Z d-d d „Z e Z d d „Z d d„Z d.dd„Zd/dd„Zdd„Zdd„Zdd„ZeZdd„ZeZdd„ZeZdd„Zdd „Zd!d"„Zd#d$„Zd%d&„Zd'd(„Zeeeee eiZ!d)d*„Z"d+d,„Z#dS)0r+a2socksocket([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. NrcOsf|tjtjfkr"d}t| |¡ƒ‚tj||||f|ž|Žd|_|jrP|j|_ nd|_ d|_ d|_ dS)Nz0Socket type must be stream or datagram, not {!r})NNNNNN) r5r<Ú SOCK_DGRAMÚ ValueErrorrrPrÚ _proxyconnr,ÚproxyÚproxy_socknameÚproxy_peername)rrKÚtyperLÚargsÚkwargsrrrrrös zsocksocket.__init__cCs@d}x6t|ƒ|kr:| |t|ƒ¡}|s0tdƒ‚||7}qW|S)z› Receive EXACTLY the number of bytes requested from the file object. Blocks until the required number of bytes have been received. r\zConnection closed unexpectedly)ÚlenÚreadr)rÚfileÚcountÚdataÚdrrrÚ_readalls zsocksocket._readallTcCs.|||||r| ¡nd|r"| ¡ndf|_dS)aäset_proxy(proxy_type, addr[, port[, rdns[, username[, password]]]]) 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*re)rr-r.r/r0r1r2rrrr@szsocksocket.set_proxycOsâ|j\}}}}}}|r"|jtjkr4tj|f|ž|ŽS|jrFt td¡‚|t kr^d} t t | ¡‚t j|f|ž|Ž|  ¡\} } d| f} tƒ|_|  ¡} |j | ¡d}| |j|| ¡\} }| \}} |\} } t  ||| f¡d|_dS)zm Implements proxy connection for UDP sockets, which happens during the bind() phase. z"Socket already bound to an addressz'UDP only supported by SOCKS5 proxy typeÚ0ó)z0.0.0.0rN)rerhr5rbrQrArdrCrr rrPZ getsocknameÚ _proxy_addrrBÚ_SOCKS5_requestrf)rrWrXr-rFrGr0r1r2rÚ_r/ÚdstreZ UDP_ASSOCIATEZrelayÚhostrrrrA*s(    zsocksocket.bindc Osš|jtjkr tj||f|ž|ŽS|js0| d¡|d}|dd…}tƒ}d}| |¡d}| |¡|  ||¡tj ||  ¡|f|ž|Ž} | |  ¡S)N)Úréÿÿÿÿsó) rhr5rbrPr^rdrArÚwriteÚ_write_SOCKS5_addressr_ÚgetvalueÚtell) rÚbytesrirjZaddressÚflagsÚheaderZRSVZ STANDALONEZsentrrrr^Ns      zsocksocket.sendtocKs6|jtjkr |j|||jf|ŽStj|||f|ŽSdS)N)rhr5rbr^rgrPr_)rr€rrjrrrr_as zsocksocket.sendc Cs°|jtjkrt |||¡S|js*| d¡tt |||¡ƒ}|  dt ¡|  d¡}t |ƒrbt dƒ‚| |¡\}}|jr |j\}}||ks”|d|fkr t td¡‚|  ¡||ffS)N)ryrr r zReceived UDP packet fragmentrzPacket filtered)rhr5rbrPr`rdrArraÚseekrrlÚordÚNotImplementedErrorÚ_read_SOCKS5_addressrgrCr) rÚbufsizerÚbufZfragZfromhostZfromportZpeerhostZpeerportrrrr`gs      zsocksocket.recvfromcOs|j||Ž\}}|S)N)r`)rrWrXr€rvrrrra{szsocksocket.recvcCs|jr|j ¡t |¡S)N)rdrDrP)rrrrrDs zsocksocket.closecCs|jS)zL Returns the bound IP address and port number at the proxy. )rf)rrrrÚget_proxy_sockname„szsocksocket.get_proxy_socknamecCs t |¡S)z> Returns the IP and port number of the proxy. )rPÚ getpeername)rrrrÚget_proxy_peernameŒszsocksocket.get_proxy_peernamecCs|jS)zˆ Returns the IP address and port number of the destination machine (note: get_proxy_peername returns the proxy) )rg)rrrrÚ get_peername”szsocksocket.get_peernamecGsd}| |||¡\|_|_dS)zI Negotiates a stream connection through a SOCKS5 server. óN)rurgrf)rÚ dest_addrZCONNECTrrrÚ_negotiate_SOCKS5szsocksocket._negotiate_SOCKS5cCsÞ|j\}}}}}} | d¡} | dd¡} zœ|r@| r@|  d¡n |  d¡|  ¡| | d¡} | dd…dkrvtd ƒ‚| dd…d krþ|  d tt|ƒƒ ¡|tt| ƒƒ ¡| ¡|  ¡| | d¡} | dd…d krätd ƒ‚| dd…d krüt d ƒ‚n6| dd…d kr4| dd…dkr,t dƒ‚ntd ƒ‚|  d|d ¡|  || ¡}|  ¡| | d¡}|dd…dkr€td ƒ‚t |dd…ƒ}|dkr¶t   |d¡}td ||¡ƒ‚| | ¡}||fS|  ¡|  ¡XdS)z‘ Send SOCKS5 request with given command (CMD field) and address (DST field). Returns resolved DST address that was used. ÚwbÚrbrssr r óz%SOCKS5 proxy server sent invalid dataórr{zSOCKS5 authentication failedóÿz7All offered SOCKS5 authentication methods were rejectedr z Unknown errorz {0:#04x}: {1}N)reÚmakefiler|ÚflushrqrÚchrrkr*rr}r„Ú SOCKS5_ERRORSÚgetrrr†rD)rZconnÚcmdrwr-r.r/r0r1r2ÚwriterÚreaderZ chosen_authZ auth_statusZresolvedÚrespÚstatusrCZbndrrrru¥sL     .        zsocksocket._SOCKS5_requestc CsH|\}}|j\}}}}}} tjdtjdi} xrtjtjfD]b} yDt | |¡} | | | | ¡t | | ¡}| t d|¡¡||fStj k r˜w8Yq8Xq8W|rÌ|  d¡} | dt t | ƒƒ  ¡| ¡nbt  ||tjtjtjtj¡}|d}|d} |dd}t | |¡} | | | | ¡t | | ¡}| t d|¡¡||fS)z~ Return the host and port packed for the SOCKS5 protocol, and the resolved address as a tuple object. róz>HÚidnarsrr%)rer5ÚAF_INETÚAF_INET6Z inet_ptonr|Ú inet_ntopÚstructÚpackrCr*r—rkr;Z AF_UNSPECr<Z IPPROTO_TCPZ AI_ADDRCONFIG)rr.rmrxr/r-rvr0r1r2Zfamily_to_byterKÚ addr_bytesZ host_bytesZ addressesZ target_addrrrrr}÷s0        z socksocket._write_SOCKS5_addresscCs–| |d¡}|dkr(t | |d¡¡}nN|dkrN| |d¡}| |t|ƒ¡}n(|dkrnt tj| |d¡¡}ntdƒ‚t d| |d ¡¡d }||fS) Nr rr%rsrŸéz%SOCKS5 proxy server sent invalid dataz>Hr r) rqr5Ú inet_ntoar„r£r¢rr¤Úunpack)rrmZatypr.Úlengthr/rrrr†!s  zsocksocket._read_SOCKS5_addresscCsŽ|j\}}}}}}| d¡} | dd¡} zLd} yt |¡} Wn4tjk rr|r^d} d} nt t |¡¡} YnX|  t ddd |¡¡|  | ¡|r¢|  |¡|  d ¡| rÄ|  |  d ¡d ¡|   ¡|  | d ¡} | dd …d krðt d ƒ‚t | d d…ƒ}|dkr&t |d¡}td ||¡ƒ‚t | dd…¡t d| dd…¡df|_| rjt | ¡|f|_n ||f|_Wd|  ¡|  ¡XdS)zB Negotiates a connection through a SOCKS4 server. rr‘rFsTz>BBHr%r r{r r)z%SOCKS4 proxy server sent invalid datar éZz Unknown errorz {0:#04x}: {1}Nz>H)rer•r5Z inet_atonrCÚ gethostbynamer|r¤r¥r*r–rqrr„Ú SOCKS4_ERRORSr™r rr¨r©rfrgrD)rrŽÚ dest_portr-r.r/r0r1r2r›rœZremote_resolver¦rržrCrrrÚ_negotiate_SOCKS40sB        ,zsocksocket._negotiate_SOCKS4cCsj|j\}}}}}}|r|nt |¡}d| d¡dt|ƒ ¡dd| d¡g} |rv|rv|  dt|d|ƒ¡|  d¡| d | ¡¡|  ¡} |   ¡} |   ¡| s´t dƒ‚y|   d d ¡\} } }Wntk ræt d ƒ‚YnX|  d ¡sút d ƒ‚y t| ƒ} Wntk r$tdƒ‚YnX| dkrVd | |¡}| dkrN|d7}t|ƒ‚d|_||f|_dS)zˆ Negotiates 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 statuséÈz{0}: {1})ii“i•za [*] Note: The HTTP proxy server may not be supported by PySocks (must be a CONNECT tunnel proxy))s0.0.0.0rN)rer5r¬r*ÚstrÚappendrZsendallÚjoinr•ÚreadlinerDrÚsplitrcr9r>r!rrfrg)rrŽr®r-r.r/r0r1r2Z http_headersZfobjZ status_linerLZ status_codeZ status_msgrCrrrÚ_negotiate_HTTPks<       zsocksocket._negotiate_HTTPc CsÈt|ƒdks|d d¡r$t d¡‚|\}}|jtjkrt|jsH| d¡t |¡}|dkrf|sfd|_ n ||f|_ dS|j \}}}}}} t |t t fƒr®t|ƒdks®|r®t |tƒs¶tdƒ‚|dkrØ||_ t |||f¡dS| ¡}yt ||¡Wn`tjk rP} z>| ¡|\}}d  ||¡} t|} d  | | ¡} t| | ƒ‚Wdd} ~ XYntXy|j|}||||ƒWnVtjk r¤} z| ¡td | ƒ‚Wdd} ~ XYn tk rÂ| ¡‚YnXdS) 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 rr8zPySocks doesn't support IPv6)ryrz0.0.0.0Nz0Invalid destination-connection (host, port) pairz{0}:{1}z!Error connecting to {0} proxy {1}z Socket error)rkr9r5rCrhrbrdrAr¬rgrer=rZÚtupler>rrPrBrtrDrÚPRINTABLE_PROXY_TYPESrÚ_proxy_negotiatorsr)rrErŽr®r-rFrGr0r1r2rCZ proxy_serverZprintable_typerZ negotiaterrrrB¨sR          zsocksocket.connectcCs4|j\}}}}}}|pt |¡}|s,tdƒ‚||fS)zD Return proxy address to connect to as tuple object zInvalid proxy type)reÚ DEFAULT_PORTSr™r)rr-rFrGr0r1r2rrrrtõs zsocksocket._proxy_addr)NNNTNN)r)r)$rrrrr,r5r¡r<rrqr@ZsetproxyrAr^r_r`rarDr‰Zgetproxysocknamer‹ZgetproxypeernamerŒrŠrrur}r†r¯r¸r r rr»rBrtrrrrr+ës: $  R*;7Mr+)NNNTNN) NNNTNNNNN):rÚ __version__r5r¤ÚerrnorrrÚiorÚosrÚ collectionsrÚbase64rZPROXY_TYPE_SOCKS4r ZPROXY_TYPE_SOCKS5r ZPROXY_TYPE_HTTPrZ PROXY_TYPESrRÚzipÚvaluesÚkeysrºZ _orgsocketrQÚIOErrorrrrrrr r!r­r˜r¼r3Zsetdefaultproxyr4Zgetdefaultproxyr7Z wrapmodulerOrPr]rYrUÚmethodr=rTr´Úsetattrr+rrrrÚ5sl         7