mitmproxy/libmproxy/protocol/http_replay.py

106 lines
4.2 KiB
Python
Raw Normal View History

from __future__ import (absolute_import, print_function, division)
2015-08-29 23:21:58 +00:00
import threading
import traceback
2015-09-16 16:45:22 +00:00
from libmproxy.exceptions import ReplayException
2015-09-17 00:13:28 +00:00
from netlib.exceptions import HttpException, TcpException
2015-09-16 16:45:22 +00:00
from netlib.http import http1
2015-08-30 13:27:29 +00:00
2015-08-29 23:21:58 +00:00
from ..controller import Channel
2015-08-30 13:27:29 +00:00
from ..models import Error, HTTPResponse, ServerConnection, make_connect_request
2015-09-03 15:01:25 +00:00
from .base import Kill
2015-08-30 13:27:29 +00:00
# TODO: Doesn't really belong into libmproxy.protocol...
2015-08-29 23:21:58 +00:00
class RequestReplayThread(threading.Thread):
name = "RequestReplayThread"
def __init__(self, config, flow, masterq, should_exit):
"""
masterqueue can be a queue or None, if no scripthooks should be
processed.
"""
self.config, self.flow = config, flow
if masterq:
self.channel = Channel(masterq, should_exit)
else:
self.channel = None
super(RequestReplayThread, self).__init__()
def run(self):
r = self.flow.request
form_out_backup = r.form_out
try:
self.flow.response = None
# If we have a channel, run script hooks.
if self.channel:
request_reply = self.channel.ask("request", self.flow)
2015-08-31 15:05:52 +00:00
if request_reply == Kill:
2015-08-29 23:21:58 +00:00
raise Kill()
elif isinstance(request_reply, HTTPResponse):
self.flow.response = request_reply
if not self.flow.response:
# In all modes, we directly connect to the server displayed
if self.config.mode == "upstream":
server_address = self.config.upstream_server.address
server = ServerConnection(server_address)
server.connect()
if r.scheme == "https":
connect_request = make_connect_request((r.host, r.port))
2015-09-16 16:45:22 +00:00
server.wfile.write(http1.assemble_request(connect_request))
server.wfile.flush()
resp = http1.read_response(
server.rfile,
connect_request,
body_size_limit=self.config.body_size_limit
)
if resp.status_code != 200:
2015-09-16 16:45:22 +00:00
raise ReplayException("Upstream server refuses CONNECT request")
2015-08-29 23:21:58 +00:00
server.establish_ssl(
self.config.clientcerts,
sni=self.flow.server_conn.sni
)
r.form_out = "relative"
else:
r.form_out = "absolute"
else:
server_address = (r.host, r.port)
server = ServerConnection(server_address)
server.connect()
if r.scheme == "https":
server.establish_ssl(
self.config.clientcerts,
sni=self.flow.server_conn.sni
)
r.form_out = "relative"
2015-09-16 16:45:22 +00:00
server.wfile.write(http1.assemble_request(r))
server.wfile.flush()
2015-08-29 23:21:58 +00:00
self.flow.server_conn = server
2015-09-19 09:59:05 +00:00
self.flow.response = HTTPResponse.wrap(http1.read_response(
2015-09-16 16:45:22 +00:00
server.rfile,
r,
body_size_limit=self.config.body_size_limit
2015-09-19 09:59:05 +00:00
))
2015-08-29 23:21:58 +00:00
if self.channel:
response_reply = self.channel.ask("response", self.flow)
2015-08-31 15:05:52 +00:00
if response_reply == Kill:
2015-08-29 23:21:58 +00:00
raise Kill()
2015-09-19 09:59:05 +00:00
except (ReplayException, HttpException, TcpException) as e:
self.flow.error = Error(str(e))
2015-08-29 23:21:58 +00:00
if self.channel:
self.channel.ask("error", self.flow)
except Kill:
2015-09-03 15:01:25 +00:00
# Kill should only be raised if there's a channel in the
2015-08-29 23:21:58 +00:00
# first place.
2015-09-03 15:01:25 +00:00
from ..proxy.root_context import Log
2015-08-29 23:21:58 +00:00
self.channel.tell("log", Log("Connection killed", "info"))
except Exception:
from ..proxy.root_context import Log
self.channel.tell("log", Log(traceback.format_exc(), "error"))
2015-08-29 23:21:58 +00:00
finally:
r.form_out = form_out_backup