-
Notifications
You must be signed in to change notification settings - Fork 221
/
handlers.py
64 lines (47 loc) · 1.61 KB
/
handlers.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
__author__ = 'xsank'
import logging
import tornado.websocket
from daemon import Bridge
from data import ClientData
from utils import check_ip, check_port
class IndexHandler(tornado.web.RequestHandler):
def get(self):
self.render("index.html")
class WSHandler(tornado.websocket.WebSocketHandler):
clients = dict()
def get_client(self):
return self.clients.get(self._id(), None)
def put_client(self):
bridge = Bridge(self)
self.clients[self._id()] = bridge
def remove_client(self):
bridge = self.get_client()
if bridge:
bridge.destroy()
del self.clients[self._id()]
@staticmethod
def _check_init_param(data):
return check_ip(data["host"]) and check_port(data["port"])
@staticmethod
def _is_init_data(data):
return data.get_type() == 'init'
def _id(self):
return id(self)
def open(self):
self.put_client()
def on_message(self, message):
bridge = self.get_client()
client_data = ClientData(message)
if self._is_init_data(client_data):
if self._check_init_param(client_data.data):
bridge.open(client_data.data)
logging.info('connection established from: %s' % self._id())
else:
self.remove_client()
logging.warning('init param invalid: %s' % client_data.data)
else:
if bridge:
bridge.trans_forward(client_data.data)
def on_close(self):
self.remove_client()
logging.info('client close the connection: %s' % self._id())