MCPcopy Index your code
hub / github.com/mitmproxy/mitmproxy / Master

Class Master

mitmproxy/master.py:19–145  ·  view source on GitHub ↗

The master handles mitmproxy's main event loop.

Source from the content-addressed store, hash-verified

17
18
19class Master:
20 """
21 The master handles mitmproxy's main event loop.
22 """
23
24 event_loop: asyncio.AbstractEventLoop
25 _termlog_addon: termlog.TermLog | None = None
26
27 def __init__(
28 self,
29 opts: options.Options | None,
30 event_loop: asyncio.AbstractEventLoop | None = None,
31 with_termlog: bool = False,
32 ):
33 self.options: options.Options = opts or options.Options()
34 self.commands = command.CommandManager(self)
35 self.addons = addonmanager.AddonManager(self)
36
37 if with_termlog:
38 self._termlog_addon = termlog.TermLog()
39 self.addons.add(self._termlog_addon)
40
41 self.log = log.Log(self) # deprecated, do not use.
42 self._legacy_log_events = log.LegacyLogEvents(self)
43 self._legacy_log_events.install()
44
45 # We expect an active event loop here already because some addons
46 # may want to spawn tasks during the initial configuration phase,
47 # which happens before run().
48 self.event_loop = event_loop or asyncio.get_running_loop()
49 self.should_exit = asyncio.Event()
50 mitmproxy_ctx.master = self
51 mitmproxy_ctx.log = self.log # deprecated, do not use.
52 mitmproxy_ctx.options = self.options
53
54 async def run(self) -> None:
55 with (
56 asyncio_utils.install_exception_handler(self._asyncio_exception_handler),
57 asyncio_utils.set_eager_task_factory(),
58 ):
59 self.should_exit.clear()
60
61 # Can we exit before even bringing up servers?
62 if ec := self.addons.get("errorcheck"):
63 await ec.shutdown_if_errored()
64 if ps := self.addons.get("proxyserver"):
65 # This may block for some proxy modes, so we also monitor should_exit.
66 await asyncio.wait(
67 [
68 asyncio_utils.create_task(
69 ps.setup_servers(), name="setup_servers", keep_ref=False
70 ),
71 asyncio_utils.create_task(
72 self.should_exit.wait(), name="should_exit", keep_ref=False
73 ),
74 ],
75 return_when=asyncio.FIRST_COMPLETED,
76 )

Callers 1

test_exception_handlerFunction · 0.90

Calls

no outgoing calls

Tested by 1

test_exception_handlerFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…