forked from meower-media/server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudlink.py
1264 lines (1175 loc) · 81.3 KB
/
cloudlink.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
#!/usr/bin/env python3
version = "0.1.7.6"
# Server based on https://github.com/Pithikos/python-websocket-server
# Client based on https://github.com/websocket-client/websocket-client
"""
CloudLink by MikeDEV
Please see https://github.com/MikeDev101/cloudlink for more details.
"""
import json
import sys
import threading
from websocket_server import WebsocketServer as ws_server
import websocket as ws_client
import time
import traceback
import sys
"""
Code formatting
(Type):(Code) | (Description)
Type: Letter
I - Info
E - Error
Code: Number, defines the code
Description: String, Describes the code
"""
def full_stack():
exc = sys.exc_info()[0]
if exc is not None:
f = sys.exc_info()[-1].tb_frame.f_back
stack = traceback.extract_stack(f)
else:
stack = traceback.extract_stack()[:-1]
trc = 'Traceback (most recent call last):\n'
stackstr = trc + ''.join(traceback.format_list(stack))
if exc is not None:
stackstr += ' ' + traceback.format_exc().lstrip(trc)
return stackstr
class API:
def server(self, ip="127.0.0.1", port=3000, threaded=False): # Runs CloudLink in server mode.
try:
if self.state == 0:
# Change the link state to 1 (Server mode)
self.state = 1
self.wss = ws_server(
host=ip,
port=port
)
# Set the server's callbacks to CloudLink's class functions
self.wss.set_fn_new_client(self._on_connection_server)
self.wss.set_fn_client_left(self._closed_connection_server)
self.wss.set_fn_message_received(self._on_packet_server)
# Format dict for storing this mode's specific data
if (not "motd" in self.statedata) or (not "motd_enable" in self.statedata):
self.statedata["motd_enable"] = False
self.statedata["motd"] = ""
if (not "secure_enable" in self.statedata) or (not "secure_keys" in self.statedata):
self.statedata["secure_enable"] = False
self.statedata["secure_keys"] = []
if not "ip_blocklist" in self.statedata:
self.statedata["ip_blocklist"] = [""]
self.statedata = {
"ulist": {
"usernames": {},
"objs": {}
}, # Username list for the "Usernames" block
"secure_enable": False, # Trusted Access enabler
"secure_keys": [], # Trusted Access keys
"gmsg": "", # Global data stream
"motd_enable": self.statedata["motd_enable"], # MOTD enabler
"motd": self.statedata["motd"], # MOTD text
"secure_enable": self.statedata["secure_enable"], # Trusted Access enabler
"secure_keys": self.statedata["secure_keys"], # Trusted Access keys
"trusted": [], # Clients that are trusted with Secure Access, references memory objects only
"ip_blocklist": self.statedata["ip_blocklist"] # Blocks clients with certain IP addresses
}
# Run the server
print("Running server on ws://{0}:{1}/".format(ip, port))
self.wss.run_forever(threaded=threaded)
else:
if self.debug:
print("Error: Attempted to switch states!")
except Exception as e:
if self.debug:
print("Error at client: {0}".format(e))
def client(self, ip="ws://127.0.0.1:3000/"): # Runs CloudLink in client mode.
try:
if self.state == 0:
# Change the link state to 2 (Client mode)
self.state = 2
self.wss = ws_client.WebSocketApp(
ip,
on_message = self._on_packet_client,
on_error = self._on_error_client,
on_open = self._on_connection_client,
on_close = self._closed_connection_client
)
# Format dict for storing this mode's specific data
self.statedata = {
"ulist": {
"usernames": []
},
}
# Run the client
self.wss.run_forever()
else:
if self.debug:
print("Error: Attempted to switch states!")
except Exception as e:
if self.debug:
print("Error at client: {0}".format(e))
def stop(self, abrupt=False): # Stops CloudLink (not sure if working)
try:
if self.state == 1:
if abrupt:
self.wss.shutdown_abruptly()
else:
self.wss.shutdown_gracefully()
self.state = 0
elif self.state == 2:
self.wss.close()
else:
if self.debug:
print("Error: Attempted to stop in an invalid mode")
except Exception as e:
if self.debug:
print("Error at stop: {0}".format(e))
def callback(self, callback_id, function): # Add user-friendly callbacks for CloudLink to be useful as a module
try:
if callback_id in self.callback_function:
self.callback_function[callback_id] = function
if self.debug:
print("Binded callback {0}.".format(callback_id))
else:
if self.debug:
print("Error: Callback {0} is not a valid callback id!".format(callback_id))
except Exception as e:
if self.debug:
print("Error at callback: {0}".format(e))
def trustedAccess(self, enable, keys): # Enables secure access to the server.
if type(enable) == bool:
if type(keys) == list:
if enable:
if self.debug:
print("Enabled Trusted Access.")
else:
if self.debug:
print("Disabled Trusted Access.")
self.statedata["secure_enable"] = enable
self.statedata["secure_keys"] = keys
else:
if self.debug:
print('Error: Cannot set Trusted Access keys: expecting <class "list">, got {0}'.format(type(enable)))
else:
if self.debug:
print('Error: Cannot set Trusted Access enable: expecting <class "bool">, got {0}'.format(type(enable)))
def sendPacket(self, msg): # User-friendly message sender for both server and client.
try:
if self.state == 1:
if ("id" in msg) and (type(msg["id"]) == dict): # Server is probably passing along the memory object for reference
if self.debug:
print("Info on sendPacket: Server passed along memory object:", msg["id"]["id"], "will try to send packet directly")
try:
client = msg["id"]
del msg["id"]
if self.debug:
print('Sending {0} to {1}'.format(msg, client["id"]))
if self._get_client_type(client) == "scratch":
if ("val" in msg) and (type(msg["val"]) == dict):
msg["val"] = json.dumps(msg["val"])
self.wss.send_message(client, json.dumps(msg))
except Exception as e:
if self.debug:
print("Error on sendPacket (server): {0}".format(full_stack()))
elif ("id" in msg) and (type(msg["id"]) == str) and (msg["cmd"] not in ["gmsg", "gvar"]):
id = msg["id"]
del msg["id"]
if id in self.statedata["ulist"]["usernames"]:
try:
client = self.statedata["ulist"]["objs"][self.statedata["ulist"]["usernames"][id]]["object"]
if self.debug:
print('Sending {0} to {1}'.format(msg, id))
if self._get_client_type(client) == "scratch":
if ("val" in msg) and (type(msg["val"]) == dict):
msg["val"] = json.dumps(msg["val"])
self.wss.send_message(client, json.dumps(msg))
except Exception as e:
if self.debug:
print("Error on sendPacket (server): {0}".format(e))
else:
try:
if self.debug:
print('Sending "{0}" to all clients'.format(json.dumps(msg)))
self._send_to_all(msg)
except Exception as e:
if self.debug:
print("Error on sendPacket (server): {0}".format(e))
elif self.state == 2:
try:
if self.debug:
print('Sending {0}'.format(json.dumps(msg)))
self.wss.send(json.dumps(msg))
except Exception as e:
if self.debug:
print("Error on sendPacket (client): {0}".format(e))
else:
print("Error: Cannot use the packet sender in current state!")
except Exception as e:
print("Error at sendPacket: {0}".format(e))
def setMOTD(self, motd, enable=True): # Sets the MOTD on the server-side.
try:
if type(enable) == bool:
if type(motd) == str:
if enable:
print('Set MOTD to "{0}".'.format(motd))
self.statedata["motd"] = str(motd)
self.statedata["motd_enable"] = True
else:
print("Disabled MOTD.")
self.statedata["motd"] = None
self.statedata["motd_enable"] = False
else:
print('Error: Cannot set MOTD text: expecting <class "str">, got {0}'.format(type(enable)))
else:
print('Error: Cannot set the enabler for MOTD: expecting <class "bool">, got {0}'.format(type(enable)))
except Exception as e:
if self.debug:
print("Error at setMOTD: {0}".format(e))
def getUsernames(self): # Returns the username list.
if self.state == 1:
return list((self.statedata["ulist"]["usernames"]).keys())
elif self.state == 2:
return self.statedata["ulist"]["usernames"]
else:
return None
def getIPofUsername(self, user): # Allows the server to track user IPs for Trusted Access, uses the username of a client.
if self.state == 1:
if not self._get_obj_of_username(user) == None:
return self._get_ip_of_obj(self._get_obj_of_username(user))
else:
if self.debug:
print("Error: Cannot use the IP getter in current state!")
return ""
def getIPofObject(self, obj): # Allows the server to track user IPs for Trusted Access, but uses the memory object of a client instead.
if self.state == 1:
return self._get_ip_of_obj(obj)
else:
if self.debug:
print("Error: Cannot use the IP getter in current state!")
return ""
def untrust(self, obj): # If a client has been trusted, the server can loose trust and refuse future packets.
if self.state == 1:
if self.statedata["secure_enable"]:
if type(obj) == dict:
if obj in self.statedata["trusted"]:
self.statedata["trusted"].remove(obj)
if self.debug:
print("Untrusted ID {0}.".format(obj["id"]))
else:
if self.debug:
print("Unable to untrust an ID that does not exist")
elif type(obj) == str:
obj = self._get_obj_of_username(obj)
if not obj == None:
if obj in self.statedata["trusted"]:
self.statedata["trusted"].remove(obj)
if self.debug:
print("Untrusted ID {0}.".format(obj["id"]))
else:
if self.debug:
print("Unable to untrust an ID that does not exist")
else:
if self.debug:
print("Unable to untrust an ID that does not exist")
else:
if self.debug:
print("Error: Cannot use the untrust function: Trusted Access not enabled!")
else:
if self.debug:
print("Error: Cannot use the untrust function in current state!")
def loadIPBlocklist(self, blist): # Loads a list of IP addresses to block
if type(blist) == list:
if not '' in blist:
blist.append("")
self.statedata["ip_blocklist"] = blist
if self.debug:
print("Loaded {0} blocked IPs into the blocklist!".format(len(self.statedata["ip_blocklist"])-1))
def blockIP(self, ip): # Blocks an IP address
if self.state == 1:
if self.statedata["secure_enable"]:
if type(ip) == str:
if not ip in self.statedata["ip_blocklist"]:
self.statedata["ip_blocklist"].append(ip)
if self.debug:
print("Blocked IP {0}!".format(ip))
else:
if self.debug:
print("Error: Cannot use the IP Block function in current state!")
def unblockIP(self, ip): # Unblocks an IP address
if self.state == 1:
if self.statedata["secure_enable"]:
if type(ip) == str:
if ip in self.statedata["ip_blocklist"]:
self.statedata["ip_blocklist"].remove(ip)
if self.debug:
print("Unblocked IP {0}!".format(ip))
else:
if self.debug:
print("Error: Cannot use the IP Block function in current state!")
def getIPBlocklist(self): # Returns the latest IP blocklist
if self.state == 1:
if self.statedata["secure_enable"]:
tmp = self.statedata["ip_blocklist"]
tmp.remove('')
return self.statedata["ip_blocklist"]
else:
if self.debug:
print("Error: Cannot use the IP Blocklist get function in current state!")
return []
def kickClient(self, obj): # Terminates a client's connection (should only be used for specific purposes)
if self.state == 1:
if self.statedata["secure_enable"]:
if type(obj) == dict:
if obj["id"] in self.statedata["ulist"]["objs"]:
# Ask the WebsocketServer to terminate the connection
obj["handler"].send_close(1000, bytes('', encoding='utf-8'))
if self.debug:
print("Kicked ID {0}.".format(obj["id"]))
else:
if self.debug:
print("Unable to kick an ID that does not exist")
elif type(obj) == str:
obj = self._get_obj_of_username(obj)
if not obj == None:
if obj["id"] in self.statedata["ulist"]["objs"]:
# Ask the WebsocketServer to terminate the connection
obj["handler"].send_close(1000, bytes('', encoding='utf-8'))
if self.debug:
print("Kicked ID {0}.".format(obj["id"]))
else:
if self.debug:
print("Unable to kick an ID that does not exist")
else:
if self.debug:
print("Unable to kick an ID that does not exist")
else:
if self.debug:
print("Error: Cannot use the kick function: Trusted Access not enabled!")
else:
if self.debug:
print("Error: Cannot use the kick function in current state!")
"""
class CLTLS: #Feature NOT YET IMPLEMENTED
def __init__(self):
pass
"""
class CloudLink(API):
def __init__(self, debug=False): # Initializes CloudLink
self.wss = None # Websocket Object
self.state = 0 # Module state
self.userlist = [] # Stores usernames set on link
self.callback_function = { # For linking external code, use with functions
"on_connect": None, # Handles new connections (server) or when connected to a server (client)
"on_error": None, # Error reporter
"on_packet": None, # Packet handler
"on_close": None # Runs code when disconnected (client) or server stops (server)
}
self.debug = debug # Print back specific data
self.statedata = {} # Place to store other garbage for modes
self.codes = { # Current set of CloudLink status/error self.codes
"Test": "I:000 | Test", # Test code
"OK": "I:100 | OK", # OK code
"Syntax": "E:101 | Syntax",
"Datatype": "E:102 | Datatype",
"IDNotFound": "E:103 | ID not found",
"InternalServerError": "E:104 | Internal",
"Loop": "E:105 | Loop detected",
"RateLimit": "E:106 | Too many requests",
"TooLarge": "E:107 | Packet too large",
"BrokenPipe": "E:108 | Broken pipe",
"EmptyPacket": "E:109 | Empty packet",
"IDConflict": "E:110 | ID conflict",
"IDSet": "E:111 | ID already set",
"TAEnabled": "I:112 | Trusted Access enabled",
"TAInvalid": "E:113 | TA Key invalid",
"TAExpired": "E:114 | TA Key expired",
"Refused": "E:115 | Refused",
"IDRequired": "E:116 | Username required",
"TALostTrust": "E:117 | Trust lost",
"Invalid": "E:118 | Invalid command",
"Blocked": "E:119 | IP Blocked",
"IPRequred": "E:120 | IP Address required",
"TooManyUserNameChanges": "E:121 | Too Many Username Changes",
"Disabled": "E:122 | Command disabled by sysadmin",
}
print("CloudLink v{0}".format(str(version))) # Report version number
if self.debug:
print("Debug enabled")
def _is_json(self, data): # Checks if something is JSON
if type(data) == dict:
return True
else:
try:
tmp = json.loads(data)
return True
except Exception as e:
return False
def _get_client_type(self, client): # Gets client types to help prevent errors
if client["id"] in self.statedata["ulist"]["objs"]:
return self.statedata["ulist"]["objs"][client["id"]]["type"]
else:
return None
def _get_obj_of_username(self, client): # Helps mitigate packet spoofing
if client in self.statedata["ulist"]["usernames"]:
return self.statedata["ulist"]["objs"][self.statedata["ulist"]["usernames"][client]]["object"]
else:
return None
def _get_username_of_obj(self, obj): # Returns the username of a client object
if obj["id"] in self.statedata["ulist"]["objs"]:
return self.statedata["ulist"]["objs"][obj["id"]]["username"]
else:
return ""
def _get_ip_of_obj(self, obj): # Returns the IP address of a client object
if obj["id"] in self.statedata["ulist"]["objs"]:
return self.statedata["ulist"]["objs"][obj["id"]]["ip"]
else:
return ""
def _is_obj_trusted(self, obj): # Checks if a client is trusted on the link
if self.statedata["secure_enable"]:
return ((obj in self.statedata["trusted"]) and (not self._is_obj_blocked(obj)))
else:
return False
def _is_obj_blocked(self, obj): # Checks if a client is IP blocked
if self.statedata["secure_enable"]:
return (self._get_ip_of_obj(obj) in self.statedata["ip_blocklist"])
else:
return False
def _send_to_all(self, payload): # "Better" (?) send to all function
tmp_payload = payload
for client in self.wss.clients:
#print("sending {0} to {1}".format(payload, client["id"]))
if self._get_client_type(client) == "scratch":
#print("sending to all, {0} is a scratcher".format(client["id"]))
if ("val" in payload) and (type(payload["val"]) == dict):
#print("stringifying nested json")
tmp_payload["val"] = json.dumps(payload["val"])
if not self.statedata["secure_enable"]:
self.wss.send_message(client, json.dumps(tmp_payload))
else:
if self._is_obj_trusted(client):
self.wss.send_message(client, json.dumps(tmp_payload))
else:
if not self.statedata["secure_enable"]:
self.wss.send_message(client, json.dumps(payload))
else:
if self._is_obj_trusted(client):
self.wss.send_message(client, json.dumps(payload))
def _server_packet_handler(self, client, server, message, listener_detected=False, listener_id=""): # The almighty packet handler, single-handedly responsible for over hundreds of lines of code
if not type(client) == type(None):
if not len(str(message)) == 0:
try:
# Parse the JSON into a dict
msg = json.loads(message)
if ("id" in msg):
if type(msg["id"]) != str:
if (not type(msg["id"]) == dict) or (not type(msg["id"]) == list):
msg["id"] = str(msg["id"])
else:
if self.debug:
print('Error: Packet "id" datatype invalid: expecting <class "str">, got {0}'.format(type(msg["cmd"])))
if listener_detected:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["Datatype"], "listener": listener_id}))
else:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["Datatype"]}))
return
# Handle the packet
if "cmd" in msg: # Verify that the packet contains the command parameter, which is needed to work.
if type(msg["cmd"]) == str:
if msg["cmd"] in ["gmsg", "pmsg", "setid", "direct", "gvar", "pvar", "ping"]:
if msg["cmd"] == "gmsg": # Handles global messages.
if False:
if "val" in msg: # Verify that the packet contains the required parameters.
if self._get_client_type(client) == "scratch":
if self._is_json(msg["val"]):
msg["val"] = json.loads(msg["val"])
if True:
if self.debug:
print("message is {0} bytes".format(len(str(msg["val"]))))
self.statedata["gmsg"] = msg["val"]
# Send the packet to all clients.
self._send_to_all({"cmd": "gmsg", "val": msg["val"]})
if listener_detected:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["OK"], "listener": listener_id}))
else:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["OK"]}))
else:
if self.debug:
print('Error: Packet too large')
if listener_detected:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["TooLarge"], "listener": listener_id}))
else:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["TooLarge"]}))
else:
if self.debug:
print('Error: Packet missing parameters')
if listener_detected:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["Syntax"], "listener": listener_id}))
else:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["Syntax"]}))
else:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["Disabled"]}))
if msg["cmd"] == "pmsg": # Handles private messages.
if ("val" in msg) and ("id" in msg): # Verify that the packet contains the required parameters.
if self._get_client_type(client) == "scratch":
if self._is_json(msg["val"]):
msg["val"] = json.loads(msg["val"])
if msg["id"] in self.statedata["ulist"]["usernames"]:
if True:
if not client == self._get_obj_of_username(msg["id"]):
try:
otherclient = self._get_obj_of_username(msg["id"])
if not len(self._get_username_of_obj(client)) == 0:
msg["origin"] = self._get_username_of_obj(client)
if (self._get_client_type(otherclient) == "scratch") and (self._is_json(msg["val"])):
tmp_val = json.dumps(msg["val"])
else:
tmp_val = msg["val"]
if self.debug:
print('Sending {0} to {1}'.format(msg, msg["id"]))
del msg["id"]
self.wss.send_message(otherclient, json.dumps({"cmd": "pmsg", "val": tmp_val, "origin": msg["origin"]}))
if listener_detected:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["OK"], "listener": listener_id}))
else:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["OK"]}))
else:
if listener_detected:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["IDRequired"], "listener": listener_id}))
else:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["IDRequired"]}))
except Exception as e:
if self.debug:
print("Error on _server_packet_handler: {0}".format(e))
if listener_detected:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["InternalServerError"], "listener": listener_id}))
else:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["InternalServerError"]}))
else:
if self.debug:
print('Error: Potential packet loop detected, aborting')
if listener_detected:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["Loop"], "listener": listener_id}))
else:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["Loop"]}))
else:
if self.debug:
print('Error: Packet too large')
if listener_detected:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["TooLarge"], "listener": listener_id}))
else:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["TooLarge"]}))
else:
if self.debug:
print('Error: ID Not found')
if listener_detected:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["IDNotFound"], "listener": listener_id}))
else:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["IDNotFound"]}))
else:
if self.debug:
print('Error: Packet missing parameters')
if listener_detected:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["Syntax"], "listener": listener_id}))
else:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["Syntax"]}))
if msg["cmd"] == "setid": # Sets the username of the client.
if False:
if "val" in msg: # Verify that the packet contains the required parameters.
if not len(str(msg["val"])) == 0:
if True:
if type(msg["val"]) == str:
if self.statedata["ulist"]["objs"][client['id']]["username"] == "":
if not msg["val"] in self.statedata["ulist"]["usernames"]:
# Add the username to the list
self.statedata["ulist"]["usernames"][msg["val"]] = client["id"]
# Set the object's username info
self.statedata["ulist"]["objs"][client['id']]["username"] = msg["val"]
if listener_detected:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["OK"], "listener": listener_id}))
else:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["OK"]}))
self._send_to_all({"cmd": "ulist", "val": self._get_ulist()})
if self.debug:
print("User {0} set username: {1}".format(client["id"], msg["val"]))
else:
if self.debug:
print('Error: Refusing to set username because it would cause a conflict')
if listener_detected:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["IDConflict"], "listener": listener_id}))
else:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["IDConflict"]}))
else:
if self.debug:
print('Error: Refusing to set username because username has already been set')
if listener_detected:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["IDSet"], "listener": listener_id}))
else:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["IDSet"]}))
else:
if self.debug:
print('Error: Packet "val" datatype invalid: expecting <class "str">, got {0}'.format(type(msg["cmd"])))
if listener_detected:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["Datatype"], "listener": listener_id}))
else:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["Datatype"]}))
else:
if self.debug:
print('Error: Packet too large')
if listener_detected:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["TooLarge"], "listener": listener_id}))
else:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["TooLarge"]}))
else:
if self.debug:
print("Error: Packet is empty")
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["EmptyPacket"]}))
else:
if self.debug:
print('Error: Packet missing parameters')
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["Syntax"]}))
else:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["Disabled"]}))
if msg["cmd"] == "direct": # Direct packet handler for server.
if self._get_client_type(client) == "scratch":
if ("val" in msg):
if (self._is_json(msg["val"])) and (type(msg["val"]) == str):
try:
msg["val"] = json.loads(msg["val"])
except json.decoder.JSONDecodeError:
if self.debug:
print("Failed to decode JSON of direct's nested data")
if listener_detected:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["Syntax"], "listener": listener_id}))
else:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["Syntax"]}))
return
if ("val" in msg):
if not self.callback_function["on_packet"] == None:
if "cmd" in msg["val"]:
if msg["val"]["cmd"] == "type":
if "val" in msg["val"]:
if self.statedata["ulist"]["objs"][client["id"]]["type"] == None: # Prevent the client from changing types
self.statedata["ulist"]["objs"][client["id"]]["type"] = msg["val"]["val"] # Set the client type
if self.debug:
if msg["val"]["val"] == "scratch":
print("Client {0} is scratch type".format(client["id"]))
elif msg["val"]["val"] == "py":
print("Client {0} is python type".format(client["id"]))
elif msg["val"]["val"] == "js":
print("Client {0} is js type".format(client["id"]))
else:
print("Client {0} is of unknown client type, claims it's {1}".format(client["id"], (msg["val"]["val"])))
#self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["OK"]}))
else:
if self.debug:
print('Error: Packet missing parameters')
if listener_detected:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["Syntax"], "listener": listener_id}))
else:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["Syntax"]}))
else:
if "val" in msg["val"]:
if len(self._get_username_of_obj(client)) == 0:
origin = client
if self.debug:
print("Handling direct custom command from {0}".format(origin['id']))
else:
origin = self._get_username_of_obj(client)
if self.debug:
print("Handling direct custom command from {0}".format(origin))
if listener_detected:
self.callback_function["on_packet"]({"cmd": msg["val"]["cmd"], "val": msg["val"]["val"], "id": origin, "listener": listener_id})
else:
self.callback_function["on_packet"]({"cmd": msg["val"]["cmd"], "val": msg["val"]["val"], "id": origin})
else:
if self.debug:
print('Error: Packet missing parameters')
if listener_detected:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["Syntax"], "listener": listener_id}))
else:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["Syntax"]}))
else:
if len(self._get_username_of_obj(client)) == 0:
origin = client
if self.debug:
print("Handling direct command from {0}".format(origin['id']))
else:
origin = self._get_username_of_obj(client)
if self.debug:
print("Handling direct command from {0}".format(origin))
if listener_detected:
self.callback_function["on_packet"]({"val": msg["val"], "id": origin, "listener": listener_id})
else:
self.callback_function["on_packet"]({"val": msg["val"], "id": origin})
else:
if self.debug:
print('Error: Packet missing parameters')
if listener_detected:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["Syntax"], "listener": listener_id}))
else:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["Syntax"]}))
if msg["cmd"] == "gvar": # Handles global variables.
if False:
if ("val" in msg) and ("name" in msg): # Verify that the packet contains the required parameters.
if self._get_client_type(client) == "scratch":
if self._is_json(msg["val"]):
msg["val"] = json.loads(msg["val"])
if not len(str(msg["name"])) > 100:
# Send the packet to all clients.
self._send_to_all({"cmd": "gvar", "val": msg["val"], "name": msg["name"]})
if listener_detected:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["OK"], "listener": listener_id}))
else:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["OK"]}))
else:
if self.debug:
print('Error: Packet too large')
if listener_detected:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["TooLarge"], "listener": listener_id}))
else:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["TooLarge"]}))
else:
if self.debug:
print('Error: Packet missing parameters')
if listener_detected:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["Syntax"], "listener": listener_id}))
else:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["Syntax"]}))
else:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["Disabled"]}))
if msg["cmd"] == "pvar": # Handles private variables.
if ("val" in msg) and ("id" in msg) and ("name" in msg): # Verify that the packet contains the required parameters.
if self._get_client_type(client) == "scratch":
if self._is_json(msg["val"]):
msg["val"] = json.loads(msg["val"])
if msg["id"] in self.statedata["ulist"]["usernames"]:
if not len(str(msg["name"])) > 1000:
if not client == self._get_obj_of_username(msg["id"]):
try:
otherclient = self._get_obj_of_username(msg["id"])
if not len(self._get_username_of_obj(client)) == 0:
msg["origin"] = self._get_username_of_obj(client)
if (self._get_client_type(otherclient) == "scratch") and ((self._is_json(msg["val"])) or (type(msg["val"]) == dict)):
tmp_val = json.dumps(msg["val"])
else:
tmp_val = msg["val"]
if self.debug:
print('Sending {0} to {1}'.format(msg, msg["id"]))
del msg["id"]
self.wss.send_message(otherclient, json.dumps({"cmd": "pvar", "val": tmp_val, "name": msg["name"], "origin": msg["origin"]}))
if listener_detected:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["OK"], "listener": listener_id}))
else:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["OK"]}))
else:
if listener_detected:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["IDRequired"], "listener": listener_id}))
else:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["IDRequired"]}))
except Exception as e:
if self.debug:
print("Error on _server_packet_handler: {0}".format(e))
if listener_detected:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["InternalServerError"], "listener": listener_id}))
else:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["InternalServerError"]}))
else:
if self.debug:
print('Error: Potential packet loop detected, aborting')
if listener_detected:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["Loop"], "listener": listener_id}))
else:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["Loop"]}))
else:
if self.debug:
print('Error: Packet too large')
if listener_detected:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["TooLarge"], "listener": listener_id}))
else:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["TooLarge"]}))
else:
if self.debug:
print('Error: ID Not found')
if listener_detected:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["IDNotFound"], "listener": listener_id}))
else:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["IDNotFound"]}))
else:
if self.debug:
print('Error: Packet missing parameters')
if listener_detected:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["Syntax"], "listener": listener_id}))
else:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["Syntax"]}))
if msg["cmd"] == "ping":
if self.debug:
print("Ping from client {0}".format(client["id"]))
if listener_detected:
self.wss.send_message(client, json.dumps({"cmd": "ping", "val": self.codes["OK"], "listener": listener_id}))
else:
self.wss.send_message(client, json.dumps({"cmd": "ping", "val": self.codes["OK"]}))
else: # Route the packet using UPL.
if ("val" in msg) and ("id" in msg): # Verify that the packet contains the required parameters.
if self._get_client_type(client) == "scratch":
if self._is_json(msg["val"]):
msg["val"] = json.loads(msg["val"])
if msg["id"] in self.statedata["ulist"]["usernames"]:
if True:
if not client == self._get_obj_of_username(msg["id"]):
try:
otherclient = self._get_obj_of_username(msg["id"])
if not len(self._get_username_of_obj(client)) == 0:
msg["origin"] = self._get_username_of_obj(client)
if (self._get_client_type(otherclient) == "scratch") and ((self._is_json(msg["val"])) or (type(msg["val"]) == dict)):
tmp_val = json.dumps(msg["val"])
else:
tmp_val = msg["val"]
if self.debug:
print('Routing {0} to {1}'.format(msg, msg["id"]))
del msg["id"]
self.wss.send_message(otherclient, json.dumps(msg))
if listener_detected:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["OK"], "listener": listener_id}))
else:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["OK"]}))
else:
if listener_detected:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["IDRequired"], "listener": listener_id}))
else:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["IDRequired"]}))
except Exception as e:
if self.debug:
print("Error on _server_packet_handler: {0}".format(e))
if listener_detected:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["InternalServerError"], "listener": listener_id}))
else:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["InternalServerError"]}))
else:
if self.debug:
print('Error: Potential packet loop detected, aborting')
if listener_detected:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["Loop"], "listener": listener_id}))
else:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["Loop"]}))
else:
if self.debug:
print('Error: Packet too large')
if listener_detected:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["TooLarge"], "listener": listener_id}))
else:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["TooLarge"]}))
else:
if self.debug:
print('Error: ID Not found')
if listener_detected:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["IDNotFound"], "listener": listener_id}))
else:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["IDNotFound"]}))
else:
if self.debug:
print('Error: Packet missing parameters')
if listener_detected:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["Syntax"], "listener": listener_id}))
else:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["Syntax"]}))
else:
if self.debug:
print('Error: Packet "cmd" datatype invalid: expecting <class "bool">, got {0}'.format(type(msg["cmd"])))
if listener_detected:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["Datatype"], "listener": listener_id}))
else:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["Datatype"]}))
else:
if self.debug:
print('Error: Packet missing "cmd" parameter')
if listener_detected:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["Syntax"], "listener": listener_id}))
else:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["Syntax"]}))
except json.decoder.JSONDecodeError:
if self.debug:
print("Error: Failed to parse JSON")
if listener_detected:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["Syntax"], "listener": listener_id}))
else:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["Syntax"]}))
except Exception as e:
if self.debug:
print("Error on _server_packet_handler: {0}".format(full_stack()))
if listener_detected:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["InternalServerError"], "listener": listener_id}))
else:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["InternalServerError"]}))
else:
if self.debug:
print("Error: Packet is empty")
if listener_detected:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["EmptyPacket"], "listener": listener_id}))
else:
self.wss.send_message(client, json.dumps({"cmd": "statuscode", "val": self.codes["EmptyPacket"]}))
def _get_ulist(self): # Generates username list
tmp_ulist = list((self.statedata["ulist"]["usernames"]).keys())
for item in tmp_ulist:
if item[0] == "%" and item[len(item)-1] == "%":
tmp_ulist.pop(tmp_ulist.index(item))
output = ""
for item in tmp_ulist:
output = output + item + ";"
return output
def _on_connection_server(self, client, server): # Server-side new connection handler
if not type(client) == type(None):
try:
if self.debug:
print("New connection: {0}".format(str(client['id'])))
# Add the client to the ulist object in memory.
self.statedata["ulist"]["objs"][client["id"]] = {"object": client, "username": "", "ip": client["handler"].ip, "type": None}
# Send the MOTD if enabled.
if self.statedata["motd_enable"]:
self.wss.send_message(client, json.dumps({"cmd": "direct", "val": {"cmd": "motd", "val": str(self.statedata["motd"])}}))
# Send server version.
self.wss.send_message(client, json.dumps({"cmd": "direct", "val": {"cmd": "vers", "val": str(version)}}))