Cleaned up code a bit

This commit is contained in:
Dionysus 2023-06-10 00:58:03 -04:00
parent 2d3b18336d
commit f04cd03d67
Signed by: acidvegas
GPG Key ID: EF4B922DB85DC9DE

View File

@ -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 <input> <output> [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'))
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'))