mirror of
https://github.com/unmojang/node-minecraft-protocol.git
synced 2025-09-30 22:51:23 -04:00
passing test: server kicks clients that do not send keepalive packets
This commit is contained in:
parent
150c89cc6d
commit
68b29db639
47
index.js
47
index.js
@ -34,7 +34,6 @@ function createServer(options) {
|
|||||||
server.on("connection", function(client) {
|
server.on("connection", function(client) {
|
||||||
client.once(0xfe, onPing);
|
client.once(0xfe, onPing);
|
||||||
client.on(0x02, onHandshake);
|
client.on(0x02, onHandshake);
|
||||||
client.on(0x00, onKeepAlive);
|
|
||||||
client.on('end', onEnd);
|
client.on('end', onEnd);
|
||||||
|
|
||||||
var keepAlive = false;
|
var keepAlive = false;
|
||||||
@ -49,19 +48,28 @@ function createServer(options) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function keepAliveLoop() {
|
function keepAliveLoop() {
|
||||||
if (keepAlive) {
|
if (! keepAlive) return;
|
||||||
// check if the last keepAlive was too long ago (kickTimeout)
|
|
||||||
if (lastKeepAlive) {
|
// check if the last keepAlive was too long ago (kickTimeout)
|
||||||
var elapsed = new Date() - lastKeepAlive;
|
var elapsed = new Date() - lastKeepAlive;
|
||||||
if (elapsed > kickTimeout) {
|
if (elapsed > kickTimeout) {
|
||||||
client.end('KeepAliveTimeout');
|
client.end('KeepAliveTimeout');
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
}
|
|
||||||
client.write(0x00, {
|
|
||||||
keepAliveId: Math.floor(Math.random() * 2147483648)
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
client.write(0x00, {
|
||||||
|
keepAliveId: Math.floor(Math.random() * 2147483648)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function onKeepAlive(packet) {
|
||||||
|
lastKeepAlive = new Date();
|
||||||
|
}
|
||||||
|
|
||||||
|
function startKeepAlive() {
|
||||||
|
keepAlive = true;
|
||||||
|
lastKeepAlive = new Date();
|
||||||
|
keepAliveTimer = setInterval(keepAliveLoop, checkTimeoutInterval);
|
||||||
|
client.on(0x00, onKeepAlive);
|
||||||
}
|
}
|
||||||
|
|
||||||
function onEnd() {
|
function onEnd() {
|
||||||
@ -69,14 +77,6 @@ function createServer(options) {
|
|||||||
clearTimeout(loginKickTimer);
|
clearTimeout(loginKickTimer);
|
||||||
}
|
}
|
||||||
|
|
||||||
function onKeepAlive(packet) {
|
|
||||||
if (keepAlive) {
|
|
||||||
lastKeepAlive = new Date();
|
|
||||||
} else {
|
|
||||||
lastKeepAlive = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function onPing(packet) {
|
function onPing(packet) {
|
||||||
if (loggedIn) return;
|
if (loggedIn) return;
|
||||||
client.write(0xff, {
|
client.write(0xff, {
|
||||||
@ -94,12 +94,11 @@ function createServer(options) {
|
|||||||
function onHandshake(packet) {
|
function onHandshake(packet) {
|
||||||
assert.ok(! onlineMode);
|
assert.ok(! onlineMode);
|
||||||
loggedIn = true;
|
loggedIn = true;
|
||||||
keepAlive = true;
|
|
||||||
client.username = packet.username;
|
client.username = packet.username;
|
||||||
|
startKeepAlive();
|
||||||
|
|
||||||
clearTimeout(loginKickTimer);
|
clearTimeout(loginKickTimer);
|
||||||
loginKickTimer = null;
|
loginKickTimer = null;
|
||||||
keepAliveTimer = setInterval(keepAliveLoop, checkTimeoutInterval);
|
|
||||||
|
|
||||||
server.emit('login', client);
|
server.emit('login', client);
|
||||||
}
|
}
|
||||||
@ -115,7 +114,7 @@ function createClient(options) {
|
|||||||
var host = options.host || 'localhost';
|
var host = options.host || 'localhost';
|
||||||
assert.ok(options.username, "username is required");
|
assert.ok(options.username, "username is required");
|
||||||
var haveCredentials = options.email && options.password;
|
var haveCredentials = options.email && options.password;
|
||||||
var keepAlive = !!options.keepAlive;
|
var keepAlive = options.keepAlive == null ? true : options.keepAlive;
|
||||||
|
|
||||||
var client = new Client({
|
var client = new Client({
|
||||||
isServer: false
|
isServer: false
|
||||||
|
32
test/test.js
32
test/test.js
@ -233,7 +233,7 @@ describe("client", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe("server", function() {
|
describe("mc-server", function() {
|
||||||
it("starts listening and shuts down cleanly", function(done) {
|
it("starts listening and shuts down cleanly", function(done) {
|
||||||
var server = mc.createServer({ 'online-mode': false });
|
var server = mc.createServer({ 'online-mode': false });
|
||||||
var listening = false;
|
var listening = false;
|
||||||
@ -275,6 +275,36 @@ describe("server", function() {
|
|||||||
if (count <= 0) done();
|
if (count <= 0) done();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
it("kicks clients that do not send keepalive packets", function(done) {
|
||||||
|
var server = mc.createServer({
|
||||||
|
'online-mode': false,
|
||||||
|
kickTimeout: 100,
|
||||||
|
checkTimeoutInterval: 10,
|
||||||
|
});
|
||||||
|
var count = 2;
|
||||||
|
server.on('connection', function(client) {
|
||||||
|
client.on('end', function(reason) {
|
||||||
|
assert.strictEqual(reason, "KeepAliveTimeout");
|
||||||
|
server.close();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
server.on('close', function() {
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
|
server.on('listening', function() {
|
||||||
|
var client = mc.createClient({
|
||||||
|
username: 'superpants',
|
||||||
|
keepAlive: false,
|
||||||
|
});
|
||||||
|
client.on('end', function() {
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
function resolve() {
|
||||||
|
count -= 1;
|
||||||
|
if (count <= 0) done();
|
||||||
|
}
|
||||||
|
});
|
||||||
it("responds to ping requests", function(done) {
|
it("responds to ping requests", function(done) {
|
||||||
var server = mc.createServer({
|
var server = mc.createServer({
|
||||||
'online-mode': false,
|
'online-mode': false,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user