diff --git a/monkey/infection_monkey/transport/base.py b/monkey/infection_monkey/transport/base.py deleted file mode 100644 index f61f7b115..000000000 --- a/monkey/infection_monkey/transport/base.py +++ /dev/null @@ -1,31 +0,0 @@ -import time -from threading import Thread - -g_last_served = None -PROXY_TIMEOUT = 2.5 - - -class TransportProxyBase(Thread): - def __init__(self, local_port, dest_host=None, dest_port=None, local_host=""): - global g_last_served - - self.local_host = local_host - self.local_port = local_port - self.dest_host = dest_host - self.dest_port = dest_port - self._stopped = False - super(TransportProxyBase, self).__init__() - self.daemon = True - - def stop(self): - self._stopped = True - - -def update_last_serve_time(): - global g_last_served - g_last_served = time.time() - - -def get_last_serve_time(): - global g_last_served - return g_last_served diff --git a/monkey/infection_monkey/transport/http.py b/monkey/infection_monkey/transport/http.py index 46dad8c52..293d86496 100644 --- a/monkey/infection_monkey/transport/http.py +++ b/monkey/infection_monkey/transport/http.py @@ -1,17 +1,9 @@ import http.server -import select -import socket import threading import urllib from logging import getLogger -from urllib.parse import urlsplit from infection_monkey.network.tools import get_interface_to_target -from infection_monkey.transport.base import ( - PROXY_TIMEOUT, - TransportProxyBase, - update_last_serve_time, -) logger = getLogger(__name__) @@ -110,56 +102,6 @@ class FileServHTTPRequestHandler(http.server.BaseHTTPRequestHandler): ) -class HTTPConnectProxyHandler(http.server.BaseHTTPRequestHandler): - timeout = 30 # timeout with clients, set to None not to make persistent connection - - def version_string(self): - return "" - - def do_CONNECT(self): - logger.info("Received a connect request!") - # just provide a tunnel, transfer the data with no modification - req = self - req.path = "https://%s/" % req.path.replace(":443", "") - - u = urlsplit(req.path) - address = (u.hostname, u.port or 443) - try: - conn = socket.create_connection(address) - except socket.error as e: - logger.debug( - "HTTPConnectProxyHandler: Got exception while trying to connect to %s: %s" - % (repr(address), e) - ) - self.send_error(504) # 504 Gateway Timeout - return - self.send_response(200, "Connection Established") - self.send_header("Connection", "close") - self.end_headers() - - conns = [self.connection, conn] - keep_connection = True - while keep_connection: - keep_connection = False - rlist, wlist, xlist = select.select(conns, [], conns, self.timeout) - if xlist: - break - for r in rlist: - other = conns[1] if r is conns[0] else conns[0] - data = r.recv(8192) - if data: - other.sendall(data) - keep_connection = True - update_last_serve_time() - conn.close() - - def log_message(self, format_string, *args): - logger.debug( - "HTTPConnectProxyHandler: %s - [%s] %s" - % (self.address_string(), self.log_date_time_string(), format_string % args) - ) - - class LockedHTTPServer(threading.Thread): """ Same as HTTPServer used for file downloads just with locks to avoid racing conditions. @@ -226,11 +168,3 @@ class LockedHTTPServer(threading.Thread): def stop(self, timeout=STOP_TIMEOUT): self._stopped = True self.join(timeout) - - -class HTTPConnectProxy(TransportProxyBase): - def run(self): - httpd = http.server.HTTPServer((self.local_host, self.local_port), HTTPConnectProxyHandler) - httpd.timeout = PROXY_TIMEOUT - while not self._stopped: - httpd.handle_request() diff --git a/monkey/infection_monkey/transport/tcp.py b/monkey/infection_monkey/transport/tcp.py deleted file mode 100644 index 83c631c3b..000000000 --- a/monkey/infection_monkey/transport/tcp.py +++ /dev/null @@ -1,88 +0,0 @@ -import select -import socket -from logging import getLogger -from threading import Thread - -from infection_monkey.transport.base import ( - PROXY_TIMEOUT, - TransportProxyBase, - update_last_serve_time, -) - -READ_BUFFER_SIZE = 8192 -SOCKET_READ_TIMEOUT = 10 - -logger = getLogger(__name__) - - -class SocketsPipe(Thread): - def __init__(self, source, dest, timeout=SOCKET_READ_TIMEOUT): - Thread.__init__(self) - self.source = source - self.dest = dest - self.timeout = timeout - self._keep_connection = True - super(SocketsPipe, self).__init__() - self.daemon = True - - def run(self): - sockets = [self.source, self.dest] - while self._keep_connection: - self._keep_connection = False - rlist, wlist, xlist = select.select(sockets, [], sockets, self.timeout) - if xlist: - break - for r in rlist: - other = self.dest if r is self.source else self.source - try: - data = r.recv(READ_BUFFER_SIZE) - except Exception: - break - if data: - try: - other.sendall(data) - update_last_serve_time() - except Exception: - break - self._keep_connection = True - - self.source.close() - self.dest.close() - - -class TcpProxy(TransportProxyBase): - def run(self): - pipes = [] - l_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - l_socket.bind((self.local_host, self.local_port)) - l_socket.settimeout(PROXY_TIMEOUT) - l_socket.listen(5) - - while not self._stopped: - try: - source, address = l_socket.accept() - except socket.timeout: - continue - - dest = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - try: - dest.connect((self.dest_host, self.dest_port)) - except socket.error: - source.close() - dest.close() - continue - - pipe = SocketsPipe(source, dest) - pipes.append(pipe) - logger.debug( - "piping sockets %s:%s->%s:%s", - address[0], - address[1], - self.dest_host, - self.dest_port, - ) - pipe.start() - - l_socket.close() - for pipe in pipes: - pipe.join()