aboutsummaryrefslogtreecommitdiffstats
path: root/libmproxy/script/script_context.py
blob: d8748cb2acb2bef4f7be12048d4b043426ba9455 (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
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
"""
The mitmproxy script context provides an API to inline scripts.
"""
from __future__ import absolute_import, print_function, division
from .. import contentviews


class ScriptContext(object):
    """
    The script context should be used to interact with the global mitmproxy state from within a
    script.
    """

    def __init__(self, master):
        self._master = master

    def log(self, message, level="info"):
        """
        Logs an event.

        By default, only events with level "error" get displayed. This can be controlled with the "-v" switch.
        How log messages are handled depends on the front-end. mitmdump will print them to stdout,
        mitmproxy sends output to the eventlog for display ("e" keyboard shortcut).
        """
        self._master.add_event(message, level)

    def kill_flow(self, f):
        """
        Kills a flow immediately. No further data will be sent to the client or the server.
        """
        f.kill(self._master)

    def duplicate_flow(self, f):
        """
        Returns a duplicate of the specified flow. The flow is also
        injected into the current state, and is ready for editing, replay,
        etc.
        """
        self._master.pause_scripts = True
        f = self._master.duplicate_flow(f)
        self._master.pause_scripts = False
        return f

    def replay_request(self, f):
        """
        Replay the request on the current flow. The response will be added
        to the flow object.
        """
        return self._master.replay_request(f, block=True, run_scripthooks=False)

    @property
    def app_registry(self):
        return self._master.apps

    def add_contentview(self, view_obj):
        contentviews.add(view_obj)

    def remove_contentview(self, view_obj):
        contentviews.remove(view_obj)