-
Notifications
You must be signed in to change notification settings - Fork 0
/
MetaPackets.py
296 lines (228 loc) · 8.35 KB
/
MetaPackets.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
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
# Copyright (C) 2014 and beyond by Jeremiah Morris
# and contributing developers.
#
# This file is part of Metaserver.
#
# Metaserver is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Metaserver is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Metaserver. If not, see <http://www.gnu.org/licenses/>.
import struct
import socket
import time
def unpack_strings(data, num_strings=1, offset=0):
maxp = len(data)
p = offset
cur_string = b''
all_strings = []
for i in range(num_strings):
foundstring = b''
if p < maxp:
nextnull = data.find(b'\x00', p)
if nextnull < 0:
p = maxp
else:
foundstring = data[p:nextnull]
p = nextnull + 1
all_strings.append(foundstring)
all_strings.append(p - offset)
return tuple(all_strings)
class PlayerDataChunk:
_fmt = struct.Struct('>xxHHHH2xHHH2xHH14x')
@staticmethod
def unpack_into(object, data, offset=0):
object.away, pri_r, pri_g, pri_b, sec_r, sec_g, sec_b, object.order, object.client_version = PlayerDataChunk._fmt.unpack_from(data, offset)
object.player_color = [ pri_r, pri_g, pri_b ]
object.team_color = [ sec_r, sec_g, sec_b ]
object.player_name, object.team_name, stringlen = unpack_strings(data, 2, offset + PlayerDataChunk._fmt.size)
class MessagePacket:
code = 3
_fmt = struct.Struct('>L')
SYNTAX_ERROR = 0
GAMES_NOT_ALLOWED = 1
INVALID_VERSION = 2
BAD_USER = 3
NOT_LOGGED_IN = 4
BAD_META_VERSION = 5
USER_LOGGED_IN = 6
UNKNOWN_GAME_TYPE = 7
LOGIN_SUCCESSFUL = 8
LOGOUT_SUCCESSFUL = 9
NOT_IN_ROOM = 10
GAME_EXISTS = 11
ACCOUNT_LOGGED_IN = 12
ROOM_FULL = 13
ACCOUNT_LOCKED = 14
NOT_SUPPORTED = 15
_messages = [
"Syntax error (unrecognized command).".encode('mac_roman'),
"Login failed (Games not allowed at this time).".encode('mac_roman'),
"Login failed (Invalid Game Version number).".encode('mac_roman'),
"Login failed (Bad user or Password).".encode('mac_roman'),
"User not logged in.".encode('mac_roman'),
"Bad metaserver version.".encode('mac_roman'),
"User already logged in!".encode('mac_roman'),
"Unknown game type!".encode('mac_roman'),
"User logged in.".encode('mac_roman'),
"User logged out.".encode('mac_roman'),
"Player not in a room!".encode('mac_roman'),
"You already created a game!".encode('mac_roman'),
"This account is already logged in!".encode('mac_roman'),
"The desired room is full!".encode('mac_roman'),
"Your account has been locked".encode('mac_roman'),
"The game server for your product has been shutdown".encode('mac_roman') ]
def __init__(self, which):
self.data = self._fmt.pack(which) + self._messages[which] + b'\x00'
class LoginPacket:
code = 100
_fmt = struct.Struct('>HHLLHH32s32s32s32s')
def __init__(self, data):
self.platform_type, self.metaserver_version, self.flags, self.user_id, self.max_authentication, player_data_size, self.service_name, self.build_date, self.build_time, self.username = self._fmt.unpack_from(data)
self.service_name = self.service_name.rstrip(b'\x00')
self.build_date = self.build_date.rstrip(b'\x00')
self.build_time = self.build_time.rstrip(b'\x00')
self.username = self.username.rstrip(b'\x00')
PlayerDataChunk.unpack_into(self, data, self._fmt.size)
class PasswordResponsePacket:
code = 109
private = True
_fmt = struct.Struct('>16s')
def __init__(self, data):
self.password_data, = self._fmt.unpack_from(data)
def decode_password(self, auth_type=0, salt=b''):
if auth_type == 0: # plaintext
self.password = self.password_data.rstrip(b'\x00')
elif auth_type == 4: # HTTPS
self.password = self.password_data
else: # unrecognized
pass
class LocalizationPacket:
code = 115
def __init__(self, data):
pass # there is data, but we don't care
class LogoutPacket:
code = 102
def __init__(self, data):
pass # should be empty
class RemoteHubRequestPacket:
code = 122
def __init__(self, data):
(self.network_version, stringlen) = unpack_strings(data)
class RoomLoginPacket:
code = 101
_fmt = struct.Struct('>32s')
def __init__(self, data):
self.token, = self._fmt.unpack_from(data)
self.token = self.token.rstrip(b'\x00')
self.username, stringlen = unpack_strings(data, 1, self._fmt.size)
class PlayerDataPacket:
code = 103
def __init__(self, data):
PlayerDataChunk.unpack_into(self, data)
class PlayerModePacket:
code = 107
_fmt = struct.Struct('>H')
def __init__(self, data):
self.deaf, = self._fmt.unpack_from(data)
self.session_id = data[self._fmt.size:len(data)]
class CreateGamePacket:
code = 104
_fmt = struct.Struct('>HH')
def __init__(self, data):
self.port, self.remote_server_id = self._fmt.unpack_from(data)
self.game_data = data[self._fmt.size:len(data)]
class StartGamePacket:
code = 114
_fmt = struct.Struct('>l8x')
def __init__(self, data):
self.game_time, = self._fmt.unpack_from(data)
class RemoveGamePacket:
code = 105
def __init__(self, data):
pass # should be empty
class IncomingChatPacket:
code = 200
_fmt = struct.Struct('>12xH2xLL')
def __init__(self, data):
self.flags, self.sender_id, self.target_id = self._fmt.unpack_from(data)
self.sender_name, self.message, stringlen = unpack_strings(data, 2, self._fmt.size)
class IncomingPrivateMessagePacket:
code = 201
private = True
_fmt = struct.Struct('>LL12xH2xLL')
def __init__(self, data):
self.header_target_id, self.echo, self.flags, self.sender_id, self.target_id = self._fmt.unpack_from(data)
self.sender_name, self.message, stringlen = unpack_strings(data, 2, self._fmt.size)
class IncomingKeepAlivePacket:
code = 202
def __init__(self, data):
pass # should be empty
class OutgoingKeepAlivePacket:
code = 202
data = None
class AcceptPacket:
code = 12
data = None
class SeedPacket:
code = 6
_fmt = struct.Struct('>h16s')
def __init__(self, max_auth, seed):
self.data = self._fmt.pack(max_auth, seed)
class LoginSuccessfulPacket:
code = 7
_fmt = struct.Struct('>L4x32s')
def __init__(self, user_id, token):
self.data = self._fmt.pack(user_id, token)
class RoomListPacket:
code = 0
_fmt = struct.Struct('>4x4sH14x')
def __init__(self, host, port):
self.data = self._fmt.pack(socket.inet_aton(host), port)
class RemoteHubListPacket:
code = 18
_fmt = struct.Struct('>H4sH')
def __init__(self, remote_servers):
self.data = b''.join(self._fmt.pack(server_id, socket.inet_aton(host), port) for server_id, host, port in remote_servers)
class RoomLoginSuccessfulPacket:
code = 9
_fmt = struct.Struct('>L4x')
def __init__(self, user_id):
self.data = self._fmt.pack(user_id)
class RoomMessagePacket:
code = 10
def __init__(self, message):
self.data = message + b'\x00'
class PlayerListPacket:
code = 1
def __init__(self, player_list, verb):
self.data = b''
for user_info in player_list:
self.data += user_info.roomPlayerDataChunk(verb)
class GameListPacket:
code = 2
def __init__(self, game_list, verb):
self.data = b''
for game_info in game_list:
self.data += game_info.dataChunk(verb)
class OutgoingChatPacket:
code = 200
_fmt = struct.Struct('>HHHHH2xH2xLL')
def __init__(self, user_info, message):
chatname = user_info.chatname
color = user_info.player_info.player_color
self.data = self._fmt.pack(0, 26 + len(chatname) + len(message), color[0], color[1], color[2], 0, user_info.user_id, 0) + chatname + b'\x00' + message + b'\x00'
class OutgoingPrivateMessagePacket:
code = 201
_fmt = struct.Struct('>LLHHHHH2xH2xLL')
def __init__(self, user_info, target_id, message):
chatname = user_info.chatname
color = user_info.player_info.player_color
self.data = self._fmt.pack(target_id, 1, 0, 26 + len(chatname) + len(message), color[0], color[1], color[2], 1, user_info.user_id, target_id) + chatname + b'\x00' + message + b'\x00'