-
Notifications
You must be signed in to change notification settings - Fork 6
/
NJRDPHandler.py
271 lines (240 loc) · 8.51 KB
/
NJRDPHandler.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import asyncio
import base64
from threading import Thread
import threading
import time
import random
import datetime
import zlib
import sys
import tempfile
import os
from PIL import Image
from io import BytesIO
def get_box(pos_x, pos_y, width, height):
return (pos_x, pos_y, pos_x + width, pos_y + height)
class NJRDPHandler(object):
def __init__(self, proto, controller, args):
self.proto = proto
self.controller = controller
self.host = None
self.port = None
self.ver = None
self.image = None
self.delimiter = None
if args["host"]:
self.host = args["host"]
if args["port"]:
self.port = args["port"]
if args["identifier"]:
self.identifier = args["identifier"]
if args["ver"]:
self.ver = args["ver"]
if args["image"]:
self.image = args["image"]
if args["version_string"]:
self.version_string = args["version_string"]
else:
self.version_string = self.ver
if self.ver == "0.6.4" or self.ver == "0.7d":
self.delimiter = "|'|'|"
if self.ver == "0.7dg":
self.delimiter = "|Hassan|"
if args["delimiter"]:
self.delimiter = args["delimiter"]
def close_connection(self):
pass
@asyncio.coroutine
def send_nj_msg(self, data):
yield from self.proto.send_nj_msg(data)
@asyncio.coroutine
def read_nj_msg(self):
data = yield from self.proto.read_nj_msg()
return data
@asyncio.coroutine
def handle_unknown_nj_command(self, msg):
self.controller.output("Unknown command, received", color="error")
try:
self.controller.output(str(msg), color="error")
except:
self.controller.output("Failed to print command", color="error")
self.controller.output(str(msg[0:1]), color="error")
class NJ_064_RDPHandler(NJRDPHandler):
@asyncio.coroutine
def handle_nj_keepalive(self):
yield from self.send_nj_msg(b"P")
@asyncio.coroutine
def get_image(self, size):
im = Image.open(self.image)
im_size = im.size
self.controller.output("Image size: {}, server requested {}% of the image size".format(im_size, size))
w,h = im_size #im sorry, i know this is a mess
size = ( (size * w)/100, (size * h)/100 )
if im_size != size:
im.thumbnail(size, Image.ANTIALIAS)
with BytesIO() as output:
im.save(output, "JPEG")
image = output.getvalue()
return image
def send_nj_sc_pk_msg(self, image):
msg = b""
msg += b"scPK"
msg += self.delimiter.encode()
msg += self.identifier.encode()
msg += self.delimiter.encode()
msg += b"~spl~"
#msg += self.delimiter.encode()
msg += image
yield from self.send_nj_msg(msg)
@asyncio.coroutine
def handle_nj_sc_stop_command(self):
self.controller.output("Received RDP stop command")
@asyncio.coroutine
def send_nj_sc_callin_msg(self):
im = Image.open(self.image)
width, height = im.size
msg = ""
msg += "sc~"
msg += self.delimiter
msg += self.identifier
msg += self.delimiter
msg += str(width)
msg += self.delimiter
msg += str(height)
msg += self.delimiter
msg += str(1) #not sure yet
yield from self.send_nj_msg(msg.encode())
@asyncio.coroutine
def handle_nj_sc_start_command(self, msg):
self.controller.output("Received RDP start command")
image_size = str(msg[1], "utf-8")
self.rip = str(msg[0], "utf-8").split(":")[0]
self.rp = int(str(msg[0], "utf-8").split(":")[1])
self.identifier = "{}:{}".format(self.rip, self.rp)
image = yield from self.get_image(int(image_size))
yield from self.send_nj_sc_pk_msg(image)
@asyncio.coroutine
def handle_nj_msg(self):
data = yield from self.read_nj_msg()
if data is None:
return
if data == b"P":
yield from self.handle_nj_keepalive()
else:
msg = data.split(self.delimiter.encode())
if msg[0] == b"!":
yield from self.handle_nj_sc_start_command(msg[1:])
elif msg[0] == b"!!":
yield from self.handle_nj_sc_stop_command()
else:
yield from self.handle_unknown_nj_command(msg)
@asyncio.coroutine
def run(self):
yield from self.send_nj_sc_callin_msg()
self.controller.output("Sent initial callback message", color="green")
while True:
yield from self.handle_nj_msg()
class NJ_07d_RDPHandler(NJRDPHandler):
@asyncio.coroutine
def send_nj_sc_callin_msg(self):
im = Image.open(self.image)
width, height = im.size
msg = ""
msg += "sc~"
msg += self.delimiter
msg += self.identifier
msg += self.delimiter
msg += str(width)
msg += self.delimiter
msg += str(height)
yield from self.send_nj_msg(msg.encode())
@asyncio.coroutine
def send_nj_sc_pk_msg(self, positions, image):
msg = b""
msg += b"scPK"
msg += self.delimiter.encode()
msg += self.identifier.encode()
msg += self.delimiter.encode()
msg += positions
msg += self.delimiter.encode()
msg += image
yield from self.send_nj_msg(msg)
#cb_msg = []
#cb_msg.append(b"scPK")
#cb_msg.append(self.identifier)
#cb_msg.append(positions)
#cb_msg.append(image)
#msg = self.delimiter.join(cb_msg)
#yield from self.send_nj_msg(msg.encode())
#yield from self.send_nj_msg(msg.encode())
@asyncio.coroutine
def handle_nj_keepalive(self):
yield from self.send_nj_msg(b"")
@asyncio.coroutine
def handle_nj_sc_start_command(self, msg):
self.controller.output("Received RDP start command")
width, height = str(msg[1], "utf-8").split("x")
self.rip = str(msg[0], "utf-8").split(":")[0]
self.rp = int(str(msg[0], "utf-8").split(":")[1])
self.identifier = "{}:{}".format(self.rip, self.rp)
size = (int(width), int(height))
print(size)
positions, image = yield from self.get_image(size)
yield from self.send_nj_sc_pk_msg(positions.encode(), image)
@asyncio.coroutine
def get_image(self, size):
im = Image.open(self.image)
im_size = im.size
print(im_size)
if im_size != size:
im.thumbnail(size, Image.ANTIALIAS)
width, height = im.size
sub_width = int(width/10)
sub_height = int(height/10)
positions = []
positions.append("{},{}".format(width, height))
positions.append(str(sub_height))
im2 = Image.new("RGB", (sub_width, sub_height * 100))
boxes = []
for x in range(0, 10):
for y in range(0, 10):
box = get_box(sub_width * x, sub_height * y, sub_width, sub_height)
positions.append("{},{}".format(sub_width * x, sub_height * y))
boxes.append(box)
x = 0
for box in boxes:
region = im.crop(box)
box2 = get_box(0, sub_height * x, sub_width, sub_height)
im2.paste(region,box2)
x += 1
with BytesIO() as output:
im2.save(output, "JPEG")
image = output.getvalue()
positions = "-".join(positions)
return positions,image
@asyncio.coroutine
def handle_nj_sc_stop_command(self):
self.controller.output("Received RDP stop command")
@asyncio.coroutine
def handle_nj_msg(self):
data = yield from self.read_nj_msg()
if data is None:
return
if data == b"":
yield from self.handle_nj_keepalive()
else:
msg = data.split(self.delimiter.encode())
if msg[0] == b"!":
yield from self.handle_nj_sc_start_command(msg[1:])
elif msg[0] == b"!!":
yield from self.handle_nj_sc_stop_command()
else:
yield from self.handle_unknown_nj_command(msg)
@asyncio.coroutine
def run(self):
yield from self.send_nj_sc_callin_msg()
self.controller.output("Sent initial callback message", color="green")
while True:
yield from self.handle_nj_msg()
class NJ_07dg_RDPHandler(NJ_07d_RDPHandler): #pretty much identical to 0.7d
pass