-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
303 lines (255 loc) · 9.47 KB
/
index.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
const rateLimit = require("express-rate-limit");
const bodyParser = require("body-parser");
const NodeCache = require("node-cache");
const database = require("./database.js");
const express = require("express");
const winston = require('winston');
const config = require("./config.json");
const moment = require('moment');
const utils = require("./utils.js");
const cors = require("cors");
const path = require("path");
const CCX = require("conceal-api");
const fs = require("fs");
// query api timeout
const apiTimeout = 3000;
// message base for winston logging
const MESSAGE = Symbol.for('message');
const logFormatter = (logEntry) => {
const base = { timestamp: new Date() };
const json = Object.assign(base, logEntry);
logEntry[MESSAGE] = JSON.stringify(json);
return logEntry;
};
const logger = winston.createLogger({
exitOnError: false, // do not exit on handled exceptions
transports: [
new winston.transports.Console(),
new winston.transports.File({
filename: path.join(utils.ensureUserDataDir(), 'info.log'),
maxsize: 10000000,
maxFiles: 5
}),
new winston.transports.File({
filename: path.join(utils.ensureUserDataDir(), 'errors.log'),
maxsize: 10000000,
maxFiles: 5,
level: 'error'
})
],
exceptionHandlers: [
new winston.transports.File({
filename: path.join(utils.ensureUserDataDir(), 'exceptions.log'),
maxsize: 10000000,
maxFiles: 5
})
],
format: winston.format(logFormatter)()
});
// update node data limiter
const updateNodeLimier = rateLimit({
windowMs: 5 * 60 * 1000, // 5 minutes
max: 30, // limit each IP to 15 requests per windowMs
message: "Too many requests created from this IP, please try again later",
handler : function (req, res, next, options) {
logger.error(`Denied update node request because of to many requests in short period from IP ${req.ip}`);
res.status(options.statusCode).send(options.message);
}
});
// update node data limiter
const listNodesLimiter = rateLimit({
windowMs: 5 * 60 * 1000, // 5 minutes
max: 300, // limit each IP to 15 requests per windowMs
message: "Too many requests created from this IP, please try again later",
handler : function (req, res, next, options) {
logger.error(`Denied list nodes request because of to many requests in short period from IP ${req.ip}`);
res.status(options.statusCode).send(options.message);
}
});
var nodeCache = new NodeCache({ stdTTL: config.cache.expire, checkperiod: config.cache.checkPeriod }); // the cache object
var storage = new database(); // create a new storage instance
var app = express(); // create express app
// cache for last uptime check
var updateCache = {};
// attach other libraries to the express application
app.enable("trust proxy", '127.0.0.1');
app.use(bodyParser.json());
app.use(cors());
// handle any application errors
app.use(function (err, req, res, next) {
if (err) {
logger.error('Error trying to execute request!', err);
res.status(500).json({
success: false,
error: err.message
});
}
});
// start listener
app.listen(config.server.port, () => {
console.log(`Server running on port ${config.server.port}`);
});
function getAllNodes(keys) {
let nodeList = [];
keys.forEach(function (value) {
let nodeData = nodeCache.get(value);
if (nodeData) {
nodeList.push(nodeCache.get(value));
}
});
return nodeList;
}
function filterResults(req, values) {
var correctHeightList = {};
var correctHeightCnt = 0;
var filteredValues = [];
var correctHeight = 0;
var isSyncedOnly = true
if (req.query.isSynced) {
isSyncedOnly = req.query.isSynced.toUpperCase() == "TRUE";
}
filteredValues = values.filter((value, index, array) => {
var isAppropriate = true;
if (req.query.hasFeeAddr) {
var hasFeeAddress = value.blockchain && value.blockchain.fee_address;
isAppropriate = isAppropriate && (((req.query.hasFeeAddr === "true") && hasFeeAddress) || ((req.query.hasFeeAddr === "false") && !hasFeeAddress));
}
if (req.query.isReachable) {
var isReachable = value.status && value.status.isReachable;
isAppropriate = isAppropriate && (((req.query.isReachable === "true") && isReachable) || ((req.query.isReachable === "false") && !isReachable));
}
if (req.query.hasSSL) {
var hasSSL = value.status && value.status.hasSSL;
isAppropriate = isAppropriate && (((req.query.hasSSL === "true") && hasSSL) || ((req.query.hasSSL === "false") && !hasSSL));
}
var nodeHeight = value.blockchain ? value.blockchain.height : 0;
correctHeightList[nodeHeight] = (correctHeightList[nodeHeight] || 0) + 1;
return isAppropriate;
});
// find the correct height
for (var propertyName in correctHeightList) {
if (correctHeightList[propertyName] > correctHeightCnt) {
correctHeightCnt = correctHeightList[propertyName];
correctHeight = propertyName;
}
}
if (isSyncedOnly) {
filteredValues = filteredValues.filter((value, index, array) => {
var nodeHeight = value.blockchain ? value.blockchain.height : 0;
return nodeHeight >= correctHeight - 2;
});
}
return filteredValues;
}
function setNodeData(data, callback) {
storage.getClientUptime({ id: [data.id], year: [moment().year()], month: [moment().month() + 1] }, function (resultData) {
data.status.lastSeen = moment().toISOString();
let nodeData = nodeCache.get(data.id);
let doCheckReachable = false;
if (resultData && resultData.uptimes && (resultData.uptimes.length == 1)) {
data.status.uptime = Math.round((resultData.uptimes[0].clientTicks / resultData.uptimes[0].serverTicks) * 100);
}
// do we need to check it
if (!updateCache[data.id] || !nodeData) {
doCheckReachable = true;
} else {
doCheckReachable = moment.duration(moment(new Date()).diff(moment(updateCache[data.id]))).asMinutes() > 15;
}
if (doCheckReachable) {
updateCache[data.id] = moment().toISOString();
let CCXApiSSL = new CCX({
daemonHost: `https://${data.url ? data.url.host : data.nodeHost}`,
daemonRpcPort: data.url ? data.url.port : data.nodePort,
timeout: apiTimeout
});
// check SSL connection first
CCXApiSSL.info().then(info => {
data.status.hasSSL = true;
data.status.isReachable = true;
callback(nodeCache.set(data.id, data, config.cache.expire));
}).catch(err => {
let CCXApi = new CCX({
daemonHost: `http://${data.url ? data.url.host : data.nodeHost}`,
daemonRpcPort: data.url ? data.url.port : data.nodePort,
timeout: apiTimeout
});
// check unsecure connection
CCXApi.info().then(info => {
data.status.hasSSL = false;
data.status.isReachable = true;
callback(nodeCache.set(data.id, data, config.cache.expire));
}).catch(err => {
data.status.hasSSL = false;
data.status.isReachable = false;
callback(nodeCache.set(data.id, data, config.cache.expire));
});
});
} else {
data.status.hasSSL = nodeData.status.hasSSL;
data.status.isReachable = nodeData.status.isReachable;
callback(nodeCache.set(data.id, data, config.cache.expire));
}
});
}
// update uptime for nodes
function checkNodesUptimeStatus() {
let keys = nodeCache.keys();
for (let key of keys) {
var nodeData = nodeCache.get(key);
if (nodeData) {
var lastSeen = moment(nodeData.status.lastSeen);
if (moment.duration(moment(new Date()).diff(lastSeen)).asMinutes() < config.uptime.period) {
storage.increaseClientTick(key);
}
}
}
// increase the server tick count
storage.increaseServerTick();
}
// get request for the list of all active nodes
app.get("/pool/list", listNodesLimiter, (req, res) => {
res.json({ success: true, list: filterResults(req, getAllNodes(nodeCache.keys())) });
});
// count all active nodes by specified filters
app.get("/pool/count", listNodesLimiter, (req, res) => {
res.json({ success: true, count: filterResults(req, getAllNodes(nodeCache.keys())).length });
});
// get the random node back to user
app.get("/pool/random", listNodesLimiter, (req, res, next) => {
var nodeList = filterResults(req, getAllNodes(nodeCache.keys()));
var randomNode = nodeList[Math.floor(Math.random() * nodeList.length)];
if (randomNode) {
let host = (randomNode.url && randomNode.url.host) ? randomNode.url.host : randomNode.nodeHost;
let port = (randomNode.url && randomNode.url.port) ? randomNode.url.port : randomNode.nodePort || 16000;
res.json({
success: true,
url: `${host}:${port}`
});
} else {
res.json({ success: false });
}
});
// post request for updating the node data
app.post("/pool/update", updateNodeLimier, (req, res, next) => {
if ((req.body) && (req.body.id) && (req.body.nodeHost) && (req.body.nodePort)) {
setNodeData(req.body, function (result) {
res.json({ success: result });
});
} else {
res.json({ success: false });
}
});
// post request for updating the node data
app.all("/pool/uptime", listNodesLimiter, (req, res, next) => {
if (req.body) {
storage.getClientUptime(req.body, function (resultData) {
res.json(resultData);
});
}
});
// get request for the list of all active nodes
app.get("/pool/stats", listNodesLimiter, (req, res) => {
res.json(nodeCache.getStats());
});
// set the interval for the uptime check of all nodes
setInterval(checkNodesUptimeStatus, config.uptime.period * 1000);