This repository has been archived by the owner on Feb 17, 2018. It is now read-only.
/
Server.js
100 lines (88 loc) · 3.58 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
var assert = require('assert'), auth = require('./auth'), proto = require('./protocol'), events = require('events'), net = require('net'), util = require('util');
function Server() {
net.Server.call(this);
}
util.inherits(Server, net.Server);
Server.prototype.useAuth = false;
Server.prototype.protoVersion = 17; // Latest stable (1.8.1).
Server.prototype.on = function on(event, f) {
if(event == 'connection')
return net.Server.prototype.on.call(this, event, function(socket) {return f(new Socket(socket, this));}.bind(this));
return net.Server.prototype.on.apply(this, arguments);
};
Server.prototype.listen = function listen(port, host, listeningListener) {
return net.Server.prototype.listen.call(this, port || 25565, host, listeningListener);
};
exports.Server = Server;
exports.createServer = function createServer(connectionListener) {
var server = new Server();
if(connectionListener)
server.on('connection', connectionListener);
return server;
};
function Socket(sock, server) {
events.EventEmitter.call(this);
this.sock = sock;
this.server = server;
this.proto = proto[this.server.protoVersion];
this.sock.on('error', this.emit.bind(this, 'error'));
this.sock.on('close', this.emit.bind(this, 'close', 'Socket closed.'));
this.sock.on('connect', function() {
this.sock.on('data', this.processPackets.bind(this));
this.on('close', function() {
if(this.posEmitter)
clearInterval(this.posEmitter);
this.sock.end(proto.pack({type:proto.packets.DISCONNECT}, this.proto.server2client));
}.bind(this));
}.bind(this));
}
util.inherits(Socket, events.EventEmitter);
Socket.prototype.processPackets = function processPackets(data) {
try {
var start = 0;
if(this.lastBuffer)
data = concatBuffers(this.lastBuffer, data);
while(start < data.length) {
var packet = proto.unpack(data, start, this.proto.client2server);
if(!packet) {
this.lastBuffer = data.slice(start);
return;
}
if(packet.type == proto.packets.HANDSHAKE) {
this.username = packet.username;
if(this.server.auth) {
this.hash = auth.genServerHash();
this.write(proto.packets.HANDSHAKE, {hash: this.hash});
} else
this.write(proto.packets.HANDSHAKE, {hash: '-'});
} else if(packet.type == proto.packets.LOGIN) {
assert(packet.protoVersion == this.server.protoVersion);
assert(packet.username == this.username);
if(this.server.useAuth && this.hash)
auth.checkServer(this.username, this.hash, function(error) {
if(error)
this.emit('error', error);
else
this.emit('login', this.username);
});
else
this.emit('login', this.username);
}
else if(packet.type == proto.packets.DISCONNECT)
this.emit('close', packet.message);
else
this.emit('packet', packet);
start += packet.length;
}
delete this.lastBuffer;
} catch(e) {
this.emit('error', e);
}
};
Socket.prototype.write = function write(type, packet) {
if(packet !== undefined)
packet.type = type;
else
packet = type;
this.sock.write(proto.pack(packet, this.proto.server2client));
};