2014-03-10 21:36:47 +00:00
|
|
|
from __future__ import absolute_import
|
2014-09-16 23:35:14 +00:00
|
|
|
import select
|
|
|
|
import socket
|
2014-03-10 20:57:50 +00:00
|
|
|
from .primitives import ProtocolHandler
|
2014-08-09 01:03:21 +00:00
|
|
|
from netlib.utils import cleanBin
|
2015-02-05 14:24:32 +00:00
|
|
|
from netlib.tcp import NetLibError
|
2014-01-30 17:56:23 +00:00
|
|
|
|
2014-09-16 23:35:14 +00:00
|
|
|
|
2014-01-30 17:56:23 +00:00
|
|
|
class TCPHandler(ProtocolHandler):
|
|
|
|
"""
|
|
|
|
TCPHandler acts as a generic TCP forwarder.
|
|
|
|
Data will be .log()ed, but not stored any further.
|
|
|
|
"""
|
2014-07-27 00:39:17 +00:00
|
|
|
|
|
|
|
chunk_size = 4096
|
|
|
|
|
2014-10-18 16:29:35 +00:00
|
|
|
def __init__(self, c, log=True):
|
|
|
|
super(TCPHandler, self).__init__(c)
|
|
|
|
self.log = log
|
|
|
|
|
2014-01-30 17:56:23 +00:00
|
|
|
def handle_messages(self):
|
2014-02-17 16:25:45 +00:00
|
|
|
self.c.establish_server_connection()
|
2014-07-27 00:39:17 +00:00
|
|
|
|
|
|
|
server = "%s:%s" % self.c.server_conn.address()[:2]
|
|
|
|
buf = memoryview(bytearray(self.chunk_size))
|
2014-01-30 17:56:23 +00:00
|
|
|
conns = [self.c.client_conn.rfile, self.c.server_conn.rfile]
|
2014-07-27 00:39:17 +00:00
|
|
|
|
2014-09-02 16:13:18 +00:00
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
r, _, _ = select.select(conns, [], [], 10)
|
|
|
|
for rfile in r:
|
|
|
|
if self.c.client_conn.rfile == rfile:
|
|
|
|
src, dst = self.c.client_conn, self.c.server_conn
|
|
|
|
direction = "-> tcp ->"
|
|
|
|
src_str, dst_str = "client", server
|
|
|
|
else:
|
|
|
|
dst, src = self.c.client_conn, self.c.server_conn
|
|
|
|
direction = "<- tcp <-"
|
|
|
|
dst_str, src_str = "client", server
|
2014-07-27 00:39:17 +00:00
|
|
|
|
2014-09-02 16:13:18 +00:00
|
|
|
closed = False
|
|
|
|
if src.ssl_established:
|
|
|
|
# Unfortunately, pyOpenSSL lacks a recv_into function.
|
2014-09-16 23:35:14 +00:00
|
|
|
# We need to read a single byte before .pending()
|
|
|
|
# becomes usable
|
|
|
|
contents = src.rfile.read(1)
|
2014-09-02 16:13:18 +00:00
|
|
|
contents += src.rfile.read(src.connection.pending())
|
|
|
|
if not contents:
|
|
|
|
closed = True
|
2014-01-30 17:56:23 +00:00
|
|
|
else:
|
2014-09-02 16:13:18 +00:00
|
|
|
size = src.connection.recv_into(buf)
|
|
|
|
if not size:
|
|
|
|
closed = True
|
2014-07-27 00:39:17 +00:00
|
|
|
|
2014-09-02 16:13:18 +00:00
|
|
|
if closed:
|
|
|
|
conns.remove(src.rfile)
|
|
|
|
# Shutdown connection to the other peer
|
|
|
|
if dst.ssl_established:
|
|
|
|
dst.connection.shutdown()
|
|
|
|
else:
|
|
|
|
dst.connection.shutdown(socket.SHUT_WR)
|
2014-01-30 17:56:23 +00:00
|
|
|
|
2014-09-02 16:13:18 +00:00
|
|
|
if len(conns) == 0:
|
|
|
|
return
|
|
|
|
continue
|
|
|
|
|
|
|
|
if src.ssl_established or dst.ssl_established:
|
2014-09-16 23:35:14 +00:00
|
|
|
# if one of the peers is over SSL, we need to send
|
|
|
|
# bytes/strings
|
|
|
|
if not src.ssl_established:
|
2014-10-18 16:29:35 +00:00
|
|
|
# we revc'd into buf but need bytes/string now.
|
2014-09-02 16:13:18 +00:00
|
|
|
contents = buf[:size].tobytes()
|
2014-10-18 16:29:35 +00:00
|
|
|
if self.log:
|
|
|
|
self.c.log(
|
|
|
|
"%s %s\r\n%s" % (
|
|
|
|
direction, dst_str, cleanBin(contents)
|
|
|
|
),
|
|
|
|
"info"
|
|
|
|
)
|
2015-02-05 14:24:32 +00:00
|
|
|
# Do not use dst.connection.send here, which may raise OpenSSL-specific errors.
|
|
|
|
dst.send(contents)
|
2014-09-02 16:13:18 +00:00
|
|
|
else:
|
|
|
|
# socket.socket.send supports raw bytearrays/memoryviews
|
2014-10-18 16:29:35 +00:00
|
|
|
if self.log:
|
|
|
|
self.c.log(
|
|
|
|
"%s %s\r\n%s" % (
|
|
|
|
direction, dst_str, cleanBin(buf.tobytes())
|
|
|
|
),
|
|
|
|
"info"
|
|
|
|
)
|
2014-09-02 16:13:18 +00:00
|
|
|
dst.connection.send(buf[:size])
|
2015-02-05 14:24:32 +00:00
|
|
|
except (socket.error, NetLibError) as e:
|
2014-09-03 10:24:14 +00:00
|
|
|
self.c.log("TCP connection closed unexpectedly.", "debug")
|
2014-09-16 23:35:14 +00:00
|
|
|
return
|