telnetlib3 4.0.3__tar.gz → 4.0.5__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/PKG-INFO +1 -1
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/docs/history.rst +10 -2
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/pyproject.toml +1 -1
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/accessories.py +1 -1
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/client_shell.py +2 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/guard_shells.py +24 -20
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/server.py +12 -4
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/server_shell.py +76 -39
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/stream_reader.py +6 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/stream_writer.py +6 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/tests/test_client_shell.py +3 -2
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/tests/test_client_unit.py +4 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/tests/test_guard_integration.py +108 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/tests/test_relay_server.py +2 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/tests/test_server.py +16 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/tests/test_server_shell_unit.py +377 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/.gitignore +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/CONTRIBUTING.rst +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/LICENSE.txt +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/README.rst +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/docs/Makefile +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/docs/api/accessories.rst +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/docs/api/client.rst +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/docs/api/client_base.rst +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/docs/api/client_shell.rst +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/docs/api/client_shell_win32.rst +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/docs/api/fingerprinting.rst +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/docs/api/guard_shells.rst +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/docs/api/mud.rst +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/docs/api/server.rst +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/docs/api/server_base.rst +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/docs/api/server_pty_shell.rst +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/docs/api/server_shell.rst +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/docs/api/session_context.rst +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/docs/api/slc.rst +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/docs/api/stream_reader.rst +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/docs/api/stream_writer.rst +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/docs/api/sync.rst +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/docs/api/telnetlib.rst +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/docs/api/telopt.rst +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/docs/api.rst +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/docs/conf.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/docs/contributing.rst +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/docs/guidebook.rst +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/docs/index.rst +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/docs/intro.rst +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/docs/make.bat +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/docs/rfcs.rst +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/docs/sphinxext/github.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/requirements-analysis.txt +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/requirements-docs.txt +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/requirements-tests.txt +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/requirements.txt +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/.gitignore +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/__init__.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/_base.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/_paths.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/_session_context.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/_types.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/client.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/client_base.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/client_shell_win32.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/encodings/__init__.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/encodings/atarist.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/encodings/atascii.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/encodings/big5bbs.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/encodings/petscii.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/fingerprinting.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/fingerprinting_display.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/mud.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/py.typed +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/relay_server.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/server_base.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/server_fingerprinting.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/server_pty_shell.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/slc.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/sync.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/telnetlib.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/telopt.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/tests/accessories.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/tests/conftest.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/tests/pty_helper.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/tests/test_accessories.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/tests/test_accessories_extra.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/tests/test_atascii_codec.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/tests/test_benchmarks.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/tests/test_big5bbs_codec.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/tests/test_charset.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/tests/test_client_shell_win32.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/tests/test_core.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/tests/test_encoding.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/tests/test_environ.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/tests/test_fingerprinting.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/tests/test_linemode.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/tests/test_mccp.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/tests/test_mud.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/tests/test_mud_negotiation.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/tests/test_naws.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/tests/test_petscii_codec.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/tests/test_pty_shell.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/tests/test_reader.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/tests/test_server_api.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/tests/test_server_cli.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/tests/test_server_fingerprinting.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/tests/test_server_mud.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/tests/test_shell.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/tests/test_slc.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/tests/test_status_logger.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/tests/test_stream_reader_extra.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/tests/test_stream_writer_full.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/tests/test_sync.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/tests/test_telnetlib.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/tests/test_telnetlib_import.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/tests/test_timeout.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/tests/test_tls.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/tests/test_tspeed.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/tests/test_ttype.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/tests/test_uvloop_integration.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/tests/test_writer.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/telnetlib3/tests/test_xdisploc.py +0 -0
- {telnetlib3-4.0.3 → telnetlib3-4.0.5}/tox.ini +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: telnetlib3
|
|
3
|
-
Version: 4.0.
|
|
3
|
+
Version: 4.0.5
|
|
4
4
|
Summary: Python Telnet server and client CLI and Protocol library
|
|
5
5
|
Project-URL: Homepage, https://github.com/jquast/telnetlib3
|
|
6
6
|
Project-URL: Documentation, https://telnetlib3.readthedocs.io
|
|
@@ -1,11 +1,19 @@
|
|
|
1
1
|
History
|
|
2
2
|
=======
|
|
3
|
+
4.0.5
|
|
4
|
+
* enhancement: ``telnetlib3-client`` client shell now drains stdout.
|
|
5
|
+
|
|
6
|
+
4.0.4
|
|
7
|
+
* bugfix: servers using ``robot_check=True`` with ``encoding=False`` raised ``TypeError: buf
|
|
8
|
+
expected bytes, got <class 'str'>``. ``telnetlib3-server`` now also accepts ``--encoding=False``
|
|
9
|
+
CLI argument. ``latin1`` encoding is used the default server shell and robot check.
|
|
10
|
+
|
|
3
11
|
4.0.3
|
|
4
12
|
* bugfix: long-running servers leaked memory through :class:`~telnetlib3.server.Server`
|
|
5
13
|
``_protocols`` list and ``_new_client`` asyncio.Queue. Both are now bounded
|
|
6
14
|
and regularly pruned.
|
|
7
|
-
* enhancement: ``telnetlib3.
|
|
8
|
-
:ghissue:`139`.
|
|
15
|
+
* enhancement: ``telnetlib3.telnetlib`` now overlays std library module space, ``import
|
|
16
|
+
telnetlib`` :ghissue:`139`.
|
|
9
17
|
* enhancement: ``telnetlib3-fingerprint-server`` and ``telnetlib3-fingerprint`` client now also
|
|
10
18
|
detect "telnet loops" and "wrong direction" errors in opposing IAC parser.
|
|
11
19
|
* removed: ``telnetlib3-fingerprint-server`` no longer integrates with the (never released)
|
|
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "telnetlib3"
|
|
7
|
-
version = "4.0.
|
|
7
|
+
version = "4.0.5" # Keep in sync with telnetlib3/accessories.py::get_version !
|
|
8
8
|
description = " Python Telnet server and client CLI and Protocol library"
|
|
9
9
|
readme = "README.rst"
|
|
10
10
|
license = "ISC"
|
|
@@ -42,7 +42,7 @@ PATIENCE_MESSAGES = [
|
|
|
42
42
|
|
|
43
43
|
def get_version() -> str:
|
|
44
44
|
"""Return the current version of telnetlib3."""
|
|
45
|
-
return "4.0.
|
|
45
|
+
return "4.0.5" # keep in sync with pyproject.toml !
|
|
46
46
|
|
|
47
47
|
|
|
48
48
|
def encoding_from_lang(lang: str) -> Optional[str]:
|
|
@@ -579,6 +579,8 @@ async def _raw_event_loop(
|
|
|
579
579
|
if raw_mode is None and want_repl():
|
|
580
580
|
state.reactivate_repl = True
|
|
581
581
|
stdout.write(out.encode())
|
|
582
|
+
if hasattr(stdout, 'drain'):
|
|
583
|
+
await stdout.drain()
|
|
582
584
|
_ts_file = telnet_writer.ctx.typescript_file
|
|
583
585
|
if _ts_file is not None:
|
|
584
586
|
_ts_file.write(out)
|
|
@@ -18,7 +18,7 @@ from __future__ import annotations
|
|
|
18
18
|
import re
|
|
19
19
|
import asyncio
|
|
20
20
|
import logging
|
|
21
|
-
from typing import Tuple, Union, Optional, Generator
|
|
21
|
+
from typing import Tuple, Union, Optional, Generator
|
|
22
22
|
from contextlib import contextmanager
|
|
23
23
|
|
|
24
24
|
# local
|
|
@@ -64,6 +64,14 @@ def _latin1_reading(
|
|
|
64
64
|
reader._decoder = None
|
|
65
65
|
|
|
66
66
|
|
|
67
|
+
def _writer_write(writer: Union[TelnetWriter, TelnetWriterUnicode], data: str) -> None:
|
|
68
|
+
"""Write string data to writer in the appropriate type (str or bytes)."""
|
|
69
|
+
if writer.is_binary_writer:
|
|
70
|
+
writer.write(data.encode("latin-1"))
|
|
71
|
+
else:
|
|
72
|
+
writer.write(data) # type: ignore[arg-type]
|
|
73
|
+
|
|
74
|
+
|
|
67
75
|
class ConnectionCounter:
|
|
68
76
|
"""Simple shared counter for limiting concurrent connections."""
|
|
69
77
|
|
|
@@ -100,12 +108,13 @@ class ConnectionCounter:
|
|
|
100
108
|
|
|
101
109
|
async def _read_line_inner(reader: Union[TelnetReader, TelnetReaderUnicode], max_len: int) -> str:
|
|
102
110
|
"""Inner loop for _read_line, separated for wait_for compatibility."""
|
|
103
|
-
_reader = cast(TelnetReaderUnicode, reader)
|
|
104
111
|
buf = ""
|
|
105
112
|
while len(buf) < max_len:
|
|
106
|
-
char = await
|
|
113
|
+
char = await reader.read(1)
|
|
107
114
|
if not char:
|
|
108
115
|
break
|
|
116
|
+
if isinstance(char, bytes):
|
|
117
|
+
char = char.decode("latin-1")
|
|
109
118
|
if char in ("\r", "\n"):
|
|
110
119
|
break
|
|
111
120
|
buf += char
|
|
@@ -166,8 +175,7 @@ async def _get_cursor_position(
|
|
|
166
175
|
:returns: (row, col) tuple or (None, None) on timeout/failure.
|
|
167
176
|
"""
|
|
168
177
|
# Send Device Status Report request
|
|
169
|
-
|
|
170
|
-
_writer.write("\x1b[6n")
|
|
178
|
+
_writer_write(writer, "\x1b[6n")
|
|
171
179
|
await writer.drain()
|
|
172
180
|
|
|
173
181
|
# Read response: ESC [ row ; col R
|
|
@@ -189,21 +197,20 @@ async def _measure_width(
|
|
|
189
197
|
|
|
190
198
|
:returns: Width in columns, or None on failure.
|
|
191
199
|
"""
|
|
192
|
-
_writer = cast(TelnetWriterUnicode, writer)
|
|
193
200
|
_, x1 = await _get_cursor_position(reader, writer, timeout)
|
|
194
201
|
if x1 is None:
|
|
195
202
|
return None
|
|
196
203
|
|
|
197
|
-
|
|
198
|
-
await
|
|
204
|
+
_writer_write(writer, text)
|
|
205
|
+
await writer.drain()
|
|
199
206
|
|
|
200
207
|
_, x2 = await _get_cursor_position(reader, writer, timeout)
|
|
201
208
|
if x2 is None:
|
|
202
209
|
return None
|
|
203
210
|
|
|
204
211
|
# Clear the test character
|
|
205
|
-
|
|
206
|
-
await
|
|
212
|
+
_writer_write(writer, f"\x1b[{x1}G" + " " * (x2 - x1) + f"\x1b[{x1}G")
|
|
213
|
+
await writer.drain()
|
|
207
214
|
|
|
208
215
|
return x2 - x1
|
|
209
216
|
|
|
@@ -230,10 +237,9 @@ async def _ask_question(
|
|
|
230
237
|
timeout: float = 10.0,
|
|
231
238
|
) -> Optional[str]:
|
|
232
239
|
"""Ask a question, echoing input and repeating prompt on blank input."""
|
|
233
|
-
_writer = cast(TelnetWriterUnicode, writer)
|
|
234
240
|
while True:
|
|
235
|
-
|
|
236
|
-
await
|
|
241
|
+
_writer_write(writer, prompt)
|
|
242
|
+
await writer.drain()
|
|
237
243
|
|
|
238
244
|
line = await _readline_with_echo(reader, writer, timeout)
|
|
239
245
|
if line is None:
|
|
@@ -242,7 +248,7 @@ async def _ask_question(
|
|
|
242
248
|
if line.strip():
|
|
243
249
|
return line
|
|
244
250
|
# Blank input - repeat prompt
|
|
245
|
-
|
|
251
|
+
_writer_write(writer, "\r\n")
|
|
246
252
|
|
|
247
253
|
|
|
248
254
|
async def robot_shell(
|
|
@@ -254,7 +260,6 @@ async def robot_shell(
|
|
|
254
260
|
|
|
255
261
|
Asks philosophical questions, logs responses, and disconnects.
|
|
256
262
|
"""
|
|
257
|
-
writer = cast(TelnetWriterUnicode, writer)
|
|
258
263
|
peername = writer.get_extra_info("peername")
|
|
259
264
|
logger.info("robot_shell: connection from %s", peername)
|
|
260
265
|
|
|
@@ -275,7 +280,7 @@ async def robot_shell(
|
|
|
275
280
|
return
|
|
276
281
|
answers.append(line2)
|
|
277
282
|
|
|
278
|
-
writer
|
|
283
|
+
_writer_write(writer, "\r\n")
|
|
279
284
|
await writer.drain()
|
|
280
285
|
finally:
|
|
281
286
|
if answers:
|
|
@@ -291,10 +296,9 @@ async def busy_shell(
|
|
|
291
296
|
|
|
292
297
|
Displays busy message, logs any input, and disconnects.
|
|
293
298
|
"""
|
|
294
|
-
writer = cast(TelnetWriterUnicode, writer)
|
|
295
299
|
logger.info("busy_shell: connection from %s (limit reached)", writer.get_extra_info("peername"))
|
|
296
300
|
|
|
297
|
-
writer
|
|
301
|
+
_writer_write(writer, "Machine is busy, do not touch! ")
|
|
298
302
|
await writer.drain()
|
|
299
303
|
|
|
300
304
|
with _latin1_reading(reader):
|
|
@@ -302,12 +306,12 @@ async def busy_shell(
|
|
|
302
306
|
if line1 is not None:
|
|
303
307
|
logger.info("busy_shell: input1=%r", line1)
|
|
304
308
|
|
|
305
|
-
writer
|
|
309
|
+
_writer_write(writer, "\r\nYou hear a distant explosion... ")
|
|
306
310
|
await writer.drain()
|
|
307
311
|
|
|
308
312
|
line2 = await _read_line(reader, timeout=30.0)
|
|
309
313
|
if line2 is not None:
|
|
310
314
|
logger.info("busy_shell: input2=%r", line2)
|
|
311
315
|
|
|
312
|
-
writer
|
|
316
|
+
_writer_write(writer, "\r\n")
|
|
313
317
|
await writer.drain()
|
|
@@ -913,7 +913,7 @@ class LinemodeServer(TelnetServer):
|
|
|
913
913
|
self.writer.iac(DO, LINEMODE)
|
|
914
914
|
|
|
915
915
|
def _negotiate_echo(self) -> None:
|
|
916
|
-
"""Skip ``WILL ECHO
|
|
916
|
+
"""Skip ``WILL ECHO``: LINEMODE EDIT client handles local echo."""
|
|
917
917
|
if self._echo_negotiated:
|
|
918
918
|
return
|
|
919
919
|
self._echo_negotiated = True
|
|
@@ -1297,7 +1297,12 @@ def parse_server_args(
|
|
|
1297
1297
|
default=_config.connect_maxwait,
|
|
1298
1298
|
help="timeout for pending negotiation",
|
|
1299
1299
|
)
|
|
1300
|
-
parser.add_argument(
|
|
1300
|
+
parser.add_argument(
|
|
1301
|
+
"--encoding",
|
|
1302
|
+
default=_config.encoding,
|
|
1303
|
+
type=lambda val: False if val.lower() == "false" else val,
|
|
1304
|
+
help="encoding name, or 'false'/'False' to disable unicode",
|
|
1305
|
+
)
|
|
1301
1306
|
parser.add_argument(
|
|
1302
1307
|
"--force-binary",
|
|
1303
1308
|
action="store_true",
|
|
@@ -1398,9 +1403,12 @@ def parse_server_args(
|
|
|
1398
1403
|
result["pty_raw"] = False
|
|
1399
1404
|
|
|
1400
1405
|
# Auto-enable force_binary for any non-ASCII encoding that uses high-bit bytes.
|
|
1401
|
-
|
|
1402
|
-
if enc_key not in ("us_ascii", "ascii"):
|
|
1406
|
+
if result["encoding"] is False:
|
|
1403
1407
|
result["force_binary"] = True
|
|
1408
|
+
else:
|
|
1409
|
+
enc_key = result["encoding"].lower().replace("-", "_")
|
|
1410
|
+
if enc_key not in ("us_ascii", "ascii"):
|
|
1411
|
+
result["force_binary"] = True
|
|
1404
1412
|
|
|
1405
1413
|
# Build SSLContext from --ssl-certfile / --ssl-keyfile
|
|
1406
1414
|
ssl_certfile = result.pop("ssl_certfile", None)
|
|
@@ -5,7 +5,7 @@ from __future__ import annotations
|
|
|
5
5
|
# std imports
|
|
6
6
|
import types
|
|
7
7
|
import asyncio
|
|
8
|
-
from typing import Union, Optional,
|
|
8
|
+
from typing import Any, Union, Optional, Coroutine, Generator
|
|
9
9
|
|
|
10
10
|
# 3rd party
|
|
11
11
|
from wcwidth import wcswidth as _wcswidth
|
|
@@ -34,36 +34,69 @@ _SEQ_STARTERS = frozenset("[])P_^(")
|
|
|
34
34
|
_SS3 = "O"
|
|
35
35
|
|
|
36
36
|
|
|
37
|
-
|
|
37
|
+
def _write(writer: Union[TelnetWriter, TelnetWriterUnicode], data: str) -> None:
|
|
38
|
+
"""Write string data to writer in the appropriate type (str or bytes)."""
|
|
39
|
+
if writer.is_binary_writer:
|
|
40
|
+
writer.write(data.encode("latin-1"))
|
|
41
|
+
else:
|
|
42
|
+
writer.write(data) # type: ignore[arg-type]
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _echo(writer: Union[TelnetWriter, TelnetWriterUnicode], data: str) -> None:
|
|
46
|
+
"""Echo string data to writer in the appropriate type (str or bytes)."""
|
|
47
|
+
if writer.is_binary_writer:
|
|
48
|
+
writer.echo(data.encode("latin-1"))
|
|
49
|
+
else:
|
|
50
|
+
writer.echo(data) # type: ignore[arg-type]
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
async def filter_ansi(
|
|
54
|
+
reader: Union[TelnetReader, TelnetReaderUnicode],
|
|
55
|
+
_writer: Union[TelnetWriter, TelnetWriterUnicode],
|
|
56
|
+
) -> str:
|
|
38
57
|
"""
|
|
39
58
|
Read and return the next non-ANSI-escape character from reader.
|
|
40
59
|
|
|
41
60
|
When wcwidth is available, handles CSI, OSC, DCS, APC, PM, charset designation, Fe, Fp, and SS3
|
|
42
61
|
sequences. Otherwise falls back to CSI and SS3 only.
|
|
43
62
|
"""
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
63
|
+
binary = reader.is_binary_reader
|
|
64
|
+
|
|
65
|
+
def _rch() -> "Coroutine[Any, Any, Union[str, bytes]]":
|
|
66
|
+
"""Read one character."""
|
|
67
|
+
return reader.read(1)
|
|
68
|
+
|
|
69
|
+
async def _next() -> str:
|
|
70
|
+
ch = await _rch()
|
|
71
|
+
if not ch:
|
|
47
72
|
return ""
|
|
73
|
+
return ch.decode("latin-1") if binary else ch # type: ignore[return-value]
|
|
74
|
+
|
|
75
|
+
char = await _next()
|
|
76
|
+
|
|
77
|
+
while True:
|
|
48
78
|
if char != ESC:
|
|
49
79
|
return char
|
|
50
80
|
|
|
51
|
-
next_char = await
|
|
81
|
+
next_char = await _next()
|
|
52
82
|
if not next_char:
|
|
53
83
|
return ""
|
|
54
84
|
|
|
55
|
-
# SS3: ESC O + one final byte (F1-F4, keypad, app-mode arrows).
|
|
56
|
-
# Handled before wcwidth's ZERO_WIDTH_PATTERN which would match
|
|
57
|
-
# ESC O as a 2-byte Fe sequence, missing the third byte.
|
|
58
85
|
if next_char == _SS3:
|
|
59
|
-
|
|
86
|
+
# SS3: ESC O + one final byte (F1-F4, keypad, app-mode arrows).
|
|
87
|
+
# Handled before wcwidth's ZERO_WIDTH_PATTERN which would match
|
|
88
|
+
# ESC O as a 2-byte Fe sequence, missing the third byte.
|
|
89
|
+
await _rch() # consume SS3 final byte
|
|
90
|
+
char = await _next()
|
|
91
|
+
if not char:
|
|
92
|
+
return ""
|
|
60
93
|
continue
|
|
61
94
|
|
|
62
95
|
buf = ESC + next_char
|
|
63
96
|
if next_char in _SEQ_STARTERS:
|
|
64
97
|
# Multi-byte: CSI, OSC, DCS, APC, PM, or charset
|
|
65
98
|
while len(buf) < 256:
|
|
66
|
-
seq_char = await
|
|
99
|
+
seq_char = await _next()
|
|
67
100
|
if not seq_char:
|
|
68
101
|
break
|
|
69
102
|
buf += seq_char
|
|
@@ -81,6 +114,10 @@ async def filter_ansi(reader: TelnetReaderUnicode, _writer: TelnetWriterUnicode)
|
|
|
81
114
|
if not match:
|
|
82
115
|
return next_char
|
|
83
116
|
|
|
117
|
+
char = await _next()
|
|
118
|
+
if not char:
|
|
119
|
+
return ""
|
|
120
|
+
|
|
84
121
|
|
|
85
122
|
def _backspace_grapheme(command: str) -> tuple[str, str]:
|
|
86
123
|
"""Remove last grapheme cluster, return (new_command, echo_str)."""
|
|
@@ -157,56 +194,53 @@ async def telnet_server_shell(
|
|
|
157
194
|
This shell provides a very simple REPL, allowing introspection and state toggling of the
|
|
158
195
|
connected client session.
|
|
159
196
|
"""
|
|
160
|
-
_reader = cast(TelnetReaderUnicode, reader)
|
|
161
|
-
writer = cast(TelnetWriterUnicode, writer)
|
|
162
|
-
|
|
163
197
|
ssl_obj = writer.get_extra_info("ssl_object")
|
|
164
198
|
if ssl_obj is not None:
|
|
165
199
|
version = ssl_obj.version() or "TLS"
|
|
166
|
-
writer
|
|
200
|
+
_write(writer, f"Ready (secure: {version})." + CR + LF)
|
|
167
201
|
else:
|
|
168
|
-
writer
|
|
202
|
+
_write(writer, "Ready." + CR + LF)
|
|
169
203
|
|
|
170
204
|
command = None
|
|
171
205
|
while not writer.is_closing():
|
|
172
206
|
if command:
|
|
173
|
-
writer
|
|
174
|
-
writer
|
|
207
|
+
_write(writer, CR + LF)
|
|
208
|
+
_write(writer, "tel:sh> ")
|
|
175
209
|
if not getattr(writer.protocol, "never_send_ga", False):
|
|
176
210
|
writer.send_ga()
|
|
177
211
|
await writer.drain()
|
|
178
212
|
|
|
179
|
-
command = await readline_async(
|
|
213
|
+
command = await readline_async(reader, writer)
|
|
180
214
|
if command is None:
|
|
181
215
|
return
|
|
182
|
-
writer
|
|
216
|
+
_write(writer, CR + LF)
|
|
183
217
|
|
|
184
218
|
if command == "quit":
|
|
185
219
|
# server hangs up on client
|
|
186
|
-
writer
|
|
220
|
+
_write(writer, "Goodbye." + CR + LF)
|
|
187
221
|
break
|
|
188
222
|
if command == "help":
|
|
189
|
-
writer
|
|
223
|
+
_write(writer, "quit, writer, slc, linemode, toggle [option|all], reader, proto, dump")
|
|
190
224
|
elif command == "writer":
|
|
191
225
|
# show 'writer' status
|
|
192
|
-
writer
|
|
226
|
+
_write(writer, repr(writer))
|
|
193
227
|
elif command == "reader":
|
|
194
228
|
# show 'reader' status
|
|
195
|
-
writer
|
|
229
|
+
_write(writer, repr(reader))
|
|
196
230
|
elif command == "proto":
|
|
197
231
|
# show 'proto' details of writer
|
|
198
|
-
writer
|
|
232
|
+
_write(writer, repr(writer.protocol))
|
|
199
233
|
elif command == "version":
|
|
200
|
-
writer
|
|
234
|
+
_write(writer, accessories.get_version())
|
|
201
235
|
elif command == "slc":
|
|
202
236
|
# show 'slc' support and data tables
|
|
203
|
-
writer
|
|
237
|
+
_write(writer, get_slcdata(writer))
|
|
204
238
|
elif command == "linemode":
|
|
205
|
-
writer
|
|
239
|
+
_write(writer, get_linemode(writer))
|
|
206
240
|
elif command.startswith("toggle"):
|
|
207
241
|
# toggle specified options
|
|
208
242
|
option = command[len("toggle ") :] or None
|
|
209
|
-
writer
|
|
243
|
+
_write(writer, do_toggle(writer, option))
|
|
210
244
|
elif command.startswith("dump"):
|
|
211
245
|
# dump [kb] [ms_delay] [drain|nodrain] [close|noclose]
|
|
212
246
|
#
|
|
@@ -232,22 +266,22 @@ async def telnet_server_shell(
|
|
|
232
266
|
except IndexError:
|
|
233
267
|
do_close = False
|
|
234
268
|
msg = f"kb_limit={kb_limit}, delay={delay}," f" drain={drain}, do_close={do_close}:\r\n"
|
|
235
|
-
writer
|
|
269
|
+
_write(writer, msg)
|
|
236
270
|
for lineout in character_dump(kb_limit):
|
|
237
271
|
if writer.is_closing():
|
|
238
272
|
break
|
|
239
|
-
writer
|
|
273
|
+
_write(writer, lineout)
|
|
240
274
|
if drain:
|
|
241
275
|
await writer.drain()
|
|
242
276
|
if delay:
|
|
243
277
|
await asyncio.sleep(delay)
|
|
244
278
|
|
|
245
279
|
if not writer.is_closing():
|
|
246
|
-
writer
|
|
280
|
+
_write(writer, f"\r\n{kb_limit} OK")
|
|
247
281
|
if do_close:
|
|
248
282
|
break
|
|
249
283
|
elif command:
|
|
250
|
-
writer
|
|
284
|
+
_write(writer, "no such command.")
|
|
251
285
|
writer.close()
|
|
252
286
|
|
|
253
287
|
|
|
@@ -275,13 +309,15 @@ def readline(
|
|
|
275
309
|
Uses ``_LineEditor`` for grapheme-aware backspace and max_visible_width
|
|
276
310
|
support.
|
|
277
311
|
"""
|
|
278
|
-
_writer = cast(TelnetWriterUnicode, writer)
|
|
279
312
|
editor = _LineEditor(max_visible_width=max_visible_width)
|
|
280
313
|
inp = yield None
|
|
281
314
|
while True:
|
|
282
315
|
echo, cmd = editor.feed(inp)
|
|
283
316
|
if echo:
|
|
284
|
-
|
|
317
|
+
if writer.is_binary_writer:
|
|
318
|
+
writer.echo(echo.encode("latin-1"))
|
|
319
|
+
else:
|
|
320
|
+
writer.echo(echo) # type: ignore[arg-type]
|
|
285
321
|
inp = yield cmd
|
|
286
322
|
|
|
287
323
|
|
|
@@ -296,11 +332,9 @@ async def readline_async(
|
|
|
296
332
|
Uses ``filter_ansi()`` to strip escape sequences and
|
|
297
333
|
``_LineEditor`` for grapheme-aware backspace and max_visible_width support.
|
|
298
334
|
"""
|
|
299
|
-
_reader = cast(TelnetReaderUnicode, reader)
|
|
300
|
-
_writer = cast(TelnetWriterUnicode, writer)
|
|
301
335
|
editor = _LineEditor(max_visible_width=max_visible_width)
|
|
302
336
|
while True:
|
|
303
|
-
next_char = await filter_ansi(
|
|
337
|
+
next_char = await filter_ansi(reader, writer)
|
|
304
338
|
if not next_char:
|
|
305
339
|
return None
|
|
306
340
|
# Skip leading LF/NUL on empty buffer -- accounts for
|
|
@@ -309,7 +343,10 @@ async def readline_async(
|
|
|
309
343
|
continue
|
|
310
344
|
echo, cmd = editor.feed(next_char)
|
|
311
345
|
if echo:
|
|
312
|
-
|
|
346
|
+
if writer.is_binary_writer:
|
|
347
|
+
writer.echo(echo.encode("latin-1"))
|
|
348
|
+
else:
|
|
349
|
+
writer.echo(echo) # type: ignore[arg-type]
|
|
313
350
|
if cmd is not None:
|
|
314
351
|
return cmd
|
|
315
352
|
|
|
@@ -24,6 +24,9 @@ class TelnetReader:
|
|
|
24
24
|
A copy of :class:`asyncio.StreamReader` with telnet-aware readline().
|
|
25
25
|
"""
|
|
26
26
|
|
|
27
|
+
#: Whether this reader returns raw bytes (True) or unicode strings (False).
|
|
28
|
+
is_binary_reader: bool = True
|
|
29
|
+
|
|
27
30
|
_source_traceback = None
|
|
28
31
|
|
|
29
32
|
def __init__(self, limit: int = _DEFAULT_LIMIT) -> None:
|
|
@@ -552,6 +555,9 @@ class TelnetReaderUnicode(TelnetReader):
|
|
|
552
555
|
configurable encoding determined by callback function.
|
|
553
556
|
"""
|
|
554
557
|
|
|
558
|
+
#: Unicode readers return strings, not raw bytes.
|
|
559
|
+
is_binary_reader: bool = False
|
|
560
|
+
|
|
555
561
|
#: Late-binding instance of :class:`codecs.IncrementalDecoder`, some
|
|
556
562
|
#: bytes may be lost if the protocol's encoding is changed after
|
|
557
563
|
#: previously receiving a partial multibyte. This isn't common in
|
|
@@ -116,6 +116,9 @@ class TelnetWriter:
|
|
|
116
116
|
A copy of :class:`asyncio.StreamWriter` with IAC interpretation.
|
|
117
117
|
"""
|
|
118
118
|
|
|
119
|
+
#: Whether this writer expects raw bytes (True) or unicode strings (False).
|
|
120
|
+
is_binary_writer: bool = True
|
|
121
|
+
|
|
119
122
|
#: Total bytes sent to :meth:`~.feed_byte`
|
|
120
123
|
byte_count = 0
|
|
121
124
|
|
|
@@ -3283,6 +3286,9 @@ class TelnetWriterUnicode(TelnetWriter):
|
|
|
3283
3286
|
discovered by ``LANG`` environment variables by NEW_ENVIRON, :rfc:`1572`.
|
|
3284
3287
|
"""
|
|
3285
3288
|
|
|
3289
|
+
#: Unicode writers receive strings, not raw bytes.
|
|
3290
|
+
is_binary_writer: bool = False
|
|
3291
|
+
|
|
3286
3292
|
def __init__(
|
|
3287
3293
|
self,
|
|
3288
3294
|
transport: asyncio.Transport,
|
|
@@ -760,8 +760,7 @@ async def test_raw_event_loop_reactivates_repl() -> None:
|
|
|
760
760
|
term = _make_term(writer)
|
|
761
761
|
term.check_auto_mode = lambda switched_to_raw, last_will_echo: None
|
|
762
762
|
|
|
763
|
-
stdout = mock.Mock()
|
|
764
|
-
stdout.write = mock.Mock()
|
|
763
|
+
stdout = mock.Mock(spec=["write"])
|
|
765
764
|
|
|
766
765
|
close_calls: list[str] = []
|
|
767
766
|
|
|
@@ -823,6 +822,7 @@ async def test_raw_event_loop_typescript_recording() -> None:
|
|
|
823
822
|
|
|
824
823
|
stdout = mock.Mock()
|
|
825
824
|
stdout.write = mock.Mock()
|
|
825
|
+
stdout.drain = mock.AsyncMock()
|
|
826
826
|
|
|
827
827
|
state = _RawLoopState(
|
|
828
828
|
switched_to_raw=True, last_will_echo=False, local_echo=False, linesep="\r\n"
|
|
@@ -839,6 +839,7 @@ async def test_raw_event_loop_typescript_recording() -> None:
|
|
|
839
839
|
want_repl=lambda: False,
|
|
840
840
|
)
|
|
841
841
|
assert "hello world" in ts_buf.getvalue()
|
|
842
|
+
stdout.drain.assert_called_once()
|
|
842
843
|
|
|
843
844
|
|
|
844
845
|
@pytest.mark.asyncio
|
|
@@ -277,6 +277,8 @@ async def test_guard_shells_busy_shell():
|
|
|
277
277
|
from telnetlib3.guard_shells import busy_shell
|
|
278
278
|
|
|
279
279
|
class MockWriter:
|
|
280
|
+
is_binary_writer = False
|
|
281
|
+
|
|
280
282
|
def __init__(self):
|
|
281
283
|
self.output = []
|
|
282
284
|
self._extra = {"peername": ("127.0.0.1", 12345)}
|
|
@@ -307,6 +309,8 @@ async def test_guard_shells_robot_check_timeout():
|
|
|
307
309
|
from telnetlib3.guard_shells import robot_check
|
|
308
310
|
|
|
309
311
|
class MockWriter:
|
|
312
|
+
is_binary_writer = False
|
|
313
|
+
|
|
310
314
|
def __init__(self):
|
|
311
315
|
self.output = []
|
|
312
316
|
self._extra = {"peername": ("127.0.0.1", 12345)}
|