/
logcat.ts
301 lines (256 loc) · 8.55 KB
/
logcat.ts
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import { CliUx, Errors, Flags } from '@oclif/core'
import inquirer from 'inquirer'
import { promises as fs } from 'fs'
import { PeerCertificate } from 'tls'
import {
DriverInfo,
handleConnectionErrors,
isLiveLogMessage,
LiveLogClient,
LiveLogClientConfig,
LiveLogMessage,
liveLogMessageFormatter,
LogLevel,
parseIpAndPort,
} from '../../../lib/live-logging'
import {
convertToId,
EventSourceError,
green,
logEvent,
red,
selectFromList,
SelectFromListConfig,
Sorting,
SseCommand,
stringTranslateToId,
TableFieldDefinition,
} from '@smartthings/cli-lib'
import { inspect } from 'util'
import { runForever } from '../../../lib/commands/drivers/logcat-util'
import { chooseHub } from '../../../lib/commands/drivers-util'
const DEFAULT_ALL_TEXT = 'all'
const DEFAULT_LIVE_LOG_PORT = 9495
const DEFAULT_LIVE_LOG_TIMEOUT = 30_000 // milliseconds
/**
* Define labels to stay consistent with other driver commands
*/
const driverFieldDefinitions: TableFieldDefinition<DriverInfo>[] = [
{
prop: 'driver_id',
label: 'Driver Id',
},
{
prop: 'driver_name',
label: 'Name',
},
]
async function promptForDrivers(fieldInfo: Sorting<DriverInfo>, list: DriverInfo[], prompt?: string): Promise<string> {
const primaryKeyName = fieldInfo.primaryKeyName
const itemIdOrIndex: string = (await inquirer.prompt({
type: 'input',
name: 'itemIdOrIndex',
message: prompt ?? 'Enter id or index',
default: DEFAULT_ALL_TEXT,
validate: input => {
return input === DEFAULT_ALL_TEXT ||
convertToId(input, primaryKeyName, list)
? true
: `Invalid id or index "${input}". Please enter an index or valid id.`
},
})).itemIdOrIndex
const inputId = itemIdOrIndex == DEFAULT_ALL_TEXT ? itemIdOrIndex : convertToId(itemIdOrIndex, primaryKeyName, list)
if (inputId === false) {
throw Error(`unable to convert ${itemIdOrIndex} to id`)
}
return inputId
}
type KnownHub = {
hostname: string
fingerprint: string
}
export default class LogCatCommand extends SseCommand<typeof LogCatCommand.flags> {
private authority!: string
private logClient!: LiveLogClient
static description = 'stream logs from installed drivers'
/* eslint-disable @typescript-eslint/naming-convention */
static flags = {
...SseCommand.flags,
all: Flags.boolean({
char: 'a',
description: 'stream from all installed drivers',
}),
'hub-address': Flags.string({
description: 'IPv4 address of hub with optionally appended port number',
}),
'connect-timeout': Flags.integer({
description: 'max time allowed when connecting to hub',
helpValue: '<milliseconds>',
default: DEFAULT_LIVE_LOG_TIMEOUT,
}),
'log-level': Flags.string({
description: 'minimum level of event to log',
helpValue: '<string>',
default: 'TRACE',
}),
}
/* eslint-enable @typescript-eslint/naming-convention */
static args = [
{
name: 'driverId',
description: 'a specific driver to stream logs from',
},
]
private getMinLogLevel(): number {
const minLogLevel = this.flags['log-level'].toUpperCase() as keyof typeof LogLevel
if (Object.keys(LogLevel).includes(minLogLevel)) {
return LogLevel[minLogLevel]
}
this.warn(`${this.flags['log-level']} is not a valid log-level. Logging all events.`)
return 0
}
private async checkServerIdentity(cert: PeerCertificate): Promise<void | never> {
const knownHubsPath = `${this.config.cacheDir}/known_hubs.json`
let knownHubs: Partial<Record<string, KnownHub>> = {}
try {
knownHubs = JSON.parse(await fs.readFile(knownHubsPath, 'utf-8'))
} catch (error) {
if (error.code !== 'ENOENT') { throw error }
}
const known = knownHubs[this.authority]
if (!known || known.fingerprint !== cert.fingerprint) {
await CliUx.ux.action.pauseAsync(async () => {
this.warn(`The authenticity of ${this.authority} can't be established. Certificate fingerprint is ${cert.fingerprint}`)
const verified = (await inquirer.prompt({
type: 'confirm',
name: 'connect',
message: 'Are you sure you want to continue connecting?',
default: false,
})).connect
if (!verified) {
this.error('Hub verification failed.')
}
knownHubs[this.authority] = { hostname: this.authority, fingerprint: cert.fingerprint }
await fs.writeFile(knownHubsPath, JSON.stringify(knownHubs))
this.warn(`Permanently added ${this.authority} to the list of known hubs.`)
})
}
}
private async chooseHubDrivers(commandLineDriverId?: string, driversList?: DriverInfo[]): Promise<string> {
const config: SelectFromListConfig<DriverInfo> = {
itemName: 'driver',
primaryKeyName: 'driver_id',
sortKeyName: 'driver_name',
listTableFieldDefinitions: driverFieldDefinitions,
}
const list = driversList !== undefined ? Promise.resolve(driversList) : this.logClient.getDrivers()
const preselectedId = await stringTranslateToId(config, commandLineDriverId, () => list)
return selectFromList(this, config,
{ preselectedId, listItems: () => list, getIdFromUser: promptForDrivers })
}
async init(): Promise<void> {
await super.init()
const askUserForHub = async (): Promise<string> => {
const hubId = await chooseHub(this, 'Select a hub.', undefined, { useConfigDefault: true })
const hubDevice = await this.client.devices.get(hubId)
const localIP = hubDevice.hub?.hubData.localIP
if (!localIP) {
throw new Errors.CLIError('Could not find hub IP address.')
}
return localIP
}
const hubIpAddress = this.flags['hub-address'] ?? await askUserForHub()
const [ipv4, port] = parseIpAndPort(hubIpAddress)
const liveLogPort = port ?? DEFAULT_LIVE_LOG_PORT
this.authority = `${ipv4}:${liveLogPort}`
const config: LiveLogClientConfig = {
authority: this.authority,
authenticator: this.authenticator,
verifier: this.checkServerIdentity.bind(this),
timeout: this.flags['connect-timeout'] ?? DEFAULT_LIVE_LOG_TIMEOUT,
userAgent: this.userAgent,
}
this.logClient = new LiveLogClient(config)
}
async run(): Promise<void> {
CliUx.ux.action.start('connecting')
// ensure host verification resolves before connecting to the event source
const installedDrivers = await this.logClient.getDrivers()
let sourceURL: string
if (this.flags.all) {
sourceURL = this.logClient.getLogSource()
} else {
const driverId = await CliUx.ux.action.pauseAsync(() => this.chooseHubDrivers(this.args.driverId, installedDrivers))
sourceURL = driverId == DEFAULT_ALL_TEXT ? this.logClient.getLogSource() : this.logClient.getLogSource(driverId)
}
await this.initSource(sourceURL)
const sourceTimeoutID = setTimeout(() => {
this.teardown()
CliUx.ux.action.stop(red('failed'))
try {
handleConnectionErrors(this.authority, 'ETIMEDOUT')
} catch (error) {
if (error instanceof Error) {
Errors.handle(error)
}
}
}, this.flags['connect-timeout']).unref() // unref lets Node exit before callback is invoked
this.source.onopen = () => {
clearTimeout(sourceTimeoutID)
if (installedDrivers.length === 0) {
this.warn('No drivers currently installed.')
}
CliUx.ux.action.stop(green('connected'))
}
this.source.onerror = (error: EventSourceError) => {
this.teardown()
CliUx.ux.action.stop(red('failed'))
this.logger.debug(`Error from eventsource. URL: ${sourceURL} Error: ${inspect(error)}`)
try {
if (error.status === 401 || error.status === 403) {
this.error(`Unauthorized at ${this.authority}`)
}
if (error.message !== undefined) {
handleConnectionErrors(this.authority, error.message)
}
this.error(`Unexpected error from event source ${inspect(error)}`)
} catch (error) {
if (error instanceof Error) {
Errors.handle(error)
}
}
}
const minLogLevel = this.getMinLogLevel()
this.source.onmessage = (event: MessageEvent<string>) => {
// TODO refactor cli-libs sse-io module to export parse utils
const message = this.parseEvent(event)
if (message.log_level >= minLogLevel) {
// TODO separate parsing and logging in sse-io so that logEvent can accept a parsed message
logEvent(event, liveLogMessageFormatter)
}
}
await runForever()
}
async catch(error: unknown): Promise<void> {
// exit gracefully for Command.exit(0)
if (error instanceof Errors.ExitError && error.oclif.exit === 0) {
this.teardown()
return
}
CliUx.ux.action.stop(red('failed'))
await super.catch(error)
}
parseEvent(event: MessageEvent): LiveLogMessage {
let message: unknown
try {
message = JSON.parse(event.data)
} catch (error) {
this.error(`Unable to parse received event. ${error.message ?? error}`)
}
if (isLiveLogMessage(message)) {
return message
} else {
throw Error('Unexpected log message type.')
}
}
}