diff options
Diffstat (limited to 'libmproxy/protocol/tcp.py')
-rw-r--r-- | libmproxy/protocol/tcp.py | 36 |
1 files changed, 29 insertions, 7 deletions
diff --git a/libmproxy/protocol/tcp.py b/libmproxy/protocol/tcp.py index 990c502a..da0c9087 100644 --- a/libmproxy/protocol/tcp.py +++ b/libmproxy/protocol/tcp.py @@ -1,8 +1,10 @@ from __future__ import absolute_import -import select, socket +import select +import socket from .primitives import ProtocolHandler from netlib.utils import cleanBin + class TCPHandler(ProtocolHandler): """ TCPHandler acts as a generic TCP forwarder. @@ -11,6 +13,10 @@ class TCPHandler(ProtocolHandler): chunk_size = 4096 + def __init__(self, c, log=True): + super(TCPHandler, self).__init__(c) + self.log = log + def handle_messages(self): self.c.establish_server_connection() @@ -34,7 +40,9 @@ class TCPHandler(ProtocolHandler): closed = False if src.ssl_established: # Unfortunately, pyOpenSSL lacks a recv_into function. - contents = src.rfile.read(1) # We need to read a single byte before .pending() becomes usable + # We need to read a single byte before .pending() + # becomes usable + contents = src.rfile.read(1) contents += src.rfile.read(src.connection.pending()) if not contents: closed = True @@ -56,15 +64,29 @@ class TCPHandler(ProtocolHandler): continue if src.ssl_established or dst.ssl_established: - # if one of the peers is over SSL, we need to send bytes/strings - if not src.ssl_established: # only ssl to dst, i.e. we revc'd into buf but need bytes/string now. + # if one of the peers is over SSL, we need to send + # bytes/strings + if not src.ssl_established: + # we revc'd into buf but need bytes/string now. contents = buf[:size].tobytes() - self.c.log("%s %s\r\n%s" % (direction, dst_str, cleanBin(contents)), "debug") + if self.log: + self.c.log( + "%s %s\r\n%s" % ( + direction, dst_str, cleanBin(contents) + ), + "info" + ) dst.connection.send(contents) else: # socket.socket.send supports raw bytearrays/memoryviews - self.c.log("%s %s\r\n%s" % (direction, dst_str, cleanBin(buf.tobytes())), "debug") + if self.log: + self.c.log( + "%s %s\r\n%s" % ( + direction, dst_str, cleanBin(buf.tobytes()) + ), + "info" + ) dst.connection.send(buf[:size]) except socket.error as e: self.c.log("TCP connection closed unexpectedly.", "debug") - return
\ No newline at end of file + return |