Skip to content

Commit

Permalink
Support client closing telnet console
Browse files Browse the repository at this point in the history
  • Loading branch information
julien-duponchelle committed Dec 16, 2015
1 parent d3c06ea commit 110a0d3
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 36 deletions.
2 changes: 0 additions & 2 deletions gns3server/handlers/api/docker_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,5 +273,3 @@ def update(request, response):
vm.console = request.json.get("console", vm.console)
vm.start_command = request.json.get("start_command", vm.start_command)
response.json(vm)


3 changes: 3 additions & 0 deletions gns3server/modules/docker/docker_vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ def _start_console(self):
Start streaming the console via telnet
"""
class InputStream:

def __init__(self):
self._data = b""

Expand Down Expand Up @@ -193,6 +194,8 @@ def _read_console_output(self, ws, out):
if msg.tp == aiohttp.MsgType.text:
out.feed_data(msg.data.encode())
else:
out.close()
ws.close()
break

def is_running(self):
Expand Down
66 changes: 34 additions & 32 deletions gns3server/utils/asyncio/telnet_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,39 +64,41 @@ def __init__(self, reader=None, writer=None):
def run(self, network_reader, network_writer):
READ_SIZE = 1024

# Send initial telnet session opening
network_writer.write(bytes([IAC, WILL, ECHO,
IAC, WILL, SGA,
IAC, WILL, BINARY,
IAC, DO, BINARY]))
yield from network_writer.drain()

network_read = asyncio.async(network_reader.read(READ_SIZE))
reader_read = asyncio.async(self._reader.read(READ_SIZE))
try:
# Send initial telnet session opening
network_writer.write(bytes([IAC, WILL, ECHO,
IAC, WILL, SGA,
IAC, WILL, BINARY,
IAC, DO, BINARY]))
yield from network_writer.drain()

while True:
done, pending = yield from asyncio.wait(
[
network_read,
reader_read
],
return_when=asyncio.FIRST_COMPLETED)
for coro in done:
data = coro.result()
if coro == network_read:
network_read = asyncio.async(network_reader.read(READ_SIZE))
# Strip that so we don't get a double prompt.
#data = data.replace(b"\r\n", b"\n")
if IAC in data:
data = yield from self._IAC_parser(data, network_reader, network_writer)
if self._writer:
self._writer.write(data)
yield from self._writer.drain()
elif coro == reader_read:
reader_read = asyncio.async(self._reader.read(READ_SIZE))
network_writer.write(data)
yield from network_writer.drain()
# TODO: manage EOF
network_read = asyncio.async(network_reader.read(READ_SIZE))
reader_read = asyncio.async(self._reader.read(READ_SIZE))

while True:
done, pending = yield from asyncio.wait(
[
network_read,
reader_read
],
return_when=asyncio.FIRST_COMPLETED)
for coro in done:
data = coro.result()
if coro == network_read:
network_read = asyncio.async(network_reader.read(READ_SIZE))
# Strip that so we don't get a double prompt.
#data = data.replace(b"\r\n", b"\n")
if IAC in data:
data = yield from self._IAC_parser(data, network_reader, network_writer)
if self._writer:
self._writer.write(data)
yield from self._writer.drain()
elif coro == reader_read:
reader_read = asyncio.async(self._reader.read(READ_SIZE))
network_writer.write(data)
yield from network_writer.drain()
except ConnectionResetError:
return

def _IAC_parser(self, buf, network_reader, network_writer):
"""
Expand Down
4 changes: 2 additions & 2 deletions tests/handlers/api/test_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ def test_docker_delete_nio(server, vm):

def test_docker_update(server, vm, tmpdir, free_console_port):
response = server.put("/projects/{project_id}/docker/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"name": "test",
"console": free_console_port,
"start_command": "yes"},
"console": free_console_port,
"start_command": "yes"},
example=True)
assert response.status == 200
assert response.json["name"] == "test"
Expand Down

0 comments on commit 110a0d3

Please sign in to comment.