2020-06-22 23:53:39 +00:00
|
|
|
"""Process individual messages from a WebSocket connection."""
|
2018-10-09 16:24:18 +00:00
|
|
|
import re
|
2021-07-15 12:46:45 +00:00
|
|
|
from mitmproxy import ctx, http
|
2018-10-09 16:24:18 +00:00
|
|
|
|
|
|
|
|
2021-07-15 12:46:45 +00:00
|
|
|
def websocket_message(flow: http.HTTPFlow):
|
|
|
|
assert flow.websocket is not None # make type checker happy
|
2018-10-09 16:24:18 +00:00
|
|
|
# get the latest message
|
2021-03-08 16:10:28 +00:00
|
|
|
message = flow.websocket.messages[-1]
|
2018-10-09 16:24:18 +00:00
|
|
|
|
2020-04-04 13:31:38 +00:00
|
|
|
# was the message sent from the client or server?
|
|
|
|
if message.from_client:
|
2021-03-08 23:40:32 +00:00
|
|
|
ctx.log.info(f"Client sent a message: {message.content!r}")
|
2020-04-04 13:31:38 +00:00
|
|
|
else:
|
2021-03-08 23:40:32 +00:00
|
|
|
ctx.log.info(f"Server sent a message: {message.content!r}")
|
2018-10-09 16:24:18 +00:00
|
|
|
|
|
|
|
# manipulate the message content
|
2022-04-26 11:53:35 +00:00
|
|
|
message.content = re.sub(rb"^Hello", b"HAPPY", message.content)
|
2020-04-04 13:31:38 +00:00
|
|
|
|
2022-04-26 11:53:35 +00:00
|
|
|
if b"FOOBAR" in message.content:
|
2020-04-04 13:31:38 +00:00
|
|
|
# kill the message and not send it to the other endpoint
|
2021-07-15 12:46:45 +00:00
|
|
|
message.drop()
|