-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.py
executable file
·205 lines (184 loc) · 4.54 KB
/
client.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
import socket
import cv2
import numpy as np
import os
import time
import pyautogui
from PIL import Image
import pyscreenshot as ImageGrab
import io
port = 10100
msg = socket.gethostbyname(socket.gethostname())
dest = ('<broadcast>',port)
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
s.sendto(msg, dest)
print msg
s.close()
TCP_IP = ''
TCP_IP_2 = ''
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
s.bind((TCP_IP_2,port))
message, address = s.recvfrom(1024)
ad = ''.join(str(address));
ad1 = ad.split()
ad2= ad1[0]
ad3=ad2[2:15]
ad3 = ad3.replace("'", "")
ad3 = ad3.replace(",", "")
TCP_IP_2 = str(ad3)
TCP_IP = message
#print TCP_IP_2
print TCP_IP
s.close()
time.sleep(1)
TCP_IP =TCP_IP.strip()
#TCP_IP = '127.0.0.1'
TCP_PORT = 5006
MESSAGE = "Hello, World!"
TCP_PORT_2 = 9656
TCP_PORT_3 = 6969
s2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s2.connect((TCP_IP, TCP_PORT_3))
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
s1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s1.connect((TCP_IP, TCP_PORT_2))
screen_width, screen_height = pyautogui.size()
wsent = 0
hsent = 0
heightstr = str(screen_height).zfill(8)
widthstr = str(screen_width).zfill(8)
while hsent < 8:
sent = s2.send(heightstr[hsent:])
if sent == 0:
raise RuntimeError("Socket connection broken")
hsent += sent
while wsent < 8:
sent = s2.send(widthstr[wsent:])
if sent == 0:
raise RuntimeError("Socket connection broken")
wsent += sent
s2.close()
pid = os.fork()
if pid!=0:
while 1:
try:
img = ImageGrab.grab()
img_np = np.array(img)
frame = cv2.cvtColor(img_np, cv2.COLOR_RGB2BGR)
pil_im = Image.fromarray(frame)
b = io.BytesIO()
pil_im.save(b, 'jpeg')
im_bytes = b.getvalue()
totalsent = 0
metasent = 0
length =len(im_bytes)
lengthstr=str(length).zfill(8)
while metasent < 8 :
sent = s.send(lengthstr[metasent:])
if sent == 0:
raise RuntimeError("Socket connection broken")
metasent += sent
while totalsent < length :
sent = s.send(im_bytes[totalsent:])
if sent == 0:
raise RuntimeError("Socket connection broken")
totalsent += sent
except Exception as e:
print ("Error is: ", e)
else:
child = pid
while True:
try:
type = s1.recv(2)
type = int(type)
if type == 1:
x = s1.recv(4)
x = int(x)
y = s1.recv(4)
y = int(y)
pyautogui.moveTo(x,y)
pyautogui.click()
if type == 2:
x = s1.recv(4)
x = int(x)
y = s1.recv(4)
y = int(y)
pyautogui.moveTo(x,y)
pyautogui.click(button='right')
if type == 0:
x = s1.recv(20)
if x == 'BackSpace':
pyautogui.press('backspace')
elif x == 'Delete':
pyautogui.press('delete')
elif x == 'Return':
pyautogui.press('enter')
elif x == 'Tab':
pyautogui.press('tab')
elif x == 'Control_L':
pyautogui.press('ctrlleft')
elif x == 'Control_R':
pyautogui.press('ctrlright')
elif x == 'Alt_L':
pyautogui.press('altleft')
elif x == 'Alt_R':
pyautogui.press('altright')
elif x == 'Shift_L':
pyautogui.press('shiftleft')
elif x == 'Shift_R':
pyautogui.press('shiftright')
elif x == 'Escape':
pyautogui.press('esc')
elif x == 'space':
pyautogui.press(' ')
elif x == 'Up':
pyautogui.press('up')
elif x == 'Down':
pyautogui.press('down')
elif x == 'Right':
pyautogui.press('right')
elif x == 'Left':
pyautogui.press('left')
elif x == 'period':
pyautogui.press('.')
elif x == 'apostrophe':
pyautogui.press('"')
elif x == 'Term':
pyautogui.hotkey('ctrl', 'alt', 't')
else:
pyautogui.press(x)
if type == 3:
farray = []
fsize = s1.recv(3)
fs = int(fsize)
print fs
filename = s1.recv(fs)
print filename
filename = filename.strip()
meta = s1.recv(8)
print meta
m = int(meta)
print m
mrec = 0
while mrec < m:
chunk = s1.recv(m - mrec)
if chunk == b'':
raise RuntimeError("Socket connection broken")
farray.append(chunk)
mrec += len(chunk)
f = ''.join(farray)
f2 = open(filename, 'wb').write(f)
if type == 4:
s.close()
s1.close()
os.system("kill "+ str(child))
sys.exit()
except Exception as a:
time.sleep(2)
print ("Error is: ", a)
s.close()
s1.close()