webssh

Web based ssh client https://github.com/huashengdun/webssh webssh.huashengdun.org/
git clone http://git.hanabi.in/repos/webssh.git
Log | Files | Refs | README | LICENSE

commit 688ca78bb2100376c9c1b56970c3b81abd2d715f
parent 80bdddc28005ff45f59565b93f69bafab2d14ade
Author: Sheng <webmaster0115@gmail.com>
Date:   Tue, 28 Aug 2018 07:55:41 +0800

Use command "locale charmap" to detect encoding

Diffstat:
Mtests/sshserver.py | 10++++------
Mtests/test_handler.py | 19+------------------
Mwebssh/handler.py | 12++----------
3 files changed, 7 insertions(+), 34 deletions(-)

diff --git a/tests/sshserver.py b/tests/sshserver.py @@ -51,13 +51,12 @@ class Server(paramiko.ServerInterface): b'UWT10hcuO4Ks8=') good_pub_key = paramiko.RSAKey(data=decodebytes(data)) - langs = ['en_US.UTF-8', 'zh_CN.GBK'] + encodings = ['UTF-8', 'GBK'] def __init__(self): self.shell_event = threading.Event() self.exec_event = threading.Event() - self.lang = random.choice(self.langs) - self.encoding = self.lang.split('.')[-1] + self.encoding = random.choice(self.encodings) def check_channel_request(self, kind, chanid): if kind == 'session': @@ -82,12 +81,11 @@ class Server(paramiko.ServerInterface): return 'password,publickey' def check_channel_exec_request(self, channel, command): - if command != b'locale': + if command != b'locale charmap': ret = False else: ret = True - result = 'LANG={lang}\nLANGUAGE=\nLC_CTYPE="{lang}"\n'.format(lang=self.lang) # noqa - channel.send(result) + channel.send(self.encoding) channel.shutdown(1) self.exec_event.set() return ret diff --git a/tests/test_handler.py b/tests/test_handler.py @@ -3,24 +3,7 @@ import paramiko from tornado.httputil import HTTPServerRequest from tests.utils import read_file, make_tests_data_path -from webssh.handler import ( - MixinHandler, IndexHandler, parse_encoding, InvalidValueError -) - - -class TestHandler(unittest.TestCase): - - def test_parse_encoding(self): - data = '' - self.assertIsNone(parse_encoding(data)) - data = 'UTF-8' - self.assertEqual(parse_encoding(data), 'UTF-8') - data = 'en_US.UTF-8' - self.assertEqual(parse_encoding(data), 'UTF-8') - data = 'LANG=en_US.UTF-8\nLANGUAGE=\nLC_CTYPE="en_US.UTF-8"\n' - self.assertEqual(parse_encoding(data), 'UTF-8') - data = 'LANGUAGE=\nLC_CTYPE="en_US.UTF-8"\n' - self.assertEqual(parse_encoding(data), 'UTF-8') +from webssh.handler import MixinHandler, IndexHandler, InvalidValueError class TestMixinHandler(unittest.TestCase): diff --git a/webssh/handler.py b/webssh/handler.py @@ -32,13 +32,6 @@ DELAY = 3 KEY_MAX_SIZE = 16384 -def parse_encoding(data): - for line in data.split('\n'): - s = line.split('=')[-1] - if s: - return s.strip('"').split('.')[-1] - - class InvalidValueError(Exception): pass @@ -176,12 +169,11 @@ class IndexHandler(MixinHandler, tornado.web.RequestHandler): def get_default_encoding(self, ssh): try: - _, stdout, _ = ssh.exec_command('locale') + _, stdout, _ = ssh.exec_command('locale charmap') except paramiko.SSHException: result = None else: - data = stdout.read() - result = parse_encoding(to_str(data)) + result = to_str(stdout.read()) return result if result else 'utf-8'