Merge pull request #57 from suchpool/patch-2

Update to stratum.js
This commit is contained in:
Matthew Little 2014-04-15 12:10:49 -06:00
commit 42178dd5ad

View File

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