-
Notifications
You must be signed in to change notification settings - Fork 0
/
receiver_linux.py
75 lines (55 loc) · 2.14 KB
/
receiver_linux.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
# run as root pliz
import asyncio
import websockets
import keyboard
import socket
import time
import subprocess
import pyscreenshot
import base64
from io import BytesIO
# looks the first write doesn't work unless we call a function on the module.
keyboard.is_pressed("e")
def im_2_b64(image):
buff = BytesIO()
image.save(buff, format="JPEG")
img_str = base64.b64encode(buff.getvalue())
return img_str
async def main():
async with websockets.connect('ws://192.168.4.148:8766', max_size=None) as websocket:
await websocket.send("CONNECT_RECEIVER")
while(True):
print("waiting for server...")
server = await websocket.recv()
if(server.startswith("KEY|")):
keyboard.write(server[4:])
if(server.startswith("CMD|")):
output = subprocess.getoutput(server[4:])
await websocket.send("RECEIVER_OUTPUT|" + output)
if(server.startswith("SCREEN|")):
im = pyscreenshot.grab()
#im.show()
await websocket.send("RECEIVER_OUTPUT|" + im_2_b64(im).decode("utf-8"))
if(server.startswith("PUT|")):
try:
split = server.split("|")
filename = split[1]
data = split[2]
f = open(filename, 'wb')
f.write(base64.b64decode(data.encode("utf-8")))
f.close()
await websocket.send("RECEIVER_OUTPUT|DONE")
except:
await websocket.send("RECEIVER_OUTPUT|ERROR")
if(server.startswith("GET|")):
try:
split = server.split("|")
source = split[1]
f = open(source, "rb")
data = base64.b64encode(f.read()).decode("utf-8")
await websocket.send("RECEIVER_OUTPUT|" + data)
except:
print("fail")
print("writing : " + server)
asyncio.get_event_loop().run_until_complete(main())
asyncio.get_event_loop().run_forever()