-
Notifications
You must be signed in to change notification settings - Fork 177
/
Server.py
391 lines (313 loc) · 12.5 KB
/
Server.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
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
import builtins
import socket
import socketserver
import struct
import random
import sys
from typing import NamedTuple, Set, Tuple
from enum import IntEnum
from datetime import datetime
DATASIZE = 0x1000
CODES: Set[int] = set()
printmode = 1
SPSTRUCT = struct.Struct(f"B7sII{DATASIZE - 16}s")
EMPTY = b"\0\0\0\0\0\0\0\0"
CONNECT_MAGIC = 0x1F2F3F4F
class ClientHeaders(IntEnum):
REQUEST_CODE = 0x00
JOIN = 0x01
DATA = 0x10
DATA_VERIFIED = 0x11
QUERY_VERIFICATION = 0x20
LEAVE = 0xFF
class ServerHeaders(IntEnum):
CODES = 0x00
NEW_PLAYER = 0x01
DATA = 0x10
DATA_VERIFIED = 0x11
RECEIVED = 0x20
VERIFY_CLEAR = 0x21
INVALID_HEADER = 0x80
NO_ROOM = 0x81
UNKNOWN_PLAYER = 0x82
LEAVE = 0xFF
class ServerPacket(NamedTuple):
header: int
game: bytes
player: int
room: int
data: bytes
@classmethod
def frombytes(cls, data):
return cls._make(SPSTRUCT.unpack(data))
def tobytes(self) -> bytes:
s = ServerPacket(self.header, self.game,
self.player, self.room, self.data)
return SPSTRUCT.pack(*tuple(s))
def getint(self, index) -> int:
return int.from_bytes(self.data[index * 4:(index + 1) * 4], 'little')
def hex(i: int) -> str:
return builtins.hex(i).upper()[2:].zfill(8)
def randint() -> int:
return random.randint(1, 0xFFFFFFFF)
def print(*args):
builtins.print(datetime.now().strftime("[%m-%d-%y %H:%M:%S]"), *args)
class Player:
def __init__(self, client, code, game: "Game") -> None:
self.client = client
self.code: int = code
self.room: Room = game.get_room(0)
self.game = game
game.join(self)
self.room.players.add(self)
def move(self, room: "Room"):
self.room.leave(self)
self.room = room
self.room.join(self)
if self.room not in self.game.rooms:
self.game.rooms.add(self.room)
def __hash__(self) -> int:
return self.code
def deliver(self, packet: ServerPacket):
if printmode == 2:
print(f"[{self.game.name}/{hex(self.room.code)}] {ServerHeaders(packet.header).name} from {hex(packet.player)} to {hex(self.code)}")
server.socket.sendto(packet.tobytes(), self.client)
class Room:
def __init__(self, game: "Game", code=0) -> None:
self.code = code
self.players: Set[Player] = set()
self.game: Game = game
self.verifiying = False
self.verified: Set[Player] = set()
self.vcopy: Set[Player] = set()
def __hash__(self) -> int:
return self.code
def join(self, player: Player):
if player not in self.players:
self.players.add(player)
if printmode:
print(
f"[{self.game.name}/{hex(self.code)}] Player {hex(player.code)} joined")
def leave(self, player: Player):
if player in self.players:
self.players.remove(player)
if printmode:
print(
f"[{self.game.name}/{hex(self.code)}] Player {hex(player.code)} left")
def deliver(self, packet: ServerPacket, sender: Player) -> int:
sent = 0
queueSends = False
if packet.header in (ClientHeaders.DATA_VERIFIED, ClientHeaders.QUERY_VERIFICATION):
if not self.vcopy:
self.vcopy = self.players.copy()
if sender not in self.vcopy:
# impostor (we let them in bc we are Stupid Crewmates
self.vcopy.add(sender)
if self.players - self.vcopy:
# some people left. L
self.vcopy &= self.players
if packet.header == ClientHeaders.DATA_VERIFIED:
self.verifiying = True
self.verified.add(sender)
if printmode:
print(
f"[{self.game.name}/{hex(self.code)}] Verifying {len(self.verified)}/{len(self.vcopy)}")
elif not self.verifiying:
# hack. if querying and not verifying, use this to force "yes"
self.verified = self.players
if self.vcopy == self.verified:
pl = b"".join([x.code.to_bytes(8, "little")
for x in self.verified])
queueSends = True
self.verified.clear()
self.vcopy.clear()
self.verifiying = False
else:
sender.deliver(ServerPacket(ServerHeaders.RECEIVED,
self.game.bytename, sender.code, self.code, bytes()))
if packet.header != ClientHeaders.QUERY_VERIFICATION:
for player in self.players:
if player.code != sender.code:
player.deliver(packet)
sent += 1
if queueSends:
for player in self.players:
player.deliver(ServerPacket(ServerHeaders.VERIFY_CLEAR,
self.game.bytename, player.code, self.code, pl))
return sent
class EmptyRoom(Room):
def __init__(self, game: "Game") -> None:
super().__init__(game, code=0)
def deliver(self):
return
def join(self, player: Player):
self.players.add(player)
def leave(self, player: Player):
try:
self.players.remove(player)
except:
pass
class Game:
def __init__(self, name: bytes) -> None:
self.name: str = name.decode().rstrip("\x00")
self.bytename: bytes = name
self.rooms: Set[Room] = {EmptyRoom(self)}
def __hash__(self) -> int:
return hash(self.name)
def join(self, player: Player) -> None:
self.rooms.add(player.room)
if printmode:
print(
f"[{self.name}] New player {hex(player.code)}{f' in {hex(player.room.code)}' if player.room.code else ''}")
def leave(self, player: Player) -> bool:
for r in self.rooms.copy():
if player in r.players:
r.players.remove(player)
self.rooms.remove(r)
if printmode:
print(
f"[{self.name}] Player {hex(player.code)} in room {hex(player.room.code)} has left")
return True
return False
def get_room(self, room: int) -> Room:
for x in self.rooms:
if x.code == room:
return x
return self.get_room(0)
def deliver(self, packet: ServerPacket, sender: Player) -> int:
return sender.room.deliver(packet, sender)
class Handler(socketserver.BaseRequestHandler):
def handle(self):
self.server: Server
self.request: Tuple[bytes, socket.socket]
data = ServerPacket.frombytes(self.request[0])
try:
ClientHeaders(data.header)
except:
return # not a valid header
if printmode == 2:
print(
f"(SERVER) {ClientHeaders(data.header).name} from {hex(data.player)} to server")
if not data.player:
if data.header != ClientHeaders.REQUEST_CODE and data.room != CONNECT_MAGIC:
return # shhhhhh
player = Player(self.client_address, randint(),
self.server.get_game(data.game))
return self.send(ServerHeaders.CODES, player)
# it begins
if data.header == ClientHeaders.REQUEST_CODE:
# let's give a room code back and reassign
p = self.server.resolve_player(data.player, data.game)
if not p:
return self.send_raw(ServerPacket(ServerHeaders.UNKNOWN_PLAYER, data.game, data.player, data.room, bytes()))
if p.room.code:
return self.send(ServerHeaders.CODES, p, len(p.room.players).to_bytes(4, 'little') + b''.join([x.code.to_bytes(8, "little")
for x in p.room.players - {p}]))
roomid = 0
while (roomid in [x.code for x in p.game.rooms]):
roomid = ((randint() & 0xFFFFFFFF) & ~
data.getint(0)) | data.getint(1)
r = Room(p.game, roomid)
p.move(r)
return self.send(ServerHeaders.CODES, p, len(r.players).to_bytes(4, 'little') + b''.join([x.code.to_bytes(8, "little")
for x in r.players - {p}]))
if data.header == ClientHeaders.JOIN:
# data will be how many players there are so the client can decide "this is too many players"
p = self.server.resolve_player(data.player, data.game)
r = p.game.get_room(data.room)
if not r.code:
return self.send(ServerHeaders.NO_ROOM, p)
p.move(r)
self.send(ServerHeaders.CODES, p, len(r.players).to_bytes(4, 'little') + b''.join([x.code.to_bytes(8, "little")
for x in r.players - {p}]))
r.deliver(ServerPacket(ServerHeaders.NEW_PLAYER,
data.game, p.code, r.code, bytes()), p)
if data.header in (ClientHeaders.DATA, ClientHeaders.DATA_VERIFIED, ClientHeaders.QUERY_VERIFICATION):
if not data.room:
return self.send_raw(ServerPacket(ServerHeaders.INVALID_HEADER, data.game, data.player, data.room, bytes()))
p = self.server.resolve_player(data.player, data.game, data.room)
if not p:
return self.send_raw(ServerPacket(ServerHeaders.UNKNOWN_PLAYER, data.game, data.player, data.room, bytes()))
p.room.deliver(data, p)
if data.header == ClientHeaders.LEAVE:
p = self.server.resolve_player(data.player, data.game, data.room)
if not p or not p.room:
return
p.room.leave(p)
if not p.room.code:
return
if p.room.players:
p.room.deliver(data, p)
else:
p.game.rooms.remove(p.room)
if printmode:
print(f"[{p.game.name}] Room {hex(p.room.code)} disbanded")
def send(self, header, player: Player, data=()):
self.request[1].sendto(ServerPacket(header, player.game.bytename,
player.code, player.room.code, bytes(data)).tobytes(), player.client)
def send_raw(self, packet: ServerPacket):
self.request[1].sendto(packet.tobytes(), self.client_address)
class Server(socketserver.ThreadingUDPServer):
def __init__(self, server_address) -> None:
super().__init__(server_address, Handler)
self.games: Set[Game] = set()
def get_game(self, name: bytes, create=True) -> Game:
for game in self.games:
if game.bytename == name:
return game
if not create:
return False
# game doesn't exist, let's make it
g = Game(name)
self.games.add(g)
if printmode:
print(f"Game {g.name} created")
return g
def resolve_player(self, player: int, game: bytes = None, room: int = -1):
g = None
r = None
if game:
g = self.get_game(game, False)
if g:
if room != -1:
for x in g.rooms:
if x.code == room:
r = x
break
def searchR(player, r=r):
for x in r.players:
if x.code == player:
return x
return None
def searchG(player, g=g):
for r in g.rooms:
if ret := searchR(player, r):
return ret
return None
if not (g or r):
def search(player):
for g in self.games:
if ret := searchG(player, g):
return ret
return None
elif g and not r:
search = searchG
else:
search = searchR
return search(player)
if len(sys.argv) < 2:
print("must have IP and port")
exit()
ip = sys.argv[1]
p = int(sys.argv[2])
server: Server = Server((ip, p))
if len(sys.argv) > 3:
for x in sys.argv[2:]:
if x == "silent":
printmode = 0
if x == "debug":
printmode = 1
if x == "verbose":
printmode = 2
print("starting..")
server.serve_forever()