gns3-server/gns3server/utils/asyncio/raw_command_server.py

127 lines
4.6 KiB
Python
Raw Normal View History

2016-05-03 17:49:33 +03:00
# -*- coding: utf-8 -*-
#
# Copyright (C) 2014 GNS3 Technologies Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import re
2016-12-09 17:41:15 +02:00
import copy
2016-05-03 17:49:33 +03:00
import asyncio
import asyncio.subprocess
import logging
log = logging.getLogger(__name__)
READ_SIZE = 4096
2016-05-03 19:01:23 +03:00
2016-05-03 17:49:33 +03:00
class AsyncioRawCommandServer:
"""
Expose a process on the network his stdoud and stdin will be forward
on network
"""
2016-05-03 19:01:23 +03:00
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")]
"""
2016-05-03 17:49:33 +03:00
self._command = command
2016-05-03 19:01:23 +03:00
self._replaces = replaces
# We limit number of process
self._lock = asyncio.Semaphore(value=4)
2016-05-03 17:49:33 +03:00
@asyncio.coroutine
def run(self, network_reader, network_writer):
yield from self._lock.acquire()
2016-05-03 17:49:33 +03:00
process = yield from asyncio.subprocess.create_subprocess_exec(*self._command,
2016-05-03 19:01:23 +03:00
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.STDOUT,
stdin=asyncio.subprocess.PIPE)
2016-05-03 17:49:33 +03:00
try:
yield from self._process(network_reader, network_writer, process.stdout, process.stdin)
except ConnectionResetError:
network_writer.close()
if process.returncode is None:
process.kill()
yield from process.wait()
self._lock.release()
2016-05-03 17:49:33 +03:00
@asyncio.coroutine
def _process(self, network_reader, network_writer, process_reader, process_writer):
2016-12-09 17:41:15 +02:00
replaces = []
# Server host from the client point of view
host = network_writer.transport.get_extra_info("sockname")[0]
for replace in self._replaces:
if b'{{HOST}}' in replace[1]:
replaces.append((replace[0], replace[1].replace(b'{{HOST}}', host.encode()), ))
else:
replaces.append((replace[0], replace[1], ))
network_read = asyncio.ensure_future(network_reader.read(READ_SIZE))
reader_read = asyncio.ensure_future(process_reader.read(READ_SIZE))
timeout = 30
2016-05-03 17:49:33 +03:00
while True:
done, pending = yield from asyncio.wait(
[
network_read,
reader_read
],
timeout=timeout,
2016-05-03 17:49:33 +03:00
return_when=asyncio.FIRST_COMPLETED)
if len(done) == 0:
raise ConnectionResetError()
2016-05-03 17:49:33 +03:00
for coro in done:
data = coro.result()
if coro == network_read:
if network_reader.at_eof():
raise ConnectionResetError()
network_read = asyncio.ensure_future(network_reader.read(READ_SIZE))
2016-05-03 17:49:33 +03:00
process_writer.write(data)
yield from process_writer.drain()
elif coro == reader_read:
if process_reader.at_eof():
raise ConnectionResetError()
reader_read = asyncio.ensure_future(process_reader.read(READ_SIZE))
2016-05-03 17:49:33 +03:00
2016-12-09 17:41:15 +02:00
for replace in replaces:
2016-05-03 19:01:23 +03:00
data = data.replace(replace[0], replace[1])
2016-05-10 18:51:40 +03:00
timeout = 2 # We reduce the timeout when the process start to return stuff to avoid problem with server not closing the connection
2016-05-03 17:49:33 +03:00
network_writer.write(data)
yield from network_writer.drain()
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG)
loop = asyncio.get_event_loop()
command = ["nc", "localhost", "80"]
2016-12-09 17:41:15 +02:00
server = AsyncioRawCommandServer(command, replaces=[(b"work", b"{{HOST}}", )])
coro = asyncio.start_server(server.run, '0.0.0.0', 4444, loop=loop)
2016-05-03 17:49:33 +03:00
s = loop.run_until_complete(coro)
try:
loop.run_forever()
except KeyboardInterrupt:
pass
# Close the server
s.close()
loop.run_until_complete(s.wait_closed())
loop.close()