-
Notifications
You must be signed in to change notification settings - Fork 23
/
getallpeers.js
173 lines (162 loc) · 4.8 KB
/
getallpeers.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
const axios = require("axios");
//const CachemanFile = require('cacheman-file');
const CachemanMemory = require("cacheman-memory");
//const cache = new CachemanFile({tmpDir: '.cache', ttl: 24*3600,});
const cache = new CachemanMemory();
const cachettl = 24 * 3600;
const cachetll4nodes = 24 * 3600;
let mySet = new Set();
let failCount = 0;
const clrcache = () => {
cache.del("masterNodelocations");
cache.del("locations");
};
const getLocation = async (ip, saveip, freegeoserverUrl) =>
new Promise(resolve => {
cache.get(ip, async (err, value) => {
if (value) {
if (!saveip) {
return resolve(value.country_name);
} else {
return resolve(value);
}
}
try {
const res = await axios.get(`http://${freegeoserverUrl}/json/${ip}`);
if (!saveip) {
cache.set(ip, res.data, cachettl);
resolve(res.data.country_name);
} else {
cache.set(ip, res.data, cachettl);
resolve(res.data);
}
} catch (e) {
console.log(
"Can't get location failCount:" +
failCount++ +
" url: " +
freegeoserverUrl +
" " +
e.message
);
if (failCount < 1000) {
getLocation(ip, saveip, freegeoserverUrl);
} else {
resolve("-");
}
}
});
});
async function getPeerByIp(ip, port) {
mySet.add(ip);
try {
const res = await axios.get("http://" + ip + ":" + port + "/peers");
if (!res.data.peers) throw new Error("Missing peers.");
// console.log(`${res.data.peers.length} peers found`);
return res.data.peers;
} catch (e) {
//console.log('Can\'t get peers');
return Promise.resolve([]);
//Promise.reject(new Error("Can\'t get peers"));
}
}
async function getFeeByIp(ip, port) {
try {
const res = await axios.get("http://" + ip + ":" + port + "/feeaddress");
if (!res.data.fee_address) throw new Error("Missing fee_address.");
//console.log(`${res.data.fee_address} fee_address found for ${ip}`);
return ip;
} catch (e) {
//console.log('Can\'t get peers');
return Promise.resolve("");
//Promise.reject(new Error("Can\'t get peers"));
}
}
const getAllpeers = async (startnode, port) =>
getPeerByIp(startnode, port).then(peers =>
Promise.all(
peers
.map(peer => {
const [ip] = peer.split(":");
if (mySet.has(ip)) {
return Promise.resolve([]);
} else {
return getPeerByIp(ip, port);
}
})
.concat(Promise.resolve(peers))
)
);
const getMasterNodes = freegeoserverUrl =>
new Promise(resolve => {
cache.get("masterNodelocations", (err, value) => {
if (value) {
resolve(
Promise.all(value.map(ip => getLocation(ip, true, freegeoserverUrl)))
);
} else {
console.log("re new cashe");
//cacheLocations().then(()=>resolve(getMasterNodes()))
resolve([]);
}
});
});
const cacheLocations = async (startnode, port, freegeoserverUrl) => {
console.log(`run cacheLocations: startnode = ${startnode}:${port}`);
console.time("getAllpeers");
const l = await getAllpeers(startnode, port);
const clearArray = l.reduce((a, b) => [...a, ...b]);
const newclearArray = new Set(clearArray);
console.log(" getAllpeers size: " + newclearArray.size);
console.timeEnd("getAllpeers");
console.time("getLocation");
const l2 = await Promise.all(
Array.from(newclearArray).map(peer => {
const [ip] = peer.split(":");
return getLocation(ip, false, freegeoserverUrl);
})
);
console.log(" get all peers location ");
console.timeEnd("getLocation");
console.time("get master nodes");
const l3 = await Promise.all(
Array.from(newclearArray).map(peer => {
const [ip] = peer.split(":");
return getFeeByIp(ip, port);
})
);
console.log(" get master nodes ");
console.timeEnd("get master nodes");
let counts = {};
//cache.set("ips", Array.from(newclearArray), 3600);
//console.log(l3.filter( item => item!=''));
cache.set("masterNodelocations", l3.filter(item => item != ""), cachettl);
l2.forEach(function(x) {
counts[x] = (counts[x] || 0) + 1;
});
//console.log(counts.length);
cache.set("locations", counts, cachetll4nodes);
mySet = new Set();
failCount = 0;
return counts;
};
const getLocations = () =>
new Promise(resolve => {
//console.log("--== run locations ==--");
cache.get("locations", (err, value) => {
console.log("--== run locations ==--");
if (value) {
resolve(value ? value : []);
} else {
//console.log("need renew cashe");
resolve([]);
//resolve(cacheLocations());
}
});
});
module.exports = {
cacheLocations,
getLocations,
getMasterNodes,
clrcache
};