Replace port in HTTP flow for docker

This commit is contained in:
Julien Duponchelle 2016-05-03 18:01:23 +02:00
parent 5fd385159c
commit f3095d94c1
No known key found for this signature in database
GPG Key ID: CE8B29639E07F5E8
2 changed files with 19 additions and 5 deletions

View File

@ -393,7 +393,13 @@ class DockerVM(BaseVM):
"""
log.debug("Forward HTTP for %s to %d", self.name, self._console_http_port)
command = ["docker", "exec", "-i", self._cid, "/gns3/bin/busybox", "nc", "127.0.0.1", str(self._console_http_port)]
server = AsyncioRawCommandServer(command)
# We replace the port in the server answer otherwise somelink could be broke
server = AsyncioRawCommandServer(command, replaces=[
(
':{}'.format(self._console_http_port).encode(),
':{}'.format(self.console).encode(),
)
])
self._telnet_servers.append((yield from asyncio.start_server(server.run, self._manager.port_manager.console_host, self.console)))
@asyncio.coroutine

View File

@ -24,21 +24,27 @@ log = logging.getLogger(__name__)
READ_SIZE = 4096
class AsyncioRawCommandServer:
"""
Expose a process on the network his stdoud and stdin will be forward
on network
"""
def __init__(self, command):
def __init__(self, command, replaces=[]):
"""
:param command: Command to run
:param replaces: List of tuple to replace in the output ex: [(b":8080", b":6000")]
"""
self._command = command
self._replaces = replaces
@asyncio.coroutine
def run(self, network_reader, network_writer):
process = yield from asyncio.subprocess.create_subprocess_exec(*self._command,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.STDOUT,
stdin=asyncio.subprocess.PIPE)
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.STDOUT,
stdin=asyncio.subprocess.PIPE)
try:
yield from self._process(network_reader, network_writer, process.stdout, process.stdin)
except ConnectionResetError:
@ -73,6 +79,8 @@ class AsyncioRawCommandServer:
reader_read = asyncio.async(process_reader.read(READ_SIZE))
for replace in self._replaces:
data = data.replace(replace[0], replace[1])
network_writer.write(data)
yield from network_writer.drain()