forked from Veallym0n/PyFiddler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fiddler.py
381 lines (297 loc) · 11.5 KB
/
fiddler.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import tornado
import tornado.web
import tornado.ioloop
import tornado.gen
import tornado.httpclient
import tornado.httputil
import tornado.websocket
import tornado.options
from tornado.options import define, options
import json
import urlparse
from uuid import uuid4
from pdb import set_trace as st
from collections import OrderedDict as odict
define('proxy', default=8888, type=int, help='run proxy server on this port')
define('console', default=8889, type=int, help='run console server on this port')
#RequestPool = {}
class RequestPoolObject(dict):
def __init__(self,*args,**kwargs):
self.update(self, *args, **kwargs)
self.callback = None
def _notify(self,key,value):
if self.callback:
self.callback(key,value)
RequestPool = RequestPoolObject()
class configure(object):
def __init__(self):
self.BlockRequest = False
self.BlockResponse = False
self.StreamResponse = False
conf = configure()
def dumpdefault(s):
if isinstance(s, ReqInfo): return s.dict()
if not type(s) in (dict, list, tuple, int, float, str, odict): return repr(s)
return s
class ReqInfo:
idenity = 0
def __init__(self, uuid, handler, block_on_response = 1, isclone = False):
self.uuid = uuid
self.handler = handler
self.blockType = ('response','request','released')[block_on_response]
self.isClone = isclone
self.id = self.newid()
@classmethod
def newid(cls):
ReqInfo.idenity+=1
return ReqInfo.idenity
@property
def _get_scheme(self):
return urlparse.urlparse(self.handler.request.uri, scheme='http').scheme
@property
def _get_body_len(self):
if self.blockType == 'request': return 0
if self.blockType in ('response','released'):
if self.handler.response.body:
return len(self.handler.response.body)
else:
return 0
@property
def _get_ctype(self):
if self.blockType == 'request': return None
if self.blockType in ('response', 'released'):
return self.handler.response.headers.get('Content-Type',None)
def dict(self,extr=False):
baseinfo = dict(
id=self.id,
code=self.handler.response.code, method=self.handler.request.method, protocol=self._get_scheme,
url=self.handler.request.uri, bodylen=self._get_body_len,
cType=self._get_ctype, Type=self.blockType
)
if not extr: return baseinfo
baseinfo['req']={}
baseinfo['res']={}
baseinfo['req']['headers'] = [i for i in self.handler.request.headers.get_all()]
baseinfo['req']['body'] = self.handler.request.body
if self.blockType in ('response','released'):
baseinfo['res']['headers'] = [i for i in self.handler.response.headers.get_all()]
baseinfo['res']['body'] = self.handler.response.body
baseinfo['res']['reason'] = self.handler.response.reason
return baseinfo
def __repr__(self):
return json.dumps(self.dict())
def block(self):
RequestPool[self.uuid]=self
RequestPool._notify(self.uuid, self.dict())
return self
def release(self):
if self.blockType == 'request':
self.handler.start_request(self.isClone)
return self
elif self.blockType == 'response':
self.handler.start_response(self.isClone)
RequestPool._notify(self.uuid, self.dict())
return self
elif self.blockType == 'released':
pass
class Proxy(tornado.web.RequestHandler):
SUPPORTED_METHODS = tornado.web.RequestHandler.SUPPORTED_METHODS + ('CONNECT',)
def __init__(self, *args, **kwargs):
super(Proxy, self).__init__(*args,**kwargs)
self.uuid = uuid4().hex
self._responsed = False
self.response = self
self.response.code = 0
self.stream_response_stage = 0
self.Req = None
def compute_etag(self):
return None
@property
def conf(self):
return self.settings.get('c')
@tornado.web.asynchronous
def get(self):
if self.conf.BlockRequest:
self.Req = ReqInfo(self.uuid, self).block()
else:
self.Req = ReqInfo(self.uuid, self).block().release()
@tornado.web.asynchronous
def post(self):
return self.get()
@tornado.web.asynchronous
def connect(self):
return self.get()
#client = self.request.connection.stream
#s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
#upstream = tornado.iostream.IOStream(s)
#read_client = lambda x:upstream.write(x)
#read_upstream = lambda x:client.write(x)
def start_response(self, isClone=False):
if isClone: return
if self.response:
self.set_status(self.response.code, self.response.reason)
self._headers = tornado.httputil.HTTPHeaders()
for k, v in self.response.headers.get_all():
if k not in ('Content-Length', 'Transfer-Encoding', 'Content-Encoding', 'Connection'):
self.add_header(k,v)
if self.response.body:
self.add_header('Content-Length',len(self.response.body))
self.write(self.response.body)
self.finish()
return
def stream_response(self,body):
if self.stream_response_stage == 2:
self.write(body)
def hdr_callback(self,header):
if not self.stream_response_stage:
http_version, code, reason = header.split(None,2)
self.set_status(code, reason)
self.stream_response_stage = 1
elif header == '\r\n':
self.stream_response_stage = 2
else:
k,v = header.split(':',1)
self._headers.add(k,v)
@tornado.gen.coroutine
def start_request(self, isClone=False):
if self.request.headers.get('Proxy-Connection'): del self.request.headers['Proxy-Connection']
if not self.request.body: self.request.body = None
streaming_callback = None
header_callback = None
if not self.conf.BlockResponse and self.conf.StreamResponse:
streaming_callback = self.stream_response
header_callback = self.hdr_callback
Fetcher = tornado.httpclient.AsyncHTTPClient()
self.request.headers['cache-control']='no-cache'
self.request.headers['Pragma']='no-cache'
self.response = yield Fetcher.fetch(
self.request.uri, method=self.request.method,
headers = self.request.headers, follow_redirects = False,
allow_nonstandard_methods = True, validate_cert = False,
streaming_callback = streaming_callback, header_callback = header_callback,
raise_error = False, body = self.request.body
)
if not isClone:
if self.conf.BlockResponse:
RequestPool[self.uuid].blockType = 'response'
RequestPool._notify(self.uuid, self.Req.dict())
else:
self.start_response()
RequestPool[self.uuid].blockType = 'released'
RequestPool._notify(self.uuid, self.Req.dict())
else:
self.start_response(isClone=True)
RequestPool[self.uuid].blockType = 'released'
RequestPool._notify(self.uuid, self.Req.dict())
class WebServer(tornado.web.RequestHandler):
@property
def conf(self):
return self.settings.get('c')
@tornado.web.asynchronous
def get(self,rtype):
if rtype == '':
#self.finish(open('main.html').read())
self.render('main.html')
elif rtype == 'show':
self.set_header('content-type','application/x-javascript')
reqpool = odict(sorted(RequestPool.items(), key=lambda x:x[1].id))
self.finish(json.dumps(reqpool, default=dumpdefault))
elif rtype == 'detail':
uuid = self.get_argument('uuid')
Req = RequestPool.get(uuid,None)
if Req:
self.finish(Req.dict(extr=True))
return
self.finish()
elif rtype == 'ctrl':
brq = self.get_argument('brq','off')
brs = self.get_argument('brs','off')
self.conf.BlockRequest = bool(brq=='on')
self.conf.BlockResponse = bool(brs=='on')
self.finish(dict(brq=self.conf.BlockRequest,brs=self.conf.BlockResponse))
return
elif rtype == 'clone':
uuid = self.get_argument('uuid')
Req = RequestPool.get(uuid, None)
if Req:
uuid = uuid4().hex
newReq = ReqInfo(uuid, Req.handler, isclone=True)
newReq.handler.response.code = 0
newReq.handler.uuid = uuid
newReq.block()
self.finish('done')
elif rtype == 'releaseall':
for r in RequestPool:
if RequestPool[r].blockType!='released': RequestPool[r].release()
self.finish('done')
elif rtype == 'clearlog':
RequestPool.clear()
self.finish('done')
return
@tornado.web.asynchronous
def post(self,rtype):
if rtype == 'release':
uuid = self.get_argument('uuid')
if not RequestPool.get(uuid,None): return
rlst = self.get_argument('rlst')
if rlst == 'request':
save_req_Info(self, RequestPool[uuid].handler)
elif rlst == 'response':
save_res_Info(self, RequestPool[uuid].handler)
RequestPool[uuid].release()
self.finish('done')
return
def save_res_Info(wb, cls):
code = wb.get_argument('code',400)
reason = wb.get_argument('reason','Bad Request')
headers = wb.get_argument('headers','')
body = wb.get_argument('body',None)
header_tuple = map(lambda x:x.split(':',1),headers.strip().split('\n'))
cls.response.code = int(code)
cls.response.reason = reason
for header in header_tuple:
try:
h,v = header
cls.response.headers[h]=v
except:
pass
if body: cls.response._body = body
def save_req_Info(wb, cls):
method = wb.get_argument('method','GET')
url = wb.get_argument('url',cls.request.uri)
headers = wb.get_argument('headers','')
body = wb.get_argument('body',None)
header_tuple = map(lambda x:x.split(':',1),headers.strip().split('\n'))
cls.request.uri = url
cls.request.method = method
for header in header_tuple:
try:
h,v = header
cls.request.headers[h]=v
except:
pass
if body: cls.request.body = body
class WSHandler(tornado.websocket.WebSocketHandler):
def open(self):
RequestPool.callback = self.on_event
def on_event(self, uuid, obj):
try:
self.write_message(json.dumps((uuid,obj)))
except Exception,e:
pass
def run():
tornado.options.parse_command_line()
proxy = tornado.web.Application([
(r'.*',Proxy)
],**dict(c=conf))
webapp = tornado.web.Application([
(r'/ws',WSHandler),
(r'/(.*)',WebServer)
],**dict(c=conf,debug=True))
proxy.listen(options.proxy)
webapp.listen(options.console)
tornado.ioloop.IOLoop.instance().start()
if __name__ == '__main__':
run()