-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathserver.py
437 lines (368 loc) · 13.8 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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
"""
This file is a part of My-PyChess application.
To run the online server, run this script.
For more information, see onlinehowto.txt
IMPORTANT NOTE:
Server.py needs atleast Python v3.6 to work.
"""
import queue
import random
import socket
import threading
import time
from urllib.request import urlopen
# These are constants that can be modified by users. Default settings
# are given. Do not change if you do not know what you are doing.
LOG = False
IPV6 = False
#=====================================================
# DO NOT MODIFY ANYTHING BELOW THIS!!
#=====================================================
# Define other constants
VERSION = "v3.2.0"
PORT = 26104
START_TIME = time.perf_counter()
LOGFILENAME = time.asctime().replace(" ", "_").replace(":", "-")
# Initialise a few global variables
busyPpl = set()
end = False
lock = False
logQ = queue.Queue()
players = []
total = totalsuccess = 0
# Function to convert string to int. Doesnt raise errors, returns None instead.
def makeInt(num):
try:
return int(num)
except ValueError:
return None
# A function to display elapsed time in desired format.
def getTime():
sec = round(time.perf_counter() - START_TIME)
minutes, sec = divmod(sec, 60)
hours, minutes = divmod(minutes, 60)
days, hours = divmod(hours, 24)
return f"{days} days, {hours} hours, {minutes} minutes, {sec} seconds"
# A function to get IP address. It can give public IP or private.
def getIp(public):
if public:
try:
ip = urlopen("https://api64.ipify.org").read().decode()
except:
ip = "127.0.0.1"
else:
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
try:
s.connect(('10.255.255.255', 1))
ip = s.getsockname()[0]
except:
ip = '127.0.0.1'
return ip
# A function to Log/Print text. Used instead of print()
def log(data, key=None, adminput=False):
global logQ
if adminput:
text = ""
elif key is None:
text = "SERVER: "
else:
text = f"Player{key}: "
if data is not None:
text += data
if not adminput:
print(text)
if LOG:
logQ.put(time.asctime() + ": " + text + "\n")
else:
logQ.put(None)
# Used instead of sock.recv(), because it returns the decoded message, handles
# TCP packet loss, timeout and other useful stuff
def read(sock, timeout=None):
try:
sock.settimeout(timeout)
msg = sock.recv(8).decode("utf-8").strip()
except:
msg = "quit"
if msg:
return msg
return "quit"
# A function to message the server, this is used instead of socket.send()
# beacause it buffers the message, handles packet loss and does not raise
# exception if message could not be sent
def write(sock, msg):
if msg:
buffedmsg = msg + (" " * (8 - len(msg)))
try:
sock.sendall(buffedmsg.encode("utf-8"))
except:
pass
# Generates a random four digit number that is unique
def genKey():
key = random.randint(1000, 9999)
for player in players:
if player[1] == key:
return genKey()
return key
# Given a players key, returns the sock object for the player
# Returns None if player does not exist
def getByKey(key):
for player in players:
if player[1] == makeInt(key):
return player[0]
# Makes the player(s) busy, ie puts the player's key in a list of busy people
def mkBusy(*keys):
global busyPpl
for key in keys:
busyPpl.add(makeInt(key))
# Makes the player(s) active, ie removes the player's key from the list of busy
def rmBusy(*keys):
global busyPpl
for key in keys:
busyPpl.discard(makeInt(key))
# This simple function handles the chess match. Returns after game ended.
# Returns True if player got disconnected during the match, false otherwise.
def game(sock1, sock2):
while True:
msg = read(sock1)
write(sock2, msg)
if msg == "quit":
return True
elif msg in ["draw", "resign", "end"]:
return False
# It handles every player's communiction with server.
def player(sock, key):
while True:
msg = read(sock)
if msg == "quit":
return
elif msg == "pStat":
log("Made request for players Stats.", key)
latestplayers = list(players)
latestbusy = list(busyPpl)
if 0 < len(latestplayers) < 11:
write(sock, "enum" + str(len(latestplayers) - 1))
for _, i in latestplayers:
if i != key:
if i in latestbusy:
write(sock, str(i) + "b")
else:
write(sock, str(i) + "a")
elif msg.startswith("rg"):
log(f"Made request to play with Player{msg[2:]}", key)
oSock = getByKey(msg[2:])
if oSock is not None:
if makeInt(msg[2:]) not in busyPpl:
mkBusy(key, msg[2:])
write(oSock, "gr" + str(key))
write(sock, "msgOk")
newMsg = read(sock)
if newMsg == "ready":
log(f"Player{key} is in a game as white")
if game(sock, oSock):
return
else:
log(f"Player{key} finished the game")
elif newMsg == "quit":
write(oSock, "quit")
return
rmBusy(key)
else:
log(f"Player{key} requested busy player")
write(sock, "errPBusy")
else:
log(f"Player{key} Sent invalid key")
write(sock, "errKey")
elif msg.startswith("gmOk"):
log(f"Accepted Player{msg[4:]} request", key)
oSock = getByKey(msg[4:])
write(oSock, "start")
log(f"Player{key} is in a game as black")
if game(sock, oSock):
return
else:
log(f"Player{key} finished the game")
rmBusy(key)
elif msg.startswith("gmNo"):
log(f"Rejected Player{msg[4:]} request", key)
write(getByKey(msg[4:]), "nostart")
rmBusy(key)
# A thread to log all the texts. Flush from logQ.
def logThread():
global logQ
while True:
time.sleep(1)
with open("SERVER_LOG_" + LOGFILENAME + ".txt", "a") as f:
while not logQ.empty():
data = logQ.get()
if data is None:
return
else:
f.write(data)
# This is a Thread that runs in background to remove disconnected people
def kickDisconnectedThread():
global players
while True:
time.sleep(10)
for sock, key in players:
try:
ret = sock.send(b"........")
except:
ret = 0
if ret > 0:
cntr = 0
diff = 8
while True:
cntr += 1
if cntr == 8:
ret = 0
break
if ret == diff:
break
diff -= ret
try:
ret = sock.send(b"." * diff)
except:
ret = 0
break
if ret == 0:
log(f"Player{key} got disconnected, removing from player list")
try:
players.remove((sock, key))
except:
pass
# This is a Thread that runs in background to collect user input commands
def adminThread():
global end, lock
while True:
msg = input().strip()
log(msg, adminput=True)
if msg == "report":
log(f"{len(players)} players are online right now,")
log(f"{len(players) - len(busyPpl)} are active.")
log(f"{total} connections attempted, {totalsuccess} were successful")
log(f"Server is running {threading.active_count()} threads.")
log(f"Time elapsed since last reboot: {getTime()}")
if players:
log("LIST OF PLAYERS:")
for cnt, (_, player) in enumerate(players):
if player not in busyPpl:
log(f" {cnt+1}. Player{player}, Status: Active")
else:
log(f" {cnt+1}. Player{player}, Status: Busy")
elif msg == "mypublicip":
log("Determining public IP, please wait....")
PUBIP = getIp(public=True)
if PUBIP == "127.0.0.1":
log("An error occurred while determining IP")
else:
log(f"This machine has a public IP address {PUBIP}")
elif msg == "lock":
if lock:
log("Aldready in locked state")
else:
lock = True
log("Locked server, no one can join now.")
elif msg == "unlock":
if lock:
lock = False
log("Unlocked server, all can join now.")
else:
log("Aldready in unlocked state.")
elif msg.startswith("kick "):
for k in msg[5:].split():
sock = getByKey(k)
if sock is not None:
write(sock, "close")
log(f"Kicking player{k}")
else:
log(f"Player{k} does not exist")
elif msg == "kickall":
log("Attempting to kick everyone.")
latestplayers = list(players)
for sock, _ in latestplayers:
write(sock, "close")
elif msg == "quit":
lock = True
log("Attempting to kick everyone.")
latestplayers = list(players)
for sock, _ in latestplayers:
write(sock, "close")
log("Exiting application - Bye")
log(None)
end = True
if IPV6:
with socket.socket(socket.AF_INET6, socket.SOCK_STREAM) as s:
s.connect(("::1", PORT, 0, 0))
else:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect(("127.0.0.1", PORT))
return
else:
log(f"Invalid command entered ('{msg}').")
log("See 'onlinehowto.txt' for help on how to use the commands.")
# Does the initial checks and lets players in.
def initPlayerThread(sock):
global players, total, totalsuccess
log("New client is attempting to connect.")
total += 1
if read(sock, 3) != "PyChess":
log("Client sent invalid header, closing connection.")
write(sock, "errVer")
elif read(sock, 3) != VERSION:
log("Client sent invalid version info, closing connection.")
write(sock, "errVer")
elif len(players) >= 10:
log("Server is busy, closing new connections.")
write(sock, "errBusy")
elif lock:
log("SERVER: Server is locked, closing connection.")
write(sock, "errLock")
else:
totalsuccess += 1
key = genKey()
log(f"Connection Successful, assigned key - {key}")
players.append((sock, key))
write(sock, "key" + str(key))
player(sock, key)
write(sock, "close")
log(f"Player{key} has Quit")
try:
players.remove((sock, key))
except:
pass
rmBusy(key)
sock.close()
# Initialize the main socket
log(f"Welcome to My-Pychess Server, {VERSION}\n")
log("INITIALIZING...")
if IPV6:
log("IPv6 is enabled. This is NOT the default configuration.")
mainSock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
mainSock.bind(("::", PORT, 0, 0))
else:
log("Starting server with IPv4 (default) configuration.")
mainSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mainSock.bind(("0.0.0.0", PORT))
IP = getIp(public=False)
if IP == "127.0.0.1":
log("This machine does not appear to be connected to a network.")
log("With this limitation, you can only serve the clients ")
log("who are on THIS machine. Use IP address 127.0.0.1\n")
else:
log(f"This machine has a local IP address - {IP}")
log("USE THIS IP IF THE CLIENT IS ON THE SAME NETWORK.")
log("For more info, read file 'onlinehowto.txt'\n")
mainSock.listen(16)
log("Successfully Started.")
log(f"Accepting connections on port {PORT}\n")
threading.Thread(target=adminThread).start()
threading.Thread(target=kickDisconnectedThread, daemon=True).start()
if LOG:
log("Logging is enabled. Starting to log all output")
threading.Thread(target=logThread).start()
while True:
s, _ = mainSock.accept()
if end:
break
threading.Thread(target=initPlayerThread, args=(s,), daemon=True).start()
mainSock.close()