def test_tuple_to_address_invalid_arg(self): """ Tests the tuple to address conversion when a wrong argument is passed """ with self.assertRaises(ValueError): try: tuple_to_address("WRONG") except ValueError as e: self.assertTrue(str(e).startswith("too many values to unpack")) raise e
def test_tuple_to_address_empty_tuple(self): """ Tests the tuple to address conversion when a tuple with empty values is passed """ with self.assertRaises(ValueError): try: tuple_to_address((None, None)) except ValueError as e: self.assertEqual("invalid argument passed: (None, None)", str(e)) raise e
def handle_stream(self, stream, address): """ Handle a new client connection with a proxy over websocket """ logger.info("Got connection from %s on %s" % (tuple_to_address(stream.socket.getpeername()), tuple_to_address(stream.socket.getsockname()))) self.ws_conn = WebSocketProxyConnection(self.ws_url, stream, address, filters=self.filters, ws_options=self.ws_options) self.ws_conn.connect()
def open(self): """ Open the connection to the service when the WebSocket connection has been established """ logger.info("Forwarding connection to server %s" % tuple_to_address(self.remote_address)) self.io_stream.connect(self.remote_address, self.on_connect)
def test_address_to_tuple(self): """ Tests the address to tuple conversions """ addr = "127.0.0.1:443" t = address_to_tuple(addr) self.assertEqual(t, ("127.0.0.1", 443)) self.assertEqual(addr, tuple_to_address(t))
def test_address_to_tuple_missing_port(self): """ Tests the address to tuple conversions when port is missing """ addr = "127.0.0.1" t = address_to_tuple(addr) self.assertEqual(t, ("127.0.0.1", None)) self.assertEqual(addr, tuple_to_address(t))
def test_address_to_tuple_missing_host(self): """ Tests the address to tuple conversions when host is missing """ addr = 443 t = address_to_tuple(addr) self.assertEqual(t, (None, 443)) self.assertEqual(addr, tuple_to_address(t))
def on_connect(self): """ Callback invoked on connection with mapped service """ logger.info("Connection established with peer at %s" % tuple_to_address(self.remote_address)) for k in range(len(self.filters)): self.filters[k] = copy.deepcopy(self.filters[k]) self.filters[k].startup() self.io_stream.read_until_close(self.on_close, self.on_peer_message)
def on_close(self, *args, **kwargs): """ When web socket gets closed, close the connection to the service too """ logger.info("Closing connection with peer at %s" % tuple_to_address(self.remote_address)) logger.debug("Received args %s and %s", args, kwargs) #if not self.io_stream._closed: for message in args: self.on_peer_message(message) if not self.io_stream.closed(): self.io_stream.close() self.close()
def on_connect(self): """ Callback invoked on connection with mapped service """ logger.info("Connection established with peer at %s" % tuple_to_address(self.remote_address)) self.io_stream.read_until_close(self.on_close, self.on_peer_message)