aboutsummaryrefslogtreecommitdiffstats
path: root/doc-src/scripts.html
blob: 3fa79a6c6fe536e92ddc6a79044c9641c7f9e7bb (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
__mitmproxy__ has a powerful scripting API that allows you to modify flows
on-the-fly or rewrite previously saved flows locally. 

The mitmproxy scripting API is event driven - a script is simply a Python
module that exposes a set of event methods. Here's a complete mitmproxy script
that adds a new header to every HTTP response before it is returned to the
client:

$!example("examples/add_header.py")!$

The first argument to each event method is an instance of ScriptContext that
lets the script interact with the global mitmproxy state. The __response__
event also gets an instance of Flow, which we can use to manipulate the
response itself.


## Events

### start(ScriptContext)

Called once on startup, before any other events.


###clientconnect(ScriptContext, ClientConnect)

Called when a client initiates a connection to the proxy. Note that
a connection can correspond to multiple HTTP requests.


###request(ScriptContext, Flow)

Called when a client request has been received. The __Flow__ object is
guaranteed to have a non-None __request__ attribute.


### response(ScriptContext, Flow)

Called when a server response has been received. The __Flow__ object is
guaranteed to have non-None __request__ and __response__ attributes.


### error(ScriptContext, Flow)

Called when a flow error has occured, 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. The __Flow__
object is guaranteed to have non-None __request__ and __error__ attributes.


### clientdisconnect(ScriptContext, ClientDisconnect)

Called when a client disconnects from the proxy.

### done(ScriptContext)

Called once on script shutdown, after any other events.


## API

The main classes you will deal with in writing mitmproxy scripts are:

<table class="kvtable">
    <tr>
        <th>libmproxy.flow.ClientConnection</th>
        <td>Describes a client connection.</td>
    </tr>
    <tr>
        <th>libmproxy.flow.ClientDisconnection</th>
        <td>Describes a client disconnection.</td>
    </tr>
    <tr>
        <th>libmproxy.flow.Error</th>
        <td>A communications error.</td>
    </tr>
    <tr>
        <th>libmproxy.flow.Flow</th>
        <td>A collection of objects representing a single HTTP transaction.</td>
    </tr>
    <tr>
        <th>libmproxy.flow.Headers</th>
        <td>HTTP headers for a request or response.</td>
    </tr>
    <tr>
        <th>libmproxy.flow.ODict</th>

        <td>A dictionary-like object for managing sets of key/value data. There
        is also a variant called CaselessODict that ignores key case for some
        calls.</td>
    </tr>
    <tr>
        <th>libmproxy.flow.Response</th>
        <td>An HTTP response.</td>
    </tr>
    <tr>
        <th>libmproxy.flow.Request</th>
        <td>An HTTP request.</td>
    </tr>
    <tr>
        <th>libmproxy.flow.ScriptContext</th>


        <td> A handle for interacting with mitmproxy's from within scripts.  </td>

    </tr>
</table>

The canonical API documentation is the code. You can view the API documentation
using pydoc (which is installed with Python by default), like this:

<pre class="terminal">
> pydoc libmproxy.flow.Request
</pre>


## Running scripts on saved flows

Sometimes, we want to run a script on __Flow__ objects that are already
complete.  This happens when you start a script, and then load a saved set of
flows from a file (see the "scripted data transformation" example on the
[mitmdump](@!urlTo("mitmdump.html")!@) page). It also happens when you run a
one-shot script on a single flow through the _|_ (pipe) shortcut in mitmproxy.

In this case, there are no client connections, and the events are run in the
following order: __start__, __request__, __response__, __error__, __done__.  If
the flow doesn't have a __response__ or __error__ associated with it, the
matching event will be skipped. 
this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include <stdarg.h> #include <linux/module.h> #include <asm-xen/xenbus.h> #include <asm-xen/net_driver_util.h> #include "common.h" #if 0 #undef DPRINTK #define DPRINTK(fmt, args...) \ printk("netback/xenbus (%s:%d) " fmt ".\n", __FUNCTION__, __LINE__, ##args) #endif struct backend_info { struct xenbus_device *dev; netif_t *netif; struct xenbus_watch backend_watch; XenbusState frontend_state; }; static int connect_rings(struct backend_info *); static void connect(struct backend_info *); static void maybe_connect(struct backend_info *); static void backend_changed(struct xenbus_watch *, const char **, unsigned int); static int netback_remove(struct xenbus_device *dev) { struct backend_info *be = dev->data; if (be->backend_watch.node) { unregister_xenbus_watch(&be->backend_watch); kfree(be->backend_watch.node); be->backend_watch.node = NULL; } if (be->netif) { netif_disconnect(be->netif); be->netif = NULL; } kfree(be); dev->data = NULL; return 0; } /** * Entry point to this code when a new device is created. Allocate the basic * structures, and watch the store waiting for the hotplug scripts to tell us * the device's handle. Switch to InitWait. */ static int netback_probe(struct xenbus_device *dev, const struct xenbus_device_id *id) { int err; struct backend_info *be = kmalloc(sizeof(struct backend_info), GFP_KERNEL); if (!be) { xenbus_dev_fatal(dev, -ENOMEM, "allocating backend structure"); return -ENOMEM; } memset(be, 0, sizeof(*be)); be->dev = dev; dev->data = be; err = xenbus_watch_path2(dev, dev->nodename, "handle", &be->backend_watch, backend_changed); if (err) goto fail; err = xenbus_switch_state(dev, NULL, XenbusStateInitWait); if (err) { goto fail; } return 0; fail: DPRINTK("failed"); netback_remove(dev); return err; } /** * Handle the creation of the hotplug script environment. We add the script * and vif variables to the environment, for the benefit of the vif-* hotplug * scripts. */ static int netback_hotplug(struct xenbus_device *xdev, char **envp, int num_envp, char *buffer, int buffer_size) { struct backend_info *be = xdev->data; netif_t *netif = be->netif; int i = 0, length = 0; char *val; DPRINTK("netback_hotplug"); val = xenbus_read(NULL, xdev->nodename, "script", NULL); if (IS_ERR(val)) { int err = PTR_ERR(val); xenbus_dev_fatal(xdev, err, "reading script"); return err; } else { add_hotplug_env_var(envp, num_envp, &i, buffer, buffer_size, &length, "script=%s", val); kfree(val); } add_hotplug_env_var(envp, num_envp, &i, buffer, buffer_size, &length, "vif=%s", netif->dev->name); envp[i] = NULL; return 0; } /** * Callback received when the hotplug scripts have placed the handle node. * Read it, and create a netif structure. If the frontend is ready, connect. */ static void backend_changed(struct xenbus_watch *watch, const char **vec, unsigned int len) { int err; long handle; struct backend_info *be = container_of(watch, struct backend_info, backend_watch); struct xenbus_device *dev = be->dev; DPRINTK(""); err = xenbus_scanf(NULL, dev->nodename, "handle", "%li", &handle); if (XENBUS_EXIST_ERR(err)) { /* Since this watch will fire once immediately after it is registered, we expect this. Ignore it, and wait for the hotplug scripts. */ return; } if (err != 1) { xenbus_dev_fatal(dev, err, "reading handle"); return; } if (be->netif == NULL) { u8 be_mac[ETH_ALEN] = { 0, 0, 0, 0, 0, 0 }; be->netif = alloc_netif(dev->otherend_id, handle, be_mac); if (IS_ERR(be->netif)) { err = PTR_ERR(be->netif); be->netif = NULL; xenbus_dev_fatal(dev, err, "creating interface"); return; } kobject_hotplug(&dev->dev.kobj, KOBJ_ONLINE); maybe_connect(be); } } /** * Callback received when the frontend's state changes. */ static void frontend_changed(struct xenbus_device *dev, XenbusState frontend_state) { struct backend_info *be = dev->data; DPRINTK(""); be->frontend_state = frontend_state; switch (frontend_state) { case XenbusStateInitialising: case XenbusStateInitialised: break; case XenbusStateConnected: maybe_connect(be); break; case XenbusStateClosing: xenbus_switch_state(dev, NULL, XenbusStateClosing); break; case XenbusStateClosed: kobject_hotplug(&dev->dev.kobj, KOBJ_OFFLINE); device_unregister(&dev->dev); break; case XenbusStateUnknown: case XenbusStateInitWait: default: xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend", frontend_state); break; } } /* ** Connection ** */ static void maybe_connect(struct backend_info *be) { if (be->netif != NULL && be->frontend_state == XenbusStateConnected) { connect(be); } } static void connect(struct backend_info *be) { int err; struct xenbus_device *dev = be->dev; err = connect_rings(be); if (err) return; err = xen_net_read_mac(dev, be->netif->fe_dev_addr); if (err) { xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename); return; } xenbus_switch_state(dev, NULL, XenbusStateConnected); } static int connect_rings(struct backend_info *be) { struct xenbus_device *dev = be->dev; unsigned long tx_ring_ref, rx_ring_ref; unsigned int evtchn; int err; DPRINTK(""); err = xenbus_gather(NULL, dev->otherend, "tx-ring-ref", "%lu", &tx_ring_ref, "rx-ring-ref", "%lu", &rx_ring_ref, "event-channel", "%u", &evtchn, NULL); if (err) { xenbus_dev_fatal(dev, err, "reading %s/ring-ref and event-channel", dev->otherend); return err; } /* Map the shared frame, irq etc. */ err = netif_map(be->netif, tx_ring_ref, rx_ring_ref, evtchn); if (err) { xenbus_dev_fatal(dev, err, "mapping shared-frames %lu/%lu port %u", tx_ring_ref, rx_ring_ref, evtchn); return err; } return 0; } /* ** Driver Registration ** */ static struct xenbus_device_id netback_ids[] = { { "vif" }, { "" } }; static struct xenbus_driver netback = { .name = "vif", .owner = THIS_MODULE, .ids = netback_ids, .probe = netback_probe, .remove = netback_remove, .hotplug = netback_hotplug, .otherend_changed = frontend_changed, }; void netif_xenbus_init(void) { xenbus_register_backend(&netback); } /* * Local variables: * c-file-style: "linux" * indent-tabs-mode: t * c-indent-level: 8 * c-basic-offset: 8 * tab-width: 8 * End: */