Introduction
The mitmproxy addon API allows you to extend mitmproxy’s functionality by creating custom addons. Addons can intercept and modify traffic, implement custom logic, and integrate with external systems.Creating an Addon
An addon is a Python class or module with methods that correspond to event hooks. When mitmproxy processes traffic, it triggers these hooks, allowing your addon to respond to different events.Basic Structure
The Addon Lifecycle
Addons go through several lifecycle stages:Load
The
load(loader) hook is called when the addon is first loaded. Use this to register options and commands.Configure
The
configure(updated) hook is called when configuration changes. The updated parameter contains the names of changed options.Key Concepts
Flows
A flow represents a single transaction between a client and server. Different protocols have different flow types:- HTTPFlow: HTTP/HTTPS traffic
- TCPFlow: Raw TCP connections
- UDPFlow: UDP datagrams
- DNSFlow: DNS queries and responses
The Context Object
The global context objectmitmproxy.ctx provides access to the master and various utilities:
The Loader Object
The loader is passed to theload() hook and provides methods for registering options and commands:
Register a new configuration option.Parameters:
name(str): Option nametypespec(type): Python type (bool, str, int, etc.)default(Any): Default valuehelp(str): Help textchoices(Sequence[str], optional): Valid choices for the option
Register a new command programmatically.Parameters:
path(str): Command path (e.g., “myaddon.mycommand”)func(Callable): Function to execute
The AddonManager
TheAddonManager class (defined in mitmproxy.addonmanager) manages all loaded addons and handles event dispatching.
Key Methods
Register a new addon and call its
load event. Also registers any sub-addons.Add addons to the end of the chain and run their load event.
Remove an addon and all its sub-addons.
Retrieve an addon by name. Names are lowercase class names by default.
Synchronously trigger an event across all addons.
Asynchronously trigger an event across all addons. Supports both sync and async hooks.
Async Support
Addons support both synchronous and asynchronous hook functions:Error Handling
Errors in addon hooks are caught and logged automatically. Your addon won’t crash mitmproxy:Logging
Use Python’s standard logging module:The old
ctx.log API is deprecated. Always use Python’s logging module.Sub-Addons
Addons can contain other addons:Next Steps
Event Hooks
Learn about all available event hooks
Commands
Create custom commands for mitmproxy
Examples
See practical addon examples
Protocol Reference
Deep dive into flow types and protocol details
API Changes
Some events have been renamed or removed in recent versions:clientconnect→client_connected(mitmproxy 7+)clientdisconnect→client_disconnected(mitmproxy 7+)serverconnect→server_connectandserver_connected(mitmproxy 7+)serverdisconnect→server_disconnected(mitmproxy 7+)add_log→ Use Python’sloggingmodule (deprecated in mitmproxy 9+)
