forked from CryptoLover705/SmartCryptoTech-services
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodes.js
97 lines (80 loc) · 2.67 KB
/
nodes.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
import schedule from "node-schedule";
import NodeCache from "node-cache";
import config from "./config.js";
import moment from "moment";
import geoip from "geoip-lite";
import axios from "axios";
export class nodes {
constructor() {
this.geoJSONArray = [];
this.addressList = [
"https://explorer.conceal.network/daemon/getpeers"
];
this.request = axios.create({
timeout: 60000, // 2 seconds
headers:{
'User-Agent': 'Conceal Services'
}
});
this.nodeCache = new NodeCache({ stdTTL: config.nodes.cache.expire, checkperiod: config.nodes.cache.checkPeriod }); // the cache object
this.dataSchedule = schedule.scheduleJob('* */6 * * *', () => {
this.updateGeoData();
});
this.updateGeoData();
}
updateGeoData = () => {
this.geoJSONArray = [];
var counter = 0;
const checkForProcessingEnd = () => {
counter++;
// check if we have processed all nodes
if (counter >= this.addressList.length) {
let nodeKeys = this.nodeCache.keys();
for (let i = 0; i < nodeKeys.length; i++) {
this.geoJSONArray.push(this.nodeCache.get(nodeKeys[i]));
}
}
}
this.request.get("https://explorer.conceal.network/pool/list?isReachable=true").then(response => {
let data = response.data;
if (data.success) {
data.list.forEach((value) => {
var address = `http://${value.nodeHost}:${value.nodePort}/getpeers`;
if (this.addressList.indexOf(address) == -1) {
this.addressList.push(address);
}
});
// now loop all the addressed from the list
this.addressList.forEach((value) => {
this.request.get(value).then(response => {
let data = response.data;
try {
data.peers.forEach((value) => {
var ipAddress = value.substr(0, value.indexOf(':'));
var nodeData = {
ipAddress: ipAddress,
lastSeen: moment(),
geoData: geoip.lookup(ipAddress)
};
// set the node data under the IP key and set its expiration time
this.nodeCache.set(ipAddress, nodeData, config.nodes.cache.expire);
});
} catch(err) {
console.log('Error:', err.message);
} finally {
checkForProcessingEnd();
}
}).catch(err => {
console.log('Error:', err.message);
checkForProcessingEnd();
});
});
}
}).catch(err => {
console.log('Error:', err.message);
});
}
getGeoData = (options) => {
return this.geoJSONArray;
}
}