2015-02-17 06:12:44 +01:00
|
|
|
var express = require('express');
|
|
|
|
var app = express();
|
2014-12-03 04:08:49 +01:00
|
|
|
var path = require('path');
|
|
|
|
var favicon = require('serve-favicon');
|
|
|
|
var bodyParser = require('body-parser');
|
2015-02-05 00:55:48 +01:00
|
|
|
|
2015-02-17 03:04:15 +01:00
|
|
|
var Primus = require('primus'),
|
|
|
|
api,
|
|
|
|
client;
|
2015-02-05 16:21:22 +01:00
|
|
|
|
2015-02-26 22:58:22 +01:00
|
|
|
var WS_SECRET = process.env.WS_SECRET || "eth-net-stats-has-a-secret";
|
|
|
|
|
2015-02-17 03:04:15 +01:00
|
|
|
var Collection = require('./models/collection');
|
|
|
|
var Nodes = new Collection();
|
|
|
|
|
2015-02-17 06:12:44 +01:00
|
|
|
var server = require('http').createServer(app);
|
|
|
|
|
2015-02-17 03:04:15 +01:00
|
|
|
api = new Primus(server, {
|
|
|
|
transformer: 'websockets',
|
|
|
|
pathname: '/api',
|
|
|
|
parser: 'JSON'
|
|
|
|
});
|
|
|
|
|
|
|
|
api.use('emit', require('primus-emit'));
|
2015-02-23 14:57:41 +01:00
|
|
|
api.use('spark-latency', require('primus-spark-latency'));
|
2015-02-17 03:04:15 +01:00
|
|
|
|
2015-02-17 06:12:44 +01:00
|
|
|
var client = new Primus(server, {
|
|
|
|
transformer: 'websockets',
|
|
|
|
pathname: '/primus',
|
|
|
|
parser: 'JSON'
|
|
|
|
});
|
|
|
|
|
2015-04-06 07:33:01 +02:00
|
|
|
var clientLatency = 0;
|
|
|
|
|
2015-02-17 06:12:44 +01:00
|
|
|
client.use('emit', require('primus-emit'));
|
|
|
|
|
2015-02-17 03:04:15 +01:00
|
|
|
api.on('connection', function(spark) {
|
2015-02-23 14:57:41 +01:00
|
|
|
console.log('Latency: ', spark.latency);
|
2015-02-17 03:04:15 +01:00
|
|
|
console.log(spark.id);
|
|
|
|
console.log(spark.address);
|
|
|
|
console.log(spark.query);
|
|
|
|
|
2015-02-18 07:06:41 +01:00
|
|
|
spark.on('hello', function(data)
|
|
|
|
{
|
2015-02-23 14:57:41 +01:00
|
|
|
console.log('Latency: ', spark.latency);
|
2015-04-28 02:52:41 +02:00
|
|
|
console.log('Got hello data from ', spark.id);
|
2015-02-17 03:04:15 +01:00
|
|
|
console.log(data);
|
2015-02-17 06:12:44 +01:00
|
|
|
|
2015-02-26 23:03:57 +01:00
|
|
|
if(typeof data.secret === 'undefined' || data.secret !== WS_SECRET)
|
2015-02-26 22:55:39 +01:00
|
|
|
{
|
|
|
|
spark.end(undefined, { reconnect: false });
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-17 06:12:44 +01:00
|
|
|
if(typeof data.id !== 'undefined' && typeof data.info !== 'undefined')
|
|
|
|
{
|
|
|
|
data.ip = spark.address.ip;
|
2015-02-18 07:06:41 +01:00
|
|
|
data.spark = spark.id;
|
2015-02-23 15:20:05 +01:00
|
|
|
data.latency = spark.latency;
|
2015-02-17 06:12:44 +01:00
|
|
|
|
|
|
|
var info = Nodes.add(data);
|
|
|
|
spark.emit('ready');
|
|
|
|
|
2015-02-17 10:30:41 +01:00
|
|
|
client.write({action: 'add', data: info});
|
2015-04-17 11:10:20 +02:00
|
|
|
|
|
|
|
var blockPropagationChart = Nodes.blockPropagationChart();
|
|
|
|
client.write({action: 'blockPropagationChart', data: blockPropagationChart});
|
2015-04-24 05:23:26 +02:00
|
|
|
|
|
|
|
var uncleCount = Nodes.getUncleCount();
|
|
|
|
client.write({action: 'uncleCount', data: uncleCount});
|
2015-02-17 06:12:44 +01:00
|
|
|
}
|
2015-02-17 03:04:15 +01:00
|
|
|
});
|
|
|
|
|
2015-02-18 07:06:41 +01:00
|
|
|
spark.on('update', function(data)
|
|
|
|
{
|
2015-02-23 14:57:41 +01:00
|
|
|
console.log('Latency: ', spark.latency);
|
2015-04-28 02:52:41 +02:00
|
|
|
console.log('Got update from ' + spark.id);
|
2015-02-17 03:04:15 +01:00
|
|
|
|
2015-02-17 06:12:44 +01:00
|
|
|
if(typeof data.id !== 'undefined' && typeof data.stats !== 'undefined')
|
|
|
|
{
|
2015-02-23 14:57:41 +01:00
|
|
|
data.stats.latency = spark.latency;
|
2015-02-17 03:04:15 +01:00
|
|
|
|
2015-04-17 11:10:20 +02:00
|
|
|
var stats = Nodes.update(data.id, data.stats);
|
2015-04-28 01:50:28 +02:00
|
|
|
if(stats !== false)
|
|
|
|
{
|
|
|
|
client.write({action: 'update', data: stats});
|
2015-04-17 11:10:20 +02:00
|
|
|
|
2015-04-28 01:50:28 +02:00
|
|
|
var blockPropagationChart = Nodes.blockPropagationChart();
|
|
|
|
client.write({action: 'blockPropagationChart', data: blockPropagationChart});
|
2015-04-24 05:23:26 +02:00
|
|
|
|
2015-04-28 01:50:28 +02:00
|
|
|
var uncleCount = Nodes.getUncleCount();
|
|
|
|
client.write({action: 'uncleCount', data: uncleCount});
|
|
|
|
}
|
2015-02-17 06:12:44 +01:00
|
|
|
}
|
|
|
|
});
|
2015-02-17 11:14:48 +01:00
|
|
|
|
2015-04-03 06:00:06 +02:00
|
|
|
spark.on('node-ping', function(data){
|
|
|
|
spark.emit('node-pong');
|
|
|
|
});
|
|
|
|
|
|
|
|
spark.on('latency', function(data)
|
|
|
|
{
|
|
|
|
if(typeof data.id !== 'undefined')
|
|
|
|
{
|
|
|
|
var stats = Nodes.updateLatency(data.id, data.latency);
|
|
|
|
|
|
|
|
client.write({action: 'latency', data: stats});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-02-18 07:06:41 +01:00
|
|
|
spark.on('end', function(data)
|
|
|
|
{
|
|
|
|
var stats = Nodes.inactive(spark.id);
|
|
|
|
|
|
|
|
client.write({action: 'inactive', data: stats});
|
2015-02-17 11:14:48 +01:00
|
|
|
});
|
2015-02-17 06:12:44 +01:00
|
|
|
});
|
2015-02-17 03:04:15 +01:00
|
|
|
|
|
|
|
client.on('connection', function(spark) {
|
|
|
|
console.log(spark.id);
|
|
|
|
console.log(spark.address);
|
|
|
|
console.log(spark.query);
|
|
|
|
|
2015-02-17 06:12:44 +01:00
|
|
|
spark.on('ready', function(data){
|
|
|
|
spark.emit('init', {nodes: Nodes.all()});
|
2015-04-17 11:10:20 +02:00
|
|
|
|
|
|
|
var blockPropagationChart = Nodes.blockPropagationChart();
|
|
|
|
spark.write({action: 'blockPropagationChart', data: blockPropagationChart});
|
2015-04-24 05:23:26 +02:00
|
|
|
|
|
|
|
var uncleCount = Nodes.getUncleCount();
|
|
|
|
spark.write({action: 'uncleCount', data: uncleCount});
|
2015-02-17 06:12:44 +01:00
|
|
|
});
|
2015-04-06 07:33:01 +02:00
|
|
|
|
|
|
|
spark.on('client-pong', function(data) {
|
|
|
|
var latency = Math.ceil(((new Date()).getTime() - clientLatency)/2);
|
|
|
|
spark.emit('client-latency', { latency: latency });
|
|
|
|
});
|
2015-02-17 06:12:44 +01:00
|
|
|
});
|
2014-12-03 04:08:49 +01:00
|
|
|
|
2015-04-06 07:33:01 +02:00
|
|
|
var latencyTimeout = setInterval(function(){
|
|
|
|
clientLatency = (new Date()).getTime();
|
|
|
|
client.write({action: 'client-ping'});
|
|
|
|
}, 5000);
|
|
|
|
|
2014-12-03 04:08:49 +01:00
|
|
|
// view engine setup
|
|
|
|
app.set('views', path.join(__dirname, 'views'));
|
|
|
|
app.set('view engine', 'jade');
|
2015-03-27 17:52:42 +01:00
|
|
|
app.use(favicon(path.join(__dirname, '/public/images/favicon.png')));
|
2014-12-03 04:08:49 +01:00
|
|
|
app.use(bodyParser.json());
|
|
|
|
app.use(bodyParser.urlencoded({ extended: false }));
|
|
|
|
app.use(express.static(path.join(__dirname, 'public')));
|
|
|
|
|
2015-02-17 06:12:44 +01:00
|
|
|
app.get('/', function(req, res) {
|
|
|
|
res.render('index', { title: 'Ethereum Network Status' });
|
|
|
|
});
|
2014-12-03 04:08:49 +01:00
|
|
|
|
|
|
|
// catch 404 and forward to error handler
|
|
|
|
app.use(function(req, res, next) {
|
|
|
|
var err = new Error('Not Found');
|
|
|
|
err.status = 404;
|
|
|
|
next(err);
|
|
|
|
});
|
|
|
|
|
|
|
|
// error handlers
|
|
|
|
if (app.get('env') === 'development') {
|
|
|
|
app.use(function(err, req, res, next) {
|
|
|
|
res.status(err.status || 500);
|
|
|
|
res.render('error', {
|
|
|
|
message: err.message,
|
|
|
|
error: err
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// production error handler
|
|
|
|
app.use(function(err, req, res, next) {
|
|
|
|
res.status(err.status || 500);
|
|
|
|
res.render('error', {
|
|
|
|
message: err.message,
|
|
|
|
error: {}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-02-17 06:12:44 +01:00
|
|
|
server.listen(process.env.PORT || 3000);
|
2014-12-03 04:08:49 +01:00
|
|
|
|
|
|
|
module.exports = app;
|