-
Notifications
You must be signed in to change notification settings - Fork 109
/
utils.py
479 lines (467 loc) · 8.34 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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
# -*- coding: utf-8 -*-
import ssl
import sys
from gzip import GzipFile
from json import JSONDecoder
from time import time
from urllib.request import Request, urlopen
__all__ = ['read_url', 'test_ip', 'check_ip', 'time_repr', 'dns_query', 'HOST', 'HOST2', 'DEFAULT_IPS']
HOST = 'translate.googleapis.com'
HOST2 = 'translate-pa.googleapis.com'
TESTIP_FORMAT = 'https://{}/translate_a/single?client=gtx&sl=en&tl=fr&q=a'
def _build_request(ip, host=HOST, testip_format=TESTIP_FORMAT):
url = testip_format.format(f'[{ip}]' if ':' in ip else ip)
request = Request(url)
request.add_header('Host', host)
return request
def new_context():
ctx = ssl._create_unverified_context() if sys.platform.startswith('darwin') else ssl.create_default_context()
old_wrap_socket = ctx.wrap_socket
def new_wrap_socket(socket, **kwargs):
kwargs["server_hostname"] = HOST
return old_wrap_socket(socket, **kwargs)
ctx.wrap_socket = new_wrap_socket
return ctx
def test_ip(ip, timeout=2.5, host=HOST, testip_format=TESTIP_FORMAT):
try:
req = _build_request(ip, host, testip_format)
start_time = time()
with urlopen(req, timeout=timeout, context=new_context()) as response:
end_time = time()
except Exception as e:
return str(e)
return end_time - start_time
def check_ip(ip, timeout=2.5):
try:
urlopen(_build_request(ip), timeout=timeout, context=new_context()).close()
except:
return False
return True
def time_repr(secs):
return f'{secs*1000:.0f}ms' if secs < 0.9995 else f'{secs:.2f}s'
def dns_query(name=HOST, server='1.1.1.1', type='A', path='/dns-query'):
# https://github.com/stamparm/python-doh/blob/master/client.py
req = Request(
f'https://{server}{path}?name={name}&type={type}',
headers={'Accept': 'application/dns-json'},
)
content = urlopen(req).read().decode()
reply = JSONDecoder().decode(content)
return [] if 'Answer' not in reply else [_['data'] for _ in reply['Answer']]
def read_url(url, timeout=3.5):
request = Request(url)
request.add_header('Accept-Encoding', 'gzip')
with urlopen(request, timeout=timeout) as response:
# Handle gzip
if response.getheader('Content-Encoding') == 'gzip':
with GzipFile(fileobj=response) as gz:
return {s.decode('utf-8').strip() for s in gz}
return {s.decode('utf-8').strip() for s in response}
DEFAULT_IPS = '''108.177.111.90
108.177.122.90
108.177.125.186
108.177.126.90
108.177.127.90
108.177.97.100
142.250.0.90
142.250.1.90
142.250.10.124
142.250.10.133
142.250.10.193
142.250.10.90
142.250.100.90
142.250.101.90
142.250.102.90
142.250.103.90
142.250.105.90
142.250.107.90
142.250.11.102
142.250.11.104
142.250.11.110
142.250.11.112
142.250.11.116
142.250.11.117
142.250.11.137
142.250.11.142
142.250.11.144
142.250.11.146
142.250.11.149
142.250.11.152
142.250.11.154
142.250.11.155
142.250.11.164
142.250.11.193
142.250.11.194
142.250.11.196
142.250.11.197
142.250.11.198
142.250.11.200
142.250.11.201
142.250.11.31
142.250.11.84
142.250.11.90
142.250.11.99
142.250.110.90
142.250.111.90
142.250.112.90
142.250.113.90
142.250.114.90
142.250.115.90
142.250.12.90
142.250.123.90
142.250.125.185
142.250.125.90
142.250.126.90
142.250.128.90
142.250.13.90
142.250.136.90
142.250.138.90
142.250.141.90
142.250.142.90
142.250.145.90
142.250.147.90
142.250.148.202
142.250.149.90
142.250.152.90
142.250.153.90
142.250.157.183
142.250.157.184
142.250.157.186
142.250.157.90
142.250.158.90
142.250.159.90
142.250.182.77
142.250.189.183
142.250.199.151
142.250.199.181
142.250.199.183
142.250.199.184
142.250.199.185
142.250.200.68
142.250.203.200
142.250.207.85
142.250.219.104
142.250.219.106
142.250.219.4
142.250.219.82
142.250.219.88
142.250.27.114
142.250.27.118
142.250.27.122
142.250.27.124
142.250.27.143
142.250.27.144
142.250.27.145
142.250.27.146
142.250.27.152
142.250.27.165
142.250.27.181
142.250.27.194
142.250.27.196
142.250.27.199
142.250.27.202
142.250.27.31
142.250.27.90
142.250.27.98
142.250.28.90
142.250.30.90
142.250.31.117
142.250.31.122
142.250.31.123
142.250.31.144
142.250.31.164
142.250.31.194
142.250.31.195
142.250.31.196
142.250.31.197
142.250.31.198
142.250.31.199
142.250.31.202
142.250.31.90
142.250.4.185
142.250.4.90
142.250.64.109
142.250.64.119
142.250.64.120
142.250.64.139
142.250.64.151
142.250.64.169
142.250.64.171
142.250.64.181
142.250.64.183
142.250.64.203
142.250.64.207
142.250.64.217
142.250.64.224
142.250.64.242
142.250.64.248
142.250.64.249
142.250.64.87
142.250.64.88
142.250.64.96
142.250.66.164
142.250.66.171
142.250.66.178
142.250.66.183
142.250.66.184
142.250.66.191
142.250.66.201
142.250.66.205
142.250.66.210
142.250.66.215
142.250.66.216
142.250.66.223
142.250.66.237
142.250.66.239
142.250.66.248
142.250.66.249
142.250.67.11
142.250.67.21
142.250.67.41
142.250.67.50
142.250.67.53
142.250.67.55
142.250.67.56
142.250.67.63
142.250.67.73
142.250.67.75
142.250.67.82
142.250.67.95
142.250.68.113
142.250.8.90
142.250.9.90
142.250.96.104
142.250.96.110
142.250.96.116
142.250.96.133
142.250.96.143
142.250.96.145
142.250.96.146
142.250.96.147
142.250.96.148
142.250.96.164
142.250.96.167
142.250.96.181
142.250.96.191
142.250.96.195
142.250.96.196
142.250.96.199
142.250.96.200
142.250.96.201
142.250.96.202
142.250.96.84
142.250.96.90
142.250.96.96
142.250.96.98
142.250.97.90
142.250.98.105
142.250.98.116
142.250.98.117
142.250.98.124
142.250.98.142
142.250.98.148
142.250.98.149
142.250.98.152
142.250.98.155
142.250.98.194
142.250.98.196
142.250.98.90
142.250.99.90
142.251.0.90
142.251.0.92
142.251.1.110
142.251.1.112
142.251.1.124
142.251.1.133
142.251.1.143
142.251.1.144
142.251.1.146
142.251.1.165
142.251.1.166
142.251.1.167
142.251.1.176
142.251.1.195
142.251.1.199
142.251.1.200
142.251.1.202
142.251.1.31
142.251.1.90
142.251.1.96
142.251.10.110
142.251.10.138
142.251.10.152
142.251.10.196
142.251.10.200
142.251.10.31
142.251.10.90
142.251.107.90
142.251.111.90
142.251.112.90
142.251.116.101
142.251.116.90
142.251.117.90
142.251.12.185
142.251.12.81
142.251.12.90
142.251.120.90
142.251.15.90
142.251.16.185
142.251.16.90
142.251.160.90
142.251.161.90
142.251.162.90
142.251.163.90
142.251.165.90
142.251.166.90
142.251.170.104
142.251.170.90
142.251.171.90
142.251.172.103
142.251.172.114
142.251.172.122
142.251.172.148
142.251.172.17
142.251.172.176
142.251.172.194
142.251.172.195
142.251.172.201
142.251.172.90
142.251.172.96
142.251.172.97
142.251.173.90
142.251.175.90
142.251.177.90
142.251.178.90
142.251.179.90
142.251.18.90
142.251.182.90
142.251.183.90
142.251.2.90
142.251.220.0
142.251.220.21
142.251.220.24
142.251.220.31
142.251.220.7
142.251.32.64
142.251.32.82
142.251.32.87
142.251.32.89
142.251.33.4
142.251.36.15
142.251.36.17
142.251.36.21
142.251.36.32
142.251.36.47
142.251.4.201
142.251.4.90
142.251.4.92
142.251.4.96
142.251.40.174
142.251.40.87
142.251.42.71
142.251.43.119
142.251.43.137
142.251.43.152
142.251.43.203
142.251.43.205
142.251.43.226
142.251.43.36
142.251.43.75
142.251.43.98
142.251.5.90
142.251.6.90
142.251.8.90
142.251.9.90
172.217.0.46
172.217.13.142
172.217.16.46
172.217.164.119
172.217.164.127
172.217.165.0
172.217.165.55
172.217.167.0
172.217.168.0
172.217.168.247
172.217.169.192
172.217.173.55
172.217.192.90
172.217.193.90
172.217.195.90
172.217.203.90
172.217.204.90
172.217.212.90
172.217.214.90
172.217.215.90
172.217.218.90
172.217.222.90
172.217.31.142
172.253.112.90
172.253.113.90
172.253.114.90
172.253.115.90
172.253.116.90
172.253.117.90
172.253.118.90
172.253.119.133
172.253.119.143
172.253.119.181
172.253.119.198
172.253.119.202
172.253.119.82
172.253.119.90
172.253.122.90
172.253.123.90
172.253.124.107
172.253.124.110
172.253.124.112
172.253.124.114
172.253.124.118
172.253.124.122
172.253.124.123
172.253.124.124
172.253.124.133
172.253.124.149
172.253.124.164
172.253.124.166
172.253.124.194
172.253.124.196
172.253.124.200
172.253.124.90
172.253.124.96
172.253.125.110
172.253.125.90
172.253.126.90
172.253.127.90
172.253.58.90
172.253.62.90
172.253.63.90
216.239.32.40
216.58.209.174
216.58.214.14
216.58.220.142
216.58.227.65
216.58.227.66
216.58.227.67
34.1.15.24
64.233.189.191
64.233.189.92
74.125.137.105
74.125.137.106
74.125.137.116
74.125.137.117
74.125.137.123
74.125.137.141
74.125.137.143
74.125.137.146
74.125.137.165
74.125.137.167
74.125.137.176
74.125.137.19
74.125.137.198
74.125.137.200
74.125.137.201
74.125.137.202
74.125.137.90
74.125.137.96
74.125.137.98
74.125.196.113'''.split()