proxytools

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

cleansocks.py (4595B)

      1 #!/usr/bin/env python
      2 # CleanSocks - Developed by acidvegas in Python (https://git.acid.vegas/proxytools)
      3 
      4 # This script will clean a list of proxies by removing duplicates, checking for valid formats (IP:PORT), & testing if the proxies are working
      5 # If a proxy is found working on multiple ports, we will only store the first working port to avoid ip duplication in the clean results.
      6 
      7 import argparse
      8 import asyncio
      9 import os
     10 import re
     11 
     12 try:
     13 	import aiosocks
     14 except ImportError:
     15 	raise SystemExit('missing pysocks module (https://pypi.org/project/aiosocks/)')
     16 
     17 try:
     18 	import aiohttp
     19 except ImportError:
     20 	raise SystemExit('missing pysocks module (https://pypi.org/project/aiosocks/)')
     21 
     22 # Globals
     23 all  = list()
     24 good = list()
     25 
     26 async def check_http_proxy(semaphore: asyncio.Semaphore, proxy: str):
     27 	'''
     28 	Checks if a HTTP proxy is working.
     29 	
     30 	:param semaphore: The semaphore to use.
     31 	:param proxy: The proxy to check.
     32 	'''
     33 	async with semaphore:
     34 		proxy_ip = proxy.split(':')[0]
     35 		async with aiohttp.ClientSession() as session:
     36 			try:
     37 				async with session.get('https://google.com', proxy=f'http://{proxy}', timeout=args.timeout) as response:
     38 					if response.status == 200:
     39 						print('\033[1;32mGOOD\033[0m \033[30m|\033[0m ' + proxy)
     40 						if proxy_ip not in all:
     41 							all.append(proxy_ip)
     42 							good.append(proxy)
     43 					else:
     44 						if args.bad:
     45 							print('\033[1;31mBAD\033[0m  \033[30m|\033[0m ' + proxy)
     46 			except:
     47 				if args.bad:
     48 					print('\033[1;31mBAD\033[0m  \033[30m|\033[0m ' + proxy)
     49 
     50 async def check_socks_proxy(semaphore: asyncio.Semaphore, proxy: str):
     51 	'''
     52 	Checks if a SOCKS proxy is working.
     53 	
     54 	:param semaphore: The semaphore to use.
     55 	:param proxy: The proxy to check.
     56 	'''
     57 	async with semaphore:
     58 		proxy_ip, proxy_port = proxy.split(':')
     59 		options = {
     60 			'proxy'      : aiosocks.Socks5Addr(proxy_ip, proxy_port),
     61 			'proxy_auth' : None,
     62 			'dst'        : ('www.google.com', 80),
     63 			'limit'      : 1024,
     64 			'ssl'        : None,
     65 			'family'     : 2
     66 		}
     67 		try:
     68 			await asyncio.wait_for(aiosocks.open_connection(**options), args.timeout)
     69 		except:
     70 			if args.bad:
     71 				print('\033[1;31mBAD\033[0m  \033[30m|\033[0m ' + proxy)
     72 		else:
     73 			print('\033[1;32mGOOD\033[0m \033[30m|\033[0m ' + proxy)
     74 			if proxy_ip not in all:
     75 				all.append(proxy_ip)
     76 				good.append(proxy)
     77 
     78 async def main(targets):
     79 	'''
     80 	Starts the main event loop.
     81 	
     82 	:param targets: The proxies to check.
     83 	'''
     84 	sema = asyncio.BoundedSemaphore(args.threads)
     85 	jobs = list()
     86 	for target in targets:
     87 		if args.socks:
     88 			jobs.append(asyncio.ensure_future(check_socks_proxy(sema, target)))
     89 		else:
     90 			jobs.append(asyncio.ensure_future(check_http_proxy(sema, target)))
     91 	await asyncio.gather(*jobs)
     92 
     93 
     94 if __name__ == '__main__':
     95 	print('#'*56)
     96 	print('#{0}#'.format(''.center(54)))
     97 	print('#{0}#'.format('CleanSOCKS Proxy Cleaner'.center(54)))
     98 	print('#{0}#'.format('Developed by acidvegas in Python'.center(54)))
     99 	print('#{0}#'.format('https://git.acid.vegas/proxytools'.center(54)))
    100 	print('#{0}#'.format(''.center(54)))
    101 	print('#'*56)
    102 
    103 	parser = argparse.ArgumentParser(usage='%(prog)s <input> <output> [options]')
    104 	parser.add_argument('input', help='file to scan')
    105 	parser.add_argument('output', help='file to output')
    106 	parser.add_argument('-s', '--socks', action='store_true', help='Check SOCKS proxies (default: HTTP)')
    107 	parser.add_argument('-b', '--bad', action='store_true', help='Show bad proxies')
    108 	parser.add_argument('-t', '--threads', help='number of threads      (default: 100)', default=100, type=int)
    109 	parser.add_argument('-x', '--timeout', help='socket timeout seconds (default:  15)', default=15, type=int)
    110 	args = parser.parse_args()
    111 
    112 	if not os.path.isfile(args.input):
    113 		raise SystemExit('no such input file')
    114 
    115 	initial = len(open(args.input).readlines())
    116 	proxies = set([proxy for proxy in re.findall(r'[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+', open(args.input).read(), re.MULTILINE)])
    117 
    118 	if not proxies:
    119 		raise SystemExit('no proxies found from input file')
    120 
    121 
    122 	print('\033[1;32mChecking {0:,} {1} proxies using {2:,} threads... \033[1;30m(Pass the -h or --help argument to change these settings)\033[0m'.format(len(proxies), 'SOCKS' if args.socks else 'HTTP', args.threads))
    123 	asyncio.run(main(proxies))
    124 
    125 	good.sort()
    126 
    127 	with open(args.output, 'w') as output_file:
    128 		output_file.write('\n'.join(good))
    129 	
    130 	print('\033[34mTotal\033[0m : ' + format(len(proxies),           ',d'))
    131 	print('\033[34mGood\033[0m  : ' + format(len(good),              ',d'))
    132 	print('\033[34mBad\033[0m   : ' + format(len(proxies)-len(good), ',d'))
    133 	print('\033[34mDupe\033[0m  : ' + format(initial-len(proxies),   ',d'))