From f04cd03d67f056d7a603f183f5e632b034c7246c Mon Sep 17 00:00:00 2001 From: acidvegas Date: Sat, 10 Jun 2023 00:58:03 -0400 Subject: [PATCH] Cleaned up code a bit --- proxytools/cleansocks.py | 46 ++++++++++++++++------------------------ 1 file changed, 18 insertions(+), 28 deletions(-) diff --git a/proxytools/cleansocks.py b/proxytools/cleansocks.py index 78b18bd..592d626 100644 --- a/proxytools/cleansocks.py +++ b/proxytools/cleansocks.py @@ -2,39 +2,35 @@ # CleanSocks - Developed by acidvegas in Python (https://git.acid.vegas/proxytools) ''' -Requirements: - PySocks (https://pypi.python.org/pypi/pysocks) -This script will clean a list of proxies by removing duplicates, checking for valid formats (IP:PORT), and testing if the proxies are working +This script will clean a list of proxies by removing duplicates, checking for valid formats (IP:PORT), & testing if the proxies are working + ''' import argparse import concurrent.futures import os import re -import sys -sys.dont_write_bytecode = True +# Globals +good = list() +print_bad = True -def is_proxy(proxy): - return re.match('^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]):(?:6553[0-5]|655[0-2][0-9]|65[0-4][0-9]{2}|6[0-4][0-9]{3}|[1-5][0-9]{4}|[1-9][0-9]{1,3}|[0-9])$', proxy) - -def test_proxy(proxy): - global good +def check(proxy): ip, port = proxy.split(':') try: sock = socks.socksocket() sock.set_proxy(socks.SOCKS5, ip, int(port)) sock.settimeout(args.timeout) - sock.connect(('www.google.com', 80)) + sock.connect('www.google.com', 80)) # NOTE: use your own host instead in-case connections are blocked except: - print('BAD | ' + proxy) + if print_bad: + print('\033[1;31mBAD\033[0m \033[30m|\033[0m ' + proxy) else: - print('GOOD | ' + proxy) + print('\033[1;32mGOOD\033[0m \033[30m|\033[0m ' + proxy) good.append(proxy) - finally: - sock.close() +# Main parser = argparse.ArgumentParser(usage='%(prog)s [options]') parser.add_argument('input', help='file to scan') parser.add_argument('output', help='file to output') @@ -47,24 +43,18 @@ 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') -proxies = set([line.strip() for line in open(args.input).readlines() if is_proxy(line)]) +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)]) if not proxies: raise SystemExit('no proxies found from input file') -deduped, ips = list(), list() -for proxy in proxies: - ip = proxy.split(':')[0] - if ip not in ips: - ips.append(ip) - deduped.append(proxy) -deduped.sort() -good = list() with concurrent.futures.ThreadPoolExecutor(max_workers=args.threads) as executor: - checks = {executor.submit(test_proxy, proxy): proxy for proxy in deduped} + checks = {executor.submit(check, proxy): proxy for proxy in proxies} 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)) -print('Total : ' + format(len(proxies), ',d')) -print('Good : ' + format(len(good), ',d')) -print('Bad : ' + format(len(proxies)-len(good), ',d')) \ No newline at end of file +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'))