proxytools

- collection of scripts for harvesting & testing proxies
git clone git://git.acid.vegas/proxytools.git
Log | Files | Refs | Archive | README | LICENSE

torglass.py (4162B)

      1 #!/usr/bin/env python
      2 # Tor Glass - Developed by acidvegas in Python (https://git.acid.vegas/proxytools)
      3 
      4 '''
      5 A simple script to pull a list of all the Tor relays / exit nodes & generate a json database.
      6 
      7 The example below will generate a map of all the Tor relays / exit nodes using the ipinfo.io API.
      8 '''
      9 
     10 import datetime
     11 
     12 try:
     13 	import stem.descriptor.remote
     14 except ImportError:
     15 	raise SystemExit('missing required library \'stem\' (https://pypi.org/project/stem/)')
     16 
     17 
     18 def get_descriptors(start_time = None) -> dict:
     19 	'''
     20 	Generate a json database of all Tor relays & exit nodes.
     21 
     22   	:param start_time: (optional) datetime object to start from	
     23 	'''
     24 
     25 	tor_map = { 'relay': [], 'exit': [] }
     26 
     27 	source = stem.descriptor.collector.get_server_descriptors(start = start_time) if start_time else stem.descriptor.remote.get_server_descriptors()
     28 
     29 	for relay in source:
     30 		data = {
     31 			'nickname'                    : relay.nickname,
     32 			'fingerprint'                 : relay.fingerprint,
     33 			'or_addresses'                : relay.or_addresses,
     34 			'published'                   : str(relay.published) if relay.published else None,
     35 			'address'                     : relay.address,
     36 			'or_port'                     : relay.or_port,
     37 			'socks_port'                  : relay.socks_port,
     38 			'dir_port'                    : relay.dir_port,
     39 			'platform'                    : str(relay.platform) if relay.platform else None,
     40 			'tor_version'                 : str(relay.tor_version),
     41 			'operating_system'            : relay.operating_system,
     42 			'uptime'                      : relay.uptime,
     43 			'contact'                     : relay.contact.decode('utf-8') if relay.contact else None,
     44 			'exit_policy'                 : str(relay.exit_policy)    if relay.exit_policy    else None,
     45 			'exit_policy_v6'              : str(relay.exit_policy_v6) if relay.exit_policy_v6 else None,
     46 			'bridge_distribution'         : relay.bridge_distribution,
     47 			'family'                      : list(relay.family) if relay.family else None,
     48 			'average_bandwidth'           : relay.average_bandwidth,
     49 			'burst_bandwidth'             : relay.burst_bandwidth,
     50 			'observed_bandwidth'          : relay.observed_bandwidth,
     51 			'link_protocols'              : relay.link_protocols,
     52 			'circuit_protocols'           : relay.circuit_protocols,
     53 			'is_hidden_service_dir'       : relay.is_hidden_service_dir,
     54 			'hibernating'                 : relay.hibernating,
     55 			'allow_single_hop_exits'      : relay.allow_single_hop_exits,
     56 			'allow_tunneled_dir_requests' : relay.allow_tunneled_dir_requests,
     57 			'extra_info_cache'            : relay.extra_info_cache,
     58 			'extra_info_digest'           : relay.extra_info_digest,
     59 			'extra_info_sha256_digest'    : relay.extra_info_sha256_digest,
     60 			'eventdns'                    : relay.eventdns,
     61 			'ntor_onion_key'              : relay.ntor_onion_key,
     62 			'protocols'                   : relay.protocols
     63 		}
     64   
     65 		if relay.exit_policy.is_exiting_allowed():
     66 			tor_map['exit'].append(data)
     67 		else:
     68 			tor_map['relay'].append(data)
     69 
     70 	return tor_map
     71 
     72 
     73 
     74 if __name__ == '__main__':
     75 	import json
     76 
     77 	print('loading Tor descriptors... (this could take a while)')
     78  
     79 	now = datetime.datetime.now(datetime.timezone.utc)
     80 	way_back = now.replace(year=now.year-1)
     81 
     82 	tor_data = get_descriptors()
     83 	#tor_data = get_descriptors(way_back)
     84 
     85 	with open('tor.json', 'w') as fd:
     86 		json.dump(tor_data['relay'], fd)
     87 	with open('tor.exit.json', 'w') as fd:
     88 		json.dump(tor_data['exit'], fd)
     89 
     90 	print('Relays: {0:,}'.format(len(tor_data['relay'])))
     91 	print('Exits : {0:,}'.format(len(tor_data['exit'])))
     92 
     93 	try:
     94 		import ipinfo
     95 	except ImportError:
     96 		raise ImportError('missing optional library \'ipinfo\' (https://pypi.org/project/ipinfo/) for map visualization')
     97 
     98 	try:
     99 		handler = ipinfo.getHandler('changeme') # put your ipinfo.io API key here
    100 		print('Relay Map: ' + handler.getMap([ip['address'] for ip in tor_data['relay']]))
    101 		print('Exit  Map: ' + handler.getMap([ip['address'] for ip in tor_data['exit']]))
    102 	except ipinfo.errors.AuthorizationError:
    103 		print('error: invalid ipinfo.io API key (https://ipinfo.io/signup)')
    104 	except Exception as ex:
    105 		print(f'error generating ipinfo map ({ex})')