proxytools/cleansocks.py

68 lines
2.5 KiB
Python
Raw Normal View History

2019-06-27 22:40:22 -07:00
#!/usr/bin/env python
2023-05-18 19:21:10 -07:00
# CleanSocks - Developed by acidvegas in Python (https://git.acid.vegas/proxytools)
2019-06-27 22:40:22 -07:00
'''
2023-06-09 21:58:03 -07:00
This script will clean a list of proxies by removing duplicates, checking for valid formats (IP:PORT), & testing if the proxies are working
2019-06-27 22:40:22 -07:00
'''
import argparse
import concurrent.futures
import os
import re
2023-06-09 21:58:03 -07:00
# Globals
good = list()
print_bad = True
2019-06-27 22:40:22 -07:00
2023-06-09 21:58:03 -07:00
def check(proxy):
2019-06-27 22:40:22 -07:00
ip, port = proxy.split(':')
try:
sock = socks.socksocket()
sock.set_proxy(socks.SOCKS5, ip, int(port))
sock.settimeout(args.timeout)
2023-06-09 21:58:03 -07:00
sock.connect('www.google.com', 80)) # NOTE: use your own host instead in-case connections are blocked
2019-06-27 22:40:22 -07:00
except:
2023-06-09 21:58:03 -07:00
if print_bad:
print('\033[1;31mBAD\033[0m \033[30m|\033[0m ' + proxy)
2019-06-27 22:40:22 -07:00
else:
2023-06-09 21:58:03 -07:00
print('\033[1;32mGOOD\033[0m \033[30m|\033[0m ' + proxy)
2019-06-27 22:40:22 -07:00
good.append(proxy)
2023-06-09 21:58:03 -07:00
# Main
print('#'*56)
print('#{0}#'.format(''.center(54)))
print('#{0}#'.format('CleanSOCKS Proxy Cleaner'.center(54)))
print('#{0}#'.format('Developed by acidvegas in Python'.center(54)))
print('#{0}#'.format('https://git.acid.vegas/proxytools'.center(54)))
print('#{0}#'.format(''.center(54)))
print('#'*56)
2019-06-27 22:40:22 -07:00
parser = argparse.ArgumentParser(usage='%(prog)s <input> <output> [options]')
parser.add_argument('input', help='file to scan')
parser.add_argument('output', help='file to output')
parser.add_argument('-t', '--threads', help='number of threads (default: 100)', default=100, type=int)
parser.add_argument('-x', '--timeout', help='socket timeout seconds (default: 15)', default=15, type=int)
args = parser.parse_args()
try:
import socks
except ImportError:
raise SystemExit('missing pysocks module (https://pypi.python.org/pypi/pysocks)')
if not os.path.isfile(args.input):
raise SystemExit('no such input file')
2023-06-09 21:58:03 -07:00
initial = len(open(args.input).readlines())
proxies = set([proxy for proxy in re.findall('[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+', open(args.input).read(), re.MULTILINE)])
2019-06-27 22:40:22 -07:00
if not proxies:
raise SystemExit('no proxies found from input file')
with concurrent.futures.ThreadPoolExecutor(max_workers=args.threads) as executor:
2023-06-09 21:58:03 -07:00
checks = {executor.submit(check, proxy): proxy for proxy in proxies}
2019-06-27 22:40:22 -07:00
for future in concurrent.futures.as_completed(checks):
checks[future]
good.sort()
with open(args.output, 'w') as output_file:
output_file.write('\n'.join(good))
2023-06-09 21:58:03 -07:00
print('\033[34mTotal\033[0m : ' + format(len(proxies), ',d'))
print('\033[34mGood\033[0m : ' + format(len(good), ',d'))
print('\033[34mBad\033[0m : ' + format(len(proxies)-len(good), ',d'))
print('\033[34mDupe\033[0m : ' + format(initial-len(proxies), ',d'))