forked from wallneradam/docker-hma-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hmaproxy.py
executable file
·204 lines (166 loc) · 6.45 KB
/
hmaproxy.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import httplib
import ssl
import urlparse
from socket import timeout
import sys
from logger import log
from proxy import ProxyRequestHandler, ThreadingProxyServer
from conf import *
from Queue import Queue, Empty
from threading import Thread, Lock
import os
from time import time, sleep
end = False
HMA_CHANGE_IP_INTERVAL = int(os.getenv('HMA_CHANGE_IP_INTERVAL', 60)) # change IP interval
lastChangedVPNNum = 0
lastChangedVPNTime = time()
ipVersion = 0
_changeIPLock = Lock()
def changeIP(proxyNum, version):
global lastChangedVPNNum, lastChangedVPNTime, ipVersion, _changeIPLock
_changeIPLock.acquire()
if version >= ipVersion:
ipVersion += 1
os.system('/opt/ip-changer.sh change %i' % (proxyNum + 1))
lastChangedVPNNum = proxyNum
lastChangedVPNTime = time()
_changeIPLock.release()
class IPChangerThread(Thread):
def __init__(self):
super(IPChangerThread, self).__init__()
self.daemon = True
self.start()
def run(self):
interval = HMA_CHANGE_IP_INTERVAL
super(IPChangerThread, self).run()
while not end:
now = time()
if now - lastChangedVPNTime > interval:
changeIP((lastChangedVPNNum + 1) % 2, sys.maxint)
sleep(1)
class RequestHandler(ProxyRequestHandler):
@staticmethod
def forwardThread(queue, proxyNum, path, command, req, req_body):
proxy = PROXIES[proxyNum]
resObj = {
'status': 0,
'errorMessage': None,
'res': None,
'res_body': None,
'proxyNum': proxyNum,
'proxy': proxy
}
retry = RETRY
timeoutRetry = RETRY_TIMEOUT
while retry > 0 and timeoutRetry > 0:
try:
_proxy = proxy.split(':')
proxyHost = _proxy[0]
proxyPort = _proxy[1]
if path.startswith('https://'):
conn = httplib.HTTPSConnection(proxyHost, proxyPort, timeout=TIMEOUT)
else:
conn = httplib.HTTPConnection(proxyHost, proxyPort, timeout=TIMEOUT)
conn.request(command, path, req_body, dict(req.headers))
res = conn.getresponse()
res_body = res.read()
if res:
resObj['res'] = res
resObj['res_body'] = res_body
if res.status > 400:
resObj['status'] = res.status
resObj['errorMessage'] = res.reason
else:
resObj['status'] = 501
resObj['errorMessage'] = 'Unknown error!'
break
except timeout:
timeoutRetry -= 1
resObj['status'] = 408
resObj['errorMessage'] = 'VPN timeout!'
except Exception as e:
resObj['status'] = 501
resObj['errorMessage'] = e.message if e.message else 'Unknown error! (exception)'
retry -= 1
sleep(RETRY_WAIT)
queue.put(resObj)
# noinspection PyBroadException
def do_GET(self):
req = self
content_length = int(req.headers.get('Content-Length', 0))
req_body = self.rfile.read(content_length) if content_length else None
if req.path[0] == '/':
if isinstance(self.connection, ssl.SSLSocket):
req.path = "https://%s%s" % (req.headers['Host'], req.path)
else:
req.path = "http://%s%s" % (req.headers['Host'], req.path)
u = urlparse.urlsplit(req.path)
scheme, netloc, path = u.scheme, u.netloc, (u.path + '?' + u.query if u.query else u.path)
if scheme not in ('http', 'https'):
return self.send_error(501, "Unsupported scheme: %s" % scheme)
if netloc:
req.headers['Host'] = netloc
setattr(req, 'headers', self.filter_headers(req.headers))
# Prevent multiple IP changes on failed requests
startIpVersion = ipVersion
# Start threads on different proxies
queue = Queue()
for proxyNum in range(2):
thread = Thread(target=RequestHandler.forwardThread,
args=(queue, proxyNum, self.path, self.command, req, req_body))
thread.daemon = True
thread.start()
resObj = None
res_body = res = None
for proxyNum in range(2):
try:
resObj = queue.get(timeout=TIMEOUT)
res = resObj['res']
res_body = resObj['res_body']
if resObj['status'] == 0:
break
except Empty:
if not resObj:
resObj = {'status': 408, 'errorMessage': 'Timeout!', 'proxyNum': proxyNum}
break
if resObj['status'] in (403, 407, 408, 429, 500, 501, 502):
changeIP(resObj['proxyNum'], startIpVersion)
# If all of them returns with error :-/
if resObj['status'] >= 500 or resObj['status'] in (408, 429): # Error
return self.send_error(resObj['status'], resObj['errorMessage'])
log.info('Proxy: %i | Status: %i | Request: %s', resObj['proxyNum'] + 1,
res.status if res else resObj['status'], self.path)
if not res:
return self.send_error(501, 'Unknown error!')
version_table = {10: 'HTTP/1.0', 11: 'HTTP/1.1'}
setattr(res, 'headers', res.msg)
setattr(res, 'response_version', version_table[res.version])
setattr(res, 'headers', self.filter_headers(res.headers))
self.wfile.write("%s %d %s\r\n" % (self.protocol_version, res.status, res.reason))
for line in res.headers.headers:
self.wfile.write(line)
self.end_headers()
self.wfile.write(res_body)
self.wfile.flush()
def send_error(self, code, message=None):
log.error("Error occured: %i %s - Request: %s", code, message, self.path)
# Start IP changer thread
ipChanger = IPChangerThread()
# Start proxy server
server_address = (HOST, PORT)
# noinspection PyRedeclaration
httpd = ThreadingProxyServer(server_address, RequestHandler)
# Start main loop
sa = httpd.socket.getsockname()
print "HMA proxy started on", sa[0], "port", sa[1], "..."
try:
httpd.serve_forever()
except KeyboardInterrupt:
print ""
finally:
print "Stopping HMA proxy..."
end = True
httpd.shutdown()
exit()