-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathping-engines.js
278 lines (222 loc) · 8.91 KB
/
ping-engines.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
// Built-in modules
const { spawn } = require('child_process')
// 3rd-party dependencies
const netPing = require('net-ping')
const Address4 = require('ip-address').Address4
// In-house modules
const { config } = require('./config.js')
const { PingData, RequestError } = require('./ping-formats.js')
class EngineNative {
constructor(){}
// PRODUCTION TODO: Do this more thoroughly/securely (i.e. read up on it and use a tested 3rd-party lib)
static sanitizeSpawnInput(intervalNumber, ipString){
let returner = {}
let pin = intervalNumber // User-set polling interval for `ping` command, in milliseconds
let ips = ipString // User-set IP (as a string)
let intervalOk = pin && (pin === Number(pin)) && (typeof pin === 'number')
if (intervalOk){
// Make double-sure that pin is a number by casting it, then make sure it's not negative or decimal
returner.intervalNumber = Math.abs(Math.floor(Number(pin)))
} else {
throw Error('DANGER: this.opt.pingIntervalMs, which is used in node\'s command-line call \'spawn\', is not a number')
}
let nothingButDigitsAndDots = (ips.match(/[^\d\.]+/) === null)
let isValidIPAddress = new Address4(ips).isValid()
let ipOk = ips && (typeof ips === 'string') && nothingButDigitsAndDots
if (ipOk){
returner.ipString = ips
} else {
throw Error('DANGER: ipString (a target\'s IP), which is used in node\'s command-line call \'spawn\', is not a number')
}
return returner
}
static regPingHandlersInbuilt(instance, pingTarget){
console.info(`Registering inbuilt ping handler for target: ${pingTarget.humanName} (${pingTarget.IPV4})`)
let sanitizedSpawnInput = EngineNative.sanitizeSpawnInput(instance.opt.pingIntervalMs, pingTarget.IPV4)
const pingProcess = spawn('ping', [
'-i',
sanitizedSpawnInput.intervalNumber / 1000, // Mac ping -i supports fractional intervals but <= 0.1s requires su privilege
sanitizedSpawnInput.ipString
])
instance.firstPingSent = true
pingProcess.on('error', (code, signal)=>{
console.error('child process hit an error with ' + `code ${code} and signal ${signal}`)
instance.sessionDirty = true
throw Error('Node child process hit an error')
})
pingProcess.stdout.on('data', (data)=>{
let dataStr = data.toString()
let pingAsStructure = EngineNative.macOSPingTextToStructure(dataStr, new Date())
pingTarget.pingList.push(new PingData(pingAsStructure))
instance.sessionDirty = true
})
pingProcess.stderr.on('data', (data)=>{
let dataStr = data.toString()
if ( config.nodeVerbose >= 3){
console.error('inbuilt ping returned error through stderr: ', dataStr)
}
// Defaults
let errorReqTime = new Date()
let errorResTime = new Date()
let errorType = RequestError.errorTypes.unknownError
// TODO: test more comprehensively for other error types
if (dataStr.match(/No route to host/)){
errorType = RequestError.errorTypes.destinationUnreachableError
}
pingTarget.requestErrorList.push(new RequestError(errorType, errorReqTime, errorResTime, dataStr))
instance.sessionDirty = true
})
pingProcess.on('close', (code)=>{
console.info(`Child process (ping) closed with code ${code}`)
})
pingProcess.on('exit', (code)=>{
console.info(`Child process (ping) exited with code ${code}`)
})
return true
}
// FRAGILE: Depends on particular structure of text output from macOS 10.12.6 Sierra's inbuilt `ping` binary
static macOSPingTextToStructure(pingText, timeResponseReceived){
const structure = {}
const roundTripTimeMsRegex = /time\=([\.\d]*) ms/
const ttlHopsRegex = /ttl\=([\.\d]*) /
const icmpSeqRegex = /icmp_seq\=([\.\d]*) /
const responseSizeRegex = /([\.\d]*) bytes from/
const timeoutRegex = /Request timeout for icmp_seq (\d*)/
// Successful connection
structure.roundTripTimeMs = pingText.match(roundTripTimeMsRegex) ? Number(pingText.match(roundTripTimeMsRegex)[1]) : null
structure.ttlHops = pingText.match(ttlHopsRegex) ? Number(pingText.match(ttlHopsRegex)[1]) : null
structure.icmpSeq = pingText.match(icmpSeqRegex) ? Number(pingText.match(icmpSeqRegex)[1]) : null
structure.responseSize = pingText.match(responseSizeRegex) ? Number(pingText.match(responseSizeRegex)[1]) : null
// No connection
if (pingText.match(timeoutRegex)){
structure.failure = true
structure.errorType = PingData.errorTypes.requestTimedOutError
structure.icmpSeq = pingText.match(timeoutRegex) ? Number(pingText.match(timeoutRegex)[1]) : null
}
// Either way
structure.timeResponseReceived = timeResponseReceived
return structure
}
}
class EngineNetPing {
constructor(){}
static regPingHandlersNetPing(instance, pingTarget){
console.info(`Registering 'net-ping' handler for target: ${pingTarget.humanName} (${pingTarget.IPV4})`)
let npOptions = {
timeout: instance.opt.timeoutLimit,
packetSize: instance.opt.pingPacketSizeBytes,
ttl: instance.opt.pingOutgoingTtlHops
}
let npSession = netPing.createSession(npOptions)
let currentIcmp = null
let unpairedRequests = []
let pingHost = (ipv4)=>{
currentIcmp = currentIcmp === null ? 0 : currentIcmp + 1
let contextIcmp = currentIcmp
let req = {
icmpSeq: contextIcmp,
timeRequestSent: new Date()
}
unpairedRequests.push(req)
let res = {
failure: null,
errorType: undefined
}
npSession.pingHost(ipv4, (err, target, sent, rcvd)=>{
EngineNetPing.processNetPingResponse(err, target, sent, rcvd, req, res, unpairedRequests, pingTarget)
instance.sessionDirty = true
})
}
npSession.on('error', (err)=>{
console.error(err.toString())
npSession.close()
instance.sessionDirty = true
throw Error('net-ping\'s underlying raw socket emitted an error')
})
let npPingChosenTarget = ()=>{
instance.firstPingSent = true
return pingHost(pingTarget.IPV4)
}
let targetPingingTick = setInterval(npPingChosenTarget, instance.opt.pingIntervalMs)
return true
}
static processNetPingResponse(err, target, sent, rcvd, req, res, unpairedRequests, pingTarget){
res.icmpSeq = req.icmpSeq // num
res.timeRequestSent = sent // Date - can be undefined if error
res.timeResponseReceived = rcvd // Date - can be undefined if error
// TODO: this needs to betteraccount for errors that occur within/deeper than net-ping (try/catch?) - could help to send a PR to nospaceships/node-net-ping for this
if (err){
res.failure = true
for (let supportedErrorStr of [
'RequestTimedOutError',
'DestinationUnreachableError',
'PacketTooBigError',
'ParameterProblemError',
'RedirectReceivedError',
'SourceQuenchError',
'TimeExceededError'
]){
if (err instanceof netPing[supportedErrorStr]){
let netPingToInternalError = {
RequestTimedOutError: 'requestTimedOutError',
DestinationUnreachableError: 'destinationUnreachableError',
PacketTooBigError: 'packetTooBigError',
ParameterProblemError: 'parameterProblemError',
RedirectReceivedError: 'redirectReceivedError',
SourceQuenchError: 'sourceQuenchError',
TimeExceededError: 'timeExceededError'
}
// Convert between net-ping's and our own errors
res.errorType = PingData.errorTypes[netPingToInternalError[supportedErrorStr]]
} else {
let handled = false
// NB: Errors emitted from raw-socket when network adapter turned off on macOS
// Should have been handled by netPing
if (err.toString().match(/No route to host/)){
res.errorType = PingData.errorTypes.destinationUnreachableError
handled = true
}
if (err.toString().match(/Network is down/)){
res.errorType = PingData.errorTypes.networkDownError
handled = true
}
if (!handled){
// Unknown misc error
console.error('processNetPingResponse: Unknown net-ping response error:')
console.error(err)
res.errorType = PingData.errorTypes.unknownError
if (process.env.NODE_ENV === 'development'){
console.error(err)
throw Error('processNetPingResponse - Unhandled error:', err)
}
}
}
}
pingTarget.pingList.push(new PingData(res))
} else {
// Successful response
res.failure = false
res.roundTripTimeMs = rcvd - sent // num - we only bother to calc if both vals are truthy
// TODO: how to get response size in net-ping? seems impossible without a PR
// TODO: how to get response ttl in net-ping? seems impossible without a PR
pingTarget.pingList.push(new PingData(res))
}
// Warn user if the system that pairs up requests with received responses has stopped working
for (let requestIndex in unpairedRequests){
if (unpairedRequests[requestIndex].icmpSeq === res.icmpSeq){
unpairedRequests.splice(requestIndex, 1)
}
}
if (unpairedRequests.length > 10){
console.warn('processNetPingResponse: Unpaired requests piling up...')
console.warn(unpairedRequests)
}
return {
req: req,
res: res,
unpairedRequests: unpairedRequests
}
}
}
exports.EngineNative = EngineNative
exports.EngineNetPing = EngineNetPing