mitmproxy/examples/addons/commands-paths.py

31 lines
787 B
Python
Raw Normal View History

"""Handle file paths as command arguments."""
2022-04-26 11:53:23 +00:00
from collections.abc import Sequence
from mitmproxy import command
from mitmproxy import ctx
from mitmproxy import flow
2021-02-03 22:00:41 +00:00
from mitmproxy import http
from mitmproxy import types
class MyAddon:
@command.command("myaddon.histogram")
def histogram(
self,
2022-04-26 11:53:23 +00:00
flows: Sequence[flow.Flow],
path: types.Path,
) -> None:
2022-04-26 11:51:11 +00:00
totals: dict[str, int] = {}
for f in flows:
2021-02-03 22:00:41 +00:00
if isinstance(f, http.HTTPFlow):
totals[f.request.host] = totals.setdefault(f.request.host, 0) + 1
with open(path, "w+") as fp:
for cnt, dom in sorted((v, k) for (k, v) in totals.items()):
fp.write(f"{cnt}: {dom}\n")
ctx.log.alert("done")
2022-04-26 11:53:35 +00:00
addons = [MyAddon()]