From 6472baacc2e745e10ab2cebfa3edad5c7e0b07dc Mon Sep 17 00:00:00 2001 From: Andrea Baccega Date: Sun, 12 Jan 2014 21:54:20 +0100 Subject: [PATCH] Removed 'authorize' emit event and created which is now replaced by an authorize function. Added ip address to the authorize event Added ability for the authorizeFn to ask StratumClient to close connection --- index.js | 38 +++++++++++++++++++++++++----------- libs/stratum.js | 52 +++++++++++++++++++++++++++---------------------- 2 files changed, 56 insertions(+), 34 deletions(-) diff --git a/index.js b/index.js index 4708e0f..3890b71 100644 --- a/index.js +++ b/index.js @@ -22,6 +22,23 @@ var pool = module.exports = function pool(coin){ algorithm: coin.options.algorithm, address: coin.options.address }); + + + // Worker authorizer fn. + var authorizeFn; + if ( typeof (coin.authorizeFn) === 'function' ) { + authorizeFn = coin.authorizeFn; + } else { + authorizeFn = function (ip, workerName, password, cback) { + // Default implementation just returns true + console.log("Athorize ["+ip+"] "+workerName+":"+password); + cback(null, true, true); + }; + } + + + + this.jobManager.on('newBlock', function(blockTemplate){ if ( typeof(_this.stratumServer ) === 'undefined') { console.warn("Stratum server still not started! cannot broadcast block!"); @@ -108,7 +125,8 @@ var pool = module.exports = function pool(coin){ console.log('Stratum server starting on port ' + coin.options.stratumPort + ' for ' + coin.options.name); _this.stratumServer = new stratum.Server({ - port: coin.options.stratumPort + port : coin.options.stratumPort, + authorizeFn : authorizeFn, }); _this.stratumServer.on('started', function(){ _this.emit('started'); @@ -127,8 +145,6 @@ var pool = module.exports = function pool(coin){ } else { this.sendMiningJob(_this.jobManager.currentJob.getJobParams()); } - }).on('authorize', function(params, resultCallback){ - resultCallback(null, true); }).on('submit', function(params, resultCallback){ var result =_this.jobManager.processShare( params.jobId, @@ -147,14 +163,14 @@ var pool = module.exports = function pool(coin){ } else { resultCallback(null, true); _this.emit('share', true, { - blockHeaderHex : result.headerHEX, - workerName : params.name, - jobId : params.jobId, - clientDifficulty : client.difficulty, - extraNonce1 : client.extraNonce1, - extraNonce2 : params.extraNonce2, - nTime : params.nTime, - nonce : params.nonce + blockHeaderHex : result.headerHEX, + workerName : params.name, + jobId : params.jobId, + clientDifficulty : client.difficulty, + extraNonce1 : client.extraNonce1, + extraNonce2 : params.extraNonce2, + nTime : params.nTime, + nonce : params.nonce }); } diff --git a/libs/stratum.js b/libs/stratum.js index 076a4da..7cb932f 100644 --- a/libs/stratum.js +++ b/libs/stratum.js @@ -23,7 +23,6 @@ var SubscriptionCounter = function(){ * Defining each client that connects to the stratum server. * Emits: * - 'subscription'(obj, cback(error, extraNonce1, extraNonce2Size)) - * - 'authorize'() FIX THIS * - 'submit' FIX THIS. **/ var StratumClient = function(options){ @@ -80,21 +79,21 @@ var StratumClient = function(options){ } function handleAuthorize(message){ - _this.emit('authorize', - { - name: message.params[0][0], - password: message.params[0][1] - }, - function(error, result){ - _this.authorized = result; - - sendJson({ - id : message.id, - result: result, - error : error + var workerName = message.params[0]; + var workerPass = message.params[1]; + options.authorizeFn(options.socket.address().address, workerName, workerPass, function(err, authorized, shouldCloseSocket) { + _this.authorized = ( ! err && authorized ); + sendJson({ + id : message.id, + result : _this.authorized, + error : err }); + + // If the authorizer wants us to close the socket lets do it. + if (typeof(shouldCloseSocket) === 'boolean' && shouldCloseSocket) { + options.socket.end(); } - ); + }); } function handleSubmit(message){ @@ -117,17 +116,17 @@ var StratumClient = function(options){ console.log("SUBMIT "+JSON.stringify(message)); _this.emit('submit', { - name : message.params[0], - jobId : message.params[1], - extraNonce2: message.params[2], - nTime : message.params[3], - nonce : message.params[4] + name : message.params[0], + jobId : message.params[1], + extraNonce2 : message.params[2], + nTime : message.params[3], + nonce : message.params[4] }, function(error, result){ sendJson({ - id : message.id, - result: result, - error : error + id : message.id, + result : result, + error : error }); } ); @@ -187,6 +186,7 @@ var StratumClient = function(options){ params: [difficulty]//[512], }); }; + this.sendMiningJob = function(jobParams){ sendJson({ id : null, @@ -211,7 +211,13 @@ var StratumServer = exports.Server = function StratumServer(options){ (function init(){ _socketServer = socketServer = net.createServer(function(c){ var subscriptionId = subscriptionCounter.next(); - var client = new StratumClient({subscriptionId: subscriptionId, socket: c}); + var client = new StratumClient( + { + subscriptionId : subscriptionId, + socket : c, + authorizeFn : options.authorizeFn + } + ); stratumClients[subscriptionId] = client; _this.emit('client', client); });