<s> """<STR_LIT>""" <EOL> import socket <EOL> from . config import conf <EOL> class AS_resolver : <EOL> server = None <EOL> options = "<STR_LIT>" <EOL> def __init__ ( self , server = None , port = <NUM_LIT> , options = None ) : <EOL> if server is not None : <EOL> self . server = server <EOL> self . port = port <EOL> if options is not None : <EOL> self . options = options <EOL> def _start ( self ) : <EOL> self . s = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> self . s . connect ( ( self . server , self . port ) ) <EOL> if self . options : <EOL> self . s . send ( self . options + b"<STR_LIT:\n>" ) <EOL> self . s . recv ( <NUM_LIT> ) <EOL> def _stop ( self ) : <EOL> self . s . close ( ) <EOL> def _parse_whois ( self , txt ) : <EOL> asn , desc = None , b"<STR_LIT>" <EOL> for l in txt . splitlines ( ) : <EOL> if not asn and l . startswith ( b"<STR_LIT>" ) : <EOL> asn = l [ <NUM_LIT:7> : ] . strip ( ) . decode ( '<STR_LIT:utf-8>' ) <EOL> if l . startswith ( b"<STR_LIT>" ) : <EOL> if desc : <EOL> desc += br"<STR_LIT:\n>" <EOL> desc += l [ <NUM_LIT:6> : ] . strip ( ) <EOL> if asn is not None and desc . strip ( ) : <EOL> desc = desc . strip ( ) . decode ( '<STR_LIT:utf-8>' ) <EOL> break <EOL> return asn , desc <EOL> def _resolve_one ( self , ip ) : <EOL> self . s . send ( b"<STR_LIT>" . join ( [ ip . encode ( '<STR_LIT:ascii>' ) ] ) + b"<STR_LIT:\n>" ) <EOL> x = b"<STR_LIT>" <EOL> while not ( b"<STR_LIT:%>" in x or b"<STR_LIT:source>" in x ) : <EOL> x += self . s . recv ( <NUM_LIT> ) <EOL> asn , desc = self . _parse_whois ( x ) <EOL> return ip , asn , desc <EOL> def resolve ( self , * ips ) : <EOL> self . _start ( ) <EOL> ret = [ ] <EOL> for ip in ips : <EOL> ip , asn , desc = self . _resolve_one ( ip ) <EOL> if asn is not None : <EOL> ret . append ( ( ip , asn , desc ) ) <EOL> self . _stop ( ) <EOL> return ret <EOL> class AS_resolver_riswhois ( AS_resolver ) : <EOL> server = "<STR_LIT>" <EOL> options = b"<STR_LIT>" <EOL> class AS_resolver_radb ( AS_resolver ) : <EOL> server = "<STR_LIT>" <EOL> options = b"<STR_LIT>" <EOL> class AS_resolver_cymru ( AS_resolver ) : <EOL> server = "<STR_LIT>" <EOL> options = None <EOL> def resolve ( self , * ips ) : <EOL> ASNlist = [ ] <EOL> try : <EOL> s = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> s . connect ( ( self . server , self . port ) ) <EOL> s . send ( b"<STR_LIT>" + b"<STR_LIT:\r\n>" . join ( [ i . encode ( '<STR_LIT:ascii>' ) for i in ips ] ) + b"<STR_LIT>" ) <EOL> r = b"<STR_LIT>" <EOL> while <NUM_LIT:1> : <EOL> l = s . recv ( <NUM_LIT> ) <EOL> if l == b"<STR_LIT>" : <EOL> break <EOL> r += l <EOL> s . close ( ) <EOL> for l in r . splitlines ( ) [ <NUM_LIT:1> : ] : <EOL> if b"<STR_LIT:|>" not in l : <EOL> continue <EOL> asn , ip , desc = [ i . decode ( '<STR_LIT:ascii>' ) for i in map ( bytes . strip , l . split ( b"<STR_LIT:|>" ) ) ] <EOL> if asn == "<STR_LIT>" : <EOL> continue <EOL> asn = int ( asn ) <EOL> ASNlist . append ( ( ip , asn , desc ) ) <EOL> except : <EOL> pass <EOL> return ASNlist <EOL> class AS_resolver_multi ( AS_resolver ) : <EOL> resolvers_list = ( AS_resolver_cymru ( ) , AS_resolver_riswhois ( ) , AS_resolver_radb ( ) ) <EOL> def __init__ ( self , * reslist ) : <EOL> if reslist : <EOL> self . resolvers_list = reslist <EOL> def resolve ( self , * ips ) : <EOL> todo = ips <EOL> ret = [ ] <EOL> for ASres in self . resolvers_list : <EOL> res = ASres . resolve ( * todo ) <EOL> resolved = [ ip for ip , asn , desc in res ] <EOL> todo = [ ip for ip in todo if ip not in resolved ] <EOL> ret += res <EOL> return ret <EOL> conf . AS_resolver = AS_resolver_multi ( ) </s>
