-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
616 lines (509 loc) · 22.3 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
var net = require('net')
let fs = require('fs')
let publicip = require("public-ip")
var colors = require('colors');
let chunks = {}
let viewingDistance = 40
let chunkSize = 16
let reportServerPort = 69
let port = 80
let worldSize = 5000
let connectToLocalhost = false
let loggingLevel = process.env.LOG_LEVEL || "debug"
// LOGGING SETUP
var winston = require('winston');
//
// Requiring `winston-papertrail` will expose
// `winston.transports.Papertrail`
//
require('winston-papertrail').Papertrail;
var winstonPapertrail = new winston.transports.Papertrail({
host: 'logs7.papertrailapp.com',
port: 36057,
colorization: "all",
level: "info",
colorize: true,
})
winstonPapertrail.on('error', function(err) {
console.log("Logging error:")
console.error(err)
});
var logger = new winston.Logger({
level: loggingLevel,
transports: [winstonPapertrail, new winston.transports.Console(), new winston.transports.File({ filename: 'combined.log' })]
});
logger.info('Loading Infinite Pixels server');
logger.info("Forking off report server...")
// for spawning report-server.js
let cp = require("child_process")
let reportServer = cp.fork("./report-server.js")
reportServer.on('error', (error) => {
logger.error("Report server error: " + error.toString())
})
if (!fs.existsSync("world")) {
logger.warn('No world folder detected, so making a new one')
fs.mkdirSync("world")
}
//setInterval(chunkUpdateTick, 2000)
setInterval(saveChunks, 10000)
class Chunk {
constructor(x, y) {
this.x = x
this.y = y
this.pixels = {}
}
}
class Client {
constructor(clientid, socket) {
this.position = {x: 0, y: 0}
this.velocity = {x: 0, y: 0}
this.clientid = clientid
this.socket = socket
}
}
let clients = {}
let server = net.createServer((socket) => {
delete socket._readableState.decoder; // To force stream to read out numbers
socket.shouldDisconnect = false
logger.info("Accepted".green + " connection from %s:%s, there are now %s connected", socket.remoteAddress, socket.remotePort, (Object.keys(clients).length + 1).toString())
socket.on("close", () => {
if (socket.client) {
//broadcastClientQuit(socket.client.clientid)
delete clients[socket.client.clientid]
} else {
logger.warn("Connection %s:%s disconnected without authenticating", socket.remoteAddress, socket.remotePort)
}
logger.info("Lost".red + " connection from %s:%s, there are now %s connected", socket.remoteAddress, socket.remotePort, Object.keys(clients).length.toString())
})
currentPacketIdentifier = null
socket.on('error', (er) => {
switch (er.code) {
// This is the expected case
case 'ECONNRESET':
if (socket.client) logger.debug("ECONNRESET" + " from player %s", socket.client.clientid)
else logger.debug("ECONNRESET" + " from %s:%s", socket.remoteAddress, socket.remotePort)
break;
// On Windows, this sometimes manifests as ECONNABORTED
case 'ECONNABORTED':
if (socket.client) logger.debug("ECONNABORTED" + " from player %s", socket.client.clientid)
else logger.debug("ECONNABORTED" + " from %s:%s", socket.remoteAddress, socket.remotePort)
break;
// This test is timing sensitive so an EPIPE is not out of the question.
// It should be infrequent, given the 50 ms timeout, but not impossible.
case 'EPIPE':
if (socket.client) logger.warn("EPIPE" + " from player %s", socket.client.clientid)
else logger.warn("EPIPE" + " from %s:%s", socket.remoteAddress, socket.remotePort)
default:
if (socket.client) logger.warn("Unknown socket error %j" + " from player %s", er, socket.client.clientid)
else logger.warn("Unknown socket error %j" + "from %s:%s", er, socket.remoteAddress, socket.remotePort)
break;
}
}
)
socket.on("data", (data) => readData(data, socket))
})
function readString(data, offset) {
let stringLength = data.readUInt8(offset)
let string = data.toString("ascii", offset + 1, offset + 1 + stringLength)
logger.debug("Read string %s of length %d with offset %d", string, stringLength, offset)
return string
}
function isWithinWorldBounds(x, y) {
if (x > worldSize) return false;
if (x < -worldSize) return false
if (y > worldSize) return false;
if (y < -worldSize) return false;
return true;
}
// Call this function at the start of a packet to pull information from the header
function getPacketInformationFromHeader (data) {
let currentPacketIdentifier = data.readUInt8()
let length = 0 // Expected length of the packet not including the identifier
if (currentPacketIdentifier === null) {
// Why is the first byte sometimes null?
return null
}
switch (currentPacketIdentifier) {
// Connection request
case 0:
stringLength = data.readUInt8(1)
length = stringLength + 1
break
// Position update
case 3:
length = 16
break
// Chunk update request
case 6:
length = 8
break
// Pixel placement packet
case 7:
length = 12
break
// Pixel removeal packet
case 8:
length = 8
break
case 9:
stringLength = data.readUInt8(17)
length = stringLength + 17
break
}
slicedData = data.slice(1)
logger.silly("Looked up packet identifier %d and got a length of %d bytes", currentPacketIdentifier, length)
return {ident: currentPacketIdentifier,
length: length,
slicedData: slicedData}
}
function endConnection(socket) {
socket.shouldDisconnect = true
socket.destroy()
}
function processPacket(data, socket) {
let identifier = currentPacketInfo.ident
if (identifier !== 0 && !socket.client) {
logger.warn("Unauthenticated client %s:%s sent a non-0 packet with ident %d, " + "disconnecting".red, socket.remoteAddress, socket.remotePort, identifier)
endConnection(socket)
return
}
switch (identifier) {
case 0:
let clientid = readString(data, 0)
let shouldAccept = shouldAcceptClient(clientid)
response = null
if (shouldAccept) {
response = new Buffer([0x01])
socket.client = new Client(clientid, socket)
socket.client.name = clientid
socket.client.colour = {r: 1.0, g: 1.0, b: 1.0}
socket.client.selectorColour = 1
clients[clientid] = socket.client
logger.info("Authenticated".green + " %s:%s as %s", socket.remoteAddress, socket.remotePort, clientid)
reportServer.send({
type: "playerJoined",
identifier: socket.remoteAddress + "#" + clientid
})
} else {
logger.info("Banned".red + " %s:%s as %s", socket.remoteAddress, socket.remotePort, clientid)
response = new Buffer([0x02])
response = Buffer.concat([response, new Buffer.from("Banned from server", "ascii")])
}
socket.write(response)
break
case 3:
let posx = data.readFloatLE(0)
let posz = data.readFloatLE(4)
let velx = data.readFloatLE(8)
let velz = data.readFloatLE(12)
socket.client.position.x = posx
socket.client.position.y = posz
socket.client.velocity.x = velx
socket.client.velocity.z = velz
logger.debug(socket.client.clientid + " updated position to p[%d, %d], v[%d, %d]", posx, posz, velx, velz)
for (key in clients) {
clnt = clients[key]
if (clnt.clientid == socket.client.clientid) continue
let updateOtherPlayersPacket = new Buffer(18)
updateOtherPlayersPacket.writeInt8(0x04, 0)
updateOtherPlayersPacket.writeFloatLE(posx, 1)
updateOtherPlayersPacket.writeFloatLE(posz, 5)
updateOtherPlayersPacket.writeFloatLE(velx, 9)
updateOtherPlayersPacket.writeFloatLE(velz, 13)
updateOtherPlayersPacket.writeUInt8(socket.client.clientid.length, 17)
updateOtherPlayersPacket = Buffer.concat([updateOtherPlayersPacket, new Buffer.from(socket.client.clientid, "ascii")])
clnt.socket.write(updateOtherPlayersPacket)
}
break
case 6:
let chunkx = data.readInt32LE(0)
let chunkz = data.readInt32LE(4)
if (!isWithinWorldBounds(chunkx, chunkz)) {
logger.warn(socket.client.clientid + " tried to request chunk outwith world size of to %d, [%d, %d]", worldSize, chunkx, chunkz)
break;
}
newChunk = getChunkAtPosition({x: chunkx, y: chunkz})
logger.debug(socket.client.clientid + " requested chunk [%d, %d]", chunkx, chunkz)
sendChunkPacket(newChunk, socket.client)
break
case 7:
// Pixel placement packet
let pixelx = data.readInt32LE(0)
let pixelz = data.readInt32LE(4)
if (!isWithinWorldBounds(pixelx, pixelz)) {
logger.warn(socket.client.clientid + " tried to place pixel outwith world size of of to %d, [%d, %d]", worldSize, pixelx, pixelz)
break;
}
let pixelid = data.readInt32LE(8)
logger.verbose(socket.client.clientid + " placed pixel at [%d, %d] in colour %d", pixelx, pixelz, pixelid)
playerPlacedPixel(pixelx, pixelz, pixelid)
break
case 8:
// Pixel removal packet
let rpixelx = data.readInt32LE(0)
let rpixelz = data.readInt32LE(4)
if (!isWithinWorldBounds(rpixelx, rpixelz)) {
logger.warn(socket.client.clientid + " tried to remove pixel outwith world size of of to %d, [%d, %d]", worldSize, pixelx, pixelz)
break;
}
playerRemovedPixel(rpixelx, rpixelz)
break
case 9:
// Player information packet
let r = data.readFloatLE(0)
let g = data.readFloatLE(4)
let b = data.readFloatLE(8)
let selectorColour = data.readInt32LE(12)
let playerName = readString(data, 16)
if (playerName.length > 20) {
console.log("Got invalid player name length: " + playerName.length + " from " + socket.client.clientid)
endConnection(socket)
return
}
if (selectorColour < 0 || selectorColour > 14) {
console.log("Got invalid selector colour " + selectorColour + " from " + socket.client.clientid)
endConnection(socket)
return
}
socket.client.name = playerName
socket.client.colour = {r: r, g: g, b: b}
socket.client.selectorColour = selectorColour
logger.verbose(socket.client.clientid + " updated name to %s, colour to {%d,%d,%d} and selector colour to %d", playerName, r, g, b, selectorColour)
for (key in clients) {
clnt = clients[key]
if (clnt.clientid == socket.client.clientid) continue
let updateOtherPlayersPacket = new Buffer(2)
updateOtherPlayersPacket.writeInt8(0xB, 0)
updateOtherPlayersPacket.writeUInt8(socket.client.clientid.length, 1)
updateOtherPlayersPacket = Buffer.concat([updateOtherPlayersPacket, new Buffer.from(socket.client.clientid, "ascii")])
let integerInfo = new Buffer(17)
integerInfo.writeFloatLE(r, 0)
integerInfo.writeFloatLE(g, 4)
integerInfo.writeFloatLE(b, 8)
integerInfo.writeInt32LE(selectorColour, 12)
integerInfo.writeUInt8(playerName.length, 16)
updateOtherPlayersPacket = Buffer.concat([updateOtherPlayersPacket, integerInfo, new Buffer.from(playerName, "ascii")])
clnt.socket.write(updateOtherPlayersPacket)
}
break
default:
logger.warn("Invalid packet identifier %s from %s - ending connection", identifier.toString(), socket.client.clientid)
endConnection(socket)
}
}
expectsNewPacket = true
packetBuffer = new Buffer(0)
expectedLengthRemaining = 0
currentPacketInfo = null
nextPacketData = null
function readData(data, socket) {
try {
while (data.length > 0 && socket.shouldDisconnect == false) {
// If we expect the next data to be a new packet, read the length and identifier
if (expectsNewPacket) {
packetBuffer = new Buffer(0)
currentPacketInfo = getPacketInformationFromHeader(data)
if (currentPacketInfo == null) {
if (socket.client) logger.warn("Tried to read packet identifier from %s but got " + "null".red + "! Disconnecting...", socket.client.clientid)
else logger.warn("Tried to read packet identifier from " + "unauthed" + " %s:%s but got " + "null".red + "! Disconnecting...", socket.remoteAddress, socket.remotePort)
endConnection(socket)
return
}
data = currentPacketInfo.slicedData
expectedLengthRemaining = currentPacketInfo.length
expectsNewPacket = false
}
// If the data is more than the rest of the packet, just concat the rest of the packet and remove it from our block
if (data.length >= expectedLengthRemaining) {
packetBuffer = Buffer.concat([packetBuffer, data.slice(0, expectedLengthRemaining)])
data = data.slice(expectedLengthRemaining)
processPacket(packetBuffer, socket)
expectsNewPacket = true
} else {
// Or if the data length is less than what we need, just add all that we can and we'll add more later
packetBuffer = Buffer.concat([packetBuffer, data.slice(0, data.length)])
data = data.slice(data.length)
}
}
} catch (e) {
if (socket.client) logger.warn("Error".red + " while processing packet for %s. Current packet info: %j", socket.client.clientid, currentPacketInfo)
else logger.warn("Error".red + " while processing packet for unauthed %s:%s. Current packet info: %j", socket.remoteAddress, socket.remotePort, currentPacketInfo)
logger.error(e)
endConnection(socket)
}
}
publicip.v4().then(ip => {
if (connectToLocalhost) ip = "localhost"
logger.info("Starting server with outwards-facing IP %s:%s", ip.green, port)
server.listen(port, ip, (err) => {
logger.info("READY".green + " - accepting connections")
reportServer.send({type: "start", ip: ip, port: reportServerPort})
})
});
server.on("error", (err) => {
logger.error("Uncaught server error: %j", err)
})
function shouldAcceptClient(clientid) {
return true;
}
function getChunkAtPosition(position) {
// Try and get chunk from memory, otherwise load from file
let chunk = chunks[position.x + "," + position.y]
if (!chunk) {
return loadChunkAtPosition(position)
} else {
logger.silly("Sending chunk [%d, %d] from cache with %d pixels", position.x, position.y, Object.keys(chunk.pixels).length)
return chunk
}
}
// Defunct, players are removed from other people's games by not sending a position update in a while
function broadcastClientQuit(clientid) {
for (cid in clients) {
if (cid == clientid) continue
let clnt = clients[cid]
let packet = new Buffer(2)
packet.writeInt8(0xA, 0)
packet.writeUInt8(clientid.length, 1)
packet = Buffer.concat([packet, new Buffer.from(clientid, "ascii")])
clnt.socket.write(packet)
}
}
function broadcastChunkPacket(chunk) {
// TODO: Only update players nearby
logger.silly("Broadcasting chunk change [%d, %d] to %d other players", chunk.x, chunk.y, Object.keys(clients).length)
for (clientid in clients) {
sendChunkPacket(chunk, clients[clientid])
}
}
function sendChunkPacket(chunk, client) {
try {
// If chunk has no pixel data just send a blank chunk packet
if (Object.keys(chunk.pixels).length == 0) {
logger.silly("Sending " + "blank" + " chunk [%d, %d] to %s", chunk.x, chunk.y, client.clientid)
let emptyChunkPacket = new Buffer(9)
emptyChunkPacket.writeInt8(0x05, 0)
emptyChunkPacket.writeInt32LE(chunk.x, 1)
emptyChunkPacket.writeInt32LE(chunk.y, 5)
client.socket.write(emptyChunkPacket)
} else {
logger.silly("Sending chunk [%d, %d] to %s", chunk.x, chunk.y, client.clientid)
// Otherwise send all the pixels
let chunkPacket = new Buffer(9 + (chunkSize * chunkSize))
chunkPacket.writeInt8(0x09, 0)
chunkPacket.writeInt32LE(chunk.x, 1)
chunkPacket.writeInt32LE(chunk.y, 5)
let byteCount = 9
for (let x = 0; x < chunkSize; x++) {
for (let y = 0; y < chunkSize; y++) {
// If the pixel is not found, set the colour to 0, which is blank
let pixelColor = 0
let pixelKey = x + "," + y
if (pixelKey in chunk.pixels) pixelColor = chunk.pixels[pixelKey]
chunkPacket.writeInt8(pixelColor, byteCount)
byteCount++
}
}
client.socket.write(chunkPacket)
}
} catch (e) {
logger.warn("Error while sending chunk [%d, %d] to %s", chunk.x, chunk.y, client.clientid)
logger.error(e)
}
}
function playerPlacedPixel(x, y, colour) {
let chunkPosition = getNearestChunkTo({x: x, y: y})
if (!isWithinWorldBounds(chunkPosition.x, chunkPosition.y)) return
let chunk = getChunkAtPosition(chunkPosition)
let relativePixelPosition = getRelativePixelPos({x: x, y: y}, chunkPosition)
chunk.pixels[relativePixelPosition.x + "," + relativePixelPosition.y] = colour
broadcastChunkPacket(chunk)
}
function playerRemovedPixel(x, y) {
let chunkPosition = getNearestChunkTo({x: x, y: y})
if (!isWithinWorldBounds(chunkPosition.x, chunkPosition.y)) return
let chunk = getChunkAtPosition(chunkPosition)
let relativePixelPosition = getRelativePixelPos({x: x, y: y}, chunkPosition)
delete chunk.pixels[relativePixelPosition.x + "," + relativePixelPosition.y]
broadcastChunkPacket(chunk)
}
function getRelativePixelPos(pixelPosition, chunkPosition) {
let x = pixelPosition.x - chunkPosition.x + (chunkSize/2)
let y = pixelPosition.y - chunkPosition.y + (chunkSize/2)
return {x: x, y: y}
}
// DEFUNCT - clients now request the chunks they want
function isChunkWithinViewingArea(client, position) {
var a = client.position.x - position.x
var b = client.position.y - position.y
return Math.sqrt( a*a + b*b ) < viewingDistance
}
// Just snaps a pos to a 16x16 grid
function getNearestChunkTo(position) {
let x = Math.round(position.x / chunkSize) * chunkSize
let y = Math.round(position.y / chunkSize) * chunkSize
return {x: x, y: y}
}
function saveChunks() {
logger.verbose("SAVING WORLD...".green)
for (chunkloc in chunks) {
saveChunk(chunks[chunkloc])
}
}
function saveChunk(chunk) {
try {
// Don't save if there are no pixels in the chunk
if (Object.keys(chunk.pixels) == 0) {
logger.debug("Skipping saving chunk [%d, %d] to file as it is blank", chunk.x, chunk.y)
return
}
logger.debug("Saving chunk [%d, %d] with %d pixels to file world/Chunk" + chunk.x + "," + chunk.y, chunk.x, chunk.y, Object.keys(chunk.pixels).length)
var wstream = fs.createWriteStream("world/Chunk" + chunk.x + "," + chunk.y);
var buffer = new Buffer(chunkSize*chunkSize)
let byteCount = 0
for (let x = 0; x < chunkSize; x++) {
for (let y = 0; y < chunkSize; y++) {
// If the pixel is not found, set the colour to 0, which is blank
let pixelColor = 0
let pixelKey = x + "," + y
if (pixelKey in chunk.pixels) pixelColor = chunk.pixels[pixelKey]
buffer.writeInt8(pixelColor, byteCount)
byteCount++
}
}
wstream.write(buffer)
wstream.end();
} catch (e) {
logger.error("Error while saving chunk [%d, %d] to file", chunk.x, chunk.y)
logger.error(e)
}
}
function loadChunkAtPosition(position) {
// TODO: Load chunk from file here, otherwise return empty chunk
if (!fs.existsSync("world/Chunk" + position.x + "," + position.y)) {
let newChunk = new Chunk(position.x, position.y)
chunks[position.x + "," + position.y] = newChunk
logger.silly("Created new chunk [%d, %d]", position.x, position.y)
return newChunk
}
let chunk = new Chunk(position.x, position.y)
chunks[position.x + "," + position.y] = chunk
try {
let buffer = fs.readFileSync("world/Chunk" + position.x + "," + position.y)
//console.log("Buffer contents: '" + buffer.toString() + "'")
let byteCount = 0
for (let x = 0; x < chunkSize; x++) {
for (let y = 0; y < chunkSize; y++) {
// If the pixel is not found, set the colour to 0, which is blank
let pixelKey = x + "," + y
let pixelColor = buffer.readInt8(byteCount)
if (pixelColor > 0) chunk.pixels[pixelKey] = pixelColor
byteCount++
}
}
logger.silly("Loaded chunk [%d, %d] from file with %d pixels", position.x, position.y, Object.keys(chunk.pixels).length)
} catch (e) {
logger.error("While loading chunk [%d, %d] from file, got an " + "error".red, position.x, position.y)
logger.error(e)
}
return chunk
}