This repository has been archived by the owner on Nov 29, 2021. It is now read-only.
forked from deadlock-delegate/crawler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
155 lines (134 loc) · 4.09 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
const Crawler = require("./src/crawler");
const { URL } = require("url");
const { orderBy } = require("lodash/collection");
var loadConfig = require("load-config-file");
loadConfig.register(".json", JSON.parse);
const config = loadConfig("config");
const crawler = new Crawler();
const args = process.argv.slice(2);
const report = (crawler) => {
const blockStats = {};
const versionStats = {};
const ipNodes = orderBy(Object.keys(crawler.nodes));
for (const ipNode of ipNodes) {
const node = crawler.nodes[ipNode];
if (node.height === undefined || node.id === undefined) {
continue;
}
if (blockStats[node.height]) {
blockStats[node.height].count += 1;
blockStats[node.height].ids[node.id].hashCount += 1;
blockStats[node.height].ids[node.id].ips.push(ipNode);
} else {
blockStats[node.height] = {};
blockStats[node.height].count = 1;
blockStats[node.height].height = node.height;
// todo block ids
blockStats[node.height].ids = {};
blockStats[node.height].ids[node.id] = {};
blockStats[node.height].ids[node.id].hashCount = 1;
blockStats[node.height].ids[node.id].ips = [ipNode];
}
if (versionStats[node.version]) {
versionStats[node.version].count += 1;
versionStats[node.version].ips.push(ipNode);
} else {
versionStats[node.version] = {
count: 1,
version: node.version,
ips: [ipNode],
};
}
}
const nodes = Object.values(crawler.nodes);
const allDelays = nodes
.filter((item) => item.latency)
.map((item) => item.latency);
const averageDelay = (
allDelays.reduce((a, b) => a + b, 0) / allDelays.length
).toFixed(2);
const maxDelay = Math.max(...allDelays);
const minDelay = Math.min(...allDelays);
const ipToInt32 = (ip) => {
return ip.split(".").reduce((int, v) => int * 256 + +v);
};
const formatIpsArray = (ips) => {
const sortedIps = ips.sort((a, b) => {
return ipToInt32(a) - ipToInt32(b);
});
let result = "[";
sortedIps.forEach((ip, index, array) => {
result += `"${ip}`;
const alias = config?.aliases[ip];
if (alias) {
result += `/${alias}`;
}
result += '"';
if (index !== array.length - 1) {
result += ",";
}
});
result += "]";
return result;
};
console.log("===========================================");
console.log(`All nodes: ${Object.keys(crawler.nodes).length}`);
console.log(`Nodes online: ${crawler.heights.length}`);
console.log(
`Nodes offline: ${
Object.keys(crawler.nodes).length - crawler.heights.length
}`
);
// height/block stats
console.log("");
console.log("");
console.log("Height and block stats:");
for (const stat of orderBy(Object.values(blockStats), ["height"], ["desc"])) {
console.log("");
console.log(` * ${stat.height} with ${stat.count} nodes. Block hashes:`);
for (const hash in stat.ids) {
console.log(
` - ${hash} (${stat.ids[hash].hashCount} nodes - ${formatIpsArray(
stat.ids[hash].ips
)})`
);
}
}
// version stats
console.log("");
console.log("");
console.log("Version stats:");
for (const stat of orderBy(
Object.values(versionStats),
["version"],
["desc"]
)) {
console.log("");
console.log(
` - ${stat.version} on ${stat.count} nodes - ${formatIpsArray(stat.ips)}`
);
}
// delay stats
console.log("");
console.log("");
console.log("Delay from this location:");
console.log(` Avg: ${averageDelay}ms`);
console.log(` Min: ${minDelay}ms`);
console.log(` Max: ${maxDelay}ms`);
console.log("");
console.log(`${ipNodes.length} IPs:`);
console.log(" " + formatIpsArray(ipNodes));
console.log("------------------------------------------");
console.log(`Finished scanning in ${new Date() - crawler.startTime}ms`);
process.exit(0);
};
const node = { ip: undefined, port: undefined };
if (args.length === 1) {
const url = new URL(args[0]);
node.ip = url.hostname;
node.port = url.port;
}
crawler
.run(node)
.then(report)
.catch((err) => console.error(err));