-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
1357 lines (1167 loc) · 60 KB
/
main.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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
""" BeaconMC - Python 3
Source for dev :
- https://wiki.vg"""
# IMPORTS - LIBRAIRIES
import math
import socket as skt
import time as tm
import random as rdm
import plugins.modulable_pluginsystem as mplsys
from typing import Literal
import threading as thread
import os
import hashlib # for md5 auth system
import platform
import pluginapi
import json
import mojangapi as m_api
import struct
import uuid
import traceback
from base64 import b64encode
try:
import nbtlib
except ModuleNotFoundError:
print("Installing missing library...")
os.system("pip install nbtlib")
import nbtlib
print("Done")
print("_________________________________________________________\nStarting BeaconMC 1.19.4\n_________________________________________________________")
dt_starting_to_start = tm.time()
lthr = []
# BASE ERROR
class OSNotCompatibleError(OSError):
pass
# CONFIG READING
print("Reading the config file...")
with open("config.txt", "r") as config:
dt = None
while dt != "":
dt = config.readline()
if dt == "":
break
dt = dt.split("=")
if dt[1][-1] == "\n":
arg = dt[1][:-1]
else:
arg = dt[1]
if dt[0] == "whitelist":
dico = {"true": False, "false": True}
public = dico[arg]
print(f"Whitelist: {arg}")
elif dt[0] == "max_players":
MAX_PLAYERS = int(arg)
print(f"Max players: {arg}")
elif dt[0] == "motd":
MOTD = arg
print(f"MOTD: {arg}")
elif dt[0] == "debug_mode":
dico = {"true": True, "false": False}
DEBUG = dico[arg]
print(f"Debug mode: {arg}")
elif dt[0] == "lang":
lang = arg
print(f"Lang: {arg}")
elif dt[0] == "online_mode":
dico = {"true": True, "false": False}
ONLINE_MODE = dico[arg]
else:
continue
print("Done.")
print("Setting up the server...")
print("Checking OS compatibility...")
COMPATIBLE_OS = ["Windows", "Linux"]
OS = platform.system()
if OS in COMPATIBLE_OS:
if OS == "Linux":
SEP = '/'
elif OS == "Windows":
SEP = "\\"
else:
raise OSNotCompatibleError(f"OS {OS} is not compatible ! Please use Linux or Windows !")
print(f"OS {OS} is compatible !")
# GLOBAL DATAS - VARIABLES
connected_players = 0
blacklist = []
whitelist = []
# public = True
users = []
logfile = ""
state = "OFF"
# GLOBAL DATAS - CONSTANTS
# ################################
# ## READ THIS ####
# ## don't touch this section ####
# ################################
SERVER_VERSION = "Alpha-dev" # Version of the server. For debug
CLIENT_VERSION = "1.19.4" # Which version the client must have to connect
PROTOCOL_VERSION = 762 # Protocol version beetween server and client. See https://minecraft.fandom.com/wiki/Protocol_version?so=search for details.
PORT = 25565 # Normal MC port
IP = "0.0.0.0"
# MAX_PLAYERS = 5
SALT_CHAR = "a-z-e-r-t-y-u-i-o-p-q-s-d-f-g-h-j-k-l-m-w-x-c-v-b-n-A-Z-E-R-T-Y-U-I-O-P-Q-S-D-F-G-H-J-K-L-M-W-X-C-V-B-N-0-1-2-3-4-5-6-7-8-9".split("-")
SALT = ''.join(rdm.choice(SALT_CHAR) for i in range(15))
CONFIG_TO_REQUEST = {"\u00A7": "\xc2\xa7", "§": "\xc2\xa7"}
# log counts
errors = 0
warnings = 0
debug = 0
info = 0
critical = 0
unknow = 0
print("")
def log(msg: str, type: int = -1):
"""Types:
- 0: info
- 1: warning
- 2: error
- 3: debug
- 4: chat
- 100: critical
- other: unknow"""
global errors, warnings, debug, info, critical, unknow
if type == 0:
t = "INFO"
info += 1
elif type == 1:
t = "WARN"
warnings += 1
elif type == 2:
t = "ERROR"
errors += 1
elif type == 3:
t = "DEBUG"
if not (DEBUG):
return
else:
debug += 1
elif type == 4:
t = "CHAT"
elif type == 100:
t = "CRITICAL"
critical += 1
else:
unknow += 1
t = "UNKNOW"
time = gettime()
text = f"[{time}] [Server/{t}]: {msg}"
print(text)
try:
with open(logfile, "+a") as file:
file.write(text + "\n")
except Exception:
print('Error in log system! Creating file... ')
os.mkdir('logs')
with open(logfile, "+w") as file:
file.write(text + "\n")
return
def gettime():
return tm.asctime(tm.localtime(tm.time())).split(" ")[-2]
def be_ready_to_log():
global logfile
nb = 1
while os.path.exists(f"logs/log{nb}.log"):
nb += 1
logfile = f"logs/log{nb}.log"
print("Log system ready !")
def encode(msg: str):
"""Convert quickly a string into bytes that will be sended to the client."""
return msg.encode()
# #######################################################################################################################################################################################################################
# #######################################################################################################################################################################################################################
# #######################################################################################################################################################################################################################
# CLASSES
class MCServer(object):
"""Minecraft server class"""
SERVER_VERSION = SERVER_VERSION
CLIENT_VERSION = CLIENT_VERSION
PROTOCOL_VERSION = PROTOCOL_VERSION
PORT = PORT
IP = IP
ONLINE_MODE = ONLINE_MODE
def __init__(self):
"""Init the server"""
self.socket = skt.socket(skt.AF_INET, skt.SOCK_STREAM) # socket creation
self.socket.bind((IP, PORT)) # bind the socket
self.list_info = []
self.list_clients = []
self.list_worlds = []
try:
with open("eula.txt", "r") as eula_file:
eula = eula_file.read().split()
if "eula=true" in eula:
pass
else:
log("You need to agree the Minecraft EULA to continue.", 1)
log("The conditions are readable here : https://www.minecraft.net/fr-ca/eula. To accept it, go to eula.txt and write 'eula=true'.", 1)
log("The server will not start until the EULA is not accepted, and if this script is modified we will not support or help you.", 1)
self.stop(False, reason="You need to accept Minecraft eula to continue.")
return
except Exception as e:
print(f"{type(e)} : {e}")
log("The eula.txt file was not found, or the server was modified !", 1)
log("You need to agree the Minecraft EULA to continue.", 2)
log("The conditions are readable here : https://www.minecraft.net/fr-ca/eula. To accept it, go to eula.txt and write 'eula=true'.", 1)
log("The server will not start until the EULA is not accepted, and if this script is modified we will not support or help you.", 1)
self.stop(False, reason="You need to agree eula to continue.")
return
def worlds_analyse(self):
"""Search for worlds in the worlds folder.
Return a list str that are the world name."""
log("Analysing worlds...", 3)
items_list = os.listdir(f"{os.getcwd()}{SEP}worlds")
lst_world = []
for item in items_list:
try:
name, extention = item.split(".")
except ValueError:
continue
if extention == ".mcworld":
lst_world.append(name)
log(f"{len(lst_world)} worlds found !", 3)
return lst_world
def log(self, msg: str, type: int = -1):
"""An alternative of main.log(). Don't delete, used by plugins."""
log(msg, type)
def start(self):
global state
"""Start the server"""
log("Starting Minecraft server...", 0)
state = "ON"
log(f"Server version: {SERVER_VERSION}", 3)
log(f"MC version: {CLIENT_VERSION}", 3)
log(f"Protocol version: {PROTOCOL_VERSION}", 3)
# self.heartbeat()
log("Loading plugins...", 0)
self.load_plugins()
log("Starting console GUI...", 0)
self.gui = ConsoleGUI()
self.gui_thr = thread.Thread(target=self.gui.mainthread)
self.gui_thr.start()
lthr.append(self.gui_thr)
log("Starting listening...", 0)
self.socket.listen(MAX_PLAYERS + 1) # +1 is for the temp connexions
self.load_worlds()
self.act = thread.Thread(target=self.add_client_thread)
self.act.start()
lthr.append(self.act)
self.main()
def load_plugins(self):
"""Load the plugins"""
self.PLUGIN_ALLOWED = mplsys.ENABLE_PLUGINS
self.PLUGIN_LIST = mplsys.PLUGIN_LIST
for pl in self.PLUGIN_LIST:
pl.set_server(self)
if pl._on_load():
pl.on_load()
def load_worlds(self):
"""Load all of the server's worlds"""
log("Loading worlds...", 0)
pre_list_worlds = self.worlds_analyse()
for world in pre_list_worlds:
w_class = World(world)
w_class.load()
self.list_worlds.append(w_class)
log(f"DONE ! Server successfully started on {round(tm.time() - dt_starting_to_start, 2)} seconds.", 0)
def main(self):
"""Main"""
global state
try:
while state == "ON":
tm.sleep(0.1)
except KeyboardInterrupt:
self.stop()
exit(0)
def stop(self, critical_stop=False, reason="Server closed", e: Exception=None):
"""stop the server"""
if critical_stop:
log("Critical server stop trigered !", 100)
log("Stopping the server...", 0)
global state
state = "OFF"
global lthr
log("Disconnecting all the clients...", 0)
if critical_stop:
for i in self.list_clients:
i: Client
i.disconnect(reason=tr.key("disconnect.server_crashed"))
else:
for i in self.list_clients:
i.disconnect(reason=tr.key("disconnect.server_closed"))
log("Closing socket...", 0)
self.socket.close()
log("Stopping all tasks...", 0)
for t in lthr:
t: thread.Thread
t.join()
...
if not (critical_stop):
log(f"Server closed with {critical} criticals, {errors} errors, {warnings} warnings, {info} infos and {unknow} unknown logs : {reason}", 0)
exit()
else:
log(f"Server closed with {critical} criticals, {errors} errors, {warnings} warnings, {info} infos and {unknow} unknown logs : {reason}", 100)
self.crash(reason, e)
exit(-1)
def crash(self, reason, e: Exception = None):
"""Generate a crash report
Arg:
- reason: str --> The crash message"""
c = 0
try:
import datetime
t = traceback.format_exc()
except Exception:
t = None
while os.path.exists("crash_reports/crash{0}".format(c)):
c += 1
with open("crash_reports/crash{0}".format(c), "w") as crashfile:
crashfile.write(f"""{datetime.datetime.now()}\nBeaconMC {SERVER_VERSION}\nFor Minecraft {CLIENT_VERSION}\n________________________________________________________________________________________________________________\nCritical error, the server had to stop. This crash report contain informations about the crash.\n________________________________________________________________________________________________________________\nCause of the crash : {reason}\n{traceback.format_exc(e)}\nDebug mode : {DEBUG}\n________________________________________________________________________________________________________________\n{t}""")
def add_client_thread(self):
"""Thread for add clients."""
global state
self.client_id_count = 0
while state == "ON":
try:
client_connection, client_info = self.socket.accept()
except OSError:
tm.sleep(0.1)
continue
cl = Client(client_connection, client_info, self)
# self.list_clients.append(cl)
thr = thread.Thread(target=cl.client_thread, args=[self.client_id_count])
thr.start()
lthr.append(thr)
self.client_id_count += 1
tm.sleep(0.1)
def is_premium(self, username: str):
"""Check if the user is a premium user. Return a boolean"""
import libs.mojangapi as mojangapi
accchecker = mojangapi.Accounts()
return accchecker.check(self.username)
def setblock(self, base_request: bytes):
"""Analyse the setblock request and modify a block"""
id = base_request[0]
x = base_request[1:2]
y = base_request[3:4]
z = base_request[5:6]
mode = base_request[7]
block = base_request[8]
# check the request
if id != "\x05":
log("A non-setblock request was sent to the bad method. Something went wrong. Please leave an issue on GitHub or on Discord !", 100)
self.stop(critical=True, reason="A non-setblock request was sent to the bad method. Something went wrong. Please leave an issue on GitHub or on Discord !")
raise RequestAnalyseException("Exception on analysing a request : bad method used (setblock andnot unknow).")
# TODO: Modify the block in the world
...
# TODO: send to every clients the modification
...
def post_to_chat(self, message: str, author: str = ""):
"""Post a message in all player's chat
Args:
- message: str --> the message to send
- author: str --> the author of the message: by default ""."""
if author == "":
msg = message
else:
msg = f"<{author}>: {message}"
for p in self.list_clients:
p: Client
p.send_msg_to_chat(msg)
log(msg, 4)
def mp(self, message: str, addressee: str, author: str):
"""Send a mp to 1 player
Args:
- message:str --> the message to send in mp
- addressee:str --> player that will receive msg
- author:str --> the author of the msg: by default ""."""
pl = self.find_player_by_username(addressee)
msg = f"{author} --> you: {message}"
pl.send_msg_to_chat(msg)
log(f"{author} --> {pl}: {message}", 4)
for pl in self.PLUGIN_LIST:
pl.on_mp(message=message, source=author, addressee=addressee)
def find_player_by_username(self, username:str):
"""Find the player socket with the username.
Arg:
- username:str --> the username of the player.
- Returns player socket
- return None if no player found
- Raise error if more than 1 player is found"""
lst = []
for p in self.list_clients:
if p.username == username:
lst.append(p)
if len(lst) == 0:
return None
elif len(lst) == 1:
return lst[0]
else:
raise TwoPlayerWithSameUsernameException(f"2 players with the same username {username} were found.")
class PacketException(Exception):
pass
class Packet(object):
# DANGER | DO NOT TOUCH
SEGMENT_BITS = 0x7F
CONTINUE_BIT = 0x80
def __init__(self, socket: skt.socket,
direction: Literal["-OUTGOING", "-INCOMING"], typep: hex = None,
packet: bytes = None, args: tuple = None):
self.type = typep
self.socket = socket
self.direction = direction
self.packet = packet
self.args = args
# if packet == None or b"" and typep == None:
# raise PacketException(f"No information provided in the Packet instance {self}")
if direction == "-INCOMING":
self.incoming_packet()
elif direction == "-OUTGOING":
self.outgoing_packet()
def incoming_packet(self):
self.unpack()
def unpack_uuid(self, uuid):
if len(uuid) != 16:
raise ValueError(f"invalid lenth {len(uuid)} for binary uuid")
hex_uuid = uuid.hex()
uuid_format = f"{hex_uuid[:8]}-{hex_uuid[8:12]}-{hex_uuid[12:16]}-{hex_uuid[16:20]}-{hex_uuid[20:]}"
return uuid_format
def wait_for_packet(self):
if self.type == "-INCOMING":
self.lenth = self.unpack_varint(self.socket.recv(1))
tc = self.lenth
if self.lenth <= 0:
raise PacketException("NullPacket")
self.id = self.unpack_varint(self.socket.recv(1))
tc -= 1
if self.id == 0:
# 2 more possibles cases
...
else:
raise PacketException("Wating to receive packet in -OUTGOING mode")
def unpack(self):
lenth = self.packet[0]
id = self.packet[1]
other = self.packet[2:]
self.type = id
self.args = other
return lenth
def outgoing_packet(self):
...
def pack_varint(self, d: int):
o = b""
# if d >= 255:
# o = d.to_bytes(2, byteorder="little")
# else:
# test
if True:
while True:
b = d & 0x7F
d >>= 7
o += struct.pack("B", b | (0x80 if d > 0 else 0))
if d == 0:
break
return o
def unpack_varint(data):
d = 0
for i in range(5):
b = data[i]
d |= (b & 0x7F) << (7 * i)
if not b & 0x80:
break
return d
def pack_data(self, d):
h = self.pack_varint(len(d))
if isinstance(d, str):
d = bytes(d, "utf-8")
return h + d
def send(self):
if self.direction == "-OUTGOING":
self.socket.send(self.__repr__())
else:
raise PacketException("Incoming packet tryied to be sended")
def __repr__(self) -> bytes:
out = self.pack_varint(self.type) # pack the type
for i in self.args:
if isinstance(i, int):
out += self.pack_varint(len(self.pack_varint(i))) + self.pack_varint(i)
elif isinstance(i, UUID):
out += (self.pack_varint(1) + self.pack_uuid(i))
elif isinstance(i, bool):
if i:
out += b"\x01"
else:
out += b"\x00"
elif isinstance(i, tuple):
...
else:
out += self.pack_data(i)
out = self.pack_varint(len(out)) + out
return out
def pack_uuid(self, uuidObject):
uuid = uuidObject.uuid
hex_uuid = uuid.replace('-', '')
binaire_uuid = bytes.fromhex(hex_uuid)
return binaire_uuid
def __str__(self):
return self.__repr__().decode()
########################################################################################################################################################################################################################
########################################################################################################################################################################################################################
########################################################################################################################################################################################################################
class UUID(object):
def __init__(self, uuid):
self.uuid = uuid
class Client(object):
def __init__(self, connexion: skt.socket, info, server: MCServer):
self.connexion = connexion
self.info = info
self.server = server
self.is_op = False
self.x = None
self.y = None
self.z = None
self.connected = True
self.protocol_state = "Handshaking"
def on_heartbeat(self):
"""Id of the packet: 0x00"""
client_protocol_version = self.request[1:]
...
def on_login_start(self):
"""Starting the login in (rq C --> S)"""
self.request
...
def client_thread(self, id):
"""Per client thread"""
self.id = id
try:
log(f"New client from {self.info}", 3)
log("Starting listening loop in Handshaking state", 3)
# Ping / Auth loop (to developp)
d_reason = "None"
misc_d = True
while self.connected and state == "ON":
# Auth loop
try:
lenth = self.connexion.recv(1)
self.request = lenth + self.connexion.recv(Packet.unpack_varint(lenth))
except ConnectionResetError:
log(f"Client {self.info} disconnected : Connexion reset.")
if self.request == "":
continue
log(self.request, 3)
self.packet = Packet(self.connexion, "-INCOMING", packet=self.request)
if self.packet.type == 0 and self.protocol_state == "Handshaking":
# Handshake
if self.packet.args[-1] == 1:
# Switch protocol state to status
self.protocol_state = "Status"
log(f"Switching to Status state for {self.info}", 3)
continue
elif self.packet.args[-1] == 2:
# Switch protocol state to login
self.protocol_state = "Login"
log(f"Switching to login state for {self.info}", 3)
continue
elif self.packet.args[-1] == 3:
# Switch protocol state to transfer
self.protocol_state = "Transfer"
log(f"Switching to transfer state for {self.info}", 3)
continue
else:
self.connected = False
self.connected = False
log(f"Disconnecting {self.info} : protocol error (unknow next state {self.packet.args[-1]} in handshake)", 3)
break
elif self.packet.type == 0 and self.protocol_state == "Status":
# Status request -> Status response (SLP)
self.SLP()
continue
elif self.packet.type == 0 and self.protocol_state == "Login":
unamelenth = self.packet.args[0]
i = 1
self.username = ""
while i <= unamelenth:
sb = self.packet.args[i:i+1]
self.username += sb.decode("utf-8")
i += 1
self.uuid = self.packet.unpack_uuid(uuid=self.packet.args[i+1:])
log(f"UUID of {self.username} is {self.uuid}.", 0)
log(f"{self.username} is logging in from {self.info}.", 0)
if len(self.server.list_clients) >= MAX_PLAYERS:
self.connected = False
misc_d = False
d_reason = "Server full"
continue
if not(public):
with open ("whitelist.json", "r") as wf:
data = json.loads(wf.read())
o = 0
for d in data:
if d["uuid"] == self.uuid:
o += 1
if o != 1:
self.connected = False
d_reason = "You are not whitelisted on this server."
misc_d = False
if o > 1:
log("User is whitelisted more than 1 time !", 1)
continue
if ONLINE_MODE:
# TODO Encryption Request
...
api_system = m_api.Accounts()
check_result = api_system.authenticate(self.username, self.uuid)
if check_result[0]:
log(f"sucessfully authenticated {self.username}.", 3)
pass
else:
log(f"Failed to authenticate {self.info} using uuid {self.uuid} and username {self.username}.", 1)
self.connected = False
d_reason = "Failed to login"
misc_d = False
# TODO Enable compression (would be optional) (in other "if" fork)
...
else:
# load player properties
...
self.properties = ()
enc_properties = []
for p in self.properties:
enc_properties.append(p["name"])
enc_properties.append(p["value"])
enc_properties.append(False)
parg = [UUID(self.uuid), self.username, len(self.properties)]
for p in enc_properties:
parg.append(p)
parg.append(not(DEBUG))
response = Packet(self.connexion, "-OUTGOING", 2, args=parg)
log(response.__repr__(), 3)
response.send()
self.server.list_clients.append(self)
break
elif self.packet.type == 1 and self.protocol_state == "Status":
payload = self.packet.args[0]
self.ping_response(payload)
self.connected = False
break
###############################################################################
if not(self.connected and state == "ON"):
# If server is stopping or the client is disconnecting (usefull ?)
if misc_d:
log(f"Disconnecting {self.info} for some misc reasons.", 3)
else:
dp = Packet(self.connexion, "-OUTGOING", typep=27, args=(f'\{"text":"{d_reason}"\}', ))
log(f"{self.username} lost connexion: {d_reason}.", 0)
self.connexion.close()
return
###############################################################################
log(f"{self.username} joined the game.", 0)
self.server.post_to_chat(f"{self.username} joined the game")
while self.connected and state == "ON":
# to clean
l = self.connexion.recv(1)
self.packet = Packet(self.connexion, "-INCOMING", packet=self.request)
...
# if self.request[0] == "\x05":
# #setblock message
# self.server.setblock(self.request)
# elif self.request[0] == "\x08":
# #pos message
# self.update_pos()
# elif self.request[0] == "\x0d":
# #chat message
# if self.request[2] == "/":#surely not that
# ... #cmd
# self.server.post_to_chat(author=self.username, message=self.request[1:])
# elif self.request[:4] == "\x13\x00\xf2\x05\x0c":
# if self.request[-5:] == "\xd5\x11\x01\x01\x00":
# #server list request
# self.connexion.send(bytes('\xca\x01\x00\xc7\x01{"previewsChat":false,"description":{"text":"{0}"},"players":{"max":{1},"online":{2}},"version":{"name":"{3}","protocol":{4}}}'.format(self.treat(MOTD), MAX_PLAYERS, connected_players, CLIENT_VERSION, PROTOCOL_VERSION)))
# else:
# c = -1
# u = ""
# while self.request[c] != "\x0f":
# u = self.request[c] + u
# c -= 1
# self.username = u
# self.joining()
self.server.list_clients.remove(self)
except ConnectionAbortedError:
log(f"Connexion aborted by client {self.info} ({self.username})", 0)
self.connexion.close()
self.connected = False
except Exception as e:
import traceback
log(f"{traceback.format_exc()}", 2)
self.connected = False
dp = Packet(self.connexion, "-OUTGOING", typep=27, args=('{"text":"Internal server error"}', ))
def ping_response(self, payload):
"""Send a response to a ping to make the client get the ping in ms of the server."""
response = Packet(self.connexion, "-OUTGOING", typep=1, args=(payload,))
response.send()
def status_request(self):
...
def bad_version(self):
"""Called to disconnect the connecting client that has a bad protocol version"""
log("A client used a bad version. Disconnecting this client...", 0)
self.connexion.send(encode(f'E\x00C{"translate":"multiplayer.disconnect.incompatible","with":["{CLIENT_VERSION}"]}'))
self.connected = False
self.connexion.close()
self.server.list_client.remove(self)
log("Client disconnected: bad Minecraft version", 0)
del(self)
def treat(self, msg):
"""Check and modify the message gived in argument in the goal of be readable by the client."""
final = ""
for char in msg:
try:
final += CONFIG_TO_REQUEST[char]
except IndexError:
final += char
return final
def update_pos(self):
"""update the client pos with the request"""
...
def unpack_uuid(self, d:bytes):
msb = struct.unpack('>Q', d[:8])[0]
lsb = struct.unpack('>Q', d[8:])[0]
return uuid.UUID(int=(msb << 64) | lsb)
def joining(self):
"""When the request is a joining request"""
self.username = self.packet.args[0]
self.uuid = self.packet.args[1]
log(f"Player {self.username} with uuid {self.uuid} is loging in !")
if connected_players >= MAX_PLAYERS:
r = tr.key("disconnect.server_full")
log(f"Disconnecting {self.username}: {r}", 0)
self.disconnect(tr.key("disconnect.server_full"))
return
# HOW TO GET THE PROTOCOL VERSION ?
if not(PROTOCOL_VERSION == PROTOCOL_VERSION):
r = tr.key("disconnect.bad_protocol")
log(f"Disconnecting {self.username} : {r}.", 0)
self.bad_version()
return
if not(self.server.is_premium(self.username)):
r = tr.key("disconnect.not_premium")
log(f"Disconnecting {self.username} : {r}.", 0)
self.disconnect(tr.key("disconnect.not_premium"))
return
if ONLINE_MODE:
p_response = Packet(self.connexion, direction="-OUTGOING", typep=1, args=("serverid", b"publick key", "verify token", ONLINE_MODE))
log(p_response, 3)
p_response.send()
packet_r = Packet(self.connexion, "-OUTGOING", typep=2, args=(self.uuid, self.username, 0, False))
log(packet_r, 0)
packet_r.send()
"""
self.p_version = self.request[1]
self.username = self.request[1:64]
self.key = self.request[65:129]
log(f"Joining request from {self.username} !", 0)
if self.server.ispremium(self.username, self.key):
log(f"{self.username} is premium.", 0)
if connected_players < MAX_PLAYERS:
if self.p_version == PROTOCOL_VERSION:
log(f"Connexion accepted for {self.username}")
#co ok !
...
else:
log(f"Failed to connect {self.username} : bad version.", 0)
#self.disconnect(tr.key("disconnect.bad_protocol")) :-(
self.bad_version()
else:
log(f"Failed to connect {self.username} : server full.", 1)
self.disconnect(tr.key("disconnect.server_full"))
else:
log(f"User {self.username} is not premium ! Access couldn't be gived.", 1)
self.disconnect(tr.key("disconnect.not_premium"))"""
def disconnect(self, reason=""):
"""Disconnect the player
!!! not disconnectED !!!"""
self.connected = False
if reason == "":
reason = tr.key("disconnect.default")
self.connexion.send(f"\x0e{reason}".encode("utf-8"))
self.connexion.close()
self.server.list_clients.remove(self)
del(self)
def do_spawn(self):
"""Make THIS CLIENT spawn"""
...
#self.connexion.send()
def identification(self):
"""Send id packet to the client"""
opdico = {True:bytes("\x64"), False: bytes("\x00")}
self.connexion.send(f"\x00{bytes(PROTOCOL_VERSION)}{bytes('Python Server 1.19.4')}{bytes(MOTD)}{opdico[self.is_op]}".encode())
def ping(self):
"""Ping sent to clients periodically."""
self.connexion.send("\x01".encode())
def int_to_hex_escape(n):
if n < 0:
raise ValueError("L'entier doit être positif.")
hex_string = n.to_bytes((n.bit_length() + 7) // 8, 'big').hex()
escaped_string = ''.join(f'\\x{hex_string[i:i+2]}' for i in range(0, len(hex_string), 2))
return escaped_string
def SLP(self):
log("Received ping", 3)
try :
with open('server-icon.png', 'rb') as image_file :
favicon = b64encode(image_file.read()).decode('utf-8')
except FileNotFoundError:
log("Server icon not found, using default BeaconMC icon", 1)
favicon = "iVBORw0KGgoAAAANSUhEUgAAAE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CGPn2bz+b22z/HDTdcx7GxU/zr/sc5OjIGAq7csZ1377mEdZ3tb9hJcXiDAIBa2UpTdFwODp7kp4depOj6pOMxLuk7myvO62d2Osv/+vJXePzQ87zjY7dh5rIUslmeePBnfOpPP8kHbvk9BodHeWD/ozx99ASJmM3OLf3ceOXlrOvqqAj8Bj848aY8MaIB1/N46ugJHn/5GFO5AqmYzds2ncXl5/YxemqCHx96iemREWKG4D98/MOcGJ/gvn2PMTE5hWVIdmw7l2t2X8LajrY31OKvHG/6Q1NBEPLciSEePvQSs4USnekk3S2NjM7MMzU6AqFPe0sLJ8YnCIVgz7at3LD3sjpze7MfnnpLHpvTGoIw5KWRMV44Pszo7BwAU2MjOMUipmGw58ILuOLinaxpb31TLf7K8ZY9NwjUe44nxid48sggAwMDbFzbxbWX76Gtpbki0Fv0uFxtvKUALB1RpCg6ZRpSqYogb7HitfFbA+D/l/E7//D0/wNnCsR1zHM6iQAAAABJRU5ErkJggg=="
response = {
"version": {"name": CLIENT_VERSION, "protocol": PROTOCOL_VERSION},
"players": {"max": MAX_PLAYERS, "online": len(self.server.list_clients), "sample": [{"name": "FewerElk", "id": "16dcb929-b271-4db3-9cc6-059a851fcce1"}]},
"description": {"text": MOTD},
"favicon": "data:image/png;base64," + favicon,
"modinfo": {"type": "FML", "modlist": []},
"enforcesSecureChat": True,
"previewsChat": True
}
response_str = json.dumps(response)
packet_response = Packet(socket=self.connexion, direction="-OUTGOING", typep=0, args=(response_str, ))
packet_response.send()
def on_SLP(self):
log("Event 'on server list ping' triggered !", 3)
request = f'\xca\x01\x00\xc7\x01\u007b"previewsChat":false,"description":\u007b"text":"{MOTD}"\u007d,"players":\u007b"max":{MAX_PLAYERS},"online":{len(self.server.list_clients)}\u007d,"version":\u007b"name":"{CLIENT_VERSION}","protocol":{PROTOCOL_VERSION}\u007d\u007d'
request = encode(request)
self.connexion.send(request, 1024)
# self.connexion.send(f'0x01{"version":{"name":"1.19.4","protocol":762},"players":{"max":100,"online":5,"sample":[{"name":"thinkofdeath","id":"4566e69f-c907-48ee-8d71-d7ba5aa00d20"}]},"description":{"text":"Hello world"},"favicon":"data:image/png;base64,<data>","enforcesSecureChat":true,"previewsChat":true}')
def send_msg_to_chat(self, msg: str):
"""Post a message in the player's chat.
Argument:
- msg:str --> the message to post on the chat"""
packet = Packet(self.connexion, "-OUTGOING", 108, args=("{'color':'#FFDE59', 'text':'" + self.username + "joined the game.}", False))
print(packet.__repr__())
packet.send()
########################################################################################################################################################################################################################
########################################################################################################################################################################################################################
########################################################################################################################################################################################################################
class NeoWorld(object):
def __init__(self, name, level=-1):
"""Args:
- name: the name of the world (str). Used to load and save worlds.
- level: the type of the world (int). Can be 0 (overworld), 1 (nether) or 2 (end). -1 by default. -1 is for unknow level (when loading for example)"""
self.name = name
if level == -1:
self.level = None
else:
self.level = level
self.loaded = False
self.generated = None
self.BASE = f"{os.getcwd()}{SEP}worlds{SEP}{name}{SEP}"
self.data = None
self.spawn_coord = None
def load(self):
"""Load the world"""
# Load world settings
nbt_file = nbtlib.load(self.BASE + "level.dat")
self.difficulty = nbt_file["Data"]["Difficulty"]
self.wonderingtraderspawnchance = nbt_file["Data"]["WanderingTraderSpawnChance"]
...
self.regions = []