From d5113e380006334980e83c780178f7cb4a644d32 Mon Sep 17 00:00:00 2001 From: Li Yong Date: Fri, 28 Sep 2012 11:25:51 +0800 Subject: [PATCH] anti none ext-hub sourced connection for proxy --- lib/dco_proxy.js | 51 ++++++++++++++++++++++++++++++++++-------------- lib/ext_hub.js | 3 +++ 2 files changed, 39 insertions(+), 15 deletions(-) diff --git a/lib/dco_proxy.js b/lib/dco_proxy.js index 647c984..5feaa52 100644 --- a/lib/dco_proxy.js +++ b/lib/dco_proxy.js @@ -56,22 +56,43 @@ utl.gracefulExit(function(){ exports.createServer = function(handler){ var server = net.createServer(function(extHubSock){ console.log('connect from ext_hub'); - extHubSocks.push(extHubSock); - (function(extHubID){ - extHubSock.on('close', function(){ - console.log('ext-hub(%d) will not send new request, in-bound socket half closed', extHubID); - delete extHubSocks[extHubID - 1]; - }); - })(extHubSocks.length); - new StreamSpliter(extHubSock, 'readInt32BE', onRequest); - function onRequest(pdu){ - counter.start(); - if (pdu.readInt32BE(0) > 0) { - handler(new Request(pdu, true), new Response(extHubSock, pdu.readUInt16BE(4))); - } else { - handler(new Request(pdu, false), new DummyResponse()); + + extHubSock.once('data', function onHandshake(data){ + + try { + var ptoken = data.readInt32BE(0); + } catch (e) { + ptoken = -1; } - } + if (ptoken !== 197610263) { + console.warn('none ext-hub connection to out proxy attempt found'); + extHubSock.end(); + extHubSock.destroy(); + return; + } + + extHubSocks.push(extHubSock); + (function(extHubID){ + extHubSock.on('close', function(){ + console.log('ext-hub(%d) will not send new request, in-bound socket half closed', extHubID); + delete extHubSocks[extHubID - 1]; + }); + })(extHubSocks.length); + new StreamSpliter(extHubSock, 'readInt32BE', onRequest); + function onRequest(pdu){ + counter.start(); + if (pdu.readInt32BE(0) > 0) { + handler(new Request(pdu, true), new Response(extHubSock, pdu.readUInt16BE(4))); + } else { + handler(new Request(pdu, false), new DummyResponse()); + } + } + + if (data.length !== 4) { + logConnOra('first chunk is not 4 bytes length.'); + oraSock.emit('data', data.slice(4)); + } + }); }); return new DcoWorkerProxy(server); }; diff --git a/lib/ext_hub.js b/lib/ext_hub.js index 50d7eed..72af8be 100644 --- a/lib/ext_hub.js +++ b/lib/ext_hub.js @@ -162,6 +162,9 @@ function WPWrapper(wpSock, id, hostp, maxPendingRpc, desc){ wpSock.on('connect', function(){ wpw.setStatus('opened', 'TCP socket connected'); + var ptoken = new Buffer(4); + ptoken.writeInt32BE(197610263, 0); + wpSock.write(ptoken); logConnWP('Connect to proxy NO.%d(%s) is ok', wpw.id, wpw.desc); wpw.queue.forEach(function(PDU){ wpSock.write(PDU);