-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoxy.py
executable file
·237 lines (204 loc) · 5.62 KB
/
moxy.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
#!/usr/bin/env python
__author__ = "Neil Loknath"
__copyright__ = "Copyright 2013, Neil Loknath"
__license__ = "BSD"
__version__ = "0.1"
__maintainer__ = "Neil Loknath"
__email__ = "[email protected]"
__status__ = "Alpha"
import urllib2
import urllib
import SocketServer
import SimpleHTTPServer
import threading
import select
import socket
import cgi
import sys
import re
from urlparse import urlparse
import moxy.controllers
from moxy.config import Config
from moxy.http.strategies import MockStrategy
class MoxyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
"""
First responder for proxied HTTP requests
"""
def __init__(self, request, client_address, server):
self.strategy = MockStrategy()
self.config = config
SimpleHTTPServer.SimpleHTTPRequestHandler.__init__(self, request, client_address, server)
def do_PUT(self):
self.invoke_with_strategy('do_PUT')
def do_DELETE(self):
self.invoke_with_strategy('do_DELETE')
def do_POST(self):
self.invoke_with_strategy('do_POST')
def do_GET(self):
self.invoke_with_strategy('do_GET')
def do_CONNECT(self):
"""
Establish tunnel and send SSL traffic
through
"""
host, port = self.path.split(':')
if not host or not port:
self.send_response(400)
return
tunnel = socket.socket()
try:
tunnel.connect((host, int(port)))
self.send_response(200, 'Connection Established')
self.send_headers("Proxy-agent: Moxy/" + str(__version__) + "\r\n")
timeout = 10000
sockets = [self.connection, tunnel]
time = 0
while True:
time += 5
(recv, _, error) = select.select(sockets, [], sockets, 5)
if time == timeout:
break
if error:
break
if recv:
for s in recv:
data = s.recv(8192)
if s is self.connection:
out = tunnel
else:
out = self.connection
if data:
out.send(data)
time = 0
except:
print "Unexpected error:", sys.exc_info()[0]
self.send_response(400)
raise
finally:
tunnel.close()
def invoke_with_strategy(self, func):
"""
Search for a strategy to handle the request
method or use the default if none is found
"""
strategy = config.find_strategy(self.path)
if strategy:
getattr(strategy, func)(self)
return
getattr(self.strategy, func)(self)
def forward_request(self, url, data=None):
"""
Send HTTP request to destination server and
write headers, response code, and response
"""
opener = urllib2.build_opener(urllib2.HTTPHandler,
MoxyHTTPErrorProcessor,
urllib2.ProxyHandler({}))
f = opener.open(urllib2.Request(url, data, self.headers))
self.send_response(f.getcode())
self.send_headers(str(f.info()))
self.send_body(f)
f.close()
def get_post_data(self):
"""
Get POST data from HTTP request, if it exists
"""
length = self.headers.getheader('content-length')
if not length:
return None
return self.rfile.read(int(length))
def send_headers(self, data):
"""
Write HTTP headers into response
"""
self.wfile.write(data)
self.end_headers()
def send_response(self, code, message=None):
"""
Send HTTP response code
"""
self.log_request(code)
if message is None:
if code in self.responses:
message = self.responses[code][0]
else:
message = ''
if self.request_version != 'HTTP/0.9':
self.wfile.write("%s %d %s\r\n" %
(self.protocol_version, code, message))
def send_body(self, f):
"""
Copy a file into the response
"""
self.copyfile(f, self.wfile)
self.wfile.flush()
class DaemonHandler(MoxyHandler):
"""
Handles requests for managing the proxy, such as
displaying and refreshing the configuration
"""
def __init__(self, request, client_address, server):
self.postvars = {}
MoxyHandler.__init__(self, request, client_address, server)
def do_POST(self):
ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
if ctype == 'multipart/form-data':
self.postvars = cgi.parse_multipart(self.rfile, pdict)
elif ctype == 'application/x-www-form-urlencoded':
length = int(self.headers.getheader('content-length'))
self.postvars = cgi.parse_qs(self.rfile.read(length), keep_blank_values=1)
print self.postvars
self.do_GET()
def do_GET(self):
path = urllib.unquote(urlparse(self.path).path).split('/')
obj = None
func = None
for segment in path:
if not segment: continue
# Replace non-valid characters with _
segment = re.sub(r'[^a-zA-Z0-9_]', '_', segment.lower())
if not obj:
obj = getattr(moxy.controllers, segment.capitalize() + 'Controller')()
continue
elif not func:
func = segment
else:
func += segment.capitalize()
if func:
getattr(obj, func)(self)
elif obj:
obj.index(self)
class MoxyHTTPErrorProcessor(urllib2.HTTPErrorProcessor):
"""
Since this is a proxy, we don't want to do any special
error handling. We let the client take care of it.
"""
def http_response(self, req, response):
return response
def start_proxy():
"""
Start proxying requests. A new process is forked for each
incoming request
"""
SocketServer.ForkingTCPServer.allow_reuse_address = 1
httpd = SocketServer.ForkingTCPServer(('', config.port), MoxyHandler)
print '\nMoxy is listening on port %i' % config.port
httpd.serve_forever()
def start_daemon():
"""
Start HTTP server to provide an interface for managing
the proxy
"""
SocketServer.TCPServer.allow_reuse_address = 1
httpd = SocketServer.TCPServer(('', config.port + 1), DaemonHandler)
print '\nMoxy management daemon is listening on port %i' % (config.port + 1)
httpd.serve_forever()
config = Config()
def main():
config.load()
t = threading.Thread(target=start_daemon)
t.daemon = True
t.start()
start_proxy()
if __name__ == '__main__':
main()