-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvncpy.py
197 lines (159 loc) · 5.86 KB
/
vncpy.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
import socketserver
import struct
import sys
import zlib
from PIL import Image
img = Image.open(sys.argv[1])
print("Loading and preprocessing image data...")
redPixels = img.getdata(0)
greenPixels = img.getdata(1)
bluePixels = img.getdata(2)
pixels = [
(redPixels[i] << 16 | greenPixels[i] << 8 | bluePixels[i])
for i in range(len(redPixels))
]
pixelsBytes = struct.pack("<" + "I" * len(pixels), *pixels)
# implementation of RFB 3.8 (does not fall back to 3.7 or 3.3 currently)
class RFBHandler(socketserver.BaseRequestHandler):
def pack_string(self, string: str):
string_bytes = string.encode()
return self.pack_u32(len(string_bytes)) + string_bytes
def send_string(self, string: str):
self.request.sendall(self.pack_string(string))
@staticmethod
def pack_u32(num):
return struct.pack(">I", num)
@staticmethod
def pack_u16(num):
return struct.pack(">H", num)
def recv_u16(self):
return struct.unpack(">H", self.request.recv(2))[0]
def recv_s32(self):
return struct.unpack(">i", self.request.recv(4))[0]
def recv_u32(self):
return struct.unpack(">I", self.request.recv(4))[0]
def handle(self):
print(f"hello {self.client_address}")
self.usingZlib = False
self.targetEncoding = 0
self.pixelFormatStruct = (
bytes(
[
32, # bits per pixel
24, # depth
0, # big endian flag
1, # true color flag
]
)
+ self.pack_u16(255) # red-max
+ self.pack_u16(255) # green-max
+ self.pack_u16(255) # blue-max
+ bytes([16, 8, 0]) # red-shift, green-shift, blue-shift
+ b"\0\0\0" # 3 padding bytes
)
# ProtocolVersion from server
self.request.sendall(b"RFB 003.008\n")
# ProtocolVersion from client
clientProtocol = self.request.recv(12)
if clientProtocol != b"RFB 003.008\n":
# terminate the connection
self.request.sendall(b"\0")
self.send_string("sorry but i don't 3.7 or lower")
return
# SecurityHandshake from server
self.request.sendall(b"\1\1")
# SecurityHandshake from client
clientSecurity = self.request.recv(1)[0]
# SecurityResult
if clientSecurity != 1:
# failed
self.request.sendall(self.pack_u32(1))
self.send_string("i only support None security")
return
else:
# success
self.request.sendall(self.pack_u32(0))
# ClientInit
sharedFlag = self.request.recv(1)[0]
# ServerInit
self.request.sendall(
self.pack_u16(img.width)
+ self.pack_u16(img.height)
+ self.pixelFormatStruct
+ self.pack_string("vncpy")
)
while True:
# main message loop
try:
message_type = self.request.recv(1)[0]
except (IndexError, ConnectionResetError):
# client terminated
print(f"bye {self.client_address}")
return
if message_type == 0:
self.setPixelFormat()
if message_type == 2:
self.setEncodings()
if message_type == 3:
self.framebufferUpdateRequest()
if message_type == 4:
# KeyEvent
self.request.recv(1 + 2 + 4) # read and ignore
if message_type == 5:
# PointerEvent
self.request.recv(1 + 2 + 2) # read and ignore
if message_type == 6:
# ClientCutText
# we have to read the length of the string to ignore it, ughh
self.request.recv(3) # padding
length = self.recv_u32()
self.request.recv(length)
def sendEntireFramebuffer(self):
messageHeader = struct.pack(
">BBH",
0, # message-type
0, # padding
1, # number-of-rectangles
)
rectangleData = pixelsBytes
if self.usingZlib:
pixelsCompressed = self.compressobj.compress(rectangleData)
pixelsCompressed += self.compressobj.flush(zlib.Z_FULL_FLUSH)
zlibHeader = struct.pack(">I", len(pixelsCompressed))
rectangleData = zlibHeader + pixelsCompressed
rectangle = (
struct.pack(">HHHHi", 0, 0, img.width, img.height, self.targetEncoding)
+ rectangleData
)
self.request.sendall(messageHeader + rectangle)
def setPixelFormat(self):
self.request.recv(3) # padding
pixelFormat = self.request.recv(16)
if pixelFormat != self.pixelFormatStruct:
print(
f"client requested {pixelFormat=} but we provide {self.pixelFormatStruct}. this will break things!!!"
)
def setEncodings(self):
self.request.recv(1) # padding
requestedEncodings = []
numberOfEncodings = self.recv_u16()
for _ in range(numberOfEncodings):
requestedEncodings.append(self.recv_s32())
if 6 in requestedEncodings:
print("client supports zlib encoding (6), setting up...")
self.usingZlib = True
self.compressobj = zlib.compressobj()
self.targetEncoding = 6
def framebufferUpdateRequest(self):
(incremental, xPos, yPos, width, height) = struct.unpack(
">?HHHH", self.request.recv(1 + 4 * 2)
)
if not incremental:
self.sendEntireFramebuffer()
def main():
addr = ("0.0.0.0", 5900)
print(f"Serving RFB on {addr}")
with socketserver.ForkingTCPServer(addr, RFBHandler) as server:
server.serve_forever()
if __name__ == "__main__":
main()