mitmproxy/libmproxy/protocol/rawtcp.py

89 lines
3.1 KiB
Python
Raw Normal View History

from __future__ import (absolute_import, print_function, division)
2015-08-29 21:08:16 +00:00
import socket
2015-09-10 23:39:33 +00:00
import six
import sys
2015-08-29 21:08:16 +00:00
from OpenSSL import SSL
2015-09-17 00:13:28 +00:00
from netlib.exceptions import TcpException
2015-08-14 08:41:11 +00:00
2015-09-17 00:13:28 +00:00
from netlib.tcp import ssl_read_select
from netlib.utils import clean_bin
2015-08-29 23:21:58 +00:00
from ..exceptions import ProtocolException
2015-08-30 13:27:29 +00:00
from .base import Layer
2015-07-24 16:29:13 +00:00
2016-01-02 15:00:27 +00:00
class TcpMessage(object):
2016-01-27 09:12:18 +00:00
2016-01-02 15:00:27 +00:00
def __init__(self, client_conn, server_conn, sender, receiver, message):
self.client_conn = client_conn
self.server_conn = server_conn
self.sender = sender
self.receiver = receiver
self.message = message
2015-08-30 13:27:29 +00:00
class RawTCPLayer(Layer):
2015-08-29 21:08:16 +00:00
chunk_size = 4096
def __init__(self, ctx, logging=True):
self.logging = logging
2015-08-30 13:27:29 +00:00
super(RawTCPLayer, self).__init__(ctx)
2015-08-29 21:08:16 +00:00
2015-07-24 16:29:13 +00:00
def __call__(self):
2015-08-18 13:59:44 +00:00
self.connect()
2015-08-29 21:08:16 +00:00
buf = memoryview(bytearray(self.chunk_size))
client = self.client_conn.connection
server = self.server_conn.connection
conns = [client, server]
try:
2015-08-29 21:08:16 +00:00
while True:
2015-09-10 09:33:03 +00:00
r = ssl_read_select(conns, 10)
2015-08-29 21:08:16 +00:00
for conn in r:
2015-08-29 23:21:58 +00:00
dst = server if conn == client else client
2015-08-29 21:08:16 +00:00
size = conn.recv_into(buf, self.chunk_size)
if not size:
conns.remove(conn)
# Shutdown connection to the other peer
if isinstance(conn, SSL.Connection):
# We can't half-close a connection, so we just close everything here.
# Sockets will be cleaned up on a higher level.
return
else:
2015-08-29 23:21:58 +00:00
dst.shutdown(socket.SHUT_WR)
2015-08-29 21:08:16 +00:00
if len(conns) == 0:
return
continue
2016-01-02 15:00:27 +00:00
tcp_message = TcpMessage(
self.client_conn, self.server_conn,
self.client_conn if dst == server else self.server_conn,
self.server_conn if dst == server else self.client_conn,
buf[:size].tobytes())
self.channel.ask("tcp_message", tcp_message)
dst.sendall(tcp_message.message)
2015-08-29 21:08:16 +00:00
if self.logging:
# log messages are prepended with the client address,
# hence the "weird" direction string.
if dst == server:
2015-08-29 23:21:58 +00:00
direction = "-> tcp -> {}".format(repr(self.server_conn.address))
2015-08-29 21:08:16 +00:00
else:
2015-08-29 23:21:58 +00:00
direction = "<- tcp <- {}".format(repr(self.server_conn.address))
2016-01-02 15:00:27 +00:00
data = clean_bin(tcp_message.message)
2015-08-29 21:08:16 +00:00
self.log(
"{}\r\n{}".format(direction, data),
"info"
)
2015-07-24 16:29:13 +00:00
2015-09-17 00:13:28 +00:00
except (socket.error, TcpException, SSL.Error) as e:
2015-09-10 23:39:33 +00:00
six.reraise(
ProtocolException,
ProtocolException("TCP connection closed unexpectedly: {}".format(repr(e))),
2015-09-10 23:39:33 +00:00
sys.exc_info()[2]
)