From 69f154d9cc3446d00c2eaad97372414e2e497509 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Tue, 8 Nov 2016 20:10:17 +0100 Subject: [PATCH] Correctly initialize connection to VPCS console --- gns3server/compute/base_node.py | 1 + gns3server/utils/asyncio/telnet_server.py | 57 ++++++++++++++--------- 2 files changed, 37 insertions(+), 21 deletions(-) diff --git a/gns3server/compute/base_node.py b/gns3server/compute/base_node.py index 8921e7c5..27e0c56f 100644 --- a/gns3server/compute/base_node.py +++ b/gns3server/compute/base_node.py @@ -332,6 +332,7 @@ class BaseNode: if not self._wrap_console: return (reader, writer) = yield from asyncio.open_connection(host="127.0.0.1", port=self._internal_console_port) + yield from AsyncioTelnetServer.write_client_intro(writer, echo=True) server = AsyncioTelnetServer(reader=reader, writer=writer, binary=True, echo=True) self._wrapper_telnet_server = yield from asyncio.start_server(server.run, self._manager.port_manager.console_host, self.console) diff --git a/gns3server/utils/asyncio/telnet_server.py b/gns3server/utils/asyncio/telnet_server.py index fd9afe1b..e1ffc61f 100644 --- a/gns3server/utils/asyncio/telnet_server.py +++ b/gns3server/utils/asyncio/telnet_server.py @@ -73,32 +73,48 @@ class AsyncioTelnetServer: # it's our job (or the wrapped app) to send back the data self._echo = echo + @staticmethod + @asyncio.coroutine + def write_client_intro(writer, echo=False): + # Send initial telnet session opening + if echo: + writer.write(bytes([IAC, WILL, ECHO])) + else: + writer.write(bytes([ + IAC, WONT, ECHO, + IAC, DONT, ECHO])) + yield from writer.drain() + + @asyncio.coroutine + def _write_intro(self, writer, binary=False, echo=False): + # Send initial telnet session opening + if echo: + writer.write(bytes([IAC, WILL, ECHO])) + else: + writer.write(bytes([ + IAC, WONT, ECHO, + IAC, DONT, ECHO])) + + if binary: + writer.write(bytes([ + IAC, WILL, SGA, + IAC, WILL, BINARY, + IAC, DO, BINARY])) + else: + writer.write(bytes([ + IAC, WONT, SGA, + IAC, DONT, SGA, + IAC, WONT, BINARY, + IAC, DONT, BINARY])) + yield from writer.drain() + @asyncio.coroutine def run(self, network_reader, network_writer): # Keep track of connected clients self._clients.add(network_writer) try: - # Send initial telnet session opening - if self._echo: - network_writer.write(bytes([IAC, WILL, ECHO])) - else: - network_writer.write(bytes([ - IAC, WONT, ECHO, - IAC, DONT, ECHO])) - - if self._binary: - network_writer.write(bytes([ - IAC, WILL, SGA, - IAC, WILL, BINARY, - IAC, DO, BINARY])) - else: - network_writer.write(bytes([ - IAC, WONT, SGA, - IAC, DONT, SGA, - IAC, WONT, BINARY, - IAC, DONT, BINARY])) - yield from network_writer.drain() + yield from self._write_intro(network_writer, echo=self._echo, binary=self._binary) yield from self._process(network_reader, network_writer) except ConnectionResetError: @@ -149,7 +165,6 @@ class AsyncioTelnetServer: return_when=asyncio.FIRST_COMPLETED) for coro in done: data = coro.result() - if coro == network_read: if network_reader.at_eof(): raise ConnectionResetError()