ethstats-server/app.js

414 lines
7.6 KiB
JavaScript
Raw Normal View History

2015-04-29 07:49:43 +02:00
var _ = require('lodash');
2015-06-03 04:06:09 +02:00
var logger = require('./lib/utils/logger');
var chalk = require('chalk');
2015-06-13 05:42:06 +02:00
var http = require('http');
2015-06-10 04:44:32 +02:00
2015-08-19 20:00:47 +02:00
// Init WS SECRET
2015-08-17 15:39:36 +02:00
var WS_SECRET;
if( !_.isUndefined(process.env.WS_SECRET) && !_.isNull(process.env.WS_SECRET) )
{
2015-08-17 16:07:26 +02:00
if( process.env.WS_SECRET.indexOf('|') > 0 )
{
WS_SECRET = process.env.WS_SECRET.split('|');
}
else
{
2015-08-24 09:05:50 +02:00
WS_SECRET = [process.env.WS_SECRET];
2015-08-17 16:07:26 +02:00
}
2015-08-19 20:00:47 +02:00
}
else
{
try {
2015-08-19 20:20:02 +02:00
var tmp_secret_json = require('./ws_secret.json');
WS_SECRET = _.values(tmp_secret_json);
2015-08-19 20:00:47 +02:00
}
catch (e)
{
2015-08-19 20:20:02 +02:00
console.error("WS_SECRET NOT SET!!!");
2015-08-19 20:00:47 +02:00
}
2015-08-17 15:39:36 +02:00
}
2015-07-30 23:13:11 +02:00
var banned = require('./lib/utils/config').banned;
2015-05-12 17:02:51 +02:00
2015-06-10 04:32:25 +02:00
// Init http server
2015-05-12 17:02:51 +02:00
if( process.env.NODE_ENV !== 'production' )
{
2015-06-10 04:04:17 +02:00
var app = require('./lib/express');
2015-06-13 05:42:06 +02:00
server = http.createServer(app);
2015-05-12 17:02:51 +02:00
}
else
2015-06-13 05:42:06 +02:00
server = http.createServer();
2015-02-17 06:12:44 +01:00
2015-06-10 04:32:25 +02:00
// Init socket vars
var Primus = require('primus');
var api;
var client;
var server;
// Init API Socket connection
2015-02-17 03:04:15 +01:00
api = new Primus(server, {
2015-04-29 07:49:43 +02:00
transformer: 'websockets',
pathname: '/api',
parser: 'JSON'
2015-02-17 03:04:15 +01:00
});
2016-10-18 18:45:35 +02:00
api.plugin('emit', require('primus-emit'));
api.plugin('spark-latency', require('primus-spark-latency'));
2015-02-17 03:04:15 +01:00
2015-06-10 04:32:25 +02:00
// Init Client Socket connection
client = new Primus(server, {
2015-04-29 07:49:43 +02:00
transformer: 'websockets',
pathname: '/primus',
parser: 'JSON'
2015-02-17 06:12:44 +01:00
});
2016-10-18 18:45:35 +02:00
client.plugin('emit', require('primus-emit'));
2015-06-10 04:32:25 +02:00
2015-06-23 22:33:33 +02:00
// Init external API
external = new Primus(server, {
transformer: 'websockets',
pathname: '/external',
parser: 'JSON'
});
2016-10-18 18:45:35 +02:00
external.plugin('emit', require('primus-emit'));
2015-06-23 22:33:33 +02:00
2015-06-10 04:32:25 +02:00
// Init collections
var Collection = require('./lib/collection');
2015-06-23 22:33:33 +02:00
var Nodes = new Collection(external);
2015-04-06 07:33:01 +02:00
2015-06-09 01:40:59 +02:00
Nodes.setChartsCallback(function (err, charts)
{
if(err !== null)
{
console.error('COL', 'CHR', 'Charts error:', err);
}
else
{
client.write({
action: 'charts',
data: charts
});
}
});
2015-02-17 06:12:44 +01:00
2015-06-10 04:32:25 +02:00
// Init API Socket events
2015-06-02 15:55:23 +02:00
api.on('connection', function (spark)
{
2015-06-03 04:06:09 +02:00
console.info('API', 'CON', 'Open:', spark.address.ip);
2015-04-29 07:49:43 +02:00
2015-06-02 15:55:23 +02:00
spark.on('hello', function (data)
2015-04-29 07:49:43 +02:00
{
2015-06-03 04:06:09 +02:00
console.info('API', 'CON', 'Hello', data['id']);
2015-04-29 07:49:43 +02:00
2015-08-17 15:39:36 +02:00
if( _.isUndefined(data.secret) || WS_SECRET.indexOf(data.secret) === -1 || banned.indexOf(spark.address.ip) >= 0 )
2015-04-29 07:49:43 +02:00
{
spark.end(undefined, { reconnect: false });
2015-06-03 04:06:09 +02:00
console.error('API', 'CON', 'Closed - wrong auth', data);
2015-04-29 07:49:43 +02:00
return false;
}
if( !_.isUndefined(data.id) && !_.isUndefined(data.info) )
{
data.ip = spark.address.ip;
data.spark = spark.id;
2015-06-03 02:51:19 +02:00
data.latency = spark.latency || 0;
2015-04-29 07:49:43 +02:00
2015-06-09 01:40:59 +02:00
Nodes.add( data, function (err, info)
{
if(err !== null)
{
console.error('API', 'CON', 'Connection error:', err);
return false;
}
if(info !== null)
{
spark.emit('ready');
console.success('API', 'CON', 'Connected', data.id);
client.write({
action: 'add',
data: info
});
}
2015-04-29 07:49:43 +02:00
});
}
});
2015-06-03 02:51:19 +02:00
2015-06-02 15:55:23 +02:00
spark.on('update', function (data)
2015-04-29 07:49:43 +02:00
{
if( !_.isUndefined(data.id) && !_.isUndefined(data.stats) )
{
2015-06-09 01:40:59 +02:00
Nodes.update(data.id, data.stats, function (err, stats)
2015-04-29 07:49:43 +02:00
{
2015-06-09 01:40:59 +02:00
if(err !== null)
{
console.error('API', 'UPD', 'Update error:', err);
}
else
{
if(stats !== null)
{
client.write({
action: 'update',
data: stats
});
console.info('API', 'UPD', 'Update from:', data.id, 'for:', stats);
Nodes.getCharts();
}
}
});
2015-06-02 17:00:32 +02:00
}
else
{
2015-06-03 04:06:09 +02:00
console.error('API', 'UPD', 'Update error:', data);
2015-04-29 07:49:43 +02:00
}
});
2015-06-03 02:51:19 +02:00
2015-06-02 15:55:23 +02:00
spark.on('block', function (data)
2015-06-01 21:51:31 +02:00
{
if( !_.isUndefined(data.id) && !_.isUndefined(data.block) )
{
2015-06-09 01:40:59 +02:00
Nodes.addBlock(data.id, data.block, function (err, stats)
2015-06-01 21:51:31 +02:00
{
2015-06-09 01:40:59 +02:00
if(err !== null)
{
console.error('API', 'BLK', 'Block error:', err);
}
else
{
if(stats !== null)
{
client.write({
action: 'block',
data: stats
});
console.success('API', 'BLK', 'Block:', data.block['number'], 'from:', data.id);
Nodes.getCharts();
}
}
});
2015-06-01 21:51:31 +02:00
}
2015-06-02 17:00:32 +02:00
else
{
2015-06-03 04:06:09 +02:00
console.error('API', 'BLK', 'Block error:', data);
2015-06-02 17:00:32 +02:00
}
2015-06-01 21:51:31 +02:00
});
2015-06-03 02:51:19 +02:00
2015-06-02 15:55:23 +02:00
spark.on('pending', function (data)
2015-06-01 20:42:50 +02:00
{
if( !_.isUndefined(data.id) && !_.isUndefined(data.stats) )
{
2015-06-09 01:40:59 +02:00
Nodes.updatePending(data.id, data.stats, function (err, stats) {
if(err !== null)
{
console.error('API', 'TXS', 'Pending error:', err);
}
if(stats !== null)
{
client.write({
action: 'pending',
data: stats
});
console.success('API', 'TXS', 'Pending:', data.stats['pending'], 'from:', data.id);
}
});
2015-06-02 17:00:32 +02:00
}
else
{
2015-06-03 04:06:09 +02:00
console.error('API', 'TXS', 'Pending error:', data);
2015-06-01 20:42:50 +02:00
}
});
2015-06-03 02:51:19 +02:00
2015-06-02 15:55:23 +02:00
spark.on('stats', function (data)
2015-06-01 23:04:34 +02:00
{
if( !_.isUndefined(data.id) && !_.isUndefined(data.stats) )
{
2015-06-02 17:00:32 +02:00
2015-06-09 01:40:59 +02:00
Nodes.updateStats(data.id, data.stats, function (err, stats)
2015-06-01 23:04:34 +02:00
{
2015-06-09 01:40:59 +02:00
if(err !== null)
{
console.error('API', 'STA', 'Stats error:', err);
}
else
{
if(stats !== null)
{
client.write({
action: 'stats',
data: stats
});
console.success('API', 'STA', 'Stats from:', data.id);
}
}
});
2015-06-02 17:00:32 +02:00
}
else
{
2015-06-03 04:06:09 +02:00
console.error('API', 'STA', 'Stats error:', data);
2015-06-01 23:04:34 +02:00
}
});
2015-06-03 02:51:19 +02:00
2015-06-02 15:55:23 +02:00
spark.on('history', function (data)
2015-04-29 07:49:43 +02:00
{
2015-06-09 01:40:59 +02:00
console.success('API', 'HIS', 'Got history from:', data.id);
2015-04-29 07:49:43 +02:00
2015-06-09 01:40:59 +02:00
var time = chalk.reset.cyan((new Date()).toJSON()) + " ";
console.time(time, 'COL', 'CHR', 'Got charts in');
2015-06-02 17:00:32 +02:00
2015-06-09 01:40:59 +02:00
Nodes.addHistory(data.id, data.history, function (err, history)
{
console.timeEnd(time, 'COL', 'CHR', 'Got charts in');
2015-06-02 17:00:32 +02:00
2015-06-09 01:40:59 +02:00
if(err !== null)
{
console.error('COL', 'CHR', 'History error:', err);
}
else
{
client.write({
action: 'charts',
data: history
});
}
});
2015-04-29 07:49:43 +02:00
});
2015-06-03 02:51:19 +02:00
2015-06-02 04:06:53 +02:00
spark.on('node-ping', function (data)
2015-04-29 07:49:43 +02:00
{
2015-06-02 03:53:26 +02:00
var start = (!_.isUndefined(data) && !_.isUndefined(data.clientTime) ? data.clientTime : null);
2015-06-02 02:06:11 +02:00
spark.emit('node-pong', {
2015-06-02 03:53:26 +02:00
clientTime: start,
2015-06-02 02:06:11 +02:00
serverTime: _.now()
});
2015-06-02 17:00:32 +02:00
2015-06-03 04:06:09 +02:00
console.info('API', 'PIN', 'Ping from:', data['id']);
2015-04-29 07:49:43 +02:00
});
2015-06-03 02:51:19 +02:00
2015-06-02 04:06:53 +02:00
spark.on('latency', function (data)
2015-04-29 07:49:43 +02:00
{
if( !_.isUndefined(data.id) )
{
2015-06-09 03:03:31 +02:00
Nodes.updateLatency(data.id, data.latency, function (err, latency)
{
2015-06-09 01:40:59 +02:00
if(err !== null)
{
console.error('API', 'PIN', 'Latency error:', err);
}
if(latency !== null)
{
2015-06-13 11:54:34 +02:00
// client.write({
// action: 'latency',
// data: latency
// });
2015-06-09 01:40:59 +02:00
console.info('API', 'PIN', 'Latency:', latency, 'from:', data.id);
}
});
2015-06-02 17:00:32 +02:00
2015-06-13 11:54:34 +02:00
if( Nodes.requiresUpdate(data.id) )
2015-04-29 07:49:43 +02:00
{
var range = Nodes.getHistory().getHistoryRequestRange();
spark.emit('history', range);
2015-06-03 04:06:09 +02:00
console.info('API', 'HIS', 'Asked:', data.id, 'for history:', range.min, '-', range.max);
2015-04-29 07:49:43 +02:00
2015-06-09 01:40:59 +02:00
Nodes.askedForHistory(true);
2015-04-29 07:49:43 +02:00
}
}
});
2015-06-03 02:51:19 +02:00
2015-06-02 15:55:23 +02:00
spark.on('end', function (data)
2015-04-29 07:49:43 +02:00
{
2015-06-09 01:40:59 +02:00
Nodes.inactive(spark.id, function (err, stats)
{
if(err !== null)
{
console.error('API', 'CON', 'Connection end error:', err);
}
else
{
client.write({
action: 'inactive',
data: stats
});
2015-04-29 07:49:43 +02:00
2015-06-09 01:40:59 +02:00
console.warn('API', 'CON', 'Connection with:', spark.id, 'ended:', data);
}
2015-04-29 07:49:43 +02:00
});
});
2015-02-17 06:12:44 +01:00
});
2015-02-17 03:04:15 +01:00
2015-06-03 02:51:19 +02:00
2015-06-02 03:33:17 +02:00
client.on('connection', function (clientSpark)
2015-04-29 07:49:43 +02:00
{
2015-06-02 03:33:17 +02:00
clientSpark.on('ready', function (data)
2015-04-29 07:49:43 +02:00
{
clientSpark.emit('init', { nodes: Nodes.all() });
2015-04-17 11:10:20 +02:00
2015-06-09 01:40:59 +02:00
Nodes.getCharts();
2015-04-29 07:49:43 +02:00
});
2015-04-06 07:33:01 +02:00
2015-06-02 03:33:17 +02:00
clientSpark.on('client-pong', function (data)
2015-04-29 07:49:43 +02:00
{
2015-06-17 03:10:27 +02:00
var serverTime = _.get(data, "serverTime", 0);
2015-06-17 03:13:55 +02:00
var latency = Math.ceil( (_.now() - serverTime) / 2 );
2015-04-29 07:49:43 +02:00
clientSpark.emit('client-latency', { latency: latency });
});
2015-02-17 06:12:44 +01:00
});
2014-12-03 04:08:49 +01:00
2015-04-29 07:49:43 +02:00
var latencyTimeout = setInterval( function ()
{
2015-06-02 03:33:17 +02:00
client.write({
action: 'client-ping',
data: {
2015-06-10 04:32:25 +02:00
serverTime: _.now()
2015-06-02 03:33:17 +02:00
}
});
2015-04-06 07:33:01 +02:00
}, 5000);
// Cleanup old inactive nodes
var nodeCleanupTimeout = setInterval( function ()
{
client.write({
action: 'init',
data: Nodes.all()
});
2015-06-09 01:40:59 +02:00
Nodes.getCharts();
2015-05-11 21:10:19 +02:00
}, 1000*60*60);
2015-02-17 06:12:44 +01:00
server.listen(process.env.PORT || 3000);
2014-12-03 04:08:49 +01:00
2015-05-12 17:02:51 +02:00
module.exports = server;