-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver-unit.py
145 lines (123 loc) · 3.96 KB
/
server-unit.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
import sys
sys.path.append("./lib")
import websockets
import json
from pynput.mouse import Controller as MouseController, Button as MouseButton
from pynput.keyboard import Controller as KeyboardController
from pynput.keyboard import Key
from lib import generatePin
import asyncio
mouse = MouseController()
keyboard = KeyboardController()
connect_client = None
pin_server = generatePin.generate_pin_token()
def mouse_release(type, postition):
if type == "press":
print("Mouse Press On", postition)
mouse.press(MouseButton[postition.lower()])
else:
print("Mouse Release On", postition)
mouse.release(MouseButton[postition.lower()])
def left_click():
mouse.click(MouseButton.left)
print("Left click performed.")
def right_click():
mouse.click(MouseButton.right)
print("Right click performed.")
# Function for key actions
def press_key(keys):
print(keys)
if isinstance(keys, list):
for key in keys:
keyboard.press(key)
for key in reversed(keys):
keyboard.release(key)
print(f"Key combination pressed.")
else:
keyboard.press(keys)
keyboard.release(keys)
print(f"Key {keys} pressed.")
def move_mouse(dx, dy):
try:
current_x, current_y = mouse.position
new_x = current_x + dx
new_y = current_y + dy
mouse.position = (new_x, new_y)
print(f"Mouse moved to ({new_x}, {new_y})")
except Exception as e:
print(f"Error moving mouse: {e}")
# Handling WebSocket
print(f'Token: {pin_server}')
async def handle_connection(websocket):
global connect_client
print(websocket)
# path = websocket.path
# try:
# query_parms = dict(item.split("=") for item in path[1:].split("&") if "=" in item)
# except Exception as e:
# await websocket.close(code=4000, reason="Invalid query parameters!")
# return
# pin_token = query_parms.get("pintoken")
# if not pin_token:
# await websocket.close(code=4000, reason="No pintoken!")
# return
# if connect_client is not None:
# await websocket.close(code=4001, reason="Server already connected...")
# return
connect_client = websocket
print(f"Client has connected: {websocket.remote_address}")
try:
async for message in websocket:
try:
data = json.loads(message)
action = data.get('action')
print(f"Received message: {data}")
if action == 'press_key':
press_key(data['key'])
elif action == 'press_sckey':
press_key(Key[data['key'].lower()])
elif action == 'press_combination':
keys = []
for item in data.get('key', []):
print(item)
if item[0] == 'press_sckey':
keys.append(Key[item[1].lower()])
elif item[0] == 'press_key':
keys.append(item[1])
press_key(keys)
elif action == 'mouse_release':
mouse_release(data.get("type"), data.get("post"))
elif action == 'left_click':
left_click()
elif action == 'right_click':
right_click()
elif action == 'move_mouse':
details = data.get("details")
dx = details["dx"]
dy = details["dy"]
move_mouse(dx, dy)
except json.JSONDecodeError:
await websocket.close(code=4002, reason="Invalid JSON format")
break
except websockets.ConnectionClosed:
print(f"Connection closed: {websocket.remote_address}")
finally:
connect_client = None
# Connection
async def main():
port_server = 8442
ip_server = "192.168.1.4" # set it however you like, can localhost or 196.168.x.x, for example this 192.168.1.4 or 0.0.0.0
server = await websockets.serve(handle_connection, ip_server, port_server)
try:
print(f'Starting Server: ws://localhost:{port_server}')
await asyncio.Future()
except asyncio.CancelledError:
print("Server Close...")
finally:
server.close()
await server.wait_closed()
if __name__ == "__main__":
try:
asyncio.run(main())
except KeyboardInterrupt:
print("Server stopped manually.")