-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvdump.js
executable file
·257 lines (191 loc) · 6.44 KB
/
vdump.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
#!/usr/bin/env node
const usage = `
Usage:
solana gossip -v | vdump.js [-max <max>] [-oi] [-csv] [-s] [-v]
-max <number>
Only show fewer than <max> nodes at location
-oi
Output node ids only
-csv
Comma separated
-s
Summary only
-v
debug output
Examples:
# list of nodes at location with up to 2 nodes
solana gossip -v | vdump.js -max 2
# list of node IDs at location with up to 3 nodes
solana gossip -v > x.x
vdump.js x.x -g -max 3 -oi
# locations and node counts only, in csv format
solana gossip -v | vdump.js -s -csv
Requires node + solana cli tools + traceroute
- sudo apt install nodejs
- https://docs.solana.com/cli/install-solana-cli-tools
You must install the JavaScript dependencies:
npm install
`
const ASN_WIDE_FIELD_CHAR = 80
const ASN_FIELD_CHAR = 48
const ASNID_FIELD_CHAR = 7
const NODE_FIELD_CHAR = 48
const IP_FIELD_CHAR = 20
async function main() {
const fs = require("fs")
const args = require('command-line-parser')()
if ( args.oi && args.csv ) {
console.log("Cannot specify -oi and -csv")
process.exit(1)
}
if ( args.oi && args.s) {
console.log("Cannot specify -oi and -s")
process.exit(1)
}
let input
if( args._args && args._args.length > 0 ) {
// read file
const file = args._args[0]
if ( !fs.existsSync(file) ) {
console.log("File not found:",file)
process.exit(1)
}
var buff = fs.readFileSync(file)
input = buff.toString()
} else {
// read stdin
var stdinBuffer = fs.readFileSync(0) // STDIN_FILENO = 0
input = stdinBuffer.toString()
}
if (!input) {
console.log("No data to work on")
process.exit(1)
}
let waiton = []
const lines = input.split(/\r?\n/)
for( let i=0; i<lines.length; i++ ) {
const line = lines[i]
const parts = line.split('|')
if( !parts[0] || !parts[1] || !parts[2] ) { continue }
const nodeIP = parts[0].trim()
const nodeID = parts[1].trim()
const gossipPort = parts[2].trim()
if( !validIP( nodeIP )) { continue }
if ( args.v ) { console.log("Querying:",nodeID,nodeIP) }
waiton.push( execQuery( nodeID, nodeIP, gossipPort ) )
}
if ( args.v ) { console.log("Waiting on",waiton.length,"queries") }
const responses = await Promise.all( waiton )
let summary = {}
let nodesGrouped = {}
for( let i=0; i<responses.length; i++ ) {
const response = responses[i]
if ( args.v && !response.ok ) {
console.log("Error executing cmd:",response.cmd,"for node:",response.nodeID,"Error:",response.err)
continue
}
if ( args.v && !response.out ) {
console.log("Error executing cmd:",response.cmd,"for node:",response.nodeID,"No output")
continue
}
if ( response.ok ) {
const [asnID,,bgpPrefix,,,,asn] = response.out.split("|").map(s => s.trim())
const sumStr = asn+" ("+asnID+")"
++summary[sumStr] || (summary[sumStr]=1)
nodesGrouped[response.nodeID] = { nodeIP: response.nodeIP, asn, asnID, bgpPrefix, sumStr }
}
}
if ( args.s ) {
console.log()
console.log("Summary counts of nodes at locations:")
console.log()
if ( args.csv ) {
console.log("location,nodes_at_location")
ascSumDo( summary, (asn,count) => { console.log(asn+","+count) } )
} else {
console.log("location".padEnd(ASN_WIDE_FIELD_CHAR," "),"nodes_at_location")
ascSumDo( summary, (asn,count) => { console.log(asn.padEnd(ASN_WIDE_FIELD_CHAR," ").substr(0,ASN_WIDE_FIELD_CHAR),count) } )
}
console.log()
process.exit(0)
}
Object.keys(nodesGrouped).forEach(function (nodeID) {
nodesGrouped[nodeID].count = summary[ nodesGrouped[nodeID].sumStr ]
})
if ( args.max ) {
Object.keys(nodesGrouped).forEach(function (nodeID) {
if ( nodesGrouped[nodeID].count > args.max ) {
delete nodesGrouped[nodeID]
}
})
}
if ( args.csv ) {
console.log("nodeID,nodeIP,BGP_Prefix,asnID,location,nodes_at_location")
ascItemDo( nodesGrouped, (nodeID,entry) => {
console.log(nodeID+ ","+ entry.nodeIP+ ","+entry.bgpPrefix+","+ entry.asnID+ ","+entry.asn+","+ entry.count)
})
} else {
if ( args.oi ) {
ascItemDo( nodesGrouped, (nodeID,_) => { console.log('- '+nodeID) } )
} else {
console.log("nodeID".padEnd(NODE_FIELD_CHAR," "),"nodeIP".padEnd(IP_FIELD_CHAR," "),"BGP_Prefix".padEnd(IP_FIELD_CHAR," "),"asnID".padEnd(ASNID_FIELD_CHAR," "),"location".padEnd(ASN_FIELD_CHAR," "),"nodes_at_location")
ascItemDo( nodesGrouped, (nodeID,entry) => {
console.log(nodeID.padEnd(NODE_FIELD_CHAR," ").substr(0,NODE_FIELD_CHAR),
entry.nodeIP.padEnd(IP_FIELD_CHAR," ").substr(0,IP_FIELD_CHAR),
entry.bgpPrefix.padEnd(IP_FIELD_CHAR," ").substr(0,IP_FIELD_CHAR),
entry.asnID.padEnd(ASNID_FIELD_CHAR," ").substr(0,ASNID_FIELD_CHAR),
entry.asn.padEnd(ASN_FIELD_CHAR," ").substr(0,ASN_FIELD_CHAR),
entry.count)
})
}
}
process.exit(0)
}
main()
.catch(err => {
console.error(err)
})
.then(() => process.exit())
function getDN( str ) {
return str.match(/[\w-]+\.\w* /)[0].trim()
}
function ascSumDo( hash, func ) {
var keys = Object.keys(hash);
keys.sort(function(a, b) {
return hash[a] - hash[b]
})
keys.forEach(function(k) {
func(k,hash[k])
})
}
function ascItemDo( hash, func ) {
var keys = Object.keys(hash);
keys.sort(function(a, b) {
return hash[a].nodeIP - hash[b].nodeIP
})
keys.forEach(function(k) {
func(k,hash[k])
})
}
function execQuery(nodeID, nodeIP) {
//const cmd = 'whois -h bgp.tools " -v '+nodeIP+'" | tail -1 | cut -d"|" -f7'
const cmd = 'whois -h bgp.tools " -v '+nodeIP+'" | tail -1 '
const exec = require('child_process').exec
return new Promise((resolve, reject) => {
exec(cmd, (error, stdout, stderr) => {
resolve(stdout? {ok: true, nodeID, nodeIP, cmd, out: stdout.trim() } : {ok: false, nodeID, cmd, err: stderr } )
})
})
}
function validIP(ipaddress) {
if (/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(ipaddress)) {
return true
}
return false
}
function validPort(port) {
if (/\d{5}/.test(port) ) {
return true
}
return false
}