-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·183 lines (156 loc) · 6.78 KB
/
index.js
File metadata and controls
executable file
·183 lines (156 loc) · 6.78 KB
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
#!/usr/bin/env node
const pcapGenerator = require('./pcap-generator')
const io = require('socket.io-client')
const minimist = require('minimist')
const prettyBytes = require('pretty-bytes')
const log = require('single-line-log').stderr
const pkgJson = require('./package.json')
const getPackageJson = require('package-json')
const cookie = require('cookie')
const formatDate = require('date-fns').format
const ALLOWED_PARAMS = ['token', 'key', 'sim', 'filename', 'api', '-', '_']
const argv = minimist(process.argv.slice(2), { string: 'sim' })
const apiKey = argv.key || argv.token
const allParams = Object.keys(argv)
const simIds = typeof argv.sim === 'string' ? [argv.sim] : argv.sim
const filename = argv.filename
const apiUrl = argv.api || 'https://api.onomondo.com'
const isWritingToStdout = argv._.includes('-')
const isWritingToFile = !!filename
let capturedPackets = 0
let capturedBytes = 0
let isAuthenticated = false
let unAuthenticatedTransportErrorCount = 0
checkPrerequisites().then(run)
async function checkPrerequisites () {
const publicVersion = await getPublicVersion()
const isUsingCorrectVersion = pkgJson.version === publicVersion
const disallowedParams = allParams.filter(param => !ALLOWED_PARAMS.includes(param))
const hasAllRequiredParams = apiKey && simIds?.length
const hasOnlyAllowedParams = disallowedParams.length === 0
const hasNoParameters = allParams.includes('_') && allParams.length === 1
const isWritingToStdoutOrFile = isWritingToStdout || isWritingToFile
const isWritingToStdoutAndFile = isWritingToStdout && isWritingToFile
const areSimsCorrectLength = simIds?.filter(id => id.length === 9).length > 0
if (isUsingCorrectVersion) console.error(`Onomondo Live ${pkgJson.version}\n`)
if (!isUsingCorrectVersion) console.error(`Onomondo Live ${pkgJson.version}. You are currently using an outdated version. The latest is ${publicVersion}.\n`)
if (hasNoParameters) {
exit([
'Use onomondo-live to capture all data between devices and the Onomondo network, seen from the Onomondo\'s perspective.',
'Output to a pcap file, or pipe to another tool that can read pcap files (like Wireshark).',
'',
'Write to file:',
'onomondo-live --key=a1b2c3 --sim=012345678 --filename=output.pcap',
'',
'Write to standard output:',
'onomondo-live --key=a1b2c3 --sim=012345678 -',
'',
'Pipe to Wireshark:',
'onomondo-live --key=a1b2c3 --sim=012345678 - | wireshark -k -i -',
'',
'You need to use the id of one or more of your sims, and an Onomondo api key.',
'You can generate an api key in the app, https://app.onomondo.com.',
'',
'If you want to listen to multiple sims you can supply multiple --sim params, like this: --sim=111111111 --sim=222222222'
].join('\n'))
}
if (!hasOnlyAllowedParams) exit(`You are using illegal parameters: ${disallowedParams.join(', ')}`)
if (!hasAllRequiredParams && !apiKey) exit('You are missing a required parameter: --key')
if (!hasAllRequiredParams && !simIds?.length) exit('You are missing a required paramter: --sim')
if (!isWritingToStdoutOrFile) exit('You are missing a required parameters: Either write to file (--filename), or to standard output (-)')
if (isWritingToStdoutAndFile) exit('You need to either write to file, or to standard output. Not both.')
if (!areSimsCorrectLength) exit('Some of the sims are not exactly 9 digits long')
}
function run () {
if (isWritingToStdout) {
// Kill process if the process piped to is closed, and not writing to a file
process.stdout.on('error', () => { }) // ignore errors
process.stdout.on('close', () => process.exit(0))
}
pcapGenerator.writeHeader({ filename, stdout: isWritingToStdout })
connect()
}
function connect () {
const socket = io(apiUrl, {
path: '/monitor',
withCredentials: true,
extraHeaders: {
'user-agent': `node-XMLHttpRequest onomondo-live/v${pkgJson.version}`
}
})
const COOKIE_NAME = 'AWSALB'
// https://socket.io/how-to/deal-with-cookies
socket.io.on('open', () => {
socket.io.engine.transport.on('pollComplete', () => {
const request = socket.io.engine.transport.pollXhr.xhr
const cookieHeader = request.getResponseHeader('set-cookie')
if (!cookieHeader) return
cookieHeader.forEach(cookieString => {
if (cookieString.includes(`${COOKIE_NAME}=`)) {
const cookieValue = cookie.parse(cookieString)
socket.io.opts.extraHeaders = {
cookie: `${COOKIE_NAME}=${cookieValue[COOKIE_NAME]}`,
'user-agent': `node-XMLHttpRequest onomondo-live/v${pkgJson.version}`
}
}
})
})
})
socket.on('connect', () => {
socket.emit('authenticate', apiKey)
})
function onerror (err) {
const isNotAuthenticated = err === 'Not authenticated'
if (isNotAuthenticated) {
exit('Authenticated failed. Api key is incorrect')
}
print(`Error: ${err}`)
}
socket.on('error', onerror)
socket.on('subscribe-error', onerror)
socket.on('disconnect', err => {
const isTransportError = 'transport error' && unAuthenticatedTransportErrorCount < 5
const hasServerForcefullyDisconnectedClient = err === 'io server disconnect'
if (hasServerForcefullyDisconnectedClient && isAuthenticated) return print('The server disconnected you')
if (hasServerForcefullyDisconnectedClient && !isAuthenticated) return print('The server disconnected you. Is the api key correct?')
if (isTransportError && !isAuthenticated) {
unAuthenticatedTransportErrorCount += 1
return
}
isAuthenticated = false
print('Connection closed. Trying to re-establish')
socket.disconnect()
setTimeout(connect, 1000)
})
socket.on('authenticated', () => {
unAuthenticatedTransportErrorCount = 0
isAuthenticated = true
print('Connected and authenticated')
simIds.forEach(simId => socket.emit('subscribe:packets', simId))
})
socket.on('subscribed:packets', ({ simId, ip }) => {
print(`Attached. SIM id=${simId}. ip=${ip}`)
})
socket.on('packets', ({ packet: hexString }) => {
const timestamp = Date.now()
const packet = Buffer.from(hexString, 'hex')
capturedPackets += 1
capturedBytes += packet.length
log(`Captured: ${capturedPackets} packet${capturedPackets === 1 ? '' : 's'} (${prettyBytes(capturedBytes)})\n`)
pcapGenerator.appendPacket({ packet, filename, timestamp, stdout: isWritingToStdout })
})
}
function print (str) {
const timestamp = formatDate(new Date(), 'yyyy-MM-dd HH:mm:ss')
console.error(`[${timestamp}] ${str}`)
}
function exit (err) {
console.error(err)
console.error()
console.error('See https://github.com/onomondo/onomondo-live for more information')
process.exit(1)
}
async function getPublicVersion () {
const pkgJson = await getPackageJson('onomondo-live')
return pkgJson.version
}