2015-10-14 19:10:05 +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 asyncio
|
|
|
|
import asyncio.subprocess
|
History support for console and telnet application, Fixes: #1083
Arrows UP/DOWN for telnet, Ref. #1083
Typo, Ref. #1083
Support async commands, Ref. #1083
Small refactor, Ref. #1083
Asyncio telnet server - connections support, Ref. #10831
Prompt-toolkit in dependencies, ref. #1083
Few comments, ref. #1083
Direct imports, ref. #1083
Windows size changed support in telnet server, ref. #1139
Fake termios
Fake termios - different approach
InputStream - copied source from prompt_toolkit
2017-07-13 15:36:37 +03:00
|
|
|
import struct
|
2015-10-14 19:10:05 +03:00
|
|
|
|
|
|
|
import logging
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
# Mostly from https://code.google.com/p/miniboa/source/browse/trunk/miniboa/telnet.py
|
|
|
|
|
|
|
|
# Telnet Commands
|
|
|
|
SE = 240 # End of sub-negotiation parameters
|
|
|
|
NOP = 241 # No operation
|
|
|
|
DATMK = 242 # Data stream portion of a sync.
|
|
|
|
BREAK = 243 # NVT Character BRK
|
|
|
|
IP = 244 # Interrupt Process
|
|
|
|
AO = 245 # Abort Output
|
|
|
|
AYT = 246 # Are you there
|
|
|
|
EC = 247 # Erase Character
|
|
|
|
EL = 248 # Erase Line
|
|
|
|
GA = 249 # The Go Ahead Signal
|
|
|
|
SB = 250 # Sub-option to follow
|
|
|
|
WILL = 251 # Will; request or confirm option begin
|
|
|
|
WONT = 252 # Wont; deny option request
|
|
|
|
DO = 253 # Do = Request or confirm remote option
|
|
|
|
DONT = 254 # Don't = Demand or confirm option halt
|
|
|
|
IAC = 255 # Interpret as Command
|
|
|
|
SEND = 1 # Sub-process negotiation SEND command
|
|
|
|
IS = 0 # Sub-process negotiation IS command
|
|
|
|
|
|
|
|
# Telnet Options
|
|
|
|
BINARY = 0 # Transmit Binary
|
|
|
|
ECHO = 1 # Echo characters back to sender
|
|
|
|
RECON = 2 # Reconnection
|
|
|
|
SGA = 3 # Suppress Go-Ahead
|
|
|
|
TMARK = 6 # Timing Mark
|
|
|
|
TTYPE = 24 # Terminal Type
|
|
|
|
NAWS = 31 # Negotiate About Window Size
|
|
|
|
LINEMO = 34 # Line Mode
|
|
|
|
|
|
|
|
READ_SIZE = 1024
|
|
|
|
|
|
|
|
|
History support for console and telnet application, Fixes: #1083
Arrows UP/DOWN for telnet, Ref. #1083
Typo, Ref. #1083
Support async commands, Ref. #1083
Small refactor, Ref. #1083
Asyncio telnet server - connections support, Ref. #10831
Prompt-toolkit in dependencies, ref. #1083
Few comments, ref. #1083
Direct imports, ref. #1083
Windows size changed support in telnet server, ref. #1139
Fake termios
Fake termios - different approach
InputStream - copied source from prompt_toolkit
2017-07-13 15:36:37 +03:00
|
|
|
class TelnetConnection(object):
|
|
|
|
"""Default implementation of telnet connection which may but may not be used."""
|
|
|
|
def __init__(self, reader, writer):
|
|
|
|
self.is_closing = False
|
|
|
|
self._reader = reader
|
|
|
|
self._writer = writer
|
|
|
|
|
|
|
|
@property
|
|
|
|
def reader(self):
|
|
|
|
return self._reader
|
|
|
|
|
|
|
|
@property
|
|
|
|
def writer(self):
|
|
|
|
return self._writer
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def connected(self):
|
History support for console and telnet application, Fixes: #1083
Arrows UP/DOWN for telnet, Ref. #1083
Typo, Ref. #1083
Support async commands, Ref. #1083
Small refactor, Ref. #1083
Asyncio telnet server - connections support, Ref. #10831
Prompt-toolkit in dependencies, ref. #1083
Few comments, ref. #1083
Direct imports, ref. #1083
Windows size changed support in telnet server, ref. #1139
Fake termios
Fake termios - different approach
InputStream - copied source from prompt_toolkit
2017-07-13 15:36:37 +03:00
|
|
|
"""Method called when client is connected"""
|
|
|
|
pass
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def disconnected(self):
|
History support for console and telnet application, Fixes: #1083
Arrows UP/DOWN for telnet, Ref. #1083
Typo, Ref. #1083
Support async commands, Ref. #1083
Small refactor, Ref. #1083
Asyncio telnet server - connections support, Ref. #10831
Prompt-toolkit in dependencies, ref. #1083
Few comments, ref. #1083
Direct imports, ref. #1083
Windows size changed support in telnet server, ref. #1139
Fake termios
Fake termios - different approach
InputStream - copied source from prompt_toolkit
2017-07-13 15:36:37 +03:00
|
|
|
"""Method called when client is disconnecting"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
def window_size_changed(self, columns, rows):
|
|
|
|
"""Method called when window size changed, only can occur when
|
|
|
|
`naws` flag is enable in server configuration."""
|
|
|
|
pass
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def feed(self, data):
|
History support for console and telnet application, Fixes: #1083
Arrows UP/DOWN for telnet, Ref. #1083
Typo, Ref. #1083
Support async commands, Ref. #1083
Small refactor, Ref. #1083
Asyncio telnet server - connections support, Ref. #10831
Prompt-toolkit in dependencies, ref. #1083
Few comments, ref. #1083
Direct imports, ref. #1083
Windows size changed support in telnet server, ref. #1139
Fake termios
Fake termios - different approach
InputStream - copied source from prompt_toolkit
2017-07-13 15:36:37 +03:00
|
|
|
"""
|
|
|
|
Handles incoming data
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
|
|
|
|
def send(self, data):
|
|
|
|
"""
|
|
|
|
Sending data back to client
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
data = data.decode().replace("\n", "\r\n")
|
|
|
|
self.writer.write(data.encode())
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
"""
|
|
|
|
Closes current connection
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
self.is_closing = True
|
|
|
|
|
|
|
|
|
2015-10-14 19:10:05 +03:00
|
|
|
class AsyncioTelnetServer:
|
History support for console and telnet application, Fixes: #1083
Arrows UP/DOWN for telnet, Ref. #1083
Typo, Ref. #1083
Support async commands, Ref. #1083
Small refactor, Ref. #1083
Asyncio telnet server - connections support, Ref. #10831
Prompt-toolkit in dependencies, ref. #1083
Few comments, ref. #1083
Direct imports, ref. #1083
Windows size changed support in telnet server, ref. #1139
Fake termios
Fake termios - different approach
InputStream - copied source from prompt_toolkit
2017-07-13 15:36:37 +03:00
|
|
|
MAX_NEGOTIATION_READ = 10
|
|
|
|
|
|
|
|
def __init__(self, reader=None, writer=None, binary=True, echo=False, naws=False, connection_factory=None):
|
|
|
|
"""
|
|
|
|
Initializes telnet server
|
|
|
|
:param naws when True make a window size negotiation
|
|
|
|
:param connection_factory: when set it's possible to inject own implementation of connection
|
|
|
|
"""
|
|
|
|
assert connection_factory is None or (connection_factory is not None and reader is None and writer is None), \
|
|
|
|
"Please use either reader and writer either connection_factory, otherwise duplicate data may be produced."
|
2015-10-14 19:10:05 +03:00
|
|
|
|
|
|
|
self._reader = reader
|
|
|
|
self._writer = writer
|
History support for console and telnet application, Fixes: #1083
Arrows UP/DOWN for telnet, Ref. #1083
Typo, Ref. #1083
Support async commands, Ref. #1083
Small refactor, Ref. #1083
Asyncio telnet server - connections support, Ref. #10831
Prompt-toolkit in dependencies, ref. #1083
Few comments, ref. #1083
Direct imports, ref. #1083
Windows size changed support in telnet server, ref. #1139
Fake termios
Fake termios - different approach
InputStream - copied source from prompt_toolkit
2017-07-13 15:36:37 +03:00
|
|
|
self._connections = dict()
|
2015-10-14 19:10:05 +03:00
|
|
|
self._lock = asyncio.Lock()
|
|
|
|
self._reader_process = None
|
|
|
|
self._current_read = None
|
|
|
|
|
2016-03-01 15:53:43 +02:00
|
|
|
self._binary = binary
|
|
|
|
# If echo is true when the client send data
|
2016-05-18 12:23:45 +03:00
|
|
|
# the data is echo on his terminal by telnet otherwise
|
2016-03-01 15:53:43 +02:00
|
|
|
# it's our job (or the wrapped app) to send back the data
|
|
|
|
self._echo = echo
|
History support for console and telnet application, Fixes: #1083
Arrows UP/DOWN for telnet, Ref. #1083
Typo, Ref. #1083
Support async commands, Ref. #1083
Small refactor, Ref. #1083
Asyncio telnet server - connections support, Ref. #10831
Prompt-toolkit in dependencies, ref. #1083
Few comments, ref. #1083
Direct imports, ref. #1083
Windows size changed support in telnet server, ref. #1139
Fake termios
Fake termios - different approach
InputStream - copied source from prompt_toolkit
2017-07-13 15:36:37 +03:00
|
|
|
self._naws = naws
|
|
|
|
|
|
|
|
def default_connection_factory(reader, writer):
|
|
|
|
return TelnetConnection(reader, writer)
|
|
|
|
|
|
|
|
if connection_factory is None:
|
|
|
|
connection_factory = default_connection_factory
|
|
|
|
|
|
|
|
self._connection_factory = connection_factory
|
2016-03-01 15:53:43 +02:00
|
|
|
|
2016-11-08 21:10:17 +02:00
|
|
|
@staticmethod
|
2018-10-15 13:05:49 +03:00
|
|
|
async def write_client_intro(writer, echo=False):
|
2016-11-08 21:10:17 +02:00
|
|
|
# Send initial telnet session opening
|
|
|
|
if echo:
|
|
|
|
writer.write(bytes([IAC, WILL, ECHO]))
|
|
|
|
else:
|
|
|
|
writer.write(bytes([
|
|
|
|
IAC, WONT, ECHO,
|
|
|
|
IAC, DONT, ECHO]))
|
2018-10-15 13:05:49 +03:00
|
|
|
await writer.drain()
|
2016-11-08 21:10:17 +02:00
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def _write_intro(self, writer, binary=False, echo=False, naws=False):
|
2016-11-08 21:10:17 +02:00
|
|
|
# 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]))
|
History support for console and telnet application, Fixes: #1083
Arrows UP/DOWN for telnet, Ref. #1083
Typo, Ref. #1083
Support async commands, Ref. #1083
Small refactor, Ref. #1083
Asyncio telnet server - connections support, Ref. #10831
Prompt-toolkit in dependencies, ref. #1083
Few comments, ref. #1083
Direct imports, ref. #1083
Windows size changed support in telnet server, ref. #1139
Fake termios
Fake termios - different approach
InputStream - copied source from prompt_toolkit
2017-07-13 15:36:37 +03:00
|
|
|
|
|
|
|
if naws:
|
|
|
|
writer.write(bytes([
|
|
|
|
IAC, DO, NAWS
|
|
|
|
]))
|
2018-10-15 13:05:49 +03:00
|
|
|
await writer.drain()
|
2016-11-08 21:10:17 +02:00
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def run(self, network_reader, network_writer):
|
2015-10-14 19:10:05 +03:00
|
|
|
# Keep track of connected clients
|
History support for console and telnet application, Fixes: #1083
Arrows UP/DOWN for telnet, Ref. #1083
Typo, Ref. #1083
Support async commands, Ref. #1083
Small refactor, Ref. #1083
Asyncio telnet server - connections support, Ref. #10831
Prompt-toolkit in dependencies, ref. #1083
Few comments, ref. #1083
Direct imports, ref. #1083
Windows size changed support in telnet server, ref. #1139
Fake termios
Fake termios - different approach
InputStream - copied source from prompt_toolkit
2017-07-13 15:36:37 +03:00
|
|
|
connection = self._connection_factory(network_reader, network_writer)
|
|
|
|
self._connections[network_writer] = connection
|
2015-10-14 19:10:05 +03:00
|
|
|
|
|
|
|
try:
|
2018-10-15 13:05:49 +03:00
|
|
|
await self._write_intro(network_writer, echo=self._echo, binary=self._binary, naws=self._naws)
|
|
|
|
await connection.connected()
|
|
|
|
await self._process(network_reader, network_writer, connection)
|
2018-09-06 10:49:12 +03:00
|
|
|
except ConnectionError:
|
2018-10-15 13:05:49 +03:00
|
|
|
async with self._lock:
|
2016-02-23 17:54:12 +02:00
|
|
|
network_writer.close()
|
2015-10-14 19:10:05 +03:00
|
|
|
if self._reader_process == network_reader:
|
|
|
|
self._reader_process = None
|
|
|
|
# Cancel current read from this reader
|
History support for console and telnet application, Fixes: #1083
Arrows UP/DOWN for telnet, Ref. #1083
Typo, Ref. #1083
Support async commands, Ref. #1083
Small refactor, Ref. #1083
Asyncio telnet server - connections support, Ref. #10831
Prompt-toolkit in dependencies, ref. #1083
Few comments, ref. #1083
Direct imports, ref. #1083
Windows size changed support in telnet server, ref. #1139
Fake termios
Fake termios - different approach
InputStream - copied source from prompt_toolkit
2017-07-13 15:36:37 +03:00
|
|
|
if self._current_read is not None:
|
|
|
|
self._current_read.cancel()
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
await connection.disconnected()
|
History support for console and telnet application, Fixes: #1083
Arrows UP/DOWN for telnet, Ref. #1083
Typo, Ref. #1083
Support async commands, Ref. #1083
Small refactor, Ref. #1083
Asyncio telnet server - connections support, Ref. #10831
Prompt-toolkit in dependencies, ref. #1083
Few comments, ref. #1083
Direct imports, ref. #1083
Windows size changed support in telnet server, ref. #1139
Fake termios
Fake termios - different approach
InputStream - copied source from prompt_toolkit
2017-07-13 15:36:37 +03:00
|
|
|
del self._connections[network_writer]
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def close(self):
|
2017-11-20 14:51:07 +02:00
|
|
|
for writer, connection in self._connections.items():
|
2018-08-24 14:57:18 +03:00
|
|
|
try:
|
|
|
|
writer.write_eof()
|
2018-10-15 13:05:49 +03:00
|
|
|
await writer.drain()
|
2018-09-28 16:04:38 +03:00
|
|
|
except (AttributeError, ConnectionError):
|
2018-08-24 14:57:18 +03:00
|
|
|
continue
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def client_connected_hook(self):
|
History support for console and telnet application, Fixes: #1083
Arrows UP/DOWN for telnet, Ref. #1083
Typo, Ref. #1083
Support async commands, Ref. #1083
Small refactor, Ref. #1083
Asyncio telnet server - connections support, Ref. #10831
Prompt-toolkit in dependencies, ref. #1083
Few comments, ref. #1083
Direct imports, ref. #1083
Windows size changed support in telnet server, ref. #1139
Fake termios
Fake termios - different approach
InputStream - copied source from prompt_toolkit
2017-07-13 15:36:37 +03:00
|
|
|
pass
|
2015-10-14 19:10:05 +03:00
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def _get_reader(self, network_reader):
|
2015-10-14 19:10:05 +03:00
|
|
|
"""
|
|
|
|
Get a reader or None if another reader is already reading.
|
|
|
|
"""
|
2018-10-15 13:05:49 +03:00
|
|
|
async with self._lock:
|
2015-10-14 19:10:05 +03:00
|
|
|
if self._reader_process is None:
|
|
|
|
self._reader_process = network_reader
|
History support for console and telnet application, Fixes: #1083
Arrows UP/DOWN for telnet, Ref. #1083
Typo, Ref. #1083
Support async commands, Ref. #1083
Small refactor, Ref. #1083
Asyncio telnet server - connections support, Ref. #10831
Prompt-toolkit in dependencies, ref. #1083
Few comments, ref. #1083
Direct imports, ref. #1083
Windows size changed support in telnet server, ref. #1139
Fake termios
Fake termios - different approach
InputStream - copied source from prompt_toolkit
2017-07-13 15:36:37 +03:00
|
|
|
if self._reader:
|
|
|
|
if self._reader_process == network_reader:
|
2018-10-15 13:05:49 +03:00
|
|
|
self._current_read = asyncio.ensure_future(self._reader.read(READ_SIZE))
|
History support for console and telnet application, Fixes: #1083
Arrows UP/DOWN for telnet, Ref. #1083
Typo, Ref. #1083
Support async commands, Ref. #1083
Small refactor, Ref. #1083
Asyncio telnet server - connections support, Ref. #10831
Prompt-toolkit in dependencies, ref. #1083
Few comments, ref. #1083
Direct imports, ref. #1083
Windows size changed support in telnet server, ref. #1139
Fake termios
Fake termios - different approach
InputStream - copied source from prompt_toolkit
2017-07-13 15:36:37 +03:00
|
|
|
return self._current_read
|
2015-10-14 19:10:05 +03:00
|
|
|
return None
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def _process(self, network_reader, network_writer, connection):
|
|
|
|
network_read = asyncio.ensure_future(network_reader.read(READ_SIZE))
|
|
|
|
reader_read = await self._get_reader(network_reader)
|
2015-10-14 19:10:05 +03:00
|
|
|
|
|
|
|
while True:
|
|
|
|
if reader_read is None:
|
2018-10-15 13:05:49 +03:00
|
|
|
reader_read = await self._get_reader(network_reader)
|
2015-10-14 19:10:05 +03:00
|
|
|
if reader_read is None:
|
2018-10-15 13:05:49 +03:00
|
|
|
done, pending = await asyncio.wait(
|
2015-10-14 19:10:05 +03:00
|
|
|
[
|
|
|
|
network_read,
|
|
|
|
],
|
|
|
|
timeout=1,
|
|
|
|
return_when=asyncio.FIRST_COMPLETED)
|
|
|
|
else:
|
2018-10-15 13:05:49 +03:00
|
|
|
done, pending = await asyncio.wait(
|
2015-10-14 19:10:05 +03:00
|
|
|
[
|
|
|
|
network_read,
|
|
|
|
reader_read
|
|
|
|
],
|
|
|
|
return_when=asyncio.FIRST_COMPLETED)
|
|
|
|
for coro in done:
|
|
|
|
data = coro.result()
|
|
|
|
if coro == network_read:
|
2016-04-07 11:57:24 +03:00
|
|
|
if network_reader.at_eof():
|
|
|
|
raise ConnectionResetError()
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
network_read = asyncio.ensure_future(network_reader.read(READ_SIZE))
|
2016-02-16 18:26:53 +02:00
|
|
|
|
2015-10-14 19:10:05 +03:00
|
|
|
if IAC in data:
|
2018-10-15 13:05:49 +03:00
|
|
|
data = await self._IAC_parser(data, network_reader, network_writer, connection)
|
History support for console and telnet application, Fixes: #1083
Arrows UP/DOWN for telnet, Ref. #1083
Typo, Ref. #1083
Support async commands, Ref. #1083
Small refactor, Ref. #1083
Asyncio telnet server - connections support, Ref. #10831
Prompt-toolkit in dependencies, ref. #1083
Few comments, ref. #1083
Direct imports, ref. #1083
Windows size changed support in telnet server, ref. #1139
Fake termios
Fake termios - different approach
InputStream - copied source from prompt_toolkit
2017-07-13 15:36:37 +03:00
|
|
|
|
2016-03-01 15:53:43 +02:00
|
|
|
if len(data) == 0:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if not self._binary:
|
|
|
|
data = data.replace(b"\r\n", b"\n")
|
|
|
|
|
2015-10-14 19:10:05 +03:00
|
|
|
if self._writer:
|
|
|
|
self._writer.write(data)
|
2018-10-15 13:05:49 +03:00
|
|
|
await self._writer.drain()
|
History support for console and telnet application, Fixes: #1083
Arrows UP/DOWN for telnet, Ref. #1083
Typo, Ref. #1083
Support async commands, Ref. #1083
Small refactor, Ref. #1083
Asyncio telnet server - connections support, Ref. #10831
Prompt-toolkit in dependencies, ref. #1083
Few comments, ref. #1083
Direct imports, ref. #1083
Windows size changed support in telnet server, ref. #1139
Fake termios
Fake termios - different approach
InputStream - copied source from prompt_toolkit
2017-07-13 15:36:37 +03:00
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
await connection.feed(data)
|
History support for console and telnet application, Fixes: #1083
Arrows UP/DOWN for telnet, Ref. #1083
Typo, Ref. #1083
Support async commands, Ref. #1083
Small refactor, Ref. #1083
Asyncio telnet server - connections support, Ref. #10831
Prompt-toolkit in dependencies, ref. #1083
Few comments, ref. #1083
Direct imports, ref. #1083
Windows size changed support in telnet server, ref. #1139
Fake termios
Fake termios - different approach
InputStream - copied source from prompt_toolkit
2017-07-13 15:36:37 +03:00
|
|
|
if connection.is_closing:
|
|
|
|
raise ConnectionResetError()
|
|
|
|
|
2015-10-14 19:10:05 +03:00
|
|
|
elif coro == reader_read:
|
History support for console and telnet application, Fixes: #1083
Arrows UP/DOWN for telnet, Ref. #1083
Typo, Ref. #1083
Support async commands, Ref. #1083
Small refactor, Ref. #1083
Asyncio telnet server - connections support, Ref. #10831
Prompt-toolkit in dependencies, ref. #1083
Few comments, ref. #1083
Direct imports, ref. #1083
Windows size changed support in telnet server, ref. #1139
Fake termios
Fake termios - different approach
InputStream - copied source from prompt_toolkit
2017-07-13 15:36:37 +03:00
|
|
|
if self._reader and self._reader.at_eof():
|
2016-04-07 11:57:24 +03:00
|
|
|
raise ConnectionResetError()
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
reader_read = await self._get_reader(network_reader)
|
2016-03-01 15:53:43 +02:00
|
|
|
|
2015-10-14 19:10:05 +03:00
|
|
|
# Replicate the output on all clients
|
History support for console and telnet application, Fixes: #1083
Arrows UP/DOWN for telnet, Ref. #1083
Typo, Ref. #1083
Support async commands, Ref. #1083
Small refactor, Ref. #1083
Asyncio telnet server - connections support, Ref. #10831
Prompt-toolkit in dependencies, ref. #1083
Few comments, ref. #1083
Direct imports, ref. #1083
Windows size changed support in telnet server, ref. #1139
Fake termios
Fake termios - different approach
InputStream - copied source from prompt_toolkit
2017-07-13 15:36:37 +03:00
|
|
|
for connection in self._connections.values():
|
|
|
|
connection.writer.write(data)
|
2018-10-15 13:05:49 +03:00
|
|
|
await connection.writer.drain()
|
History support for console and telnet application, Fixes: #1083
Arrows UP/DOWN for telnet, Ref. #1083
Typo, Ref. #1083
Support async commands, Ref. #1083
Small refactor, Ref. #1083
Asyncio telnet server - connections support, Ref. #10831
Prompt-toolkit in dependencies, ref. #1083
Few comments, ref. #1083
Direct imports, ref. #1083
Windows size changed support in telnet server, ref. #1139
Fake termios
Fake termios - different approach
InputStream - copied source from prompt_toolkit
2017-07-13 15:36:37 +03:00
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def _read(self, cmd, buffer, location, reader):
|
History support for console and telnet application, Fixes: #1083
Arrows UP/DOWN for telnet, Ref. #1083
Typo, Ref. #1083
Support async commands, Ref. #1083
Small refactor, Ref. #1083
Asyncio telnet server - connections support, Ref. #10831
Prompt-toolkit in dependencies, ref. #1083
Few comments, ref. #1083
Direct imports, ref. #1083
Windows size changed support in telnet server, ref. #1139
Fake termios
Fake termios - different approach
InputStream - copied source from prompt_toolkit
2017-07-13 15:36:37 +03:00
|
|
|
""" Reads next op from the buffer or reader"""
|
|
|
|
try:
|
|
|
|
op = buffer[location]
|
|
|
|
cmd.append(op)
|
|
|
|
return op
|
|
|
|
except IndexError:
|
2018-10-15 13:05:49 +03:00
|
|
|
op = await reader.read(1)
|
History support for console and telnet application, Fixes: #1083
Arrows UP/DOWN for telnet, Ref. #1083
Typo, Ref. #1083
Support async commands, Ref. #1083
Small refactor, Ref. #1083
Asyncio telnet server - connections support, Ref. #10831
Prompt-toolkit in dependencies, ref. #1083
Few comments, ref. #1083
Direct imports, ref. #1083
Windows size changed support in telnet server, ref. #1139
Fake termios
Fake termios - different approach
InputStream - copied source from prompt_toolkit
2017-07-13 15:36:37 +03:00
|
|
|
buffer.extend(op)
|
|
|
|
cmd.append(buffer[location])
|
|
|
|
return op
|
|
|
|
|
|
|
|
def _negotiate(self, data, connection):
|
|
|
|
""" Performs negotiation commands"""
|
|
|
|
|
|
|
|
command, payload = data[0], data[1:]
|
|
|
|
if command == NAWS:
|
|
|
|
if len(payload) == 4:
|
|
|
|
columns, rows = struct.unpack(str('!HH'), bytes(payload))
|
|
|
|
connection.window_size_changed(columns, rows)
|
|
|
|
else:
|
|
|
|
log.warning('Wrong number of NAWS bytes')
|
|
|
|
else:
|
|
|
|
log.debug("Not supported negotiation sequence, received {} bytes", len(data))
|
2015-10-14 19:10:05 +03:00
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
async def _IAC_parser(self, buf, network_reader, network_writer, connection):
|
2015-10-14 19:10:05 +03:00
|
|
|
"""
|
|
|
|
Processes and removes any Telnet commands from the buffer.
|
|
|
|
|
|
|
|
:param buf: buffer
|
|
|
|
:returns: buffer minus Telnet commands
|
|
|
|
"""
|
|
|
|
|
History support for console and telnet application, Fixes: #1083
Arrows UP/DOWN for telnet, Ref. #1083
Typo, Ref. #1083
Support async commands, Ref. #1083
Small refactor, Ref. #1083
Asyncio telnet server - connections support, Ref. #10831
Prompt-toolkit in dependencies, ref. #1083
Few comments, ref. #1083
Direct imports, ref. #1083
Windows size changed support in telnet server, ref. #1139
Fake termios
Fake termios - different approach
InputStream - copied source from prompt_toolkit
2017-07-13 15:36:37 +03:00
|
|
|
|
2015-10-14 19:10:05 +03:00
|
|
|
skip_to = 0
|
|
|
|
while True:
|
|
|
|
# Locate an IAC to process
|
|
|
|
iac_loc = buf.find(IAC, skip_to)
|
|
|
|
if iac_loc < 0:
|
|
|
|
break
|
|
|
|
|
|
|
|
# Get the TELNET command
|
|
|
|
iac_cmd = bytearray([IAC])
|
|
|
|
try:
|
|
|
|
iac_cmd.append(buf[iac_loc + 1])
|
|
|
|
except IndexError:
|
2018-10-15 13:05:49 +03:00
|
|
|
d = await network_reader.read(1)
|
2015-10-14 19:10:05 +03:00
|
|
|
buf.extend(d)
|
|
|
|
iac_cmd.append(buf[iac_loc + 1])
|
|
|
|
|
|
|
|
# Is this just a 2-byte TELNET command?
|
History support for console and telnet application, Fixes: #1083
Arrows UP/DOWN for telnet, Ref. #1083
Typo, Ref. #1083
Support async commands, Ref. #1083
Small refactor, Ref. #1083
Asyncio telnet server - connections support, Ref. #10831
Prompt-toolkit in dependencies, ref. #1083
Few comments, ref. #1083
Direct imports, ref. #1083
Windows size changed support in telnet server, ref. #1139
Fake termios
Fake termios - different approach
InputStream - copied source from prompt_toolkit
2017-07-13 15:36:37 +03:00
|
|
|
if iac_cmd[1] not in [WILL, WONT, DO, DONT, SB]:
|
2015-10-14 19:10:05 +03:00
|
|
|
if iac_cmd[1] == AYT:
|
|
|
|
log.debug("Telnet server received Are-You-There (AYT)")
|
|
|
|
network_writer.write(b'\r\nYour Are-You-There received. I am here.\r\n')
|
|
|
|
elif iac_cmd[1] == IAC:
|
|
|
|
# It's data, not an IAC
|
|
|
|
iac_cmd.pop()
|
|
|
|
# This prevents the 0xff from being
|
|
|
|
# interrupted as yet another IAC
|
|
|
|
skip_to = iac_loc + 1
|
|
|
|
log.debug("Received IAC IAC")
|
|
|
|
elif iac_cmd[1] == NOP:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
log.debug("Unhandled telnet command: "
|
|
|
|
"{0:#x} {1:#x}".format(*iac_cmd))
|
History support for console and telnet application, Fixes: #1083
Arrows UP/DOWN for telnet, Ref. #1083
Typo, Ref. #1083
Support async commands, Ref. #1083
Small refactor, Ref. #1083
Asyncio telnet server - connections support, Ref. #10831
Prompt-toolkit in dependencies, ref. #1083
Few comments, ref. #1083
Direct imports, ref. #1083
Windows size changed support in telnet server, ref. #1139
Fake termios
Fake termios - different approach
InputStream - copied source from prompt_toolkit
2017-07-13 15:36:37 +03:00
|
|
|
elif iac_cmd[1] == SB: # starts negotiation commands
|
|
|
|
negotiation = []
|
|
|
|
for pos in range(2, self.MAX_NEGOTIATION_READ):
|
2018-10-15 13:05:49 +03:00
|
|
|
op = await self._read(iac_cmd, buf, iac_loc + pos, network_reader)
|
History support for console and telnet application, Fixes: #1083
Arrows UP/DOWN for telnet, Ref. #1083
Typo, Ref. #1083
Support async commands, Ref. #1083
Small refactor, Ref. #1083
Asyncio telnet server - connections support, Ref. #10831
Prompt-toolkit in dependencies, ref. #1083
Few comments, ref. #1083
Direct imports, ref. #1083
Windows size changed support in telnet server, ref. #1139
Fake termios
Fake termios - different approach
InputStream - copied source from prompt_toolkit
2017-07-13 15:36:37 +03:00
|
|
|
negotiation.append(op)
|
|
|
|
if op == SE:
|
|
|
|
# ends negotiation commands
|
|
|
|
break
|
|
|
|
|
|
|
|
# SE command is followed by IAC, remove the last two operations from stack
|
|
|
|
self._negotiate(negotiation[0:-2], connection)
|
2015-10-14 19:10:05 +03:00
|
|
|
|
|
|
|
# This must be a 3-byte TELNET command
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
iac_cmd.append(buf[iac_loc + 2])
|
|
|
|
except IndexError:
|
2018-10-15 13:05:49 +03:00
|
|
|
d = await network_reader.read(1)
|
2015-10-14 19:10:05 +03:00
|
|
|
buf.extend(d)
|
|
|
|
iac_cmd.append(buf[iac_loc + 2])
|
|
|
|
# We do ECHO, SGA, and BINARY. Period.
|
2016-03-01 15:53:43 +02:00
|
|
|
if iac_cmd[1] == DO:
|
|
|
|
if iac_cmd[2] not in [ECHO, SGA, BINARY]:
|
|
|
|
network_writer.write(bytes([IAC, WONT, iac_cmd[2]]))
|
|
|
|
log.debug("Telnet WON'T {:#x}".format(iac_cmd[2]))
|
|
|
|
else:
|
|
|
|
if iac_cmd[2] == SGA:
|
|
|
|
if self._binary:
|
|
|
|
network_writer.write(bytes([IAC, WILL, iac_cmd[2]]))
|
|
|
|
else:
|
|
|
|
network_writer.write(bytes([IAC, WONT, iac_cmd[2]]))
|
|
|
|
log.debug("Telnet WON'T {:#x}".format(iac_cmd[2]))
|
|
|
|
|
|
|
|
elif iac_cmd[1] == DONT:
|
|
|
|
log.debug("Unhandled DONT telnet command: "
|
|
|
|
"{0:#x} {1:#x} {2:#x}".format(*iac_cmd))
|
|
|
|
elif iac_cmd[1] == WILL:
|
History support for console and telnet application, Fixes: #1083
Arrows UP/DOWN for telnet, Ref. #1083
Typo, Ref. #1083
Support async commands, Ref. #1083
Small refactor, Ref. #1083
Asyncio telnet server - connections support, Ref. #10831
Prompt-toolkit in dependencies, ref. #1083
Few comments, ref. #1083
Direct imports, ref. #1083
Windows size changed support in telnet server, ref. #1139
Fake termios
Fake termios - different approach
InputStream - copied source from prompt_toolkit
2017-07-13 15:36:37 +03:00
|
|
|
if iac_cmd[2] not in [BINARY, NAWS]:
|
2016-11-07 12:16:51 +02:00
|
|
|
log.debug("Unhandled WILL telnet command: "
|
|
|
|
"{0:#x} {1:#x} {2:#x}".format(*iac_cmd))
|
2016-03-01 15:53:43 +02:00
|
|
|
elif iac_cmd[1] == WONT:
|
|
|
|
log.debug("Unhandled WONT telnet command: "
|
|
|
|
"{0:#x} {1:#x} {2:#x}".format(*iac_cmd))
|
2015-10-14 19:10:05 +03:00
|
|
|
else:
|
|
|
|
log.debug("Unhandled telnet command: "
|
|
|
|
"{0:#x} {1:#x} {2:#x}".format(*iac_cmd))
|
|
|
|
|
|
|
|
# Remove the entire TELNET command from the buffer
|
|
|
|
buf = buf.replace(iac_cmd, b'', 1)
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
await network_writer.drain()
|
2015-10-14 19:10:05 +03:00
|
|
|
|
|
|
|
# Return the new copy of the buffer, minus telnet commands
|
|
|
|
return buf
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2016-03-01 15:53:43 +02:00
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
2015-10-14 19:10:05 +03:00
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
|
2018-10-15 13:05:49 +03:00
|
|
|
process = loop.run_until_complete(asyncio.ensure_future(asyncio.subprocess.create_subprocess_exec("/bin/sh", "-i",
|
2018-08-24 14:57:18 +03:00
|
|
|
stdout=asyncio.subprocess.PIPE,
|
|
|
|
stderr=asyncio.subprocess.STDOUT,
|
|
|
|
stdin=asyncio.subprocess.PIPE)))
|
2016-03-01 15:53:43 +02:00
|
|
|
server = AsyncioTelnetServer(reader=process.stdout, writer=process.stdin, binary=False, echo=False)
|
2015-10-14 19:10:05 +03:00
|
|
|
|
2016-03-01 15:53:43 +02:00
|
|
|
coro = asyncio.start_server(server.run, '127.0.0.1', 4444, loop=loop)
|
2015-10-14 19:10:05 +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()
|