a G2]~ @s8dZddlZddlZzddlZddlZWn*eyRddlmZddlm ZYn0ddl Z ddZ ddZ e ZGdddejZGd d d ejZGd d d ejejZed kr4ddlZzeejdZWneefydZYn0eee jdeZe de!d"#e de!d"#dS)z SocksiPy + urllib2 handler version: 0.3 author: e This module provides a Handler which you can use with urllib2 to allow it to tunnel your connection through a socks.sockssocket socket, with out monkey patching the original socket... NcCs|}|||SN)copyupdate)abdr0/usr/lib/python3.9/site-packages/sockshandler.py merge_dicts r cCsNz6d|vrttj|nd|vr.t|nWdSWnYdS0dSdS)N:.FT)socketZ inet_ptonZAF_INET6Z inet_aton)srrr is_ips  rc@seZdZdddZddZdS)SocksiPyConnectionNTc Os0||||||f|_tjj|g|Ri|dSr) proxyargshttplibHTTPConnection__init__ self proxytype proxyaddr proxyportrdnsusernamepasswordargskwargsrrr r+szSocksiPyConnection.__init__c Cs|j\}}}}}}|o|tv}z:t|j|jf|jd||||||tjtj dff }WqWqtj y}z8|rdt |vrt |jsd}t |nWYd}~qd}~00q||_dS)NZ0x5bF)rsocks4_no_rdnssocksZcreate_connectionhostporttimeoutr Z IPPROTO_TCPZ TCP_NODELAYZ SOCKS4Errorstrraddsock) rrrrrrrr'errr connect/s   zSocksiPyConnection.connect)NTNN__name__ __module__ __qualname__rr)rrrr r*s rc@seZdZdddZddZdS)SocksiPyConnectionSNTc Os0||||||f|_tjj|g|Ri|dSr)rrHTTPSConnectionrrrrr rDszSocksiPyConnectionS.__init__cCsxt||jj|j|jd|_|jjst|jrtzt |j |jWn,t yr|j t j|jYn0dS)N)Zserver_hostname)rr)Z_contextZ wrap_socketr'r"Zcheck_hostnameZ_check_hostnamesslZmatch_hostnameZ getpeercert ExceptionZshutdownr Z SHUT_RDWRcloserrrr r)Hs   zSocksiPyConnectionS.connect)NTNNr*rrrr r.Cs r.c@s$eZdZddZddZddZdS)SocksiPyHandlercOs||_||_tj|dSr)rkwurllib2 HTTPHandlerr)rrrrrr rTszSocksiPyHandler.__init__csdfdd }||S)Nrcs(tj|}tj|||d|}|SN)r"r#r$)r r5rrr"r#r$rr5Zconnr3rr buildZs z(SocksiPyHandler.http_open..build)NrZdo_openrZreqr:rr3r http_openYszSocksiPyHandler.http_opencsdfdd }||S)Nrcs(tj|}tj|||d|}|Sr8)r r5r.rr9r3rr r:as z)SocksiPyHandler.https_open..build)Nrr;r<rr3r https_open`szSocksiPyHandler.https_openN)r+r,r-rr=r>rrrr r4Ssr4__main__riZ#Z localhostzHTTP: zhttp://httpbin.org/ipzHTTPS: zhttps://httpbin.org/ip)$__doc__r r0r6r ImportErrorZurllib.requestZrequestZ http.clientZclientr!r rsetr rrr/r.r7Z HTTPSHandlerr4r+sysintargvr# ValueError IndexErrorZ build_openerZPROXY_TYPE_SOCKS5openerprintopenreaddecoderrrr s0