-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.py
214 lines (179 loc) · 6.26 KB
/
proxy.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#! /usr/bin/env python
# -*- coding:utf-8 -*-
import socket
import ssh
import logger
import basesocket
import utils.encoder
PROTO_LOGIN = 1
PROTO_SESSION = 3
PROTO_LOGOUT = 4
PROTO_RESIZE = 5
class Proxy(basesocket.BaseSocket):
WEBSOCKET_OPCODE_TEXT = 0x01
WEBSOCKET_OPCODE_BINARY = 0x02
WEBSOCKET_OPCODE_CLOSE = 0x08
WEBSOCKET_OPECODE_PING = 0x09
WEBSOCKET_OPECODE_PONG = 0x0A
PROTO_MAP = {
PROTO_LOGIN: 'login',
PROTO_SESSION: 'session',
PROTO_RESIZE: 'resize',
PROTO_LOGOUT: 'logout'
}
def __init__(self, sock):
super(Proxy, self).__init__(sock)
self.opcode = 0
self.pong = False
self.remainData = b''
self.sshs = {}
self.encoder = utils.encoder.Encoder()
def close(self, ioLoop):
ioLoop.delEvent(self, ioLoop.E_ALL)
super(Proxy, self).close()
ioLoop.stop()
def onRead(self, ioLoop):
buffer = self.sock.recv(1024)
if not buffer:
self.close(ioLoop)
return
buffer = self.remainData + buffer
result = self.parseData(buffer)
if result is False:
self.close(ioLoop)
return
if self.pong:
self.pong = False
self.sendData(result, self.WEBSOCKET_OPECODE_PONG)
else:
proIdx, data = self.encoder.decode(result)
response = self.do(ioLoop, proIdx, data)
if not response:
return
self.sendData(response)
def sendClose(self):
self.sendData(b'', self.WEBSOCKET_OPCODE_CLOSE)
def sendPong(self, data=b''):
self.sendData(data, self.WEBSOCKET_OPECODE_PONG)
def sendPing(self, data=b''):
self.sendData(data, self.WEBSOCKET_OPECODE_PING)
def parseData(self, data):
header = 0
maskKey = 0
headerLen = 0
payloadLen = 0
payloadData = []
# TODO: 添加FIN位判断
header = data[0]
if ((header & 0x0f) == self.WEBSOCKET_OPCODE_CLOSE):
self.sendClose()
return False
if ((header & 0x0f) == self.WEBSOCKET_OPECODE_PING):
self.pong = True
pay = data[1]
mask = pay & 0x80
if not mask:
return False
payloadLen = pay & 0x7f
headerLen = 2
if payloadLen == 126:
payloadLen = data[2] << 8 | data[3]
headerLen += 2
elif payloadLen == 127:
payloadLen = data[2] << 56 | data[3] << 48 | data[4] << 40 \
| data[5] << 32 | data[6] << 24 | data[7] << 16 | data[8] << 8 | data[9]
headerLen += 8
maskKey = data[headerLen: headerLen + 4]
headerLen += 4
dataLen = len(data)
remainLen = dataLen - headerLen
if remainLen < payloadLen:
remainData = self.sock.recv(payloadLen - remainLen)
data += remainData
data = data[headerLen: headerLen + payloadLen]
for i, d in enumerate(data):
c = d ^ maskKey[i % 4]
payloadData.append(c)
self.opcode = header & 0x0f
self.remainData = data[headerLen + payloadLen:]
return bytes(payloadData)
def sendData(self, data: bytes, opcode=0):
dataLen = len(data)
header = 0x82 | self.opcode
if opcode:
header |= opcode
payloadLen = 0
extendPayloadLens = []
payloadData = data
headerLst = [header]
if dataLen <= 125:
payloadLen = dataLen
elif dataLen <= 65535:
payloadLen = 126
extendPayloadLens.append((dataLen >> 8) & 0xff)
extendPayloadLens.append((dataLen) & 0xff)
elif dataLen <= 2 ** 64 - 1:
payloadLen = 127
extendPayloadLens.append((dataLen >> 56) & 0xff)
extendPayloadLens.append((dataLen >> 48) & 0xff)
extendPayloadLens.append((dataLen >> 40) & 0xff)
extendPayloadLens.append((dataLen >> 32) & 0xff)
extendPayloadLens.append((dataLen >> 24) & 0xff)
extendPayloadLens.append((dataLen >> 16) & 0xff)
extendPayloadLens.append((dataLen >> 8) & 0xff)
extendPayloadLens.append((dataLen) & 0xff)
headerLst.append(payloadLen)
if (extendPayloadLens):
headerLst.extend(extendPayloadLens)
headerData = bytes(headerLst)
result = headerData + payloadData
self.sock.sendall(result)
def do(self, ioLoop, proIdx, data):
funName = self.PROTO_MAP.get(proIdx, None)
if not funName:
logger.getLogger('Proxy').error(
'can\'t found protocol id: %s', proIdx)
return
fun = getattr(self, funName)
return fun(ioLoop, data)
def login(self, ioLoop, data):
termId = data['termId']
oSsh = ssh.Ssh(None, self, data)
ret = oSsh.login()
if ret:
return self.encoder.encode('loginResponse', {'code': -1, 'termId': termId, 'msg': ret})
ioLoop.addEvent(oSsh, ioLoop.E_READ)
self.sshs[termId] = oSsh
return self.encoder.encode('loginResponse', {'code': 0, 'termId': termId})
def session(self, ioLoop, data):
termId = data['termId']
msg = data['msg']
oSsh = self.sshs.get(termId, None)
if not oSsh:
logger.getLogger('Proxy').error(
'session can\'t found. termId: %s', termId)
return
oSsh.session(msg, ioLoop)
def logout(self, ioLoop, data):
termId = data['termId']
oSsh = self.sshs.get(termId, None)
if not oSsh:
logger.getLogger('Proxy').error(
'session can\'t found. termId: %s', termId)
return
oSsh.logout(ioLoop)
self.sshs.pop(termId)
def resize(self, ioLoop, data):
row = data['row']
col = data['col']
for _, oSsh in self.sshs.items():
oSsh.resize(col, row)
# call by ssh
def sshLogout(self, termId, ioLoop):
data = self.encoder.encode('logout', {'termId': termId})
self.sendData(data)
self.sshs.pop(termId)
# call by ssh
def sshSession(self, termId, msg, ioLoop):
data = self.encoder.encode('session', {'termId': termId, 'msg': msg})
self.sendData(data)