Mitmproxy, push own WebSocket message

Viewed 632

I inspect a HTTPS WebSocket traffic with Mitmproxy. Currently I can read/edit WS messages with:

class Intercept:
    def websocket_message(self, flow):
        print(flow.messages[-1])

def start():
    return Intercept()

.. as attached script to Mitmproxy.

How do I push/inject my own message to the client? Not edit existing one, but add a new message.

1 Answers

You can do this with inject.websocket:

from mitmproxy import ctx

class Intercept:
    def websocket_message(self, flow):
        print(flow.messages[-1])
        to_client = True
        ctx.master.commands.call("inject.websocket", flow, to_client, b"Hello World!", True)

def start():
    return Intercept()

There are more examples in the documentation.

Related