From 4b6bc9081e33f8f108c1c49c722b8e26b9ea9266 Mon Sep 17 00:00:00 2001 From: vapao Date: Thu, 28 Apr 2022 13:54:04 +0800 Subject: [PATCH] =?UTF-8?q?U=20=E4=BC=98=E5=8C=96web=E7=BB=88=E7=AB=AF?= =?UTF-8?q?=E5=AF=B9=E4=B8=AD=E6=96=87=E7=BC=96=E7=A0=81=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spug_api/consumer/consumers.py | 10 +++++++--- spug_web/src/pages/ssh/Terminal.js | 8 +------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/spug_api/consumer/consumers.py b/spug_api/consumer/consumers.py index 6e20b77..3fd2938 100644 --- a/spug_api/consumer/consumers.py +++ b/spug_api/consumer/consumers.py @@ -93,7 +93,11 @@ class SSHConsumer(WebsocketConsumer): if not data: self.close(3333) break - self.send(bytes_data=data) + try: + text = data.decode() + except UnicodeDecodeError: + text = data.decode(encoding='GBK', errors='ignore') + self.send(text_data=text) def receive(self, text_data=None, bytes_data=None): data = text_data or bytes_data @@ -120,7 +124,7 @@ class SSHConsumer(WebsocketConsumer): self.close() def _init(self): - self.send(bytes_data=b'\r\33[KConnecting ...\r') + self.send(text_data='\r\33[KConnecting ...\r') host = Host.objects.filter(pk=self.id).first() if not host: self.send(text_data='Unknown host\r\n') @@ -128,7 +132,7 @@ class SSHConsumer(WebsocketConsumer): try: self.ssh = host.get_ssh().get_client() except Exception as e: - self.send(bytes_data=f'Exception: {e}\r\n'.encode()) + self.send(text_data=f'Exception: {e}\r\n'.encode()) self.close() return self.chan = self.ssh.invoke_shell(term='xterm') diff --git a/spug_web/src/pages/ssh/Terminal.js b/spug_web/src/pages/ssh/Terminal.js index 08935a8..64aee49 100644 --- a/spug_web/src/pages/ssh/Terminal.js +++ b/spug_web/src/pages/ssh/Terminal.js @@ -24,7 +24,7 @@ function WebSSH(props) { term.write('WebSocket connecting ... '); const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'; const socket = new WebSocket(`${protocol}//${window.location.host}/api/ws/ssh/${props.id}/?x-token=${X_TOKEN}`); - socket.onmessage = e => _read_as_text(e.data); + socket.onmessage = e => term.write(e.data) socket.onopen = () => { term.write('ok') term.focus(); @@ -68,12 +68,6 @@ function WebSSH(props) { } } - function _read_as_text(data) { - const reader = new window.FileReader(); - reader.onload = () => term.write(reader.result); - reader.readAsText(data, 'utf-8') - } - return (