-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchecker.py
More file actions
360 lines (322 loc) · 11.2 KB
/
checker.py
File metadata and controls
360 lines (322 loc) · 11.2 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import asyncio
import time
import warnings
import zlib
from .errors import (
BadResponseError,
BadStatusError,
ProxyConnError,
ProxyEmptyRecvError,
ProxyRecvError,
ProxySendError,
ProxyTimeoutError,
ResolveError,
)
from .judge import Judge, get_judges
from .negotiators import NGTRS
from .resolver import Resolver
from .utils import get_all_ip, get_headers, get_status_code, log, parse_headers
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
class Checker:
"""Proxy checker."""
def __init__(
self,
judges,
max_tries=3,
timeout=8,
verify_ssl=False,
strict=False,
dnsbl=None,
real_ext_ip=None,
types=None,
post=False,
#loop=None,
):
Judge.clear()
self._judges = get_judges(judges, timeout, verify_ssl)
self._method = 'POST' if post else 'GET'
self._max_tries = max_tries
self._real_ext_ip = real_ext_ip
self._strict = strict
self._dnsbl = dnsbl or []
self._types = types or {}
self._loop = asyncio.get_running_loop()
#self._resolver = Resolver(loop=self._loop)
self._resolver = Resolver()
self._req_http_proto = not types or bool(
('HTTP', 'CONNECT:80', 'SOCKS4', 'SOCKS5') & types.keys()
)
self._req_https_proto = not types or bool(('HTTPS',) & types.keys())
self._req_smtp_proto = not types or bool(
('CONNECT:25',) & types.keys()
) # noqa
self._ngtrs = {proto for proto in types or NGTRS}
async def check_judges(self):
# TODO: need refactoring
log.debug('Start check judges')
stime = time.time()
await asyncio.gather(
*[j.check(real_ext_ip=self._real_ext_ip) for j in self._judges]
)
self._judges = [j for j in self._judges if j.is_working]
log.debug(
'%d judges added. Runtime: %.4f;'
% (len(self._judges), time.time() - stime)
)
nojudges = []
disable_protocols = []
if len(Judge.available['HTTP']) == 0:
nojudges.append('HTTP')
disable_protocols.extend(['HTTP', 'CONNECT:80', 'SOCKS4', 'SOCKS5'])
self._req_http_proto = False
# for coroutines, which is already waiting
Judge.ev['HTTP'].set()
if len(Judge.available['HTTPS']) == 0:
nojudges.append('HTTPS')
disable_protocols.append('HTTPS')
self._req_https_proto = False
# for coroutines, which is already waiting
Judge.ev['HTTPS'].set()
if len(Judge.available['SMTP']) == 0:
# nojudges.append('SMTP')
disable_protocols.append('SMTP')
self._req_smtp_proto = False
# for coroutines, which is already waiting
Judge.ev['SMTP'].set()
for proto in disable_protocols:
if proto in self._ngtrs:
self._ngtrs.remove(proto)
#if nojudges:
# warnings.warn(
# 'Not found judges for the {nojudges} protocol.\n'
# 'Checking proxy on protocols {disp} is disabled.'.format(
# nojudges=nojudges, disp=disable_protocols
# ),
# UserWarning,
# )
#if self._judges:
# log.debug('Loaded: %d proxy judges' % len(set(self._judges)))
#else:
#RuntimeError('Not found judges')
def _types_passed(self, proxy):
if not self._types:
return True
for proto, lvl in proxy.types.copy().items():
req_levels = self._types.get(proto)
if not req_levels or (lvl in req_levels):
if not self._strict:
return True
else:
if self._strict:
del proxy.types[proto]
if self._strict and proxy.types:
return True
proxy.log(
'Protocol or the level of anonymity differs from the requested'
)
return False
async def _in_DNSBL(self, host):
_host = '.'.join(reversed(host.split('.'))) # reverse address
tasks = []
for domain in self._dnsbl:
query = '.'.join([_host, domain])
tasks.append(self._resolver.resolve(query, logging=False))
responses = await asyncio.gather(*tasks, return_exceptions=True)
if any([r for r in responses if not isinstance(r, ResolveError)]):
return True
return False
async def check(self, proxy):
if self._dnsbl:
if await self._in_DNSBL(proxy.host):
proxy.log('Found in DNSBL')
return False
if self._req_http_proto:
await Judge.ev['HTTP'].wait()
if self._req_https_proto:
await Judge.ev['HTTPS'].wait()
if self._req_smtp_proto:
await Judge.ev['SMTP'].wait()
if proxy.expected_types:
ngtrs = proxy.expected_types & self._ngtrs
else:
ngtrs = self._ngtrs
results = []
for proto in ngtrs:
if proto == 'CONNECT:25':
result = await self._check_conn_25(proxy, proto)
else:
result = await self._check(proxy, proto)
results.append(result)
proxy.is_working = True if any(results) else False
if proxy.is_working and self._types_passed(proxy):
return True
return False
async def _check_conn_25(self, proxy, proto):
judge = Judge.get_random(proto)
proxy.log('Selected judge: %s' % judge)
result = False
for attempt in range(self._max_tries):
try:
proxy.ngtr = proto
await proxy.connect()
await proxy.ngtr.negotiate(host=judge.host, ip=judge.ip)
except ProxyTimeoutError:
continue
except (
ProxyConnError,
ProxyRecvError,
ProxySendError,
ProxyEmptyRecvError,
BadStatusError,
BadResponseError,
):
break
else:
proxy.types[proxy.ngtr.name] = None
result = True
break
finally:
proxy.close()
return result
async def _check(self, proxy, proto):
judge = Judge.get_random(proto)
proxy.log('Selected judge: %s' % judge)
result = False
for attempt in range(self._max_tries):
try:
proxy.ngtr = proto
await proxy.connect()
await proxy.ngtr.negotiate(host=judge.host, ip=judge.ip)
headers, content, rv = await _send_test_request(
self._method, proxy, judge
)
except ProxyTimeoutError:
continue
except (
ProxyConnError,
ProxyRecvError,
ProxySendError,
ProxyEmptyRecvError,
BadStatusError,
BadResponseError,
):
break
else:
content = _decompress_content(headers, content)
result = _check_test_response(proxy, headers, content, rv)
if result:
if proxy.ngtr.check_anon_lvl:
lvl = _get_anonymity_lvl(
self._real_ext_ip, proxy, judge, content
)
else:
lvl = None
proxy.types[proxy.ngtr.name] = lvl
break
finally:
proxy.close()
return result
def _request(method, host, path, fullpath=False, data=''):
hdrs, rv = get_headers(rv=True)
hdrs['Host'] = host
hdrs['Connection'] = 'close'
hdrs['Content-Length'] = len(data)
if method == 'POST':
hdrs['Content-Type'] = 'application/octet-stream'
kw = {
'method': method,
'path': 'http://%s%s' % (host, path) if fullpath else path, # HTTP
'headers': '\r\n'.join(('%s: %s' % (k, v) for k, v in hdrs.items())),
'data': data,
}
req = (
('{method} {path} HTTP/1.1\r\n{headers}\r\n\r\n{data}')
.format(**kw)
.encode()
)
return req, rv
async def _send_test_request(method, proxy, judge):
resp, content, err = None, None, None
request, rv = _request(
method=method,
host=judge.host,
path=judge.path,
fullpath=proxy.ngtr.use_full_path,
)
try:
await proxy.send(request)
resp = await proxy.recv()
code = get_status_code(resp)
if code != 200:
err = BadStatusError
raise err
headers, content, *_ = resp.split(b'\r\n\r\n', maxsplit=1)
except ValueError:
err = BadResponseError
raise err
finally:
proxy.log('Get: %s' % ('success' if content else 'failed'), err=err)
log.debug(
'{h}:{p} [{n}]: ({j}) rv: {rv}, response: {resp}'.format(
h=proxy.host,
p=proxy.port,
n=proxy.ngtr.name,
j=judge.url,
rv=rv,
resp=resp,
)
)
return headers, content, rv
def _decompress_content(headers, content):
headers = parse_headers(headers)
is_compressed = headers.get('Content-Encoding') in ('gzip', 'deflate')
is_chunked = headers.get('Transfer-Encoding') == 'chunked'
if is_compressed:
# gzip: zlib.MAX_WBITS|16;
# deflate: -zlib.MAX_WBITS;
# auto: zlib.MAX_WBITS|32;
if is_chunked:
# b'278\r\n\x1f\x8b...\x00\r\n0\r\n\r\n' => b'\x1f\x8b...\x00
content = b''.join(content.split(b'\r\n')[1::2])
try:
content = zlib.decompress(content, zlib.MAX_WBITS | 32)
except zlib.error:
content = b''
return content.decode('utf-8', 'ignore')
def _check_test_response(proxy, headers, content, rv):
verIsCorrect = rv in content
refSupported = get_headers()['Referer'] in content
cookieSupported = get_headers()['Cookie'] in content
foundIP = get_all_ip(content)
if all([verIsCorrect, foundIP, refSupported, cookieSupported]):
proxy.log('Response: correct')
return True
else:
proxy.log(
'Response: not correct; ip: %s, rv: %s, ref: %s, cookie: %s'
% (bool(foundIP), verIsCorrect, refSupported, cookieSupported)
)
return False
def _get_anonymity_lvl(real_ext_ip, proxy, judge, content):
content = content.lower()
foundIP = get_all_ip(content)
via = (content.count('via') > judge.marks['via']) or (
content.count('proxy') > judge.marks['proxy']
)
if real_ext_ip in foundIP:
lvl = 'Transparent'
elif via:
lvl = 'Anonymous'
else:
lvl = 'High'
proxy.log(
'A: {lvl}; {ip}; via(p): {via}'.format(lvl=lvl[:4], ip=foundIP, via=via)
)
return lvl
class ProxyChecker(Checker):
def __init__(self, *args, **kwargs):
warnings.warn(
'`ProxyChecker` is deprecated, use `Checker` instead.',
DeprecationWarning,
)
super().__init__(*args, **kwargs)