<s> """<STR_LIT>""" <EOL> """<STR_LIT>""" <EOL> import socket <EOL> import struct <EOL> import sys <EOL> PROXY_TYPE_SOCKS4 = <NUM_LIT:1> <EOL> PROXY_TYPE_SOCKS5 = <NUM_LIT:2> <EOL> PROXY_TYPE_HTTP = <NUM_LIT:3> <EOL> _defaultproxy = None <EOL> _orgsocket = socket . socket <EOL> class ProxyError ( Exception ) : pass <EOL> class GeneralProxyError ( ProxyError ) : pass <EOL> class Socks5AuthError ( ProxyError ) : pass <EOL> class Socks5Error ( ProxyError ) : pass <EOL> class Socks4Error ( ProxyError ) : pass <EOL> class HTTPError ( ProxyError ) : pass <EOL> _generalerrors = ( "<STR_LIT:success>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> _socks5errors = ( "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> _socks5autherrors = ( "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> _socks4errors = ( "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> def setdefaultproxy ( proxytype = None , addr = None , port = None , rdns = True , username = None , password = None ) : <EOL> """<STR_LIT>""" <EOL> global _defaultproxy <EOL> _defaultproxy = ( proxytype , addr , port , rdns , username , password ) <EOL> def wrapmodule ( module ) : <EOL> """<STR_LIT>""" <EOL> if _defaultproxy != None : <EOL> module . socket . socket = socksocket <EOL> else : <EOL> raise GeneralProxyError ( ( <NUM_LIT:4> , "<STR_LIT>" ) ) <EOL> class socksocket ( socket . socket ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , family = socket . AF_INET , type = socket . SOCK_STREAM , proto = <NUM_LIT:0> , _sock = None ) : <EOL> _orgsocket . __init__ ( self , family , type , proto , _sock ) <EOL> if _defaultproxy != None : <EOL> self . __proxy = _defaultproxy <EOL> else : <EOL> self . __proxy = ( None , None , None , None , None , None ) <EOL> self . __proxysockname = None <EOL> self . __proxypeername = None <EOL> def __recvall ( self , count ) : <EOL> """<STR_LIT>""" <EOL> data = self . recv ( count ) <EOL> while len ( data ) < count : <EOL> d = self . recv ( count - len ( data ) ) <EOL> if not d : raise GeneralProxyError ( ( <NUM_LIT:0> , "<STR_LIT>" ) ) <EOL> data = data + d <EOL> return data <EOL> def setproxy ( self , proxytype = None , addr = None , port = None , rdns = True , username = None , password = None ) : <EOL> """<STR_LIT>""" <EOL> self . __proxy = ( proxytype , addr , port , rdns , username , password ) <EOL> def __negotiatesocks5 ( self , destaddr , destport ) : <EOL> """<STR_LIT>""" <EOL> if ( self . __proxy [ <NUM_LIT:4> ] != None ) and ( self . __proxy [ <NUM_LIT:5> ] != None ) : <EOL> self . sendall ( struct . pack ( '<STR_LIT>' , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) ) <EOL> else : <EOL> self . sendall ( struct . pack ( '<STR_LIT>' , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) ) <EOL> chosenauth = self . __recvall ( <NUM_LIT:2> ) <EOL> if chosenauth [ <NUM_LIT:0> : <NUM_LIT:1> ] != chr ( <NUM_LIT> ) . encode ( ) : <EOL> self . close ( ) <EOL> raise GeneralProxyError ( ( <NUM_LIT:1> , _generalerrors [ <NUM_LIT:1> ] ) ) <EOL> if chosenauth [ <NUM_LIT:1> : <NUM_LIT:2> ] == chr ( <NUM_LIT> ) . encode ( ) : <EOL> pass <EOL> elif chosenauth [ <NUM_LIT:1> : <NUM_LIT:2> ] == chr ( <NUM_LIT> ) . encode ( ) : <EOL> self . sendall ( chr ( <NUM_LIT> ) . encode ( ) + chr ( len ( self . __proxy [ <NUM_LIT:4> ] ) ) + self . __proxy [ <NUM_LIT:4> ] + chr ( len ( self . __proxy [ <NUM_LIT:5> ] ) ) + self . __proxy [ <NUM_LIT:5> ] ) <EOL> authstat = self . __recvall ( <NUM_LIT:2> ) <EOL> if authstat [ <NUM_LIT:0> : <NUM_LIT:1> ] != chr ( <NUM_LIT> ) . encode ( ) : <EOL> self . close ( ) <EOL> raise GeneralProxyError ( ( <NUM_LIT:1> , _generalerrors [ <NUM_LIT:1> ] ) ) <EOL> if authstat [ <NUM_LIT:1> : <NUM_LIT:2> ] != chr ( <NUM_LIT> ) . encode ( ) : <EOL> self . close ( ) <EOL> raise Socks5AuthError ( ( <NUM_LIT:3> , _socks5autherrors [ <NUM_LIT:3> ] ) ) <EOL> else : <EOL> self . close ( ) <EOL> if chosenauth [ <NUM_LIT:1> ] == chr ( <NUM_LIT> ) . encode ( ) : <EOL> raise Socks5AuthError ( ( <NUM_LIT:2> , _socks5autherrors [ <NUM_LIT:2> ] ) ) <EOL> else : <EOL> raise GeneralProxyError ( ( <NUM_LIT:1> , _generalerrors [ <NUM_LIT:1> ] ) ) <EOL> req = struct . pack ( '<STR_LIT>' , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) <EOL> try : <EOL> ipaddr = socket . inet_aton ( destaddr ) <EOL> req = req + chr ( <NUM_LIT> ) . encode ( ) + ipaddr <EOL> except socket . error : <EOL> if self . __proxy [ <NUM_LIT:3> ] : <EOL> ipaddr = None <EOL> req = req + chr ( <NUM_LIT> ) . encode ( ) + chr ( len ( destaddr ) ) . encode ( ) + destaddr <EOL> else : <EOL> ipaddr = socket . inet_aton ( socket . gethostbyname ( destaddr ) ) <EOL> req = req + chr ( <NUM_LIT> ) . encode ( ) + ipaddr <EOL> req = req + struct . pack ( "<STR_LIT>" , destport ) <EOL> self . sendall ( req ) <EOL> resp = self . __recvall ( <NUM_LIT:4> ) <EOL> if resp [ <NUM_LIT:0> : <NUM_LIT:1> ] != chr ( <NUM_LIT> ) . encode ( ) : <EOL> self . close ( ) <EOL> raise GeneralProxyError ( ( <NUM_LIT:1> , _generalerrors [ <NUM_LIT:1> ] ) ) <EOL> elif resp [ <NUM_LIT:1> : <NUM_LIT:2> ] != chr ( <NUM_LIT> ) . encode ( ) : <EOL> self . close ( ) <EOL> if ord ( resp [ <NUM_LIT:1> : <NUM_LIT:2> ] ) <= <NUM_LIT:8> : <EOL> raise Socks5Error ( ( ord ( resp [ <NUM_LIT:1> : <NUM_LIT:2> ] ) , _socks5errors [ ord ( resp [ <NUM_LIT:1> : <NUM_LIT:2> ] ) ] ) ) <EOL> else : <EOL> raise Socks5Error ( ( <NUM_LIT:9> , _socks5errors [ <NUM_LIT:9> ] ) ) <EOL> elif resp [ <NUM_LIT:3> : <NUM_LIT:4> ] == chr ( <NUM_LIT> ) . encode ( ) : <EOL> boundaddr = self . __recvall ( <NUM_LIT:4> ) <EOL> elif resp [ <NUM_LIT:3> : <NUM_LIT:4> ] == chr ( <NUM_LIT> ) . encode ( ) : <EOL> resp = resp + self . recv ( <NUM_LIT:1> ) <EOL> boundaddr = self . __recvall ( ord ( resp [ <NUM_LIT:4> : <NUM_LIT:5> ] ) ) <EOL> else : <EOL> self . close ( ) <EOL> raise GeneralProxyError ( ( <NUM_LIT:1> , _generalerrors [ <NUM_LIT:1> ] ) ) <EOL> boundport = struct . unpack ( "<STR_LIT>" , self . __recvall ( <NUM_LIT:2> ) ) [ <NUM_LIT:0> ] <EOL> self . __proxysockname = ( boundaddr , boundport ) <EOL> if ipaddr != None : <EOL> self . __proxypeername = ( socket . inet_ntoa ( ipaddr ) , destport ) <EOL> else : <EOL> self . __proxypeername = ( destaddr , destport ) <EOL> def getproxysockname ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __proxysockname <EOL> def getproxypeername ( self ) : <EOL> """<STR_LIT>""" <EOL> return _orgsocket . getpeername ( self ) <EOL> def getpeername ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __proxypeername <EOL> def __negotiatesocks4 ( self , destaddr , destport ) : <EOL> """<STR_LIT>""" <EOL> rmtrslv = False <EOL> try : <EOL> ipaddr = socket . inet_aton ( destaddr ) <EOL> except socket . error : <EOL> if self . __proxy [ <NUM_LIT:3> ] : <EOL> ipaddr = struct . pack ( "<STR_LIT>" , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) <EOL> rmtrslv = True <EOL> else : <EOL> ipaddr = socket . inet_aton ( socket . gethostbyname ( destaddr ) ) <EOL> req = struct . pack ( "<STR_LIT>" , <NUM_LIT> , <NUM_LIT> , destport ) + ipaddr <EOL> if self . __proxy [ <NUM_LIT:4> ] != None : <EOL> req = req + self . __proxy [ <NUM_LIT:4> ] <EOL> req = req + chr ( <NUM_LIT> ) . encode ( ) <EOL> if rmtrslv : <EOL> req = req + destaddr + chr ( <NUM_LIT> ) . encode ( ) <EOL> self . sendall ( req ) <EOL> resp = self . __recvall ( <NUM_LIT:8> ) <EOL> if resp [ <NUM_LIT:0> : <NUM_LIT:1> ] != chr ( <NUM_LIT> ) . encode ( ) : <EOL> self . close ( ) <EOL> raise GeneralProxyError ( ( <NUM_LIT:1> , _generalerrors [ <NUM_LIT:1> ] ) ) <EOL> if resp [ <NUM_LIT:1> : <NUM_LIT:2> ] != chr ( <NUM_LIT> ) . encode ( ) : <EOL> self . close ( ) <EOL> if ord ( resp [ <NUM_LIT:1> : <NUM_LIT:2> ] ) in ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) : <EOL> self . close ( ) <EOL> raise Socks4Error ( ( ord ( resp [ <NUM_LIT:1> : <NUM_LIT:2> ] ) , _socks4errors [ ord ( resp [ <NUM_LIT:1> : <NUM_LIT:2> ] ) - <NUM_LIT> ] ) ) <EOL> else : <EOL> raise Socks4Error ( ( <NUM_LIT> , _socks4errors [ <NUM_LIT:4> ] ) ) <EOL> self . __proxysockname = ( socket . inet_ntoa ( resp [ <NUM_LIT:4> : ] ) , struct . unpack ( "<STR_LIT>" , resp [ <NUM_LIT:2> : <NUM_LIT:4> ] ) [ <NUM_LIT:0> ] ) <EOL> if rmtrslv != None : <EOL> self . __proxypeername = ( socket . inet_ntoa ( ipaddr ) , destport ) <EOL> else : <EOL> self . __proxypeername = ( destaddr , destport ) <EOL> def __negotiatehttp ( self , destaddr , destport ) : <EOL> """<STR_LIT>""" <EOL> if not self . __proxy [ <NUM_LIT:3> ] : <EOL> addr = socket . gethostbyname ( destaddr ) <EOL> else : <EOL> addr = destaddr <EOL> self . sendall ( ( "<STR_LIT>" + addr + "<STR_LIT::>" + str ( destport ) + "<STR_LIT>" + "<STR_LIT>" + destaddr + "<STR_LIT>" ) . encode ( ) ) <EOL> resp = self . recv ( <NUM_LIT:1> ) <EOL> while resp . find ( "<STR_LIT>" . encode ( ) ) == - <NUM_LIT:1> : <EOL> resp = resp + self . recv ( <NUM_LIT:1> ) <EOL> statusline = resp . splitlines ( ) [ <NUM_LIT:0> ] . split ( "<STR_LIT:U+0020>" . encode ( ) , <NUM_LIT:2> ) <EOL> if statusline [ <NUM_LIT:0> ] not in ( "<STR_LIT>" . encode ( ) , "<STR_LIT>" . encode ( ) ) : <EOL> self . close ( ) <EOL> raise GeneralProxyError ( ( <NUM_LIT:1> , _generalerrors [ <NUM_LIT:1> ] ) ) <EOL> try : <EOL> statuscode = int ( statusline [ <NUM_LIT:1> ] ) <EOL> except ValueError : <EOL> self . close ( ) <EOL> raise GeneralProxyError ( ( <NUM_LIT:1> , _generalerrors [ <NUM_LIT:1> ] ) ) <EOL> if statuscode != <NUM_LIT:200> : <EOL> self . close ( ) <EOL> raise HTTPError ( ( statuscode , statusline [ <NUM_LIT:2> ] ) ) <EOL> self . __proxysockname = ( "<STR_LIT>" , <NUM_LIT:0> ) <EOL> self . __proxypeername = ( addr , destport ) <EOL> def connect ( self , destpair ) : <EOL> """<STR_LIT>""" <EOL> if ( not type ( destpair ) in ( list , tuple ) ) or ( len ( destpair ) < <NUM_LIT:2> ) or ( type ( destpair [ <NUM_LIT:0> ] ) != type ( '<STR_LIT>' ) ) or ( type ( destpair [ <NUM_LIT:1> ] ) != int ) : <EOL> raise GeneralProxyError ( ( <NUM_LIT:5> , _generalerrors [ <NUM_LIT:5> ] ) ) <EOL> if self . __proxy [ <NUM_LIT:0> ] == PROXY_TYPE_SOCKS5 : <EOL> if self . __proxy [ <NUM_LIT:2> ] != None : <EOL> portnum = self . __proxy [ <NUM_LIT:2> ] <EOL> else : <EOL> portnum = <NUM_LIT> <EOL> _orgsocket . connect ( self , ( self . __proxy [ <NUM_LIT:1> ] , portnum ) ) <EOL> self . __negotiatesocks5 ( destpair [ <NUM_LIT:0> ] , destpair [ <NUM_LIT:1> ] ) <EOL> elif self . __proxy [ <NUM_LIT:0> ] == PROXY_TYPE_SOCKS4 : <EOL> if self . __proxy [ <NUM_LIT:2> ] != None : <EOL> portnum = self . __proxy [ <NUM_LIT:2> ] <EOL> else : <EOL> portnum = <NUM_LIT> <EOL> _orgsocket . connect ( self , ( self . __proxy [ <NUM_LIT:1> ] , portnum ) ) <EOL> self . __negotiatesocks4 ( destpair [ <NUM_LIT:0> ] , destpair [ <NUM_LIT:1> ] ) <EOL> elif self . __proxy [ <NUM_LIT:0> ] == PROXY_TYPE_HTTP : <EOL> if self . __proxy [ <NUM_LIT:2> ] != None : <EOL> portnum = self . __proxy [ <NUM_LIT:2> ] <EOL> else : <EOL> portnum = <NUM_LIT> <EOL> _orgsocket . connect ( self , ( self . __proxy [ <NUM_LIT:1> ] , portnum ) ) <EOL> self . __negotiatehttp ( destpair [ <NUM_LIT:0> ] , destpair [ <NUM_LIT:1> ] ) <EOL> elif self . __proxy [ <NUM_LIT:0> ] == None : <EOL> _orgsocket . connect ( self , ( destpair [ <NUM_LIT:0> ] , destpair [ <NUM_LIT:1> ] ) ) <EOL> else : <EOL> raise GeneralProxyError ( ( <NUM_LIT:4> , _generalerrors [ <NUM_LIT:4> ] ) ) </s>
