aboutsummaryrefslogtreecommitdiffstats
path: root/examples/addons/commands-paths.py
blob: f37a0fbc5d44ffcf4a87e2631a3d1507c856a57d (plain)
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
import typing

from mitmproxy import command
from mitmproxy import ctx
from mitmproxy import flow
from mitmproxy import types


class MyAddon:
    def __init__(self):
        self.num = 0

    @command.command("myaddon.histogram")
    def histogram(
        self,
        flows: typing.Sequence[flow.Flow],
        path: types.Path,
    ) -> None:
        totals = {}
        for f in flows:
            totals[f.request.host] = totals.setdefault(f.request.host, 0) + 1

        fp = open(path, "w+")
        for cnt, dom in sorted([(v, k) for (k, v) in totals.items()]):
            fp.write("%s: %s\n" % (cnt, dom))

        ctx.log.alert("done")


addons = [
    MyAddon()
]