forked from LiskArchive/lisk-explorer
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
269 lines (229 loc) · 7.42 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
* LiskHQ/lisk-explorer
* Copyright © 2018 Lisk Foundation
*
* See the LICENSE file at the top-level directory of this distribution
* for licensing information.
*
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation,
* no part of this software, including this file, may be copied, modified,
* propagated, or distributed except according to the terms contained in the
* LICENSE file.
*
* Removal or modification of this copyright notice is prohibited.
*
*/
const express = require('express');
let config = require('./config');
const routes = require('./api');
const path = require('path');
const cache = require('./cache');
const program = require('commander');
const async = require('async');
const packageJson = require('./package.json');
const split = require('split');
const logger = require('./utils/logger');
const request = require('request');
const app = express();
const api = require('./lib/api');
const utils = require('./utils');
program
.version(packageJson.version)
.option('-c, --config <path>', 'config file path')
.option('-p, --port <port>', 'listening port number')
.option('-h, --host <ip>', 'listening host name or ip')
.option('-rp, --redisPort <port>', 'redis port')
.parse(process.argv);
if (program.config) {
// eslint-disable-next-line import/no-dynamic-require
config = require(path.resolve(process.cwd(), program.config));
}
app.set('host', program.host || config.host);
app.set('port', program.port || config.port);
if (program.redisPort) {
config.redis.port = program.redisPort;
}
const client = require('./redis')(config);
app.locals.redis = client;
app.use((req, res, next) => {
req.redis = client;
return next();
});
app.candles = new utils.candles(config, client);
app.exchange = new utils.exchange(config);
app.delegates = new api.delegates(app);
app.knownAddresses = new utils.knownAddresses(app, config, client);
app.orders = new utils.orders(config, client);
app.set('version', packageJson.version);
app.set('strict routing', true);
app.set('lisk address', config.lisk.http);
app.set('lisk websocket address', config.lisk.ws);
app.set('freegeoip address', config.freegeoip);
app.set('exchange enabled', config.exchangeRates.enabled);
app.set('uiMessage', config.uiMessage);
app.use((req, res, next) => {
res.setHeader('X-Frame-Options', 'DENY');
res.setHeader('X-Content-Type-Options', 'nosniff');
res.setHeader('X-XSS-Protection', '1; mode=block');
/* eslint-disable */
const connectSrc = `ws://${req.get('host')} wss://${req.get('host')}`;
const contentSecurityPolicy = [
`default-src 'self';`,
`frame-ancestors 'none';`,
`connect-src 'self' ${connectSrc} https://www.google-analytics.com https://*.crazyegg.com;`,
`img-src 'self' https:;`,
`style-src 'self' 'unsafe-inline' https://fonts.googleapis.com;`,
`script-src 'self' 'unsafe-eval' 'unsafe-inline' https://tagmanager.google.com/ https://www.googletagmanager.com/ https://www.google-analytics.com/ https://dnn506yrbagrg.cloudfront.net/ https://*.ipify.org/ https://*.crazyegg.com/ http://trk.cetrk.com/ https://s3.amazonaws.com/trk.cetrk.com/;`,
`font-src 'self' https://fonts.gstatic.com data:`,
].join(' ');
/* eslint-enable */
res.setHeader('Content-Security-Policy', contentSecurityPolicy);
return next();
});
app.use(express.static(path.join(__dirname, 'public')));
const morgan = require('morgan');
app.use(morgan('combined', {
skip(req, res) {
return parseInt(res.statusCode, 10) < 400;
},
stream: split().on('data', (data) => {
logger.error(data);
}),
}));
app.use(morgan('combined', {
skip(req, res) {
return parseInt(res.statusCode, 10) >= 400;
},
stream: split().on('data', (data) => {
logger.debug(data);
}),
}));
const compression = require('compression');
app.use(compression());
const methodOverride = require('method-override');
app.use(methodOverride('X-HTTP-Method-Override'));
const bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true,
}));
const allowCrossDomain = function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
};
app.use(allowCrossDomain);
app.use((req, res, next) => {
logger.debug(req.originalUrl);
try {
decodeURIComponent(req.path);
} catch (err) {
logger.debug(err);
return res.redirect('/');
}
if (req.originalUrl === undefined || req.originalUrl.split('/')[1] !== 'api') {
return next();
}
if (cache.cacheIgnoreList.indexOf(req.originalUrl) >= 0) {
return next();
}
return req.redis.get(req.originalUrl, (err, json) => {
if (err) {
logger.warn(err);
return next();
} else if (json) {
try {
json = JSON.parse(json);
} catch (e) {
return next();
}
return res.json(json);
}
return next();
});
});
logger.info('Loading routes...');
routes(app);
logger.info('Routes loaded');
app.use((req, res, next) => {
logger.debug(req.originalUrl);
if (req.originalUrl === undefined || req.originalUrl.split('/')[1] !== 'api' || !req.json) {
return next();
}
if (cache.cacheIgnoreList.indexOf(req.originalUrl) >= 0) {
return res.json(req.json);
}
const ttl = cache.cacheTTLOverride[req.originalUrl] || config.cacheTTL;
req.redis.setex(req.originalUrl, ttl, JSON.stringify(req.json), (err) => {
if (err) {
logger.warn(err);
}
});
return res.json(req.json);
});
app.get('*', (req, res, next) => {
if (req.url.indexOf('api') !== 1) {
return res.sendFile(path.join(__dirname, 'public', 'index.html'));
}
return next();
});
const getNodeConstants = () => new Promise((success, error) => {
request.get({
url: `${app.get('lisk address')}/node/constants`,
json: true,
}, (err, response, body) => {
if (err) {
return error({ success: false, error: err.message });
} else if (response.statusCode === 200) {
if (body && body.data) {
const results = {
version: body.data.version,
epoch: new Date(body.data.epoch) / 1000,
protocolVersion: body.data.protocolVersion,
nethash: body.data.nethash || body.data.networkId,
majorVersion: body.data.version.split('.')[0],
minorVersion: body.data.version.split('.')[1],
};
app.set('nodeConstants', { ...body.data, ...results });
return success(results);
}
}
return error({ success: false, error: body.error });
});
});
const SERVER_FAILURE_TIMEOUT = 5000; // ms
const status = { NOT_RUNNING: 0, OK: 1 };
let serverStatus = status.NOT_RUNNING;
const startServer = (cb) => {
getNodeConstants().then((result) => {
logger.info(`Connected to the node ${app.get('lisk address')}, Lisk Core version ${result.version}`);
Object.keys(result).forEach((item) => {
app.set(`node.${item}`, result[item]);
});
const server = app.listen(app.get('port'), app.get('host'), (err) => {
if (err) {
logger.info(err);
} else {
logger.info(`Lisk Explorer started at ${app.get('host')}:${app.get('port')}`);
const io = require('socket.io').listen(server);
require('./sockets')(app, io);
app.delegates.loadAllDelegates();
app.knownAddresses.load();
serverStatus = status.OK;
}
});
}).catch(() => {
logger.error(`The following node ${app.get('lisk address')} has an incompatible API or is not available.`);
setTimeout(cb, SERVER_FAILURE_TIMEOUT);
});
};
const loadRates = (cb) => {
app.exchange.loadRates();
cb(null);
};
async.parallel([
loadRates,
], () => {
async.until(() => (serverStatus === status.OK), startServer);
});