This repository has been archived by the owner on Nov 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxychecker.py
194 lines (163 loc) · 7.61 KB
/
proxychecker.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env python
# coding: utf-8
# PROXY Checker v0.1.0
# By Amani Toama [email protected]
from itertools import count
# modules in standard library
import requests
import time
import re
import argparse
from requests.exceptions import ProxyError, ConnectTimeout
from colorama import init, Fore, Style
import sys
import signal
init()
def initialize():
init(autoreset=True)
parser = argparse.ArgumentParser(description='Check Proxies from a file or a single proxy.')
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('-t', '--txt', help='Text file containing proxies, one per line.')
group.add_argument('-s', '--single', help="Single proxy URL (e.g., http://10.20.200.13:80)", type=str)
parser.add_argument('-p', '--protocol',
help="Protocol to use with the single proxy (e.g., socks4), if you don't know the protocol type none",
type=str, default="http")
group.add_argument('-u', '--url', help="Input an url the retrieve txt response containing proxies, one per line",
type=str)
args = parser.parse_args()
return args
# ctrl + c interrupting
def signal_handler(sig, frame):
print(f'\nYou pressed Ctrl+C! Exiting Code 0 \n')
# Perform any cleanup if needed
sys.exit(0)
def print_banner():
print(rf"""
{Fore.LIGHTMAGENTA_EX}
_____ ______ _____ _ _ __ __ _______ _ _ _______ _______ _ _ _______ ______
|_____] |_____/ | | \___/ \_/ | |_____| |______ | |____/ |______ |_____/
| | \_ |_____| _/ \_ | |_____ | | |______ |_____ | \_ |______ | \_
{Style.DIM}{Fore.CYAN}# Proxy Checker Tool by Amani Toama [email protected]
--------------------------------------------------------------------------
{Style.BRIGHT}{Fore.WHITE}
""")
# extract ip func
def extract_ip(address):
ip = address.split(':')[0]
return ip
# country detective func
def get_ip_country(ip):
res = requests.get(f"http://ip-api.com/json/{ip}")
data = res.json()
if res.status_code == 200:
return data['country']
else:
return "error"
def check_proxy(proxy, proxy_type):
proxies = {proxy_type: proxy}
try:
start_time = time.time()
response = requests.get('http://httpbin.org/ip', proxies=proxies, timeout=5)
end_time = time.time()
if response.status_code == 200:
latency = end_time - start_time
return True, latency
except (ProxyError, ConnectTimeout, requests.exceptions.RequestException):
return False, None
return False, None
def identify_proxy_type(proxy):
if re.match(r'^https?://', proxy):
return 'http'
elif re.match(r'^http://', proxy):
return 'http'
elif re.match(r'^socks5://', proxy):
return 'socks5'
elif re.match(r'^socks4://', proxy):
return 'socks4'
else:
return 'http'
print_banner()
def main(proxy_file_path=None,proxy_url=None, single_proxy=None, protocol=None):
signal.signal(signal.SIGINT, signal_handler)
print(f"{Style.RESET_ALL}{Fore.WHITE}Checking Proxy... ")
print(rf"""{Fore.YELLOW}
___________________________________________________________________________________________________________
PROXY | Latency | Status | Country | Protocol
------------------------------------------------------------------------------------------------------------
""")
try:
if proxy_file_path:
with open(proxy_file_path, 'r') as file:
proxies = file.readlines()
for i, proxy in enumerate(proxies):
proxy = proxy.strip()
ip = extract_ip(proxy)
cntry = get_ip_country(ip)
proto = identify_proxy_type(proxy)
if proxy:
status, latency = check_proxy(proxy, identify_proxy_type(proxy))
if status:
print(
f"{Fore.GREEN} {proxy:^32} | {latency:.2f}sec | {'True' if status else 'False':<9} | {cntry:^15} | {proto:^10} ")
else:
print(
f"{Fore.RED} {proxy:^32} | {'---':^17} | {'True' if status else 'False':<9} | {cntry:^15} | {proto:^10} ")
if (i + 1) % 10 == 0: # Every 10 proxies, ask to continue or terminate
if input(f"\n{Fore.BLUE}Press 'q' to quit or any other key to continue: ").lower() == 'q':
print(f"{Fore.YELLOW}Proxy checking terminated by user.")
break
elif single_proxy:
protocol_type = protocol if protocol else identify_proxy_type(single_proxy)
cntry = get_ip_country(extract_ip(single_proxy))
status, latency = check_proxy(single_proxy, protocol_type)
proto = identify_proxy_type(single_proxy)
if status:
print(
f"{Fore.GREEN} {single_proxy:^32} | {latency:.2f}sec | {'True' if status else 'False':<9} | {cntry:^15} | {proto:^10} ")
else:
print(
f"{Fore.RED} {single_proxy:^32} | {'---':^17} | {'True' if status else 'False':<9} | {cntry:^15} | {proto:^10} ")
elif proxy_url:
try:
res = requests.get(proxy_url)
if res.status_code == 200:
data = res.text.splitlines()
proxies = data
for i, proxy in enumerate(proxies):
proxy = proxy.strip()
ip = extract_ip(proxy)
cntry = get_ip_country(ip)
proto = identify_proxy_type(proxy)
if proxy:
status, latency = check_proxy(proxy, identify_proxy_type(proxy))
if status:
print(
f"{Fore.GREEN} {proxy:^32} | {latency:.2f}sec | {'True' if status else 'False':<9} | {cntry:^15} | {proto:^10} ")
else:
print(
f"{Fore.RED} {proxy:^32} | {'---':^17} | {'True' if status else 'False':<9} | {cntry:^15} | {proto:^10} ")
if (i + 1) % 15 == 0: # Every 15 proxies, ask to continue or terminate
if input(f"\n{Fore.BLUE}Press 'q' to quit or any other key to continue: ").lower() == 'q':
print(f"{Fore.YELLOW}Proxy checking terminated by user.")
break
except requests.exceptions.RequestException as e:
print(f"{Fore.RED}Network error occurred: {str(e)}")
except FileNotFoundError:
print(f"{Fore.RED}File {proxy_file_path} not found.")
# try:
# while True:
# pass
# except KeyboardInterrupt:
# # Catch KeyboardInterrupt to ensure the program exits gracefully
# print('\nExiting due to Ctrl+C')
# sys.exit(0)
if __name__ == "__main__":
args = initialize()
if args.single:
if args.protocol == "none":
args.protocol = identify_proxy_type(args.single)
main(single_proxy=args.single, protocol=args.protocol)
elif args.txt:
main(proxy_file_path=args.txt)
elif args.url:
main(proxy_url=args.url)