Commit a8935079 by Sheng

Made WsockHandler more robust

parent 2c36c386
...@@ -114,7 +114,8 @@ def run_ssh_server(port=2200, running=True): ...@@ -114,7 +114,8 @@ def run_ssh_server(port=2200, running=True):
chan.send('\r\n\r\nWelcome!\r\n\r\n') chan.send('\r\n\r\nWelcome!\r\n\r\n')
if username == 'bar': if username == 'bar':
print(chan.recv(1024)) msg = chan.recv(1024)
chan.send(msg)
chan.close() chan.close()
t.close() t.close()
......
...@@ -136,5 +136,23 @@ class TestApp(AsyncHTTPTestCase): ...@@ -136,5 +136,23 @@ class TestApp(AsyncHTTPTestCase):
ws = yield tornado.websocket.websocket_connect(ws_url) ws = yield tornado.websocket.websocket_connect(ws_url)
msg = yield ws.read_message() msg = yield ws.read_message()
self.assertIn(b'Welcome!', msg) self.assertIn(b'Welcome!', msg)
# message will be ignored silently
yield ws.write_message('hello')
yield ws.write_message('"hello"')
yield ws.write_message('[hello]')
yield ws.write_message(json.dumps({'resize': []}))
yield ws.write_message(json.dumps({'resize': {}}))
yield ws.write_message(json.dumps({'resize': [100]}))
yield ws.write_message(json.dumps({'resize': [100]*10}))
yield ws.write_message(json.dumps({'resize': [-1, -1]}))
yield ws.write_message(json.dumps({'data': [1]}))
yield ws.write_message(json.dumps({'data': (1,)}))
yield ws.write_message(json.dumps({'data': {'a': 2}}))
yield ws.write_message(json.dumps({'data': 1}))
yield ws.write_message(json.dumps({'data': 2.1}))
yield ws.write_message(json.dumps({'key-non-existed': 'hello'}))
yield ws.write_message(json.dumps({'resize': [79, 23], 'data': 'bye'})) yield ws.write_message(json.dumps({'resize': [79, 23], 'data': 'bye'}))
msg = yield ws.read_message()
self.assertEqual(b'bye', msg)
ws.close() ws.close()
...@@ -2,6 +2,7 @@ import io ...@@ -2,6 +2,7 @@ import io
import json import json
import logging import logging
import socket import socket
import struct
import threading import threading
import traceback import traceback
import weakref import weakref
...@@ -9,6 +10,7 @@ import paramiko ...@@ -9,6 +10,7 @@ import paramiko
import tornado.web import tornado.web
from tornado.ioloop import IOLoop from tornado.ioloop import IOLoop
from tornado.util import basestring_type
from webssh.worker import Worker, recycle_worker, workers from webssh.worker import Worker, recycle_worker, workers
try: try:
...@@ -200,15 +202,25 @@ class WsockHandler(MixinHandler, tornado.websocket.WebSocketHandler): ...@@ -200,15 +202,25 @@ class WsockHandler(MixinHandler, tornado.websocket.WebSocketHandler):
def on_message(self, message): def on_message(self, message):
logging.debug('{!r} from {}:{}'.format(message, *self.src_addr)) logging.debug('{!r} from {}:{}'.format(message, *self.src_addr))
worker = self.worker_ref() worker = self.worker_ref()
msg = json.loads(message) try:
msg = json.loads(message)
except ValueError: # py2
return
except json.decoder.JSONDecodeError: # py3
return
if not isinstance(msg, dict):
return
resize = msg.get('resize') resize = msg.get('resize')
if resize: if resize:
try: try:
worker.chan.resize_pty(*resize) worker.chan.resize_pty(*resize)
except paramiko.SSHException: except (TypeError, struct.error, paramiko.SSHException):
pass pass
data = msg.get('data') data = msg.get('data')
if data: if data and isinstance(data, basestring_type):
worker.data_to_dst.append(data) worker.data_to_dst.append(data)
worker.on_write() worker.on_write()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment