2021-06-24 14:13:56 +00:00
|
|
|
#!/usr/bin/env python3
|
2022-02-03 11:25:11 +00:00
|
|
|
import asyncio
|
2016-11-01 22:58:27 +00:00
|
|
|
import click
|
|
|
|
|
|
|
|
from mitmproxy.addons import dumper
|
|
|
|
from mitmproxy.test import tflow
|
|
|
|
from mitmproxy.test import taddons
|
|
|
|
|
|
|
|
|
2022-02-03 11:25:11 +00:00
|
|
|
def run_async(coro):
|
|
|
|
"""
|
|
|
|
Run the given async function in a new event loop.
|
|
|
|
This allows async functions to be called synchronously.
|
|
|
|
"""
|
|
|
|
loop = asyncio.new_event_loop()
|
|
|
|
try:
|
|
|
|
return loop.run_until_complete(coro)
|
|
|
|
finally:
|
|
|
|
loop.close()
|
|
|
|
|
|
|
|
|
2016-11-01 22:58:27 +00:00
|
|
|
def show(flow_detail, flows):
|
|
|
|
d = dumper.Dumper()
|
2018-02-23 23:33:48 +00:00
|
|
|
with taddons.context() as ctx:
|
2016-11-01 22:58:27 +00:00
|
|
|
ctx.configure(d, flow_detail=flow_detail)
|
|
|
|
for f in flows:
|
2022-02-03 11:25:11 +00:00
|
|
|
run_async(ctx.cycle(d, f))
|
2016-11-01 22:58:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
@click.group()
|
|
|
|
def cli():
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@cli.command()
|
|
|
|
@click.option('--level', default=1, help='Detail level')
|
|
|
|
def tcp(level):
|
2022-02-03 11:11:30 +00:00
|
|
|
f1 = tflow.ttcpflow()
|
2016-11-01 22:58:27 +00:00
|
|
|
show(level, [f1])
|
|
|
|
|
|
|
|
|
|
|
|
@cli.command()
|
|
|
|
@click.option('--level', default=1, help='Detail level')
|
|
|
|
def large(level):
|
2022-02-03 11:11:30 +00:00
|
|
|
f1 = tflow.tflow(resp=True)
|
2016-11-01 22:58:27 +00:00
|
|
|
f1.response.headers["content-type"] = "text/html"
|
|
|
|
f1.response.content = b"foo bar voing\n" * 100
|
|
|
|
show(level, [f1])
|
|
|
|
|
|
|
|
|
|
|
|
@cli.command()
|
|
|
|
@click.option('--level', default=1, help='Detail level')
|
|
|
|
def small(level):
|
2022-02-03 11:11:30 +00:00
|
|
|
f1 = tflow.tflow(resp=True)
|
2016-11-01 22:58:27 +00:00
|
|
|
f1.response.headers["content-type"] = "text/html"
|
|
|
|
f1.response.content = b"<html><body>Hello!</body></html>"
|
|
|
|
|
2022-02-03 11:11:30 +00:00
|
|
|
f2 = tflow.tflow(err=True)
|
2016-11-01 22:58:27 +00:00
|
|
|
|
|
|
|
show(
|
|
|
|
level,
|
|
|
|
[
|
|
|
|
f1, f2,
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
cli()
|