From a773bb13e734c11ea62dc38b396b5c0e81c56730 Mon Sep 17 00:00:00 2001 From: Brian Schlenker Date: Thu, 26 Mar 2015 01:50:57 -0700 Subject: [PATCH] added hook for before client receives ping createServer takes additional option 'beforePing' which is a function receiving the response to send back to the client and the client object, and returning a modified response to send back. The client now has the serverHost, serverPort, and protocolVersion pulled from the handshake packet. This can be used to proxy the client to different backend servers depending on the domain name. --- src/index.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/index.js b/src/index.js index 3889c43..557e7f9 100644 --- a/src/index.js +++ b/src/index.js @@ -44,6 +44,9 @@ function createServer(options) { var kickTimeout = options.kickTimeout || 10 * 1000; var checkTimeoutInterval = options.checkTimeoutInterval || 4 * 1000; var onlineMode = options['online-mode'] == null ? true : options['online-mode']; + // a function receiving the default status object and the client + // and returning a modified response object. + var beforePing = options.beforePing || null; var serverKey = ursa.generatePrivateKey(1024); @@ -117,6 +120,10 @@ function createServer(options) { "favicon": server.favicon }; + if (beforePing) { + response = beforePing(response, client) || response; + } + client.once([states.STATUS, 0x01], function(packet) { client.write(0x01, { time: packet.time }); client.end(); @@ -151,6 +158,9 @@ function createServer(options) { } function onHandshake(packet) { + client.serverHost = packet.serverHost; + client.serverPort = packet.serverPort; + client.protocolVersion = packet.protocolVersion; if (packet.nextState == 1) { client.state = states.STATUS; } else if (packet.nextState == 2) {