forked from RocketMap/RocketMap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.py
83 lines (56 loc) · 2.27 KB
/
proxy.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import logging
import requests
import sys
from queue import Queue
from threading import Thread
log = logging.getLogger(__name__)
# Simple function to do a call to Niantic's system for testing proxy connectivity
def check_proxy(proxy_queue, timeout, proxies):
proxy_test_url = 'https://sso.pokemon.com/'
proxy = proxy_queue.get()
if proxy and proxy[1]:
log.debug('Checking proxy: %s', proxy[1])
try:
proxy_response = requests.get(proxy_test_url, proxies={'http': proxy[1], 'https': proxy[1]}, timeout=timeout)
if proxy_response.status_code == 200:
log.debug('Proxy %s is ok', proxy[1])
proxy_queue.task_done()
proxies.append(proxy[1])
return True
else:
proxy_error = "Wrong status code - " + str(proxy_response.status_code)
except requests.ConnectTimeout:
proxy_error = "Connection timeout (" + str(timeout) + " second(s) ) via proxy " + proxy[1]
except requests.ConnectionError:
proxy_error = "Failed to connect to proxy " + proxy[1]
except Exception as e:
proxy_error = e
else:
proxy_error = "Empty proxy server"
log.warning('%s', proxy_error)
proxy_queue.task_done()
return False
# Check all proxies and return a working list with proxies
def check_proxies(args):
proxy_queue = Queue()
total_proxies = len(args.proxy)
log.info('Checking %d proxies...', total_proxies)
proxies = []
for proxy in enumerate(args.proxy):
proxy_queue.put(proxy)
t = Thread(target=check_proxy,
name='check_proxy',
args=(proxy_queue, args.proxy_timeout, proxies))
t.daemon = True
t.start()
# This is painfull but we need to wait here untill proxy_queue is completed so we have a working list of proxies
proxy_queue.join()
working_proxies = len(proxies)
if working_proxies == 0:
log.error('Proxy was configured but no working proxies was found! We are aborting!')
sys.exit(1)
else:
log.info('Proxy check completed with %d working proxies of %d configured', working_proxies, total_proxies)
return proxies