# Event Hooks
Addons hook into mitmproxy’s internal mechanisms through event hooks. These are
implemented on addons as methods with a set of well-known names. Many events
receive Flow
objects as arguments - by modifying these objects, addons can
change traffic on the fly. For instance, here is an addon that adds a response
header with a count of the number of responses seen:
"""Add an HTTP header to each response."""
class AddHeader:
def __init__(self):
self.num = 0
def response(self, flow):
self.num = self.num + 1
flow.response.headers["count"] = str(self.num)
addons = [AddHeader()]
# Available Hooks
The following addons list all available event hooks.
Called when an addon is first loaded. This event receives a Loader object, which contains methods for adding options and commands. This method is where the addon configures itself.
Called when the proxy is completely up and running. At this point, you can expect all addons to be loaded and all options to be set.
Called when configuration changes. The updated argument is a set-like object containing the keys of all changed options. This event is called during startup with all options in the updated set.
Called when the addon shuts down, either by being removed from the mitmproxy instance, or when mitmproxy itself shuts down. On shutdown, this event is called after the event loop is terminated, guaranteeing that it will be the final event an addon sees. Note that log handlers are shut down at this point, so calls to log functions will produce no output.
A client has connected to mitmproxy. Note that a connection can correspond to multiple HTTP requests.
Setting client.error kills the connection.
Mitmproxy is about to connect to a server. Note that a connection can correspond to multiple requests.
Setting data.server.error kills the connection.
The full HTTP request has been read.
Note: If request streaming is active, this event fires after the entire body has been streamed.
HTTP trailers, if present, have not been transmitted to the server yet and can still be modified.
Enabling streaming may cause unexpected event sequences: For example, response
may now occur
before request
because the server replied with "413 Payload Too Large" during upload.
The full HTTP response has been read.
Note: If response streaming is active, this event fires after the entire body has been streamed. HTTP trailers, if present, have not been transmitted to the client yet and can still be modified.
An HTTP error has occurred, e.g. invalid server responses, or interrupted connections. This is distinct from a valid server HTTP error response, which is simply a response with an HTTP error code.
Every flow will receive either an error or an response event, but not both.
An HTTP CONNECT request was received. This event can be ignored for most practical purposes.
This event only occurs in regular and upstream proxy modes when the client instructs mitmproxy to open a connection to an upstream host. Setting a non 2xx response on the flow will return the response to the client and abort the connection.
CONNECT requests are HTTP proxy instructions for mitmproxy itself and not forwarded. They do not generate the usual HTTP handler events, but all requests going over the newly opened connection will.
An HTTP CONNECT request is about to be sent to an upstream proxy. This event can be ignored for most practical purposes.
This event can be used to set custom authentication headers for upstream proxies.
CONNECT requests do not generate the usual HTTP handler events, but all requests going over the newly opened connection will.
HTTP CONNECT was successful
This may fire before an upstream connection has been established
if connection_strategy
is set to lazy
(default)
HTTP CONNECT has failed.
This can happen when the upstream server is unreachable or proxy authentication is required.
In contrast to the error
hook, flow.error
is not guaranteed to be set.
A TCP connection has received a message. The most recent message will be flow.messages[-1]. The message is user-modifiable.
A UDP connection has received a message. The most recent message will be flow.messages[-1]. The message is user-modifiable.
Mitmproxy has received a TLS ClientHello message.
This hook decides whether a server connection is needed to negotiate TLS with the client (data.establish_server_tls_first)
TLS negotation between mitmproxy and a client is about to start.
An addon is expected to initialize data.ssl_conn.
(by default, this is done by mitmproxy.addons.tlsconfig
)
TLS negotation between mitmproxy and a server is about to start.
An addon is expected to initialize data.ssl_conn.
(by default, this is done by mitmproxy.addons.tlsconfig
)
Called when a WebSocket message is received from the client or server. The most recent message will be flow.messages[-1]. The message is user-modifiable. Currently there are two types of messages, corresponding to the BINARY and TEXT frame types.
Network layers are being switched. You may change which layer will be used by setting data.layer.
(by default, this is done by mitmproxy.addons.NextLayer)
Deprecated: Starting with mitmproxy 9, users should use the standard Python logging module instead, for example
by calling logging.getLogger().addHandler()
.
Called whenever a new log entry is created through the mitmproxy context. Be careful not to log from this event, which will cause an infinite loop!