U 优化web终端对中文编码支持

pull/494/head
vapao 2022-04-28 13:54:04 +08:00
parent e77cad7492
commit 4b6bc9081e
2 changed files with 8 additions and 10 deletions

View File

@ -93,7 +93,11 @@ class SSHConsumer(WebsocketConsumer):
if not data: if not data:
self.close(3333) self.close(3333)
break 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): def receive(self, text_data=None, bytes_data=None):
data = text_data or bytes_data data = text_data or bytes_data
@ -120,7 +124,7 @@ class SSHConsumer(WebsocketConsumer):
self.close() self.close()
def _init(self): 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() host = Host.objects.filter(pk=self.id).first()
if not host: if not host:
self.send(text_data='Unknown host\r\n') self.send(text_data='Unknown host\r\n')
@ -128,7 +132,7 @@ class SSHConsumer(WebsocketConsumer):
try: try:
self.ssh = host.get_ssh().get_client() self.ssh = host.get_ssh().get_client()
except Exception as e: 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() self.close()
return return
self.chan = self.ssh.invoke_shell(term='xterm') self.chan = self.ssh.invoke_shell(term='xterm')

View File

@ -24,7 +24,7 @@ function WebSSH(props) {
term.write('WebSocket connecting ... '); term.write('WebSocket connecting ... ');
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'; 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}`); 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 = () => { socket.onopen = () => {
term.write('ok') term.write('ok')
term.focus(); 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 ( return (
<div className={styles.termContainer}> <div className={styles.termContainer}>
<div className={styles.terminal} ref={container}/> <div className={styles.terminal} ref={container}/>