-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
586 lines (558 loc) · 15 KB
/
client.py
File metadata and controls
586 lines (558 loc) · 15 KB
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
import socket
import struct
import threading
import time
import tkinter
import cv2
import mouse
import mss
import numpy as np
import pyautogui
# for windows
keycodeMappingWin = {
0x08: 'backspace', # VK_BACK
# 0x5B: 'super', # VK_LWIN , 按win按键会出问题,直接屏蔽
0x09: 'tab', # VK_TAB
0x0c: 'clear', # VK_CLEAR
0x0d: 'enter', # VK_RETURN
0x0d: 'return', # VK_RETURN
0x10: 'shift', # VK_SHIFT
0x11: 'ctrl', # VK_CONTROL
0x12: 'alt', # VK_MENU
0x13: 'pause', # VK_PAUSE
0x14: 'capslock', # VK_CAPITAL
0x15: 'kana', # VK_KANA
0x17: 'junja', # VK_JUNJA
0x18: 'final', # VK_FINAL
0x19: 'hanja', # VK_HANJA
0x1b: 'esc', # VK_ESCAPE
0x1c: 'convert', # VK_CONVERT
0x1d: 'nonconvert', # VK_NONCONVERT
0x1e: 'accept', # VK_ACCEPT
0x1f: 'modechange', # VK_MODECHANGE
0x20: 'space', # VK_SPACE
0x21: 'pgup', # VK_PRIOR
0x22: 'pgdn', # VK_NEXT
0x23: 'end', # VK_END
0x24: 'home', # VK_HOME
0x25: 'left', # VK_LEFT
0x26: 'up', # VK_UP
0x27: 'right', # VK_RIGHT
0x28: 'down', # VK_DOWN
0x29: 'select', # VK_SELECT
0x2a: 'print', # VK_PRINT
0x2b: 'execute', # VK_EXECUTE
0x2c: 'prtsc', # VK_SNAPSHOT
0x2d: 'insert', # VK_INSERT
0x2e: 'del', # VK_DELETE
0x2f: 'help', # VK_HELP
0x30: '0',
0x31: '1',
0x32: '2',
0x33: '3',
0x34: '4',
0x35: '5',
0x36: '6',
0x37: '7',
0x38: '8',
0x39: '9',
0x41: 'a',
0x42: 'b',
0x43: 'c',
0x44: 'd',
0x45: 'e',
0x46: 'f',
0x47: 'g',
0x48: 'h',
0x49: 'i',
0x4a: 'j',
0x4b: 'k',
0x4c: 'l',
0x4d: 'm',
0x4e: 'n',
0x4f: 'o',
0x50: 'p',
0x51: 'q',
0x52: 'r',
0x53: 's',
0x54: 't',
0x55: 'u',
0x56: 'v',
0x57: 'w',
0x58: 'x',
0x59: 'y',
0x5a: 'z',
# 0x5b: 'win', # VK_LWIN , 按win按键会出问题,直接屏蔽
# 0x5c: 'winright', # VK_RWIN , 按win按键会出问题,直接屏蔽
0x5d: 'apps', # VK_APPS
0x5f: 'sleep', # VK_SLEEP
0x60: 'num0', # VK_NUMPAD0
0x61: 'num1', # VK_NUMPAD1
0x62: 'num2', # VK_NUMPAD2
0x63: 'num3', # VK_NUMPAD3
0x64: 'num4', # VK_NUMPAD4
0x65: 'num5', # VK_NUMPAD5
0x66: 'num6', # VK_NUMPAD6
0x67: 'num7', # VK_NUMPAD7
0x68: 'num8', # VK_NUMPAD8
0x69: 'num9', # VK_NUMPAD9
0x6a: 'multiply', # VK_MULTIPLY ??? Is this the numpad *?
0x6b: 'add', # VK_ADD ??? Is this the numpad +?
0x6c: 'separator', # VK_SEPARATOR ??? Is this the numpad enter?
0x6d: 'subtract', # VK_SUBTRACT ??? Is this the numpad -?
0x6e: 'decimal', # VK_DECIMAL
0x6f: 'divide', # VK_DIVIDE
0x70: 'f1', # VK_F1
0x71: 'f2', # VK_F2
0x72: 'f3', # VK_F3
0x73: 'f4', # VK_F4
0x74: 'f5', # VK_F5
0x75: 'f6', # VK_F6
0x76: 'f7', # VK_F7
0x77: 'f8', # VK_F8
0x78: 'f9', # VK_F9
0x79: 'f10', # VK_F10
0x7a: 'f11', # VK_F11
0x7b: 'f12', # VK_F12
0x7c: 'f13', # VK_F13
0x7d: 'f14', # VK_F14
0x7e: 'f15', # VK_F15
0x7f: 'f16', # VK_F16
0x80: 'f17', # VK_F17
0x81: 'f18', # VK_F18
0x82: 'f19', # VK_F19
0x83: 'f20', # VK_F20
0x84: 'f21', # VK_F21
0x85: 'f22', # VK_F22
0x86: 'f23', # VK_F23
0x87: 'f24', # VK_F24
0x90: 'numlock', # VK_NUMLOCK
0x91: 'scrolllock', # VK_SCROLL
0xa0: 'shiftleft', # VK_LSHIFT
0xa1: 'shiftright', # VK_RSHIFT
0xa2: 'ctrlleft', # VK_LCONTROL
0xa3: 'ctrlright', # VK_RCONTROL
0xa4: 'altleft', # VK_LMENU
0xa5: 'altright', # VK_RMENU
0xa6: 'browserback', # VK_BROWSER_BACK
0xa7: 'browserforward', # VK_BROWSER_FORWARD
0xa8: 'browserrefresh', # VK_BROWSER_REFRESH
0xa9: 'browserstop', # VK_BROWSER_STOP
0xaa: 'browsersearch', # VK_BROWSER_SEARCH
0xab: 'browserfavorites', # VK_BROWSER_FAVORITES
0xac: 'browserhome', # VK_BROWSER_HOME
0xad: 'volumemute', # VK_VOLUME_MUTE
0xae: 'volumedown', # VK_VOLUME_DOWN
0xaf: 'volumeup', # VK_VOLUME_UP
0xb0: 'nexttrack', # VK_MEDIA_NEXT_TRACK
0xb1: 'prevtrack', # VK_MEDIA_PREV_TRACK
0xb2: 'stop', # VK_MEDIA_STOP
0xb3: 'playpause', # VK_MEDIA_PLAY_PAUSE
0xb4: 'launchmail', # VK_LAUNCH_MAIL
0xb5: 'launchmediaselect', # VK_LAUNCH_MEDIA_SELECT
0xb6: 'launchapp1', # VK_LAUNCH_APP1
0xb7: 'launchapp2', # VK_LAUNCH_APP2
0xba: ";",
0xbb: "=",
0xbc: ",",
0xbd: "-",
0xbe: ".",
0xbf: "/",
0xc0: "`",
0xdb: "[",
0xdc: "\\",
0xdd: "]",
0xde: "'",
}
# for linux
keycodeMappingX11 = {
48: "'",
59: ',',
20: '-',
60: '.',
61: '/',
19: '0',
10: '1',
11: '2',
12: '3',
13: '4',
14: '5',
15: '6',
16: '7',
17: '8',
18: '9',
47: ';',
21: '=',
34: '[',
51: '\\',
35: ']',
49: '`',
38: 'a',
56: 'b',
54: 'c',
40: 'd',
26: 'e',
41: 'f',
42: 'g',
43: 'h',
31: 'i',
44: 'j',
45: 'k',
46: 'l',
58: 'm',
57: 'n',
32: 'o',
33: 'p',
24: 'q',
27: 'r',
39: 's',
28: 't',
30: 'u',
55: 'v',
25: 'w',
53: 'x',
29: 'y',
52: 'z',
86: 'add',
64: 'altleft',
108: 'altright',
135: 'apps',
22: 'backspace',
66: 'capslock',
37: 'ctrlleft',
105: 'ctrlright',
129: 'decimal',
119: 'del',
106: 'divide',
116: 'down',
115: 'end',
36: 'enter',
9: 'esc',
67: 'f1',
76: 'f10',
95: 'f11',
96: 'f12',
68: 'f2',
69: 'f3',
70: 'f4',
71: 'f5',
72: 'f6',
73: 'f7',
74: 'f8',
75: 'f9',
146: 'help',
110: 'home',
118: 'insert',
113: 'left',
63: 'multiply',
90: 'num0',
87: 'num1',
88: 'num2',
89: 'num3',
83: 'num4',
84: 'num5',
85: 'num6',
79: 'num7',
80: 'num8',
81: 'num9',
77: 'numlock',
117: 'pagedown',
112: 'pageup',
127: 'pause',
107: 'prtscr',
114: 'right',
78: 'scrolllock',
50: 'shiftleft',
62: 'shiftright',
65: 'space',
82: 'subtract',
23: 'tab',
111: 'up',
133: 'winleft',
134: 'winright',
}
# for mac
keycodeMappingOsx = {
39: "'",
43: ',',
27: '-',
47: '.',
44: '/',
29: '0',
18: '1',
19: '2',
20: '3',
21: '4',
23: '5',
22: '6',
26: '7',
28: '8',
25: '9',
41: ';',
24: '=',
50: '`',
0: 'a',
11: 'b',
8: 'c',
2: 'd',
14: 'e',
3: 'f',
5: 'g',
4: 'h',
34: 'i',
38: 'j',
40: 'k',
37: 'l',
46: 'm',
45: 'n',
31: 'o',
35: 'p',
12: 'q',
15: 'r',
1: 's',
17: 't',
32: 'u',
9: 'v',
13: 'w',
7: 'x',
16: 'y',
6: 'z',
58: 'altleft',
51: 'backspace',
57: 'capslock',
59: 'ctrlleft',
62: 'ctrlright',
117: 'del',
125: 'down',
119: 'end',
36: 'enter',
53: 'esc',
122: 'f1',
109: 'f10',
103: 'f11',
111: 'f12',
105: 'f13',
107: 'f14',
113: 'f15',
106: 'f16',
64: 'f17',
79: 'f18',
80: 'f19',
120: 'f2',
90: 'f20',
99: 'f3',
118: 'f4',
96: 'f5',
97: 'f6',
98: 'f7',
100: 'f8',
101: 'f9',
63: 'fn',
114: 'help',
115: 'home',
104: 'kana',
123: 'left',
121: 'pagedown',
116: 'pageup',
124: 'right',
56: 'shiftleft',
60: 'shiftright',
49: 'space',
48: 'tab',
126: 'up',
73: 'volumedown',
74: 'volumemute',
72: 'volumeup',
93: 'yen',
55: 'command',
61: 'optionright',
102: 'eisu'
}
def getKeycodeMapping(plat):
if plat == b'win':
return keycodeMappingWin
elif plat == b'x11':
return keycodeMappingX11
elif plat == b'osx':
return keycodeMappingOsx
else:
return {}
class RemoteDesktopClient:
def __init__(self, ui):
super().__init__()
# UI
self.root = ui
# socket 连接
self.sock = None
self.sock_connected = False
self.check_interval = 10
# 线程锁
self.lock = threading.Lock()
# 压缩后np图像
self.img = None
# 编码后的图像
self.imbyt = None
# 压缩比 1-100 数值越小,压缩比越高,图片质量损失越严重
self.IMQUALITY = 100
# 画面周期
self.IDLE = 0.01
# 设置窗口标题
self.root.title("远程桌面被控端")
# 设置窗口大小,在屏幕居中显示
win_width, win_height = 350, 230
win_x = (self.root.winfo_screenwidth() - win_width) // 2
win_y = (self.root.winfo_screenheight() - win_height) // 2
self.root.geometry(f'{win_width}x{win_height}+{win_x}+{win_y}')
# 禁止调整窗口大小
self.root.resizable(False, False)
# 绑定关闭窗口事件
self.root.protocol("WM_DELETE_WINDOW", self.on_close)
# 窗口组件
tkinter.Label(self.root, text="目标地址:", ).grid(row=0, column=0, sticky=tkinter.E)
self.server_ip = tkinter.Entry(self.root, textvariable=tkinter.StringVar(value='127.0.0.1'))
self.server_ip.grid(row=0, column=1, sticky=tkinter.W)
tkinter.Label(self.root, text="目标端口:").grid(row=1, column=0, sticky=tkinter.E)
self.server_port = tkinter.Entry(self.root, textvariable=tkinter.StringVar(value='54321'))
self.server_port.grid(row=1, column=1, sticky=tkinter.W)
self.connect_button = tkinter.Button(self.root, text="启动连接", width=10, command=self.connect)
self.connect_button.grid(row=2, column=0, sticky=tkinter.E)
self.hide_button = tkinter.Button(self.root, text="隐藏窗口", width=10, command=self.hide_window)
self.hide_button.grid(row=2, column=1, sticky=tkinter.E)
# 设置行和列的权重,使组件居中
self.root.columnconfigure(0, weight=1)
self.root.rowconfigure(0, weight=1)
self.root.columnconfigure(1, weight=1)
self.root.rowconfigure(1, weight=1)
self.root.columnconfigure(2, weight=1)
self.root.rowconfigure(2, weight=1)
# 定时检查连接,断开就重连
threading.Thread(target=self.check_connect, daemon=True).start()
def check_connect(self):
while True:
time.sleep(self.check_interval)
try:
if self.sock:
# 尝试发送一个空字节来检查连接
self.sock.sendall(b'')
except Exception as e:
# 关闭原套接字
self.disconnect()
# 连接
self.connect()
print(e)
def connect(self):
try:
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ip = self.server_ip.get()
port = int(self.server_port.get())
self.sock.connect((ip, port))
self.sock_connected = True
self.connect_button['text'] = '连接成功!'
self.connect_button.config(state=tkinter.DISABLED)
# 发送屏幕画面的线程
threading.Thread(target=self.send_screen, daemon=True).start()
# 处理远程控制的线程
threading.Thread(target=self.receive_control, daemon=True).start()
except Exception as e:
self.sock_connected = False
self.connect_button['text'] = '连接超时!'
self.connect_button.config(state=tkinter.NORMAL)
print(e)
def disconnect(self):
if self.sock:
self.sock.close()
self.sock = None
self.sock_connected = False
def send_screen(self):
while True:
with mss.mss() as sct:
# 显示器尺寸信息列表:索引 0 是所有显示器组合的尺寸,索引 1 是主显示器尺寸,以此类推
monitors = sct.monitors[:]
screenshot = sct.grab(monitors[1])
# 压缩图像,通过jpg格式
# 将mss的ScreenShot对象转换为numpy数组
img = np.array(screenshot)
# 图像编码
_, buffer = cv2.imencode('.png', img)
# 将压缩后的图像数据转换回numpy数组
compressed_img = np.asarray(buffer, np.uint8)
# 序列化图像数据
message = struct.pack("Q", len(compressed_img)) + compressed_img.astype(np.uint8).tobytes()
self.sock.sendall(message)
time.sleep(self.IDLE)
def receive_control(self):
# 鼠标滚轮灵敏度
SCROLL_NUM = 50
# 按键映射
keycodeMapping = {}
def Op(key, op, ox, oy):
# print(key, op, ox, oy)
# 退出程序指令:(0, 0, ...)
if key == 0:
if op == 0:
self.on_close()
# 鼠标移动指令:(4, ...)
elif key == 4:
# 鼠标移动
mouse.move(ox, oy)
elif key == 1:
if op == 100:
# 左键按下
pyautogui.mouseDown(button=pyautogui.LEFT)
elif op == 117:
# 左键弹起
pyautogui.mouseUp(button=pyautogui.LEFT)
elif key == 2:
# 滚轮事件
if op == 0:
# 向上
pyautogui.scroll(-SCROLL_NUM)
else:
# 向下
pyautogui.scroll(SCROLL_NUM)
elif key == 3:
# 鼠标右键
if op == 100:
# 右键按下
pyautogui.mouseDown(button=pyautogui.RIGHT)
elif op == 117:
# 右键弹起
pyautogui.mouseUp(button=pyautogui.RIGHT)
else:
k = keycodeMapping.get(key)
if k is not None:
if op == 100:
pyautogui.keyDown(k)
elif op == 117:
pyautogui.keyUp(k)
try:
plat = b''
while True:
plat += self.sock.recv(3 - len(plat))
if len(plat) == 3:
break
# print("Plat:", plat.decode())
keycodeMapping = getKeycodeMapping(plat)
base_len = 6
while True:
cmd = b''
rest = base_len - 0
while rest > 0:
cmd += self.sock.recv(rest)
rest -= len(cmd)
key = cmd[0]
op = cmd[1]
x = struct.unpack('>H', cmd[2:4])[0]
y = struct.unpack('>H', cmd[4:6])[0]
Op(key, op, x, y)
except Exception as e:
print(e)
def hide_window(self):
self.root.withdraw()
def on_close(self):
self.disconnect()
self.root.destroy()
if __name__ == '__main__':
root = tkinter.Tk()
rdc = RemoteDesktopClient(root)
root.mainloop()