-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
84 lines (74 loc) · 3.09 KB
/
main.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
import time
import requests
from random import choice
class SimpleProxyChecker:
def __init__(self, ip: str, port: str, timeout: int = 10):
self.ip = ip
self.port = port
self.timeout = timeout
self.judges_ip = ['https://api.ipify.org/', ]
self.judges_azenv = ['http://www.proxy-listen.de/azenv.php',
'http://mojeip.net.pl/asdfa/azenv.php',
'http://azenv.net/',
'http://www.proxyjudge.biz/az.php'
]
self.my_real_ip = self._get_my_ip()
self.result_dict = {'ip': self.ip, 'port': self.port}
def _get_my_ip(self) -> str:
"""Get my real ip"""
return requests.get(choice(self.judges_ip)).text
def _check_protocol_get_ip(self):
"""Protocol, proxy_ip, speed"""
self._work_protocol = []
self._ip_with_proxy = ''
for protocol in ['http', 'socks4', 'socks5']:
proxy = {"http": f"{protocol}://{self.ip}:{self.port}", "https": f"{protocol}://{self.ip}:{self.port}"}
try:
start = time.time()
res = requests.get(choice(self.judges_ip), proxies=proxy, timeout=self.timeout)
if res.status_code == 200:
self.result_dict['speed'] = round(time.time() - start, 2)
self._work_protocol.append(protocol)
_ip_with_proxy = res.text
self._get_anonim_level(protocol)
except Exception:
pass
self.result_dict['type'] = self._work_protocol
def _get_anonim_level(self, protocol):
private_headers = [
'VIA',
'X-FORWARDED-FOR',
'X-FORWARDED',
'FORWARDED-FOR',
'FORWARDED-FOR-IP',
'FORWARDED',
'CLIENT-IP',
'PROXY-CONNECTION'
]
try:
proxy = {"http": f"{protocol}://{self.ip}:{self.port}", "https": f"{protocol}://{self.ip}:{self.port}"}
_res = requests.get(choice(self.judges_azenv), proxies=proxy, timeout=self.timeout)
_answer = _res.text
if self.my_real_ip in _answer:
self.result_dict['anonim_level'] = 'Transparent'
elif any([i in _res.text for i in private_headers]):
self.result_dict['anonim_level'] = 'Anonymous'
else:
self.result_dict['anonim_level'] = 'High'
except Exception:
self.result_dict['anonim_level'] = 'Unknown'
def get_country(self):
"""Country in iso format"""
try:
_res = requests.get(url='https://ip2c.org/' + self.ip)
if _res.text[0] == '1':
self.result_dict['geo'] = _res.text.split(';')[1]
else:
self.result_dict['geo'] = 'Unknown'
except:
self.result_dict['geo'] = 'Unknown'
def main_check(self) -> dict or bool:
self._check_protocol_get_ip()
if not self.result_dict['type']: return False
self.get_country()
return self.result_dict