-
Notifications
You must be signed in to change notification settings - Fork 1
/
addon-commands.py
598 lines (460 loc) · 18.4 KB
/
addon-commands.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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
import hashlib
import json
import os
import pathlib
import urllib.parse
import re
import shlex
import shutil
import subprocess
from collections.abc import Sequence
from urllib.parse import unquote, urljoin, urlparse, parse_qs
import pyperclip
from mitmproxy import command
from mitmproxy import ctx
from mitmproxy import http
from mitmproxy.flow import Flow
with open(os.path.join(
pathlib.Path(__file__).parent.absolute(), "config.json")) as f:
json_config = json.load(f)
map_local_base_urls = json_config['mapLocalBaseUrls']
map_local_dir = json_config['mapLocalDir']
def is_url_in_map_local_base_urls(url):
for base_url in map_local_base_urls:
if base_url in url:
return True
return False
def get_after_base_url_from_local_base(without_parameters_url):
for base_url in map_local_base_urls:
if base_url in without_parameters_url:
return without_parameters_url.replace(base_url, '')
return without_parameters_url
def get_status_code():
"""
:return: the response status code as a string
"""
ctx.master.commands.execute("cut.clip @focus response.status_code")
return pyperclip.paste()
def trim_response_content(s):
"""
:return: a string
"""
if isinstance(s, bytes):
s = s.decode('utf-8')
if len(s) >= 2100:
s = s[:2100] + "..."
return s
def get_time_from_timestamps():
ctx.master.commands.execute("cut.clip @focus request.timestamp_start")
start = pyperclip.paste()
ctx.master.commands.execute("cut.clip @focus response.timestamp_end")
end = pyperclip.paste()
return float(end) - float(start)
def get_curl_formatted():
ctx.master.commands.execute("export.clip curl @focus")
s = pyperclip.paste()
parts = shlex.split(s)
output = parts[0] + " \\\n"
next_cmd = False
markdown_table = ""
for s in parts[1:]:
parsed_url = urlparse(s)
if parsed_url.scheme and parsed_url.netloc:
# unquote since the export.clip command returns a string like this:
# ?alias=projectStatus%3Astatus
s = unquote(s)
if next_cmd:
output += "'" + s + "' \\\n"
next_cmd = False
elif s.startswith("--"):
output += " " + s + " \\\n"
next_cmd = False
elif s.startswith("-"):
output += " " + s + " "
next_cmd = True
else:
# pass -g for the unescaped URL so the curl can be executed in the
# terminal
output += " -g \\\n"
# pretty much always, this is the URL at the very end of the command
output += " '" + s + "' \\\n"
query_params = parse_qs(urlparse(s).query)
if len(query_params) >= 1:
url = strip_base_url_prefix(s)
if '?' in url:
url = url[:url.index('?')]
ctx.master.commands.execute("cut.clip @focus request.method")
method = pyperclip.paste()
full = method + " " + url
param_list = [{"Parameter": key, "Value": value[0]} for key, value in query_params.items()]
markdown_table = "\n<details>\n<summary>" + full + "</summary>\n\n"
for param in param_list:
markdown_table += f"`{param['Parameter']}`\n{param['Value']}\n\n"
markdown_table += '</details>'
next_cmd = False
output = output.strip()
if output.endswith("\\"):
output = output[:-1]
return "```bash\n" + output + "\n```\n" + markdown_table
class CurlAddon:
@command.command("cu")
def do(self) -> None:
pyperclip.copy(get_curl_formatted())
ctx.log.alert("Copied cURL with ```bash to clipboard")
class UrlAddon:
@command.command("u")
def do(self) -> None:
ctx.master.commands.execute("cut.clip @focus request.url")
url = unquote(pyperclip.paste())
pyperclip.copy(url)
ctx.log.alert("Copied to clipboard: " + url)
class ShortUrlMarkdownAddon:
@command.command("ur")
def do(self) -> None:
"""Copy the method plus the URL with the base and the params cut off
with surrounding Markdown code blocks.
Example: `GET printouts/printhours.pdf`
"""
ctx.master.commands.execute("cut.clip @focus request.url")
url = strip_base_url_prefix(unquote(pyperclip.paste()))
ctx.master.commands.execute("cut.clip @focus request.method")
method = pyperclip.paste()
full = "`" + method + " " + url + "`"
pyperclip.copy(full)
ctx.log.alert("Copied to clipboard: " + full)
class ShortUrlAddon:
@command.command("url")
def do(self) -> None:
"""Copy the method plus the URL with the base and the params cut off.
Example: GET printouts/printhours.pdf
"""
ctx.master.commands.execute("cut.clip @focus request.url")
raw_url = unquote(pyperclip.paste())
url = strip_base_url_prefix(raw_url)
if raw_url != url:
if "?" in url:
url = url[:url.index("?")]
ctx.master.commands.execute("cut.clip @focus request.method")
method = pyperclip.paste()
pyperclip.copy(method + " " + url)
ctx.log.alert("Copied to clipboard: " + method + " " + url)
class AllResponseBodyAddon:
@command.command("copyall")
def do(self, flows: Sequence[Flow]) -> None:
if len(flows) != 1:
ctx.log.alert("This can only operate on a single flow!")
else:
flow = flows[0]
curl = get_curl_formatted()
code = get_status_code()
time = get_time_from_timestamps()
response = flow.response.content
if len(response) == 0:
pyperclip.copy(curl + "\nTook " +
"{:.2f}".format(time) + "s with " +
"status code " + code +
" with no response body.")
ctx.log.alert("Copied cURL and status code to clipboard. " +
"There was no response body!")
else:
# noinspection PyBroadException
try:
as_json = "```json\n" + trim_response_content(
json.dumps(json.loads(response), indent=2)) + \
"\n```"
except Exception:
as_json = "```\n" + \
trim_response_content(response) + \
"```"
pyperclip.copy(curl + "\n" + "Took " +
"{:.2f}".format(time) + "s with " +
"status code " + code + " to return:\n\n" +
as_json)
ctx.log.alert(
"Copied cURL, response body and status code to clipboard")
class AllResponseBodyKey:
@command.command("a")
def do(self) -> None:
ctx.master.commands.execute("copyall @focus")
class AllResponseWithoutBodyAddon:
@command.command("ab")
def do(self) -> None:
curl = get_curl_formatted()
code = get_status_code()
time = get_time_from_timestamps()
pyperclip.copy(curl + "\nTook " +
"{:.2f}".format(time) + "s with " + "status code " +
code + ".")
ctx.log.alert("Copied cURL and status code to clipboard")
# if is_request is false, it is assumed that this is a request
def copy_content_as_json(content, is_request):
if content == b'':
ctx.log.alert("The flow does not have a request body!")
else:
# noinspection PyBroadException
try:
as_json = json.dumps(json.loads(content), indent=2)
pyperclip.copy(as_json)
if is_request:
ctx.log.alert("Copied JSON request body to clipboard")
else:
ctx.log.alert("Copied JSON request body to clipboard")
except Exception:
pyperclip.copy(content.decode('utf-8'))
if is_request:
ctx.log.alert("Copied non-JSON request body to clipboard")
else:
ctx.log.alert("Copied non-JSON response body to clipboard")
class RequestBodyAddon:
@command.command("copyrequest")
def do(self, flows: Sequence[Flow]) -> None:
if len(flows) != 1:
ctx.log.alert("This can only operate on a single flow!")
else:
flow = flows[0]
content = flow.request.content
copy_content_as_json(content, is_request=True)
class RequestBodyKey:
@command.command("req")
def do(self) -> None:
ctx.master.commands.execute("copyrequest @focus")
class ResponseBodyAddon:
@command.command("copyresponse")
def do(self, flows: Sequence[Flow]) -> None:
if len(flows) != 1:
ctx.log.alert("This can only operate on a single flow!")
else:
flow = flows[0]
content = flow.response.content
copy_content_as_json(content, is_request=False)
class ResponseBodyKey:
@command.command("resp")
def do(self) -> None:
ctx.master.commands.execute("copyresponse @focus")
class KeyBindingAddon:
@command.command("k")
def do(self) -> None:
ctx.master.commands.execute("console.view.keybindings")
class FlowResumeAddon:
@command.command("r")
def do(self) -> None:
ctx.master.commands.execute("flow.resume @focus")
class InterceptAddon:
@command.command("intercept.inner")
def do(self, flows: Sequence[Flow]) -> None:
for f in flows:
url = re.escape(f.request.url)
ctx.master.commands.execute(
f"console.command set intercept '~u {url} & ~s'")
class InterceptKey:
@command.command("cept")
def do(self) -> None:
ctx.master.commands.execute("intercept.inner @focus")
def get_url_without_parameters(url):
path = urljoin(url, urlparse(url).path)
# drop /v1/ for a cleaner file system
path = path.replace("/index.php/v1/", "/index.php/")
# strip trailing / to be able to create a file on file system
if path.endswith('/'):
return path[:-1]
return path
def create_url_parameters_hash(url):
query = urlparse(url).query
if query:
return ' ' + hashlib.sha256(query.encode('utf-8')).hexdigest()[0:12]
return ''
def append_to_debug(data):
home_dir = os.path.expanduser("~")
debug_file = os.path.join(home_dir, "debug.py")
with open(debug_file, "a") as df:
df.write(data + '\n')
# noinspection PyShadowingNames
def map_flow_to_local_path(flow):
url = flow.request.pretty_url
if 'stripUrlParts' in json_config:
query_params = parse_qs(urlparse(url).query)
for key in json_config['stripUrlParts']:
query_params.pop(key, None)
new_query_string = "&".join(f"{key}={value[0]}" for key, value in query_params.items())
# noinspection PyProtectedMember
url = urlparse(url)._replace(query=new_query_string).geturl()
without_parameters = get_url_without_parameters(url)
after_base = get_after_base_url_from_local_base(without_parameters)
base_name = os.path.basename(after_base)
return os.path.join(map_local_dir, os.path.dirname(after_base),
flow.request.method + ' ' +
base_name) + create_url_parameters_hash(url) + ".json"
class MapLocalRequests:
""" Map to local response bodies from a directory. """
@staticmethod
def request(flow: http.HTTPFlow) -> None:
local_file = map_flow_to_local_path(flow)
if os.path.exists(local_file):
with open(local_file) as f:
data = json.loads(f.read())
flow.response = http.Response.make(
data['statusCode'], json.dumps(data['response'], indent=2),
data['headers'])
def format_any_content(content):
if content == 'b':
return None
try:
return json.loads(content)
except (json.JSONDecodeError, UnicodeDecodeError):
try:
return content.decode('utf-8')
except UnicodeDecodeError:
# note that this is the case for POST files
return "unmappable content for JSON"
def strip_base_url_prefix(url):
for base_url in map_local_base_urls:
if url.startswith(base_url):
return url[len(base_url):]
return url
def check_if_flow_ignored(flow):
if 'ignoreForLocalMapping' in json_config:
url = strip_base_url_prefix(flow.request.pretty_url)
for ignore in json_config['ignoreForLocalMapping']:
parts = ignore.split(" ")
if parts[0] == flow.request.method and parts[1] == url:
return True
return False
class CreateLocal:
@command.command("local")
def do(self, flows: Sequence[Flow]) -> None:
has_error = False
single_flow = len(flows) == 1
no_response_count = 0
unmappable_response_count = 0
for flow in flows:
if flow.response:
url = flow.request.pretty_url
if is_url_in_map_local_base_urls(url):
if check_if_flow_ignored(flow):
continue
local_file = map_flow_to_local_path(flow)
local_dir = os.path.dirname(local_file)
if not os.path.exists(local_dir):
os.makedirs(local_dir)
response_headers = {}
for k, v in flow.response.headers.items():
response_headers[k] = v
request_content = format_any_content(flow.request.content)
resp = format_any_content(flow.response.content)
if request_content == "unmappable content for JSON" or \
resp == "unmappable content for JSON":
unmappable_response_count += 1
continue
req = flow.request
data = {
'response': resp,
'headers': response_headers,
'statusCode': flow.response.status_code,
'url': req.method + ' ' + req.pretty_url,
'requestBody': request_content,
}
json_string = json.dumps(data, indent=2)
if single_flow:
if 'startCommandOnLocalMap' in json_config and \
len(json_config['startCommandOnLocalMap']) >= 1:
with open(local_file, 'w+') as f1:
f1.write(json_string)
command = json_config['startCommandOnLocalMap']
cmd = map(lambda x: x.replace('###', local_file), command)
subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=subprocess.PIPE)
else:
c = ctx.master.spawn_editor(json_string)
with open(local_file, 'w+') as f2:
f2.write(c)
else:
with open(local_file, 'w+') as f3:
f3.write(json_string)
else:
if single_flow:
has_error = True
ctx.log.alert(
'Configured base URLs are not present: ' +
str(map_local_base_urls))
else:
no_response_count += 1
if not has_error:
if no_response_count == 0 and unmappable_response_count == 0:
ctx.log.alert('Saved ' + str(len(flows)) + ' flow(s) to ' +
map_local_dir)
else:
ctx.log.alert('Saved ' + str(
len(flows) - no_response_count -
unmappable_response_count) + ' flow(s) to ' +
map_local_dir + ' --- ' +
str(no_response_count) +
' flow(s) without response --- ' +
str(unmappable_response_count) +
' flow(s) unmappable to JSON')
class CreateAllLocalKey:
@command.command("la")
def do(self) -> None:
ctx.master.commands.execute("local @shown")
class CreateLocalKey:
@command.command("l")
def do(self) -> None:
ctx.master.commands.execute("local @focus")
class DeleteLocalRequest:
@command.command("localdelete")
def do(self, flows: Sequence[Flow]) -> None:
for flow in flows:
url = flow.request.pretty_url
if is_url_in_map_local_base_urls(url):
local_file = map_flow_to_local_path(flow)
try:
os.remove(local_file)
ctx.log.alert('Deleted ' + local_file)
except FileNotFoundError:
ctx.log.alert('Local mapping files do not exist!')
else:
ctx.log.alert('Configured base URLs are not present: ' +
str(map_local_base_urls))
class DeleteLocalRequestKey:
@command.command("ld")
def do(self) -> None:
ctx.master.commands.execute("localdelete @focus")
class ClearMappedLocalRequests:
@command.command("lc")
def do(self) -> None:
shutil.rmtree(map_local_dir)
os.mkdir(map_local_dir)
ctx.log.alert('Deleted everything under ' + map_local_dir)
class SetupViewFilterKey:
@command.command("f")
def do(self) -> None:
ctx.master.commands.execute("console.command set view_filter")
addons = [
MapLocalRequests(),
CreateLocal(),
DeleteLocalRequest(),
DeleteLocalRequest(),
ClearMappedLocalRequests(),
# other functionality
CurlAddon(),
UrlAddon(),
ShortUrlAddon(),
ShortUrlMarkdownAddon(),
RequestBodyAddon(),
ResponseBodyAddon(),
KeyBindingAddon(),
InterceptAddon(),
FlowResumeAddon(),
AllResponseBodyAddon(),
AllResponseWithoutBodyAddon(),
# keys
SetupViewFilterKey(),
DeleteLocalRequestKey(),
CreateLocalKey(),
CreateAllLocalKey(),
ResponseBodyKey(),
RequestBodyKey(),
AllResponseBodyKey(),
InterceptKey(),
]