diff --git a/config.default.js b/config.default.js index 75bc7fa..ca38121 100644 --- a/config.default.js +++ b/config.default.js @@ -1,8 +1,6 @@ var config = { name: 'Node', type: 'C++', - rpcHost: 'localhost', - rpcPort: '8080', serverHost: 'localhost', serverPort: '3000' }; diff --git a/lib/node.js b/lib/node.js index a1ef3a4..8512984 100644 --- a/lib/node.js +++ b/lib/node.js @@ -1,6 +1,8 @@ var web3 = require('ethereum.js'); var _ = require('underscore'); var os = require('os'); +var slugs = require('slugs'); +var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest; var MAX_BLOCKS_HISTORY = 12, LOWEST_TIMESTAMP = 0; @@ -9,14 +11,15 @@ function Node(options) { this.options = options; this.info = { + ip: this.getExternalIp(), name: options.name, - ip: options.rpcHost, type: options.type, os: os.platform(), os_v: os.release() }; - this.info.id = this.info.ip; + this.info.id = this.makeId(); + console.log(this.info); this.stats = { active: false, listening: false, @@ -39,7 +42,7 @@ function Node(options) this.chainWatch = false; this.updateInterval = false; - var sock = new web3.providers.HttpSyncProvider('http://' + this.options.rpcHost + ':' + this.options.rpcPort); + var sock = new web3.providers.HttpSyncProvider('http://' + (process.env.RPC_HOST || 'localhost') + ':' + (process.env.RPC_PORT || '8080')); web3.setProvider(sock); this.init(); @@ -47,6 +50,23 @@ function Node(options) return this; } +Node.prototype.getExternalIp = function() +{ + var request = new XMLHttpRequest(); + request.open('GET', 'http://curlmyip.com/', false); + request.send(); + + if(request.status !== 200) + return 'unknown'; + + return request.responseText.trim(); +} + +Node.prototype.makeId = function() +{ + return slugs(this.info.name + ' ' + this.info.type + ' ' + this.info.os + ' ' + this.info.os_v + ' ' + this.info.ip); +} + Node.prototype.isActive = function() { this.stats.uptime.inc++; diff --git a/package.json b/package.json index 3d09715..fb47481 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,8 @@ "debug": "~2.0.0", "ethereum.js": "*", "express.io": "^1.1.13", + "xmlhttprequest": "*", + "slugs": "^0.1.3", "underscore": "^1.7.0" } }