-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
532 lines (461 loc) · 16.9 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
import express from 'express';
import fetch from 'node-fetch';
import client from 'prom-client';
const app = express();
const port = 80;
// Available functions - fetchAllProposals, fetchPublicProposals, fetchRegistrationFee, ServicePricing.
async function fetchAllProposals() {
const url = 'https://discovery.mysterium.network/api/v3/proposals?&access_policy=all';
const response = await fetch(url);
const data = await response.json();
return data;
}
async function fetchPublicProposals() {
const url = 'https://discovery.mysterium.network/api/v3/proposals';
const response = await fetch(url);
const pubData = await response.json();
return pubData;
}
async function fetchRegistrationFee() {
const url = 'https://transactor.mysterium.network/api/v1/fee/137/register';
const response = await fetch(url);
const data = await response.json();
const feeInNormalUnits = data.fee / 1e18;
return feeInNormalUnits;
}
async function ServicePricing() {
const url = 'https://discovery.mysterium.network/api/v4/prices';
const response = await fetch(url);
const price = await response.json();
return price;
}
const register = new client.Registry();
const totalNodesGauge = new client.Gauge({
name: 'mysterium_total_nodes',
help: 'Total number of Mysterium Network Nodes',
});
const publicNodesGauge = new client.Gauge({
name: 'mysterium_public_nodes',
help: 'Total number of public Mysterium Network Nodes',
});
const avgQualityGauge = new client.Gauge({
name: 'mysterium_avg_quality',
help: 'Average quality of Mysterium Network Nodes',
});
const avgLatencyGauge = new client.Gauge({
name: 'mysterium_avg_latency',
help: 'Average latency of Mysterium Network Nodes',
});
const totalBandwidthGauge = new client.Gauge({
name: 'mysterium_total_bandwidth',
help: 'Total bandwidth of the Mysterium Network Nodes',
});
const avgBandwidthGauge = new client.Gauge({
name: 'mysterium_avg_bandwidth',
help: 'Average bandwidth of Mysterium Network Nodes',
});
const avgPublicQualityGauge = new client.Gauge({
name: 'mysterium_avg_public_quality',
help: 'Average quality of public Mysterium Network Nodes',
});
const avgPublicLatencyGauge = new client.Gauge({
name: 'mysterium_avg_public_latency',
help: 'Average latency of public Mysterium Nodes',
});
const totalPublicBandwidthGauge = new client.Gauge({
name: 'mysterium_total_public_bandwidth',
help: 'Total bandwidth of the public Mysterium Network Nodes',
});
const avgPublicBandwidthGauge = new client.Gauge({
name: 'mysterium_avg_public_bandwidth',
help: 'Average bandwidth of public Mysterium Network Nodes',
});
const feeGauge = new client.Gauge({
name: 'mysterium_current_fee',
help: 'Mysterium Network registration/settlement fees in MYST - powered by Transactor API.',
});
const residential_wireguard_gib = new client.Gauge({
name: 'mysterium_residential_wireguard_gib',
help: 'Residential Wireguard price per GiB',
});
const residential_scraping_gib = new client.Gauge({
name: 'mysterium_residential_scraping_gib',
help: 'Residential Scraping price per GiB',
});
const residential_data_transfer_gib = new client.Gauge({
name: 'mysterium_residential_data_transfer_gib',
help: 'Residential Data Transfer price per GiB',
});
const residential_dvpn_gib = new client.Gauge({
name: 'mysterium_residential_dvpn_gib',
help: 'Residential DVPN price per GiB',
});
const other_wireguard_gib = new client.Gauge({
name: 'mysterium_other_wireguard_gib',
help: 'Other Wireguard price per GiB',
});
const other_scraping_gib = new client.Gauge({
name: 'mysterium_other_scraping_gib',
help: 'Other Scraping price per GiB',
});
const other_data_transfer_gib = new client.Gauge({
name: 'mysterium_other_data_transfer_gib',
help: 'Other Data Transfer price per GiB',
});
const other_dvpn_gib = new client.Gauge({
name: 'mysterium_other_dvpn_gib',
help: 'Other DVPN price per GiB',
});
register.registerMetric(totalNodesGauge);
register.registerMetric(publicNodesGauge);
register.registerMetric(avgQualityGauge);
register.registerMetric(avgLatencyGauge);
register.registerMetric(totalBandwidthGauge);
register.registerMetric(avgBandwidthGauge);
register.registerMetric(avgPublicQualityGauge);
register.registerMetric(avgPublicLatencyGauge);
register.registerMetric(totalPublicBandwidthGauge);
register.registerMetric(avgPublicBandwidthGauge);
register.registerMetric(feeGauge);
register.registerMetric(residential_wireguard_gib);
register.registerMetric(residential_scraping_gib);
register.registerMetric(residential_data_transfer_gib);
register.registerMetric(residential_dvpn_gib);
register.registerMetric(other_wireguard_gib);
register.registerMetric(other_scraping_gib);
register.registerMetric(other_data_transfer_gib);
const bandwidthPerCountryGauges = {};
const nodesPerCountryGauges = {};
function createDynamicGauges(data, name, help) {
Object.keys(data).forEach(key => {
const bandwidthGaugeName = `${name}_bandwidth_${key}`;
const nodesGaugeName = `${name}_nodes_${key}`;
console.log(`Creating gauges for ${key}: ${bandwidthGaugeName}, ${nodesGaugeName}`);
if (!bandwidthPerCountryGauges[key] || !nodesPerCountryGauges[key]) {
bandwidthPerCountryGauges[key] = new client.Gauge({
name: bandwidthGaugeName,
help: `${help} bandwidth for country ${key}`,
});
nodesPerCountryGauges[key] = new client.Gauge({
name: nodesGaugeName,
help: `${help} nodes for country ${key}`,
});
register.registerMetric(bandwidthPerCountryGauges[key]);
register.registerMetric(nodesPerCountryGauges[key]);
}
bandwidthPerCountryGauges[key].set(data[key]);
nodesPerCountryGauges[key].set(data[key]);
});
}
app.get('/', async (req, res) => {
try {
const [data, pubData, fee, price] = await Promise.all([fetchAllProposals(), fetchPublicProposals(), fetchRegistrationFee(), ServicePricing()]);
const totalNodes = data.length;
const publicNodes = pubData.length;
let totalQuality = 0;
let totalLatency = 0;
let totalBandwidth = 0;
let nodesPerCountry = {};
let bandwidthPerCountry = {};
let pubTotalQuality = 0;
let pubTotalLatency = 0;
let pubTotalBandwidth = 0;
let pubNodesPerCountry = {};
// Pricing data from Discovery API v4: https://discovery.mysterium.network/api/v4/prices
let resi_wireguard_gib_value = 0;
let resi_scraping_gib_value = 0;
let resi_data_transfer_gib_value = 0;
let resi_dvpn_gib_value = 0;
let other_wireguard_gib_value = 0;
let other_scraping_gib_value = 0;
let other_data_transfer_gib_value = 0;
let other_dvpn_gib_value = 0;
data.forEach(item => {
if (item.quality) {
totalQuality += item.quality.quality || 0;
totalLatency += item.quality.latency || 0;
totalBandwidth += item.quality.bandwidth || 0;
const country = item.location.country;
if (bandwidthPerCountry[country]) {
bandwidthPerCountry[country] += item.quality.bandwidth || 0;
} else {
bandwidthPerCountry[country] = item.quality.bandwidth || 0;
}
}
const country = item.location.country;
if (nodesPerCountry[country]) {
nodesPerCountry[country] += 1;
} else {
nodesPerCountry[country] = 1;
}
});
pubData.forEach(item => {
if (item.quality) {
pubTotalQuality += item.quality.quality || 0;
pubTotalLatency += item.quality.latency || 0;
pubTotalBandwidth += item.quality.bandwidth || 0;
}
const country = item.location.country;
if (pubNodesPerCountry[country]) {
pubNodesPerCountry[country] += 1;
} else {
pubNodesPerCountry[country] = 1;
}
});
console.log('Service pricing data:', price);
if (price && price.defaults && price.defaults.current) {
const { residential, other } = price.defaults.current;
if (residential) {
resi_wireguard_gib_value = parseFloat(residential.wireguard?.price_per_gib_human_readable) || 0;
resi_scraping_gib_value = parseFloat(residential.scraping?.price_per_gib_human_readable) || 0;
resi_data_transfer_gib_value = parseFloat(residential.data_transfer?.price_per_gib_human_readable) || 0;
resi_dvpn_gib_value = parseFloat(residential.dvpn?.price_per_gib_human_readable) || 0;
}
if (other) {
other_wireguard_gib_value = parseFloat(other.wireguard?.price_per_gib_human_readable) || 0;
other_scraping_gib_value = parseFloat(other.scraping?.price_per_gib_human_readable) || 0;
other_data_transfer_gib_value = parseFloat(other.data_transfer?.price_per_gib_human_readable) || 0;
other_dvpn_gib_value = parseFloat(other.dvpn?.price_per_gib_human_readable) || 0;
}
}
const avgQuality = totalQuality / totalNodes;
const avgLatency = totalLatency / totalNodes;
const avgBandwidth = totalBandwidth / totalNodes;
const pubAvgQuality = pubTotalQuality / publicNodes;
const pubAvgLatency = pubTotalLatency / publicNodes;
const pubAvgbandwidth = pubTotalBandwidth / publicNodes;
const responseData = {
total_nodes: totalNodes,
avg_quality: avgQuality,
avg_latency: avgLatency,
total_bandwidth: totalBandwidth,
avg_bandwidth: avgBandwidth,
public_nodes: publicNodes,
pub_avg_quality: pubAvgQuality,
pub_avg_latency: pubAvgLatency,
pub_total_bandwidth: pubTotalBandwidth,
pub_avg_bandwidth: pubAvgbandwidth,
current_fee: fee,
residential_wireguard_gib: resi_wireguard_gib_value,
residential_scraping_gib: resi_scraping_gib_value,
residential_data_transfer_gib: resi_data_transfer_gib_value,
residential_dvpn_gib: resi_dvpn_gib_value,
other_wireguard_gib: other_wireguard_gib_value,
other_scraping_gib: other_scraping_gib_value,
other_data_transfer_gib: other_data_transfer_gib_value,
other_dvpn_gib: other_dvpn_gib_value,
bandwidthPerCountry: bandwidthPerCountry,
nodes_per_country: nodesPerCountry,
};
totalNodesGauge.set(totalNodes);
publicNodesGauge.set(publicNodes);
avgQualityGauge.set(avgQuality);
avgLatencyGauge.set(avgLatency);
totalBandwidthGauge.set(totalBandwidth);
avgBandwidthGauge.set(avgBandwidth);
avgPublicQualityGauge.set(pubAvgQuality);
avgPublicLatencyGauge.set(pubAvgLatency);
totalPublicBandwidthGauge.set(pubTotalBandwidth);
avgPublicBandwidthGauge.set(pubAvgbandwidth);
feeGauge.set(fee);
residential_wireguard_gib.set(resi_wireguard_gib_value);
residential_scraping_gib.set(resi_scraping_gib_value);
residential_data_transfer_gib.set(resi_data_transfer_gib_value);
residential_dvpn_gib.set(resi_dvpn_gib_value);
other_wireguard_gib.set(other_wireguard_gib_value);
other_scraping_gib.set(other_scraping_gib_value);
other_data_transfer_gib.set(other_data_transfer_gib_value);
other_dvpn_gib.set(other_dvpn_gib_value);
createDynamicGauges(bandwidthPerCountry, 'mysterium_bandwidth', 'Total bandwidth');
createDynamicGauges(nodesPerCountry, 'mysterium_nodes', 'Total nodes');
res.json(responseData);
} catch (error) {
console.error('Failed to fetch node statistics:', error);
res.status(500).json({ error: 'Failed to fetch node statistics' });
}
});
client.collectDefaultMetrics({ register });
app.get('/metrics', async (req, res) => { //1
res.set('Content-Type', register.contentType);
res.end(await register.metrics());
});
app.get('/fee', async (req, res) => { //2
try {
const fee = await fetchRegistrationFee();
res.json({ fee });
} catch (error) {
console.error('Failed to fetch registration fee:', error);
res.status(500).json({ error: 'Failed to fetch registration fee' });
}
});
app.get('/price', async (req, res) => { //3
try {
const price = await ServicePricing();
res.json(price);
} catch (error) {
console.error('Failed to fetch service pricing:', error);
res.status(500).json({ error: 'Failed to fetch service pricing' });
}
});
app.get('/proposals', async (req, res) => { //4
try {
const data = await fetchAllProposals();
res.json(data);
} catch (error) {
console.error('Failed to fetch provider data:', error);
res.status(500).json({ error: 'Failed to fetch provider data' });
}
});
app.get("/providers", async (req, res) => { //5
try {
const data = await fetchAllProposals();
res.json({ total_providers: data.length });
} catch (error) {
console.error('Failed to fetch provider count:', error);
res.status(500).json({ error: 'Failed to fetch provider count' });
}
});
app.get("/public_providers", async (req, res) => { //6
try {
const data = await fetchPublicProposals();
res.json({ total_providers: data.length });
} catch (error) {
console.error('Failed to fetch public provider count:', error);
res.status(500).json({ error: 'Failed to fetch public provider count' });
}
});
app.get('/total_bandwidth', async (req, res) => { // 7 - in Gbps
try {
const data = await fetchAllProposals();
let total_bandwidth = 0;
data.forEach(item => {
if (item.quality) {
total_bandwidth += item.quality.bandwidth || 0;
}
});
res.json({ total_bandwidth });
} catch (error) {
console.error('Failed to fetch total bandwidth:', error);
res.status(500).json({ error: 'Failed to fetch total bandwidth' });
}
});
app.get('/public_total_bandwidth', async (req, res) => { // 8 - in Gbps
try {
const data = await fetchPublicProposals();
let total_bandwidth = 0;
data.forEach(item => {
if (item.quality) {
total_bandwidth += item.quality.bandwidth || 0;
}
});
res.json({ total_bandwidth });
} catch (error) {
console.error('Failed to fetch public total bandwidth:', error);
res.status(500).json({ error: 'Failed to fetch public total bandwidth' });
}
});
app.get('/avg_quality', async (req, res) => { // 9
try {
const data = await fetchAllProposals();
let total_quality = 0;
data.forEach(item => {
if (item.quality) {
total_quality += item.quality.quality || 0;
}
});
const avg_quality = total_quality / data.length;
res.json({ avg_quality });
} catch (error) {
console.error('Failed to fetch average quality:', error);
res.status(500).json({ error: 'Failed to fetch average quality' });
}
});
app.get('/public_avg_quality', async (req, res) => { // 10
try {
const data = await fetchPublicProposals();
let total_quality = 0;
data.forEach(item => {
if (item.quality) {
total_quality += item.quality.quality || 0;
}
});
const avg_quality = total_quality / data.length;
res.json({ avg_quality });
} catch (error) {
console.error('Failed to fetch public average quality:', error);
res.status(500).json({ error: 'Failed to fetch public average quality' });
}
});
app.get('/avg_latency', async (req, res) => { // 11
try {
const data = await fetchAllProposals();
let total_latency = 0;
data.forEach(item => {
if (item.quality) {
total_latency += item.quality.latency || 0;
}
});
const avg_latency = total_latency / data.length;
res.json({ avg_latency });
} catch (error) {
console.error('Failed to fetch average latency:', error);
res.status(500).json({ error: 'Failed to fetch average latency' });
}
});
app.get('/public_avg_latency', async (req, res) => { // 12
try {
const data = await fetchPublicProposals();
let total_latency = 0;
data.forEach(item => {
if (item.quality) {
total_latency += item.quality.latency || 0;
}
});
const avg_latency = total_latency / data.length;
res.json({ avg_latency });
} catch (error) {
console.error('Failed to fetch public average latency:', error);
res.status(500).json({ error: 'Failed to fetch public average latency' });
}
});
app.get('/public_providers', async (req, res) => { // 13
try {
const data = await fetchPublicProposals();
res.json({ public_providers: data.length });
} catch (error) {
console.error('Failed to fetch public providers:', error);
res.status(500).json({ error: 'Failed to fetch public providers' });
}
});
//The code has 14 API Endpoints in total
app.get ('/help', async (req, res) => { //14
res.json({
endpoints: [
'/metrics', //1
'/fee', //2
'/price', //3
'/proposals', //4
'/providers', //5
'/public_providers', //6
'/total_bandwidth', //7
'/public_total_bandwidth', //8
'/avg_quality', //9
'/public_avg_quality', //10
'/avg_latency', //11
'/public_avg_latency', //12
'/public_providers', //13
]
});
}
);
setInterval(async () => {
try {
await fetch('http://localhost');
} catch (error) {
console.error('Failed to fetch node statistics:', error);
}
}
, 60000); //60000 - 1 min
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});