diff --git a/lib/stratum.js b/lib/stratum.js index da3b039..e7d0f8d 100644 --- a/lib/stratum.js +++ b/lib/stratum.js @@ -45,7 +45,7 @@ var StratumClient = function(options){ if (totalShares >= banning.checkThreshold){ var percentBad = (_this.shares.invalid / totalShares) * 100; if (percentBad >= banning.invalidPercent){ - _this.emit('ban', _this.socket.remoteAddress); + _this.emit('ban', _this.remoteAddress); _this.socket.end(); } else //reset shares @@ -116,7 +116,7 @@ var StratumClient = function(options){ function handleAuthorize(message, replyToSocket){ _this.workerIP = options.socket.address().address; - _this.workerName = message.params[0]; + _this.workerName = message.params[0].toLowerCase(); _this.workerPass = message.params[1]; options.authorizeFn(_this.workerIP, _this.workerName, _this.workerPass, function(result) { _this.authorized = (!result.error && result.authorized); @@ -187,6 +187,13 @@ var StratumClient = function(options){ var socket = options.socket; var dataBuffer = ''; socket.setEncoding('utf8'); + socket.once('data', function(d){ + if (d.indexOf('PROXY') === 0){ + _this.remoteAddress = d.split(' ')[2]; + console.log('detected proxy source IP address of ' + _this.remoteAddress); + } + _this.emit('checkBan'); + }); socket.on('data', function(d){ dataBuffer += d; if (Buffer.byteLength(dataBuffer, 'utf8') > 1024){ @@ -203,8 +210,10 @@ var StratumClient = function(options){ try { messageJson = JSON.parse(message); } catch(e) { - _this.emit('malformedMessage', message); - socket.end(); + if (d.indexOf('PROXY') !== 0){ + _this.emit('malformedMessage', message); + socket.end(); + } return; } @@ -275,13 +284,13 @@ var StratumClient = function(options){ this.manuallyAuthClient = function (username, password) { handleAuthorize({id: 1, params: [username, password]}, false /*do not reply to miner*/); - } + }; this.manuallySetValues = function (otherClient) { _this.extraNonce1 = otherClient.extraNonce1; _this.previousDifficulty = otherClient.previousDifficulty; _this.difficulty = otherClient.difficulty; - } + }; }; StratumClient.prototype.__proto__ = events.EventEmitter.prototype; @@ -317,19 +326,20 @@ var StratumServer = exports.Server = function StratumServer(ports, connectionTim } }, 1000 * banning.purgeInterval); - this.handleNewClient = function (socket){ - if (banning && banning.enabled && socket.remoteAddress in bannedIPs){ - var bannedTime = bannedIPs[socket.remoteAddress]; + var checkBan = function(client){ + if (banning && banning.enabled && client.remoteAddress in bannedIPs){ + var bannedTime = bannedIPs[client.remoteAddress]; if ((Date.now() - bannedTime) < bannedMS){ - socket.end(); + client.socket.end(); return null; } else { - delete bannedIPs[socket.remoteAddress]; + delete bannedIPs[client.remoteAddress]; } } + }; - // If we're here the client was not banned. + this.handleNewClient = function (socket){ socket.setKeepAlive(true); var subscriptionId = subscriptionCounter.next(); @@ -344,7 +354,6 @@ var StratumServer = exports.Server = function StratumServer(ports, connectionTim } ); - stratumClients[subscriptionId] = client; _this.emit('client.connected', client); client.on('socketDisconnect', function() { @@ -352,6 +361,8 @@ var StratumServer = exports.Server = function StratumServer(ports, connectionTim _this.emit('client.disconnected', client); }).on('ban', function(ipAddress){ _this.banIP(ipAddress); + }).on('checkBan', function(){ + checkBan(client); }); return subscriptionId; };