U "Ù@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„Z1d7D]:Z2e3e0e2dƒZ4e5e4e ƒse0j6 7e2¡e8e0e2e1e2ƒƒqGd8d9„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/python38/lib/python3.8/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)ÚProxyConnectionErrorNrrrrrr Ysr c@s eZdZdS)ÚSOCKS5AuthErrorNrrrrrr!Zsr!c@s eZdZdS)Ú SOCKS5ErrorNrrrrrr"[sr"c@s eZdZdS)Ú SOCKS4ErrorNrrrrrr#\sr#c@s eZdZdS)Ú HTTPErrorNrrrrrr$]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_proxyssþr6cCstjS)z> Returns the default proxy, set by set_default_proxy. )r.r/rrrrÚget_default_proxy€sr7cCstjrt|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 r:c  CsF|\} } |  d¡r|  d¡} |r4| d¡r4| d¡}d} t ||dtj¡D]Þ} | \}}}}}d}z†t|||ƒ}| dk rŠ| D]}|j|Žqzt|tt fƒr¢|  |¡|dk r¾|  ||||||¡|dk rÐ|  |¡|  | | f¡|WStjk r&}z|} |dk r| ¡d}W5d}~XYqJXqJ| dk r8| ‚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Ústripr8Ú getaddrinfoÚ SOCK_STREAMr.Z setsockoptÚ isinstanceÚintÚfloatZ settimeoutÚ set_proxyÚbindÚconnectÚerrorÚclose)Ú dest_pairr0Ú 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–sB       ÿ    rRc@seZdZdZdd„ZeƒZdS)Ú _BaseSocketzJAllows Python 2's "delegated" methods such as send() to be overridden cOsDtj|f|ž|Žtƒ|_|jD]}t||ƒ|j|<t||ƒq dSr)Ú _orig_socketrÚdictÚ _savedmethodsÚ _savenamesÚgetattrÚdelattr)rÚposÚkwÚnamerrrrÔs  z_BaseSocket.__init__N)rrrrrÚlistrWrrrrrSÑsrScs ‡fdd„S)Ncs|jˆ||ŽSr)rV)rrZr[©r\rrÚßóz_makemethod..rr^rr^rÚ _makemethodÞsra)Ú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) r8r?Ú SOCK_DGRAMÚ ValueErrorrrSrÚ _proxyconnr/ÚproxyÚproxy_socknameÚproxy_peername)rrNÚtyperOÚargsÚkwargsrrrrrös zsocksocket.__init__cCs<d}t|ƒ|kr8| |t|ƒ¡}|s.tdƒ‚||7}q|S)z› Receive EXACTLY the number of bytes requested from the file object. Blocks until the required number of bytes have been received. r`úConnection 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-ri)rr0r1r2r3r4r5rrrrCsþzsocksocket.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ó)ú0.0.0.0rN)rirlr8rfrTrDrhrFrr rrSZ getsocknameÚ _proxy_addrrEÚ_SOCKS5_requestrj)rrZr[r0rIrJr3r4r5rÚ_r2ÚdstriZ UDP_ASSOCIATEZrelayÚhostrrrrD*s(    zsocksocket.bindc Osš|jtjkr tj||f|ž|ŽS|js0| d¡|d}|dd…}tƒ}d}| |¡d}| |¡|  ||¡tj ||  ¡|f|ž|Ž} | |  ¡S)N©Úréÿÿÿÿsó) rlr8rfrSrbrhrDrÚwriteÚ_write_SOCKS5_addressrcÚgetvalueÚtell) rÚbytesrmrnZaddressÚflagsÚheaderZRSVZ STANDALONEÚsentrrrrbNs      zsocksocket.sendtocKs6|jtjkr |j|||jf|ŽStj|||f|ŽSdSr)rlr8rfrbrkrSrc)rr‡rˆrnrrrrcas zsocksocket.sendc Cs°|jtjkrt |||¡S|js*| d¡tt |||¡ƒ}|  dt ¡|  d¡}t |ƒrbt dƒ‚| |¡\}}|jr |j\}}||ks”|d|fkr t td¡‚|  ¡||ffS)Nrr r zReceived UDP packet fragmentrzPacket filtered)rlr8rfrSrdrhrDrreÚseekrrqÚordÚNotImplementedErrorÚ_read_SOCKS5_addressrkrFr) rÚbufsizerˆÚbufZfragZfromhostZfromportZpeerhostZpeerportrrrrdgs      zsocksocket.recvfromcOs|j||Ž\}}|Sr)rd)rrZr[r‡r|rrrre{szsocksocket.recvcCs|jr|j ¡t |¡Sr)rhrGrSrrrrrGs zsocksocket.closecCs|jS)zL Returns the bound IP address and port number at the proxy. )rjrrrrÚget_proxy_sockname„szsocksocket.get_proxy_socknamecCs t |¡S)z> Returns the IP and port number of the proxy. )rSÚ getpeernamerrrrÚ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) )rkrrrrÚ get_peername”szsocksocket.get_peernamecGsd}| |||¡\|_|_dS)zI Negotiates a stream connection through a SOCKS5 server. óN)r{rkrj)rÚ dest_addrZCONNECTrrrÚ_negotiate_SOCKS5s ÿzsocksocket._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 ||¡ƒ‚| | ¡}||fW¢S|  ¡|  ¡XdS)z‘ Send SOCKS5 request with given command (CMD field) and address (DST field). Returns resolved DST address that was used. ÚwbÚrbrssr r óú%SOCKS5 proxy server sent invalid dataór•r‚zSOCKS5 authentication failedóÿz7All offered SOCKS5 authentication methods were rejectedr ú Unknown errorú {0:#04x}: {1}N)riÚmakefilerGrƒÚflushrvrÚchrrpr-r!r„rŒÚ SOCKS5_ERRORSÚgetr"rrŽ)rZconnÚcmdr}r0r1r2r3r4r5ÚwriterÚreaderZ chosen_authZ auth_statusZresolvedÚrespÚstatusrFZbndrrrr{¥sV     ÿþý         zsocksocket._SOCKS5_requestc CsL|\}}|j\}}}}}} tjdtjdi} tjtjfD]j} zJt | |¡} | | | | ¡t | | ¡}| t d|¡¡||fWStj k ržYq6Yq6Xq6|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•óú>HÚidnarxrr()rir8ÚAF_INETÚAF_INET6Z inet_ptonrƒÚ inet_ntopÚstructÚpackrFr-r¢rpr>Z AF_UNSPECr?Z IPPROTO_TCPZ AI_ADDRCONFIG)rr1rrr~r2r0r|r3r4r5Zfamily_to_byterNÚ 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 r•r(rxrªér›r«r r) rvr8Ú inet_ntoarŒr¯r®rr°Úunpack)rrrZatypr1Úlengthr2rrrrŽ!s  zsocksocket._read_SOCKS5_addresscCsŽ|j\}}}}}}| d¡} | dd¡} zLd} zt |¡} 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|_W5|  ¡|  ¡XdS)zB Negotiates a connection through a SOCKS4 server. r˜r™rFsTz>BBHr(r r‚r¬r,z%SOCKS4 proxy server sent invalid datar éZržrŸNr«)rir rGr8Z inet_atonrFÚ gethostbynamerƒr°r±r-r¡rvrrŒÚ SOCKS4_ERRORSr¤r#rr´rµrjrk)rr–Ú dest_portr0r1r2r3r4r5r¦r§Zremote_resolver²r¨r©rFrrrÚ_negotiate_SOCKS40sB        ,zsocksocket._negotiate_SOCKS4cCsj|j\}}}}}}|r|nt |¡}d| d¡dt|ƒ ¡dd| d¡g} |rv|rv|  dt|d|ƒ¡|  d¡| d | ¡¡|  ¡} |   ¡} |   ¡| s´t dƒ‚z|   d d ¡\} } }Wntk ræt d ƒ‚YnX|  d ¡sút d ƒ‚z 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 roú 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)rir8r¸r-ÚstrÚappendrZsendallÚjoinr ÚreadlinerGrÚsplitrgr<rAr$rrjrk)rr–rºr0r1r2r3r4r5Z http_headersZfobjZ status_linerOZ status_codeZ status_msgrFrrrÚ_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| ¡}zt ||¡Wn`tjk rP} z>| ¡|\}}d  ||¡} t|} d  | | ¡} t| | ƒ‚W5d} ~ XYntXz|j|}||||ƒWnVtjk r¤} z| ¡td | ƒ‚W5d} ~ 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 rr;zPySocks doesn't support IPv6rryNz0Invalid destination-connection (host, port) pairz{0}:{1}z!Error connecting to {0} proxy {1}z Socket error)rpr<r8rFrlrfrhrDr¸rkrir@r]ÚtuplerArrSrErzrGrÚPRINTABLE_PROXY_TYPESr Ú_proxy_negotiatorsr)rrHr–rºr0rIrJr3r4r5rFZ proxy_serverZprintable_typerZ negotiaterrrrE¨sZ       ÿþý ÿ zsocksocket.connectcCs4|j\}}}}}}|pt |¡}|s,tdƒ‚||fS)zD Return proxy address to connect to as tuple object zInvalid proxy type)riÚ DEFAULT_PORTSr¤r)rr0rIrJr3r4r5rrrrzõs zsocksocket._proxy_addr)NNNTNN)r)r)$rrrrr/r8r­r?rrvrCZsetproxyrDrbrcrdrerGr‘Zgetproxysocknamer“Zgetproxypeernamer”r’r—r{r„rŽr»rÄr r rrÇrErzrrrrr.ësB $  R*;7ýMr.)NNNTNN) NNNTNNNNN):rÚ __version__r8r°ÚerrnorrrÚiorÚosrÚ collectionsrÚbase64rZPROXY_TYPE_SOCKS4r ZPROXY_TYPE_SOCKS5r ZPROXY_TYPE_HTTPrZ PROXY_TYPESrUÚzipÚvaluesÚkeysrÆZ _orgsocketrTÚIOErrorrrr r!r"r#r$r¹r£rÈr6Zsetdefaultproxyr7Zgetdefaultproxyr:Z wrapmodulerRrSrar\rXÚmethodr@rWrÀÚsetattrr.rrrrÚs~6      þù þ  ü ;