-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathchatreader.py
More file actions
398 lines (351 loc) · 16.9 KB
/
chatreader.py
File metadata and controls
398 lines (351 loc) · 16.9 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
"""
"Quiet mode" example proxy
Allows a client to turn on "quiet mode" which hides chat messages
This client doesn't handle system messages, and assumes none of them contain chat messages
"""
from __future__ import print_function
import argparse
import socket
import math
from sys import argv
import struct
from twisted.internet import reactor,ssl
from quarry.types.uuid import UUID
from quarry.net.proxy import DownstreamFactory, Bridge
import twisted.internet._sslverify as v
from mcrcon import MCRcon
v.platformTrust = lambda : None
argv.pop(0)
class QuietBridge(Bridge):
quiet_mode = False
fly = 0
hover = 0
freeze = False
def packet_upstream_chat_command(self, buff):
command = buff.unpack_string()
if command == "quiet":
self.toggle_quiet_mode()
buff.discard()
elif command.split(" ")[0] == "levitate":
self.send_system("Levitating")
buff.save()
if self.fly == 0:
self.fly = 0.5
else:
self.fly = 0
try:
x, y, z, ground = self.prev_pos
yaw, pitch, ground = self.prev_look
buf = struct.pack('>dddffBBB', x, y+self.fly, z, yaw, pitch, 0, 0, 0)
self.downstream.send_packet('player_position_and_look', buf)
except:
self.send_system("Teleport on [all] failed")
buff.discard()
elif command.split(" ")[0] == "tpw":
self.send_system("Teleporting on the [all] axis")
buff.save()
try:
x, y, z, ground = self.prev_pos
yaw, pitch, ground = self.prev_look
buf = struct.pack('>dddffBBB', x+int(command.split(" ")[1]), y+int(command.split(" ")[2]), z+int(command.split(" ")[3]), yaw, pitch, 0, 0, 0)
self.downstream.send_packet('player_position_and_look', buf)
except:
self.send_system("Teleport on [all] failed")
buff.discard()
elif command.split(" ")[0] == "tpy":
self.send_system("Teleporting on the Y axis")
buff.save()
try:
x, y, z, ground = self.prev_pos
yaw, pitch, ground = self.prev_look
buf = struct.pack('>dddffBBB', x, y+int(command.split(" ")[1]), z, yaw, pitch, 0, 0, 0)
self.downstream.send_packet('player_position_and_look', buf)
except:
self.send_system("Teleport on Y failed")
buff.discard()
elif command.split(" ")[0] == "tpz":
self.send_system("Teleporting on the Z axis")
buff.save()
try:
x, y, z, ground = self.prev_pos
yaw, pitch, ground = self.prev_look
buf = struct.pack('>dddffBBB', x, y, z+int(command.split(" ")[1]), yaw, pitch, 0, 0, 0)
self.downstream.send_packet('player_position_and_look', buf)
except:
self.send_system("Teleport on Z failed")
buff.discard()
elif command.split(" ")[0] == "tpx":
self.send_system("Teleporting on the X axis")
buff.save()
try:
x, y, z, ground = self.prev_pos
yaw, pitch, ground = self.prev_look
buf = struct.pack('>dddffBBB', x+int(command.split(" ")[1]), y, z, yaw, pitch, 0, 0, 0)
self.downstream.send_packet('player_position_and_look', buf)
except:
self.send_system("Teleport on X failed")
buff.discard()
else:
buff.restore()
self.upstream.send_packet("chat_command", buff.read())
def packet_upstream_chat_message(self, buff):
buff.save()
chat_message = self.read_chat(buff, "upstream")
self.logger.info(" >> %s" % chat_message)
if chat_message.startswith(".quiet"):
self.toggle_quiet_mode()
elif chat_message == ".freeze":
self.freeze = not self.freeze
elif self.quiet_mode and not chat_message.startswith("."):
# Don't let the player send chat messages in quiet mode
msg = "Can't send messages while in quiet mode, moron."
self.send_system(msg)
elif chat_message.split(" ")[0] == ".rcon":
cmd = chat_message[6:]
rcon = MCRcon("10.0.0.16","test",12346)
rcon.connect()
try:
self.send_system(str(rcon.command(cmd)))
except Exception as e:
self.send_system("Rcon command execution failed")
self.send_system(str(e))
elif chat_message.split(" ")[0] == ".levitate":
buff.save()
msg = ""
if self.fly == 0:
self.fly = 0.125
msg = ""
else:
self.fly = 0
msg = "Not "
self.send_system(f"{msg}levitating")
try:
x, y, z, ground = self.prev_pos
yaw, pitch, ground = self.prev_look
buf = struct.pack('>dddffBBB', x, y+self.fly, z, yaw, pitch, 0, 0, 0)
self.downstream.send_packet('player_position_and_look', buf)
except:
self.send_system("Could not levitate")
buff.discard()
elif chat_message.split(" ")[0] == ".float":
buff.save()
msg = ""
if self.fly == 0:
self.fly = 0.00125
msg = ""
else:
self.fly = 0
msg = "Not "
self.send_system(f"{msg}floating")
try:
x, y, z, ground = self.prev_pos
yaw, pitch, ground = self.prev_look
buf = struct.pack('>dddffBBB', x, y+self.fly, z, yaw, pitch, 0, 0, 0)
self.downstream.send_packet('player_position_and_look', buf)
except:
self.send_system("Could not float")
buff.discard()
elif chat_message == ".hlp":
self.send_system("AxelProxy HELP:")
self.send_system(".rcon <command> - Use RCON to run a command\n(Note: you can't run actual op commands in CHAT)\n(aka: use rcon for op commands)")
self.send_system(".tp<x,y, or z> <amount> - Teleports you on <x,y, or z> by <amount>")
self.send_system(".tpw <x> <y> <z> - Teleports you by x, y, and z on x, y, and z respectively")
self.send_system(".hover - Displays you 1 block above your real position\n(For everyone BUT you)")
self.send_system(".levitate - Makes you go up 1/8 of a block every packet")
self.send_system(".hlp - Hey, what's your IQ?")
elif chat_message.split(" ")[0] == ".hover":
buff.save()
msg = ""
if self.hover == 0:
self.hover = 1
msg = ""
else:
self.hover = 0
msg = "Not "
self.send_system(f"{msg}hovering")
buff.discard()
elif chat_message.split(" ")[0] == ".tpw":
self.send_system("Teleporting on the [all] axis")
buff.save()
try:
x, y, z, ground = self.prev_pos
yaw, pitch, ground = self.prev_look
buf = struct.pack('>dddffBBB', x+int(chat_message.split(" ")[1]), y+int(chat_message.split(" ")[2]), z+int(chat_message.split(" ")[3]), yaw, pitch, 0, 0, 0)
self.downstream.send_packet('player_position_and_look', buf)
except:
self.send_system("Teleport on [all] failed")
buff.discard()
elif chat_message.split(" ")[0] == ".tpy":
self.send_system("Teleporting on the Y axis")
buff.save()
try:
x, y, z, ground = self.prev_pos
yaw, pitch, ground = self.prev_look
buf = struct.pack('>dddffBBB', x, y+int(chat_message.split(" ")[1]), z, yaw, pitch, 0, 0, 0)
self.downstream.send_packet('player_position_and_look', buf)
except:
self.send_system("Teleport on Y failed")
buff.discard()
elif chat_message.split(" ")[0] == ".tpz":
self.send_system("Teleporting on the Z axis")
buff.save()
try:
x, y, z, ground = self.prev_pos
yaw, pitch, ground = self.prev_look
buf = struct.pack('>dddffBBB', x, y, z+int(chat_message.split(" ")[1]), yaw, pitch, 0, 0, 0)
self.downstream.send_packet('player_position_and_look', buf)
except:
self.send_system("Teleport on Z failed")
buff.discard()
elif chat_message.split(" ")[0] == ".tpx":
self.send_system("Teleporting on the X axis")
buff.save()
try:
x, y, z, ground = self.prev_pos
yaw, pitch, ground = self.prev_look
buf = struct.pack('>dddffBBB', x+int(chat_message.split(" ")[1]), y, z, yaw, pitch, 0, 0, 0)
self.downstream.send_packet('player_position_and_look', buf)
except:
self.send_system("Teleport on X failed")
buff.discard()
else:
# Pass to upstream
buff.restore()
self.upstream.send_packet("chat_message", buff.read())
print(f"Sent message: \"{chat_message}\"")
def toggle_quiet_mode(self):
# Switch mode
self.quiet_mode = not self.quiet_mode
action = self.quiet_mode and "enabled" or "disabled"
msg = "Quiet mode %s" % action
self.send_system(msg)
def packet_upstream_player_position(self, buff):
if self.freeze == False:
buff.save()
x, y, z, ground = struct.unpack('>dddB', buff.read())
print(f"[*] player_position {x} / {y} / {z} | {ground}")
self.prev_pos = (x, y, z, ground)
if self.fly > 0:
yaw,pitch,_ = self.prev_look
buf2 = struct.pack('>dddffBBB', x, y+self.fly, z, yaw, pitch, 0, 0, 0)
self.downstream.send_packet('player_position_and_look', buf2)
buf = struct.pack('>dddB', x, y+self.hover, z, ground)
self.upstream.send_packet('player_position', buf)
else:
buff.save()
x, y, z, ground = struct.unpack('>dddB', buff.read())
print(f"[*] player_position {x} / {y} / {z} | {ground}\nreal packet: {x} / {y} / {z} | {ground}")
buf = struct.pack('>dddB', self.prev_pos[0], self.prev_pos[1], self.prev_pos[2], self.prev_pos[3])
self.upstream.send_packet('player_position', buf)
def packet_upstream_player_position_and_look(self, buff):
if self.freeze == False:
buff.save()
x, y, z, yaw, pitch, ground = struct.unpack('>dddffB', buff.read())
print(f"[*] player_position+player_look {x} / {y} / {z} | {yaw} / {pitch} | {ground}")
self.prev_pos = (x, y, z, ground)
self.prev_look = (yaw, pitch, ground)
if self.fly > 0:
buf2 = struct.pack('>dddffBBB', x, y+self.fly, z, yaw, pitch, 0, 0, 0)
self.downstream.send_packet('player_position_and_look', buf2)
buf = struct.pack('>dddffB', x, y+self.hover, z, yaw, pitch, ground)
self.upstream.send_packet('player_position_and_look', buf)
else:
buff.save()
x, y, z, yaw, pitch, ground = struct.unpack('>dddffB', buff.read())
print(f"[*] player_position+player_look {x} / {y} / {z} | {yaw} / {pitch} | {ground}\nreal packet: {x} / {y} / {z} | {yaw} / {pitch} | {ground}")
buf = struct.pack('>dddffB', self.prev_pos[0], self.prev_pos[1], self.prev_pos[2], self.prev_look[0], self.prev_look[1], self.prev_pos[3])
self.upstream.send_packet('player_position_and_look', buf)
def packet_upstream_player_look(self, buff):
if self.freeze == False:
buff.save()
yaw, pitch, ground = struct.unpack('>ffB', buff.read())
print(f"[*] player_look {yaw} / {pitch} | {ground}")
self.prev_look = (yaw, pitch, ground)
buf = struct.pack('>ffB', yaw, pitch, ground)
self.upstream.send_packet('player_look', buf)
else:
buff.save()
yaw, pitch, ground = struct.unpack('>ffB', buff.read())
print(f"[*] player_look {self.prev_look[0]} / {self.prev_look[1]} | {self.prev_look[2]}\nreal packet: {yaw} / {pitch} | {ground}")
buf = struct.pack('>ffB', self.prev_look[0], self.prev_look[1], self.prev_look[2])
self.upstream.send_packet('player_look', buf)
def packet_downstream_chat_message(self, buff):
chat_message = self.read_chat(buff, "downstream")
self.logger.info(" :: %s" % chat_message)
# All chat messages on 1.19+ are from players and should be ignored in quiet mode
if self.quiet_mode and self.downstream.protocol_version >= 759:
return
# Ignore message that look like chat when in quiet mode
if chat_message is not None and self.quiet_mode and chat_message.startswith("<"):
return
# Pass to downstream
buff.restore()
self.downstream.send_packet("chat_message", buff.read())
def read_chat(self, buff, direction):
buff.save()
if direction == "upstream":
p_text = buff.unpack_string()
buff.discard()
return p_text
elif direction == "downstream":
# 1.19.1+
if self.downstream.protocol_version >= 760:
p_signed_message = buff.unpack_signed_message()
buff.unpack_varint() # Filter result
p_position = buff.unpack_varint()
p_sender_name = buff.unpack_chat()
buff.discard()
if p_position not in (1, 2): # Ignore system and game info messages
# Sender name is sent separately to the message text
return ":: <%s> %s" % (
p_sender_name, p_signed_message.unsigned_content or p_signed_message.body.message)
return
p_text = buff.unpack_chat().to_string()
# 1.19+
if self.downstream.protocol_version == 759:
p_unsigned_text = buff.unpack_optional(lambda: buff.unpack_chat().to_string())
p_position = buff.unpack_varint()
buff.unpack_uuid() # Sender UUID
p_sender_name = buff.unpack_chat()
buff.discard()
if p_position not in (1, 2): # Ignore system and game info messages
# Sender name is sent separately to the message text
return "<%s> %s" % (p_sender_name, p_unsigned_text or p_text)
elif self.downstream.protocol_version >= 47: # 1.8.x+
p_position = buff.unpack('B')
buff.discard()
if p_position not in (1, 2) and p_text.strip(): # Ignore system and game info messages
return p_text
else:
return p_text
def send_system(self, message):
if self.downstream.protocol_version >= 760: # 1.19.1+
self.downstream.send_packet("system_message",
self.downstream.buff_type.pack_chat(message),
self.downstream.buff_type.pack('?', False)) # Overlay false to put in chat
elif self.downstream.protocol_version == 759: # 1.19
self.downstream.send_packet("system_message",
self.downstream.buff_type.pack_chat(message),
self.downstream.buff_type.pack_varint(1)) # Type 1 for system chat message
else:
self.downstream.send_packet("chat_message",
self.downstream.buff_type.pack_chat(message),
self.downstream.buff_type.pack('B', 0),
self.downstream.buff_type.pack_uuid(UUID(int=0)))
class QuietDownstreamFactory(DownstreamFactory):
bridge_class = QuietBridge
motd = "Axel's Proxy Server"
def main(argv):
# Parse options
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ssl.CertificateOptions(verify=False)
# Create factory
factory = QuietDownstreamFactory()
factory.connect_host = "10.0.0.16"
factory.connect_port = 12345
# Listen
factory.listen("localhost", 25565)
reactor.run()
if __name__ == "__main__":
import sys
main(sys.argv[1:])