-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathutils.py
125 lines (99 loc) · 4.87 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import contextlib
import os
import json
import time
import logging
import requests
from tqdm import tqdm
from yaspin import yaspin
from concurrent.futures import ThreadPoolExecutor
def load_proxy_sources(file_path):
with open(file_path, 'r') as file:
return json.load(file)
def proxy_sources():
return load_proxy_sources('proxy_sources.json')
def http_check(PROXY_LIST_FILE):
# combine two methods into one, so that when it's called it'll differentiate based upon what type of proxy parameter is fed into it
TEST_URL = "http://httpbin.org/ip" # https://httpbin.org/anything # https://api.myip.com/ #FIXME add in time to reach judge sites in milliseconds
TIMEOUT = 5
logging.basicConfig(filename='output/error.log', level=logging.ERROR)
def test_proxy(proxy):
with contextlib.suppress(Exception):
response = requests.get(TEST_URL, proxies={'http': proxy}, timeout=TIMEOUT)
if 100 <= response.status_code < 400:
return proxy
return None
def main():
start_time = time.time()
http_proxies = []
with open(PROXY_LIST_FILE, 'r') as f:
http_proxies = [line.strip() for line in f.readlines()]
cpu_count = os.cpu_count()
max_threads = cpu_count * 420 or 1000
print(f"[*] Utilizing {max_threads:,} threads, calculated from your device's {cpu_count} CPU cores.")
with yaspin().bouncingBar as sp:
sp.text = f"Initializing threads for {PROXY_LIST_FILE}..."
with ThreadPoolExecutor(max_workers=max_threads) as executor:
results = []
progress_started = False
for result in tqdm(executor.map(test_proxy, http_proxies), total=len(http_proxies), desc="[*] Checking HTTP Proxies", ascii=" #", unit= " prox"):
if not progress_started:
sp.text = "Threads initialized."
sp.ok("[_OK_]")
sp.stop()
progress_started = True
results.append(result)
http_valid_proxies = [proxy for proxy in results if proxy is not None]
end_time = time.time()
total_time = end_time - start_time
total_minutes = total_time // 60
remaining_seconds = total_time % 60
print("[*] Total time for Execution:", f"{int(total_minutes)} minute(s) and {round(remaining_seconds, 2)} seconds")
print(f"[*] Valid HTTP Proxies: {len(http_valid_proxies):,}\n")
with open(PROXY_LIST_FILE, 'w') as f:
for proxy in http_valid_proxies:
f.write(proxy + '\n')
main()
def https_check(PROXY_LIST_FILE):
# FIXME tell the user the anonymity level of the proxy
# FIXME add in socks4/5 verification
TEST_URL = "https://api.myip.com:443/"
TIMEOUT = 5
logging.basicConfig(filename='error.log', level=logging.ERROR)
def test_proxy(proxy):
with contextlib.suppress(Exception):
response = requests.get(TEST_URL, proxies={'https': proxy}, timeout=TIMEOUT)
if 100 <= response.status_code < 400:
return proxy
return None
def main():
start_time = time.time()
https_proxies = []
with open(PROXY_LIST_FILE, 'r') as f:
https_proxies = [line.strip() for line in f.readlines()]
cpu_count = os.cpu_count()
max_threads = cpu_count * 250 or 1000
print(f"[*] Utilizing {max_threads:,} threads, calculated from your device's {cpu_count} CPU cores.")
with yaspin().bouncingBar as sp:
sp.text = f"Initializing threads for {PROXY_LIST_FILE}..."
with ThreadPoolExecutor(max_workers=max_threads) as executor:
results = []
progress_started = False
for result in tqdm(executor.map(test_proxy, https_proxies), total=len(https_proxies), desc="[*] Checking HTTPS Proxies", ascii=" #", unit= " prox"):
if not progress_started:
sp.text = "Threads initialized."
sp.ok("[_OK_]")
sp.stop()
progress_started = True
results.append(result)
https_valid_proxies = [proxy for proxy in results if proxy is not None]
end_time = time.time()
total_time = end_time - start_time
total_minutes = total_time // 60
remaining_seconds = total_time % 60
print("[*] Total time for Execution:", f"{int(total_minutes)} minute(s) and {round(remaining_seconds, 2)} seconds")
print(f"[*] Valid HTTPS Proxies: {len(https_valid_proxies):,}")
with open(PROXY_LIST_FILE, 'w') as f:
for proxy in https_valid_proxies:
f.write(proxy + '\n')
main()