mitmproxy/pathod/protocols/websockets.py

55 lines
2.0 KiB
Python
Raw Normal View History

import time
from pathod import language
from mitmproxy import exceptions
2015-09-17 00:39:07 +00:00
class WebsocketsProtocol:
def __init__(self, pathod_handler):
self.pathod_handler = pathod_handler
def handle_websocket(self, logger):
while True:
with logger.ctx() as lg:
started = time.time()
try:
frm = language.websockets_frame.Frame.from_file(self.pathod_handler.rfile)
except exceptions.NetlibException as e:
lg("Error reading websocket frame: %s" % e)
return None, None
ended = time.time()
2016-08-16 16:31:50 +00:00
lg(repr(frm))
retlog = dict(
type="inbound",
protocol="websockets",
started=started,
duration=ended - started,
frame=dict(
),
cipher=None,
)
if self.pathod_handler.tls_established:
retlog["cipher"] = self.pathod_handler.get_current_cipher()
self.pathod_handler.addlog(retlog)
ld = language.websockets.NESTED_LEADER
if frm.payload.startswith(ld):
nest = frm.payload[len(ld):]
try:
2016-06-08 11:07:56 +00:00
wf_gen = language.parse_websocket_frame(nest.decode())
except language.exceptions.ParseException as v:
logger.write(
"Parse error in reflected frame specifcation:"
" %s" % v.msg
)
return None, None
for frm in wf_gen:
with logger.ctx() as lg:
frame_log = language.serve(
frm,
self.pathod_handler.wfile,
self.pathod_handler.settings
)
lg("crafting websocket spec: %s" % frame_log["spec"])
self.pathod_handler.addlog(frame_log)