-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
216 lines (175 loc) · 6.41 KB
/
server.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
import socket
import threading
import keyboard
import brotli
import numpy as np
import cv2
import configparser
from PIL import ImageGrab
import struct
import queue
import pickle
import mouse
import pyogg
# Read configuration
config = configparser.ConfigParser()
config.read('serverconfig.ini')
# general config
compression = int(config["general"]["compression"])
# video config
jpegquality = int(config["video"]["quality"])
formatcodec = config["video"]["format"]
resX = int(config["video"]["x"])
resY = int(config["video"]["y"])
# audio config
aenable = bool(int(config["audio"]["enable"]))
abitrate = int(config["audio"]["bitrate"])
# server config
HOST = config["server"]["ip"]
PORT = int(config["server"]["port"])
screensize = ()
def capture_screen():
global screensize
screenshot = ImageGrab.grab()
screensize = screenshot.size
img_np = np.array(screenshot)
img_bgr = cv2.cvtColor(img_np, cv2.COLOR_RGB2BGR)
return img_bgr
def imagenc(image, quality=90):
if formatcodec == "webp":
retval, buffer = cv2.imencode('.webp', image, [int(cv2.IMWRITE_WEBP_QUALITY), quality])
elif formatcodec == "jpeg":
retval, buffer = cv2.imencode('.jpeg', image, [int(cv2.IMWRITE_JPEG_QUALITY), quality])
elif formatcodec == "avif":
retval, buffer = cv2.imencode('.avif', image, [int(cv2.IMWRITE_AVIF_QUALITY), quality])
else:
raise TypeError(f"{formatcodec} is not supported")
if not retval:
raise ValueError("image encoding failed.")
return np.array(buffer).tobytes()
def translate_coordinates(x, y, resized_width, resized_height):
translated_x = int(x * screensize[0] / resized_width)
translated_y = int(y * screensize[1] / resized_height)
return translated_x, translated_y
def convert_quality(quality):
brotli_quality = int(quality / 100 * 11)
lgwin = int(10 + (quality / 100 * (24 - 10)))
return brotli_quality, lgwin
client_sockets = []
buffer = queue.Queue(maxsize=10)
first = True
running = False
def capture():
while running:
# Capture the screen
screen_image = capture_screen()
# Resize the image
stretch_near = cv2.resize(screen_image, (resX, resY), interpolation=cv2.INTER_NEAREST)
# Encode the image
encoded = imagenc(stretch_near, jpegquality)
#compressed = lz4.frame.compress(encoded, lz4.frame.COMPRESSIONLEVEL_MAX)
bquality, lgwin = convert_quality(compression)
compressed = brotli.compress(encoded, quality=bquality, lgwin=lgwin)
data_length = struct.pack('!III', len(compressed), resX, resY)
data2send = data_length + compressed
buffer.put(data2send)
def handle_client():
global running, first
try:
while running:
data2send = buffer.get()
for i in client_sockets:
try:
i.sendall(data2send)
except Exception as e:
i.close()
client_sockets.remove(i)
if not client_sockets:
running = False
first = True
print("No clients connected. Server is standby")
break
except socket.error:
pass
except Exception as e:
print(f"Error in handle_client: {e}")
def handle_client_commands(client_socket):
try:
while True:
try:
# Receive the length of the data
data_length = receive_exact(client_socket, 4)
if not data_length:
break
commandmetadata = struct.unpack('!I', data_length)
command_data = receive_exact(client_socket, commandmetadata[0])
command = pickle.loads(command_data)
if command:
action = command["action"]
data = command["data"]
if action == "move_mouse":
x, y = data["x"], data["y"]
rx, ry = translate_coordinates(x, y, resX, resY)
print(f"move mouse to x: {rx} | y: {ry}")
elif action == "click_mouse":
button = data["button"]
state = data["state"]
if button == 1:
if state == "down":
mouse.press()
else:
mouse.release()
elif button == 2:
if state == "down":
mouse.press(mouse.MIDDLE)
else:
mouse.release(mouse.MIDDLE)
elif button == 3:
if state == "down":
mouse.press(mouse.RIGHT)
else:
mouse.release(mouse.RIGHT)
#elif button == 4:
# mouse.wheel()
#elif button == 5:
# mouse.wheel(-1)
elif action == "keyboard":
key = data["key"]
state = data["state"]
if state == "down":
keyboard.press(key)
else:
keyboard.release(key)
else:
print(command)
except socket.error:
break
except Exception as e:
print(f"Error in handle_client_commands: {e}")
def receive_exact(socket, n):
"""Helper function to receive exactly n bytes."""
data = b''
while len(data) < n:
packet = socket.recv(n - len(data))
if not packet:
return None
data += packet
return data
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen()
print(f"Server started on {HOST}:{PORT}")
while True:
conn, addr = s.accept()
print(f'{addr} is connected')
client_sockets.append(conn)
if first:
running = True
# Start the capture thread
capture_thread = threading.Thread(target=capture)
handle_client_thread = threading.Thread(target=handle_client)
capture_thread.start()
handle_client_thread.start()
first = False
command_thread = threading.Thread(target=handle_client_commands, args=(conn,))
command_thread.start()