-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
1355 lines (1232 loc) · 39.5 KB
/
server.js
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
"use strict"
/**
An instance object contains:
.pastHorizonFrameNumber: int
.pastHorizonPerfTime: timestamp
.pastHorizonState: a game state
.pastHorizonControllerStatus: map from id numbers to controller status objects
.events: map from int to list of events (not necessarily sorted yet)
.broadcastControllers: map from id numbers to controllers
.suspended: boolean, if true then pastHorizonPerfTime may be in the distance past
.advanceTimeout: timeout handle for state advancing
.playset: a playset
A controller status object has .u username and .i last known input string (default "")
A game state object should contain at least a controller-username mapping
and game information relevant to the connected users, but it is a black box
to the core server code and only the playset code needs to understand it.
An event contains:
.k: "c" connect, "d" disconnect, "o" command, "f" frame
.f: int frame number
.i: input string when .k=="f"
.o: command string when .k=="o"
.s: for "o" only, positive integer serial, always increasing within a frame
.c: int controller number (not sent from client to server)
.u: username when .k=="c"
.d: user config string when .k=="c" (mainly for use by client
user-management UI and playset ui 'avatar' assignment; is also
available to playset game state logic, but be wary of "modeling for
advantage" customization)
.t: pong timestamp, only when echoing .k=="f" from client to server
Canonical sort order of events:
.k primary, "c"<"o"<"f"<"d"
.c secondary, ordered as integer not string
.s tertiary for "o", ordered as integer not string
A controller object contains (mostly not until connection is established):
.socket: websocket object (which has .okayController injected into it to point back)
.username: string
.config: user's config (as of moment of login)
.instance: instance
.instanceName: instance name
.playsetName: instance.playset name
.minFrameNumber: int, events stamped earlier than this are out-of-order
.timeout: handle to a cancelable timeout
.lastCommandNumber: last command serial number for this frame, or 0
A user object [not to be confused with a controller object] contains:
.password ("hashed", though the hash function may sometimes be a no-op)
.username: string, limited to ASCII
.config: unicode string available to playset via connect events,
server-validated only for length and not for content
.selfServeAddress: if present, this user was created via self-serve from
this address
.admin: boolean
Server-to-client network messages:
{k:"E", e:errorString} (error, hanging up)
{k:"D", d:completionString} (successful api call, hanging up)
{k:"U", l:[instanceName... ordered], n:defaultInstanceName} (prelogin)
any instance-controller event, which may additionally have "t" attached as a timing pong
{k:"W", t:timingPong} (login wait, with initial pong time)
{k:"S", g:serializedGameState, p:playsetName,
x:{controllerID:{'u':username,'i':inputString}... },
e:[instancecontrollerevents... unsorted],
c:controllerID, f:frameNumberOfSerializedState,
m:initialChatTokenCount, l:chatMessageMaxLength, r:fps} (login)
{k:"F", f: frameNumber} (frame horizon has advanced)
{k:"F", f: frameNumber, h: hash} (above, and client should sync-test)
{k:"g",c:controllerID,u:username,m:message} (global chat)
{k:"G", n:messageCount} (granting client permission to send n
global chat messages; this adds to previous
unspent permissions if any)
client-to-server network messages for normal login:
"o" or "f" instance-controller events
array of "o" or "f" instance-controller events
{k:"l", u:username, p:password, n:instanceName}
{k:"g",m:message}
client-to-server network messages for self-serve API calls:
{k:"prelogin"} no credentials needed, return info for login page
{k:"selfServeCreateUser",u:username, p:password, d:config}
{k:"changeMyPassword",u:username, p:password, n:newPassword, d:config}
{k:"getMyConfig",u:username, p:password} (completion string is the config)
{k:"setMyConfig",u:username, p:password, d:config}
client-to-server network messages for admin API calls:
{k:"dirtyShutdown",u:username, p:password, r:reason} (only forensic save)
{k:"cleanShutdown",u:username, p:password, r:reason} (save for next startup)
(later, admin versions of the self-serve operations)
open issues not addressed here:
- state saving as anything other than a shutdown
- local port for admin operations
- rotating state saves to avoid data loss, loading the right one
- revelation of hidden state/rng rolls
- handling of partially hidden state
- handling of hidden inputs
- propagating data between different instances, and keeping that synced up
- gating instances (e.g. whitelists, bans, need another instance's approval)
- non-real-time instances for games without client-side prediction
- non-instance-specific messaging
- admin operations for user management, including grant/revoke admin
- fixed codes for error/success reasons
- (client-side) breaking long message arrays down to stay under max length
*/
const fs=require('fs');
const performance=require('perf_hooks').performance;
const crypto=require('crypto');
const ws=require('ws');
var httpServer;
var wsServer;
var config; // general server parameters
var users; // map from usernames to users
var instances; // map from instance names instance state objects
var controllers; // map from controller ID numbers to controller objects
var nextControllerID; // int
var selfServeUserCounts; // how many self-serve users there are from an IP
// maps from usernames to controller objects
var inboxControllers, liveControllers, outboxControllers;
var requiredOrigin; // if using https, need this origin or local loopback
const FPS=30;
const PAST_HORIZON_FRAMES=FPS/2, FUTURE_HORIZON_FRAMES=FPS*3/2;
const TIMEOUT_MILLIS=5000;
const DEFAULT_HASH_SYNC_INTERVAL=FPS*5;
const DEFAULT_FRAME_BROADCAST_INTERVAL=FPS/4;
const DEFAULT_MAX_CHAT_MESSAGE_LENGTH=1024;
const DEFAULT_CHAT_BURST_SIZE=5;
const DEFAULT_CHAT_WAIT_MILLIS=2000;
const MIN_USERNAME_LENGTH=3;
const MAX_USERNAME_LENGTH=16;
const MIN_PASSWORD_LENGTH=3;
const MAX_PASSWORD_LENGTH=64;
const MAX_USER_CONFIG_LENGTH=10000;
const MAX_INBOUND_MESSAGE_LENGTH=20000;
// set this true if hashes are desyncing and you want lots of
// console output about it
const DUMP_HASH_STATES=false;
const STATE_FILENAME="./serverstate.json";
var playsets;
var playsetCommandRateLimits;
var playsetInputLengthLimits;
var playsetArgumentLengthLimits;
function defaultPlaysetAdvanceGameState(state,connects,
commands,inputs,disconnects) {
// exact cutpaste between client and server code
for(var i in connects) {
this.applyConnect(state,connects[i].c,connects[i].u,connects[i].d);
}
var controllerCommands={}
for(var i in commands) {
if(this.applyCommand) {
this.applyCommand(state,commands[i].c,commands[i].o,commands[i].a);
}
if(commands[i].c in controllerCommands) {
controllerCommands[commands[i].c].push({o:commands[i].o,a:commands[i].a});
}
else {
controllerCommands[commands[i].c]=[{o:commands[i].o,a:commands[i].a}]
}
}
for(var i in inputs) {
this.applyControllerFrame(state,inputs[i].c,inputs[i].i,
controllerCommands[inputs[i].c]||[]);
}
if(this.applyStateFrame) { this.applyStateFrame(state); }
for(var i in disconnects) {
this.applyDisconnect(state,disconnects[i]);
}
}
function registerPlayset(playset) {
var defaultSerialization=true
if(!("serializeGameState" in playset)) {
playset.serializeGameState=function(gameState) {
return JSON.stringify(gameState);
}
}
else {
defaultSerialization=false;
}
if(!("deserializeGameState" in playset)) {
playset.deserializeGameState=function(gameStateString) {
return JSON.parse(gameStateString);
}
}
else {
defaultSerialization=false;
}
if(defaultSerialization && !("hashGameState" in playset)) {
playset.hashGameState=defaultGameStateHash;
}
var name=playset.getName();
// server doesn't call copy or advanceClient so doesn't need their defaults
playsets[playset.getName()]=playset;
if(playset.getCommandLimits) {
playsetCommandRateLimits[name]=playset.getCommandLimits();
}
else {
playsetCommandRateLimits[name]={}
}
if(playset.getInputLengthLimit) {
playsetInputLengthLimits[name]=playset.getInputLengthLimit();
}
else {
// setting default too high to have an effect, since the message would get
// the user kicked anyway
playsetInputLengthLimits[name]=MAX_INBOUND_MESSAGE_LENGTH;
}
if(playset.getArgumentLengthLimit) {
playsetArgumentLengthLimits[name]=playset.getArgumentLengthLimit();
}
else {
playsetArgumentLengthLimits[name]=MAX_INBOUND_MESSAGE_LENGTH;
}
if(!("advanceGameState" in playset)) {
playset.advanceGameState=defaultPlaysetAdvanceGameState;
}
}
function getPlayset(name) {
return playsets[name]||null;
}
function loadPlaysets() {
// assumption for now: we're only calling this once
// and don't need reload logic
global.registerPlayset=registerPlayset
playsets={}
playsetInputLengthLimits={}
playsetArgumentLengthLimits={}
playsetCommandRateLimits={}
require("./web/playsets.js")
delete global.registerPlayset
}
function initServer() {
const cert=require('./cert.js')
loadPlaysets();
loadServerState();
if(cert.secure) {
const https=require('https')
httpServer=https.createServer({
cert:fs.readFileSync(cert.fullchain),
key:fs.readFileSync(cert.privkey),
});
requiredOrigin=cert.origin;
}
else {
const http=require('http')
httpServer=http.createServer();
requiredOrigin=null;
}
wsServer=new ws.Server({
server:httpServer,
maxLength:8192
});
wsServer.on('connection',onSocketConnection);
httpServer.listen(8081);
}
function serializeServerState() {
var o={
config:config,
users:users,
nextControllerID:nextControllerID,
instances:{}
}
for(var i in instances) {
var instance=instances[i];
var playset=instance.playset;
o.instances[i]={
playsetName:playset.getName(),
state:playset.serializeGameState(instance.pastHorizonState),
controllerStatus:instance.pastHorizonControllerStatus,
}
}
return JSON.stringify(o,null,1);
}
function loadServerState() {
// for now we're just loading state from a single file, not looking
// at filenames to pick one
var o=JSON.parse(fs.readFileSync("./serverstate.json"));
config=o.config||{};
selfServeUserCounts={}
users=o.users;
for(var username in users) {
var u=users[username]
if("selfServeAddress" in u) {
selfServeUserCounts[u.selfServeAddress]=
(selfServeUserCounts[u.selfServeAddress]||0)+1;
}
if(u.plaintextPassword) {
u.password=makePasswordHash(u.plaintextPassword);
delete u.plaintextPassword;
}
if(!("config" in u)) { u.config=""; }
}
nextControllerID=o.nextControllerID;
controllers={}
liveControllers={}
inboxControllers={}
outboxControllers={}
instances={}
for(var k in o.instances) {
var inst=o.instances[k];
var playset=getPlayset(inst.playsetName);
var state=inst.state
if(typeof(state)=="string") {
// convenience: manual serverstate.json can use an unescaped object
// for a simple game state. automatic persisting will always go
// through serializer.
state=playset.deserializeGameState(state);
}
// convenience: manual serverstate.json can omit controller info.
// automatic persisting needs to include it so that any controllers
// that were connected as of persist time get disconnect game events
// on rehydrating.
var controllerStatus=("controllerStatus" in inst)?inst.controllerStatus:{};
instances[k]={
playset:playset,
pastHorizonFrameNumber:1,
pastHorizonState:state,
pastHorizonPerfTime:performance.now()-PAST_HORIZON_FRAMES*FPS/1000,
pastHorizonControllerStatus:controllerStatus,
events:{1:[]},
broadcastControllers:{},
suspended:true,
};
// any controllers that were connected at save time are disconnected
// now, let the game state find out when computing from frame 1 to frame 2
for(var c in inst.controllerStatus) {
instances[k].events[1].push({"k":"d","c":c|0,"f":1});
}
}
}
function onSocketConnection(socket,request) {
var isLocal=(request.connection.remoteAddress=="::1" ||
request.connection.remoteAddress=="127.0.0.1" ||
request.connection.remoteAddress=="::ffff:127.0.0.1");
if(!isLocal && requiredOrigin && request.headers.origin!=requiredOrigin) {
try {
socket.send(JSON.stringify({"k":"E","e":"origin header mismatch"}));
}
catch(e) { }
try {
socket.close();
}
catch(e) { }
return;
}
var controller={
id:nextControllerID,
socket:socket,
remoteAddress:request.connection.remoteAddress,
isLocal:isLocal,
lifecycle:"new",
}
socket.on('message',onSocketMessage);
socket.on('error',onSocketError);
socket.on('close',onSocketClose);
socket.okayController=controller;
resetConnectionTimeout(controller);
controllers[nextControllerID]=controller;
++nextControllerID;
}
function resetConnectionTimeout(controller) {
if(controller.timeout)
{
clearTimeout(controller.timeout);
}
controller.timeout=setTimeout(
function() {
handleControllerTimeout(controller);
},
TIMEOUT_MILLIS
);
}
function abandonConnectionTimeout(controller) {
if(controller.timeout)
{
clearTimeout(controller.timeout);
delete controller.timeout;
}
}
function handleControllerTimeout(controller) {
//console.log("tick (controller timeout)");
controllerError(controller,"connection timed out");
}
function onSocketMessage(e) {
var controller=this.okayController;
if(controller.disconnected)
{
//console.log("ignoring message from disconnected controller",e);
return;
}
try {
var message=JSON.parse(e);
}
catch(err) {
controllerError(controller,"server could not parse network message");
return;
}
if(typeof(message)!="object") {
controllerError(controller,"non-JSON network message");
return;
}
if(Array.isArray(message)) {
for(var i=0;i<message.length && !controller.disconnected;++i)
{
if(typeof(message[i])!="object") {
controllerError(controller,"non-JSON network message");
return;
}
onInboundMessage(controller,message[i]);
}
}
else {
onInboundMessage(controller,message)
}
}
function onInboundMessage(controller,message) {
switch(message.k) {
// normal flow
case "f": onFrameMessage(controller,message); break;
case "o": onCommandMessage(controller,message); break;
case "l": onLoginMessage(controller,message); break;
case "g": onGlobalChatMessage(controller,message); break;
// API calls
case "prelogin": onPreloginMessage(controller,message); break;
case "selfServeCreateUser": onCreateUserMessage(controller,message); break;
case "changeMyPassword": onChangePasswordMessage(controller,message); break;
case "getMyConfig": onGetConfigMessage(controller,message); break;
case "setMyConfig": onSetConfigMessage(controller,message); break;
case "dirtyShutdown": case "cleanShutdown":
onShutdownMessage(controller,message);
break;
default:
controllerError(controller,"unknown message type");
break;
}
}
function onSocketError() {
var controller=this.okayController;
if(controller.disconnected) { return; }
controllerError(controller,"server detected network error");
}
function onSocketClose() {
var controller=this.okayController;
if(controller.disconnected) { return; }
disconnectController(controller);
}
function makePasswordHash(password) {
var salt=crypto.randomBytes(4).toString('hex');
var hash=crypto.createHash('sha256');
hash.update(salt);
hash.update(password+"");
return salt+"#"+hash.digest('hex');
}
function doesPasswordMatchHash(password,hashWanted) {
var salt=hashWanted.slice(0,hashWanted.indexOf("#"));
var hash=crypto.createHash('sha256');
hash.update(salt);
hash.update(password+"");
var digest=hash.digest('hex');
return hashWanted==salt+"#"+digest
}
function onPreloginMessage(controller,message) {
var instanceList;
if(config.instanceDisplayList) {
instanceList=config.instanceDisplayList;
}
else {
instanceList=Object.getOwnPropertyNames(instances);
instanceList.sort();
}
var defaultInstance;
if(config.defaultInstance) {
defaultInstance=config.defaultInstance;
}
else {
defaultInstance=instanceList[0];
}
try {
controller.socket.send(JSON.stringify({k:"U",n:defaultInstance,
l:instanceList}));
}
catch(e) {
};
try {
controller.socket.close();
}
catch(e) {
};
disconnectController(controller);
}
function onLoginMessage(controller,message) {
if(controller.lifecycle!="new") {
controllerError(controller,"client sent login message at inappropriate time");
return;
}
if(!(message.u in users &&
doesPasswordMatchHash(message.p,users[message.u].password))) {
controllerError(controller,"incorrect username/password");
return;
}
var instanceName=message.n+"";
if(!(instanceName in instances)) {
controllerError(controller,"instance name does not exist");
return;
}
if(message.u in inboxControllers ||
message.u in liveControllers) {
controllerError(controller,"you are already logged in (check other browser tabs)");
return;
}
var instance=instances[instanceName];
unsuspendInstance(instance);
controller.instance=instance;
controller.instanceName=instanceName;
controller.playsetName=instance.playset.getName();
controller.username=""+message.u;
controller.socket.send(JSON.stringify({
k:"W",
t:getTimingPongForInstance(instance)
}));
if(controller.username in outboxControllers) {
controller.lifecycle="inbox";
inboxControllers[controller.username]=controller;
// the next message is server-to-client, so the client is expected
// to be inactive and shouldn't be timed out
abandonConnectionTimeout(controller);
}
else {
makeControllerLive(controller);
}
}
function makeControllerLive(controller) {
controller.lifecycle="live"
liveControllers[controller.username]=controller;
var instanceFrameNow=getPresentFrameNumber(controller.instance);
controller.lastCommandNumber=0;
controller.minFrameNumber=instanceFrameNow;
controller.commandRateCounters={}
var connectEvent={
"c":controller.id,
"u":controller.username,
"f":instanceFrameNow,
"d":users[controller.username].config,
"k":"c",
};
broadcastEventToInstance(controller.instance,connectEvent,false);
subscribeControllerToBroadcasts(controller);
controller.globalChatTokens=config.globalChatBurstSize||
DEFAULT_CHAT_BURST_SIZE;
sendInstanceSnapshot(controller);
resetConnectionTimeout(controller);
}
function controllerError(controller,errorString) {
try {
controller.socket.send(JSON.stringify({k:"E",e:errorString}));
}
catch(e) {
};
try {
controller.socket.close();
}
catch(e) {
};
disconnectController(controller);
}
function controllerDone(controller,resultString) {
try {
controller.socket.send(JSON.stringify({k:"D",d:resultString}));
}
catch(e) {
};
try {
controller.socket.close();
}
catch(e) {
};
disconnectController(controller);
}
function disconnectController(controller) {
if(controller.lifecycle=="live") {
controller.lifecycle="outbox"
outboxControllers[controller.username]=controller;
delete liveControllers[controller.username]
var frameNumber=getPresentFrameNumber(controller.instance);
if(frameNumber<controller.minFrameNumber) {
frameNumber=controller.minFrameNumber;
}
unsubscribeControllerFromBroadcasts(controller);
var disconnectEvent={
"c":controller.id,
"f":frameNumber,
"k":"d",
}
broadcastEventToInstance(controller.instance,disconnectEvent,false);
}
abandonConnectionTimeout(controller);
if(controller.id in controllers && controller.lifecycle!="outbox") {
if(controller.lifecycle=="inbox") {
delete inboxControllers[controller.username];
}
delete controllers[controller.id]
}
controller.disconnected=true;
}
function onFrameMessage(controller,message) {
if(validateFrameOrCommandMessage(controller,message)) {
var inp=""+message.i
if(inp.length>playsetInputLengthLimits[controller.playsetName]) {
controllerError(controller,"client sent too-large input message");
return;
}
controller.minFrameNumber=message.f+1;
controller.lastCommandNumber=0;
controller.commandRateCounters={}
var event={
"c":controller.id,
"f":message.f,
"k":"f",
"i":inp
};
if(inp!=controller.lastFrameInput) {
controller.lastFrameInput=inp;
broadcastEventToInstance(controller.instance,event,true);
}
else {
// always put it in the instance, and always ping back
storeAndEchoInstanceEvent(controller.instance,event,true);
}
resetConnectionTimeout(controller);
}
// else we either errored out, or we are refusing to acknowledge an out-of-date event
}
function onCommandMessage(controller,message) {
if(validateFrameOrCommandMessage(controller,message)) {
var serial=message.s|0;
if(!serial) {
controllerError(controller,"client sent command message without serial");
return;
}
var arg=(message.a||"")+""
var playsetName=controller.playsetName;
if(arg.length>playsetArgumentLengthLimits[playsetName]) {
controllerError(controller,"client sent too-large command argument");
return;
}
var cmd=message.o+""
if(!(cmd in playsetCommandRateLimits[playsetName])) {
controllerError(controller,"client sent invalid command for this playset");
return;
}
if(cmd in controller.commandRateCounters &&
controller.commandRateCounters[cmd]>=
playsetCommandRateLimits[playsetName][cmd]) {
controllerError(controller,"client exceeded command rate limit");
return;
}
if(message.f>controller.minFrameNumber) {
// the point of retroactive event acceptance moves forward,
// since we won't accept an event stamped for a frame any earlier
// than a seen command; having moved that point forward,
// we allow command serial numbers and rate limits to reset.
controller.minFrameNumber=message.f;
controller.lastCommandNumber=0;
controller.commandRateCounters={}
}
if(serial<=controller.lastCommandNumber) {
controllerError(controller,"client sent out-of-order command message");
}
var event={
"c":controller.id,
"f":message.f,
"k":"o",
"o":cmd,
"a":arg,
"s":serial,
}
controller.lastCommandNumber=serial;
if(cmd in controller.commandRateCounters) {
++controller.commandRateCounters[cmd];
}
else {
controller.commandRateCounters[cmd]=1;
}
broadcastEventToInstance(controller.instance,event,false);
resetConnectionTimeout(controller);
}
// else we either errored out, or we are refusing to acknowledge an out-of-date event
}
function validateFrameOrCommandMessage(controller,message) {
if(controller.lifecycle!="live") {
controllerError(controller,"game message sent without a valid login");
return false
}
if(!("f" in message)) {
controllerError(controller,"malformed message, no frame number");
return false
}
if(message.f !== message.f|0) {
controllerError(controller,"malformed message, non-integer frame number");
return false
}
if((message.f|0)<controller.minFrameNumber) {
controllerError(controller,"out-of-order message");
return false
}
var present=getPresentFrameNumber(controller.instance);
if((message.f|0)>present+FUTURE_HORIZON_FRAMES) {
controllerError(controller,"client timestamps are running too fast");
return false
}
if(message.f<controller.instance.pastHorizonFrameNumber) {
// invalid, but in a "don't ack this, it's too lagged" way rather than a
// constraint violation
//console.log("message.f<controller.instance.pastHorizonFrameNumber");
return false;
}
return true;
}
function onGlobalChatMessage(controller,message) {
if(controller.lifecycle!="live" || !controller.globalChatTokens) {
controllerError(controller,"client sent global chat too quickly");
return
}
var m=message.m+""
var maxChatMessageLength=config.maxChatMessageLength||
DEFAULT_MAX_CHAT_MESSAGE_LENGTH;
if(m.length>maxChatMessageLength) {
controllerError(controller,
"client sent a global chat message that was too long");
return;
}
--controller.globalChatTokens;
broadcastMessageGlobally({
"k":"g",
"c":controller.id,
"u":controller.username,
"m":m
})
grantGlobalChatTokenSoon(controller);
}
function grantGlobalChatTokenSoon(controller) {
setTimeout(
function() {
if(controller.lifecycle=="live") {
++controller.globalChatTokens;
try {
controller.socket.send(JSON.stringify({"k":"G"}));
}
catch(e) {}
}
},
config.globalChatWaitMillis||DEFAULT_CHAT_WAIT_MILLIS
);
}
function validateUsername(username,controller) {
if(username.length<MIN_USERNAME_LENGTH) {
controllerError(controller,"username too short, minimum "+MIN_USERNAME_LENGTH);
return false
}
if(username.length>MAX_USERNAME_LENGTH) {
controllerError(controller,"username too short, minimum "+MAX_USERNAME_LENGTH);
return false
}
for(var i=0;i<username.length;++i) {
var code=username.codePointAt(i);
if( (code<65 || code>90) && (code<97 || code>122) &&
(code<48 || code>57)) {
controllerError(controller,"usernames must be ASCII alphanumeric");
return false
}
if(i==0 && code>=48 && code<=57) {
controllerError(controller,"usernames may not start with a number");
return false;
}
}
return true;
}
function validatePassword(password,controller) {
if(typeof(password)!="string") {
controllerError(controller,"malformed password data (not a Unicode string)");
return false
}
if(password.length<MIN_PASSWORD_LENGTH) {
controllerError(controller,"password too short, minimum "+MIN_PASSWORD_LENGTH);
return false
}
if(password.length>MAX_PASSWORD_LENGTH) {
controllerError(controller,"password too short, minimum "+MAX_PASSWORD_LENGTH);
return false
}
// any unicode string in the right range is fine, no need to get weird
return true;
}
function validateUserConfigLength(config,message) {
if(config.length>MAX_USER_CONFIG_LENGTH) {
controllerError(controller,"user config data is too long");
return false
}
return true;
}
function onCreateUserMessage(controller,message) {
// later: alternate flow for the admin version of the message
var usernameWanted=message.u+"";
if(!validateUsername(usernameWanted,controller)) { return; }
// for passwords, validate type instead of coercing, to avoid
// accidental "undefined" or "[object Object]" passwords
var passwordWanted=message.p;
if(!validatePassword(passwordWanted,controller)) { return; }
var configWanted=(message.c||"")+"";
if(!validateUserConfigLength(configWanted,controller)) { return; }
var existingCount=selfServeUserCounts[controller.remoteAddress]||0;
if(!("selfServeUserLimit" in config) ||
existingCount>=config.selfServeUserLimit) {
controllerError(controller,
"you are not authorized for self-serve user creation");
}
if(usernameWanted in users) {
controllerError(controller,
"username already in use");
}
users[usernameWanted]={
username:usernameWanted,
password:makePasswordHash(passwordWanted),
config:configWanted,
admin:false,
selfServeAddress:controller.remoteAddress
}
selfServeUserCounts[controller.remoteAddress]=existingCount+1;
controllerDone(controller,"user created");
}
function onChangePasswordMessage(controller,message) {
// later: alternate flow for the admin version of the message
var passwordWanted=message.p+"";
if(!validatePassword(passwordWanted,controller)) { return; }
if(!(message.u in users &&
doesPasswordMatchHash(message.p,users[message.u].password))) {
controllerError(controller,"incorrect username/password");
return;
}
users[message.u].password=makePasswordHash(passwordWanted);
controllerDone(controller,"password changed");
}
function onGetConfigMessage(controller,message) {
// later: alternate flow for the admin version of the message
if(!(message.u in users &&
doesPasswordMatchHash(message.p,users[message.u].password))) {
controllerError(controller,"incorrect username/password");
return;
}
controllerDone(controller,users[message.u].config);
}
function onSetConfigMessage(controller,message) {
// later: alternate flow for the admin version of the message
var configWanted=(message.d||"")+"";
if(!validateUserConfigLength(configWanted,controller)) { return; }
if(!(message.u in users &&
doesPasswordMatchHash(message.p,users[message.u].password))) {
controllerError(controller,"incorrect username/password");
return;
}
users[message.u].config=configWanted;
controllerDone(controller,users[message.u].config);
}
function onShutdownMessage(controller,message) {
if(!(message.u in users &&
doesPasswordMatchHash(message.p,users[message.u].password))) {
controllerError(controller,"incorrect username/password");
return;
}
if(!users[message.u].admin) {
controllerError(controller,"you are not authorized to shut down the server");
return;
}
controllerDone(controller,"shutdown in progress");
var reason=(message.r+"")||"server shutdown"
var controllerIDs=Object.getOwnPropertyNames(controllers);
for(var i in controllerIDs) {
controllerError(controllers[controllerIDs[i]],reason);
}
var toSave=serializeServerState();
var d=new Date();
var timeString=d.toISOString().replace(/:/g,"_");
var saveFilename;
// later: make save rotations cleaner to allow for better daemonization
if(message.k=="cleanShutdown") {
saveFilename="statebackup_"+timeString+".json";
fs.writeFileSync(saveFilename,toSave);
fs.writeFileSync(STATE_FILENAME,toSave);
}
else {
saveFilename="dirtystate_"+timeString+".json";
fs.writeFileSync(saveFilename,toSave);
}
wsServer.close();
httpServer.close();
console.log("Shut down, state backup is to "+saveFilename);
if(message.r) {
console.log("Reason: "+reason);
}
}
function subscribeControllerToBroadcasts(controller) {
controller.instance.broadcastControllers[controller.id]=controller;
}
function unsubscribeControllerFromBroadcasts(controller) {
if(controller.id in controller.instance.broadcastControllers) {
delete controller.instance.broadcastControllers[controller.id];
}
}
function addInstanceEvent(instance,event) {
if(!instance.events[event.f]) {
instance.events[event.f]=[event];
}
else {
instance.events[event.f].push(event);
}
}