-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
82 lines (66 loc) · 2.16 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import asyncio
import os
import threading
from abc import ABC
import tornado.httpserver
import tornado.ioloop
import tornado.web
import tornado.websocket
from ssh_server import SSH
class IndexHandler(tornado.web.RequestHandler, ABC):
def get(self):
self.render('index.html')
class WebSocketHandler(tornado.websocket.WebSocketHandler, ABC):
def __init__(self, application, request, **kwargs):
super().__init__(application, request, **kwargs)
self.ssh = None
def check_origin(self, origin):
return True
def _reading(self):
asyncio.set_event_loop(asyncio.new_event_loop())
while True:
try:
data = self.ssh.read()
if data:
res = {'data': data.decode('utf-8')}
self.write_message(res)
except Exception as e:
print(e)
self.write_message({'data': ''})
def open(self):
args = self.request.arguments
host = args["h"][0]
port = int(args["p"][0])
username = args["u"][0]
passwd = args["passwd"][0]
self.ssh = SSH(host, port, username, passwd)
if self.ssh.chan:
t = threading.Thread(target=self._reading)
t.setDaemon(True)
t.start()
else:
self.write_message({'data': 'auth failed'})
def on_message(self, message):
try:
self.ssh.send(message)
except Exception as e:
print(e)
def on_close(self):
print("WebSocket 关闭")
self.ssh.close()
class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r'/', IndexHandler),
(r'/ws', WebSocketHandler)
]
settings = {
"template_path": os.path.join(os.path.dirname(__file__), "templates"),
"static_path": os.path.join(os.path.dirname(__file__), "static"),
}
tornado.web.Application.__init__(self, handlers, **settings)
if __name__ == '__main__':
app = Application()
server = tornado.httpserver.HTTPServer(app)
server.listen(9040)
tornado.ioloop.IOLoop.instance().start()