/
idrac6.ts
453 lines (426 loc) · 15 KB
/
idrac6.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/**
* @module iDrac6
*/
import { IDrac6Options } from "./interfaces/iDrac6Options.js";
import { get } from "lodash-es";
import { URL, URLSearchParams } from "url";
import {
iDrac6ConfigError,
iDrac6MissingConfigError,
} from "./errors/ConfigErrors.js";
import { IDrac6Session } from "./interfaces/iDrac6Session.js";
import { join, dirname } from "path";
import fsExtra from "fs-extra";
import { unlink } from "fs/promises";
import ky from "ky-universal";
import { iDrac6LoginError } from "./errors/iDrac6Error.js";
import { Agent } from "https";
import { homedir } from "os";
import { IDrac6DataTypes, PowerActions, PowerState } from "./enums/iDrac6.js";
import debug from "debug";
import {
iDracMultipleDataResult,
iDracTemperature,
} from "./interfaces/idrac.js";
import { XMLParser } from "fast-xml-parser";
import { NormalizedOptions } from "ky/distribution/types/options.js";
const { pathExists, readJSON, writeJSON, ensureDir } = fsExtra;
const parser = new XMLParser();
/**
* Main iDrac6 Class
*/
export class iDrac6 {
/**
* Static Exports
*/
/**
* Export all iDrac Data Types you can request currently
*/
static IDRAC_DATA_TYPES = IDrac6DataTypes;
/**
* All power states this library could return
*/
static POWER_STATES = PowerState;
/**
* All power actions you can send with this library
*/
static POWER_ACTIONS = PowerActions;
/**
* Internal data
*/
private options?: IDrac6Options;
private host?: string;
private port?: number;
private ssl?: boolean;
private localSession?: IDrac6Session;
private sessionDebug = debug("iDrac6 Session");
private idracDebug = debug("iDrac6");
private idracAction = debug("iDrac6 Actions");
private CookieRegex = new RegExp(/_appwebSessionId_=(.*?);/gm);
/**
* Initial this library.
* @param options
*/
constructor(options?: IDrac6Options) {
this.options = options;
}
/**
* Update current instance options.
* *Note*: This function also validates them. So make sure they are correct.
* @param options
*/
public updateOptions(options?: IDrac6Options): void {
this.options = options;
this.validateOptions();
}
/**
* Get multiple idrac data types at once to save some time
* @param data
*/
public async getMultipleData(
data: IDrac6DataTypes[]
): Promise<iDracMultipleDataResult> {
this.idracAction("Get multiple data types %O", data);
const rawData = await this.getData(data);
const idracResult: iDracMultipleDataResult = {};
for (const type of data) {
if (type === IDrac6DataTypes.PowerState) {
idracResult[IDrac6DataTypes.PowerState] =
this._getPowerState(rawData);
} else if (type === IDrac6DataTypes.Temperature) {
idracResult[IDrac6DataTypes.Temperature] =
this._getTemperature(rawData);
} else if (
[
IDrac6DataTypes.BiosVersion,
IDrac6DataTypes.FirmwareVersion,
IDrac6DataTypes.LifecycleControllerFirmwareVersion,
IDrac6DataTypes.SystemRevision,
IDrac6DataTypes.HostName,
IDrac6DataTypes.SystemDescription,
].includes(type)
) {
idracResult[type] = this.extractKey(data, type);
}
}
return idracResult;
}
private extractKey(data: any, key: string) {
return get(data, "root." + key, null);
}
/**
* Get current idrac power state
*/
public async getPowerState(): Promise<PowerState> {
this.idracAction("Get power state");
const result = await this.getData([IDrac6DataTypes.PowerState]);
const pwState = this._getPowerState(result);
this.idracAction("Got power state: %d", pwState);
return pwState;
}
/**
*
* @param data
*/
private _getPowerState(data: any) {
return get(data, "root.pwState", PowerState.INVALID) as PowerState;
}
/**
* Send an action to set a new power state
* @param action
*/
public async sendPowerAction(
action: PowerActions = PowerActions.ON
): Promise<void> {
this.idracAction("Send power state: %d", action);
const searchParams = new URLSearchParams();
searchParams.set("set", "pwState:" + action);
await this.ky("data", {
searchParams,
});
}
/**
*
*/
public async getTemperature() {
this.idracAction("Get temperature");
const result = await this.getData([IDrac6DataTypes.Temperature]);
const tempData = this._getTemperature(result);
this.idracAction("Got temperature data %O", tempData);
return tempData;
}
/**
*
* @param data
*/
private _getTemperature(data: any): iDracTemperature {
const sensorData = get(
data,
"root.sensortype.thresholdSensorList.sensor"
);
return {
name: get(sensorData, "name"),
id: 1,
temperature: get(sensorData, "reading"),
unit: get(sensorData, "units"),
};
}
/**
* Validate current instance options.
* Method also gets called if you invoke updateOptions()
*/
public validateOptions() {
if (!this.options)
throw new iDrac6MissingConfigError(
"You forgot to provide a config"
);
if (!this.options.username)
throw new iDrac6ConfigError("username", true, "Missing username");
if (!this.options.password)
throw new iDrac6ConfigError("password", true, "Missing password");
if (!this.options.address)
throw new iDrac6ConfigError("address", true, "Missing address");
const url = new URL(this.options.address);
this.host = url.hostname;
const port = parseInt(url.port, 10);
this.port = Number.isFinite(port) ? port : 443;
this.ssl = url.protocol === "https:";
}
/**
*
* @param session
*/
private async saveSession(session?: IDrac6Session) {
if (this.options?.sessionOptions?.saveSession) {
const path =
this.options.sessionOptions.path ??
join(join(homedir(), "./idrac6/"), "./session.json");
const dir = dirname(path);
if (!session) {
await unlink(path);
} else {
this.sessionDebug("Session location: %s", path);
await ensureDir(dir);
await writeJSON(path, session);
}
}
this.localSession = session;
}
private async getSession(): Promise<IDrac6Session | false> {
this.validateOptions();
if (this.options?.sessionOptions?.saveSession) {
const path =
this.options.sessionOptions.path ??
join(join(homedir(), "./idrac6/"), "./session.json");
if (await pathExists(path)) {
this.sessionDebug("Session location: %s", path);
const data: IDrac6Session = await readJSON(path, {
throws: false,
});
if (data && !this.options.newAuth) {
return data;
} else if (data && this.options.newAuth) {
if (data.st1 && data.st2) {
return data;
}
}
}
}
if (this.localSession) {
return this.localSession;
}
return false;
}
/**
* You don't need to invoke this function by yourself.
* It gets invoked automatically, if there is no session or the current is not valid anymore.
*/
public async login() {
this.validateOptions();
if (!this.options) return;
const searchParams = new URLSearchParams();
searchParams.append("user", this.options.username);
searchParams.append("password", this.options.password);
this.sessionDebug("Try to login.");
const response = await this.baseKy("data/login", {
method: this.options.newAuth ? "POST" : "post",
body: this.options.newAuth ? searchParams.toString() : searchParams,
headers: this.options.newAuth
? {
"Content-Type": "application/x-www-form-urlencoded",
"Content-Length": `${searchParams.toString().length}`,
}
: {},
});
const text = await response.text();
this.sessionDebug("Login response xml: %s", text);
const parsed = parser.parse(text);
console.log(parsed);
const result: number = get(parsed, "root.authResult", 1);
const forwardUrl = get(parsed, "root.forwardUrl");
this.sessionDebug("Login response: %O", parsed);
if (result === 0) {
// Success
const setCookieString = response.headers.get("set-cookie");
let ST1;
let ST2;
if (this.options.newAuth) {
let stString = forwardUrl?.split?.("?")?.[1];
let stStrings = stString?.split?.(",");
if (stStrings?.length >= 2) {
for (const stValueString of stStrings) {
const [key, value] = stValueString?.split?.("=") ?? [
null,
null,
];
if (key && value) {
if (key === "ST1") {
ST1 = value;
} else if (key === "ST2") {
ST2 = value;
}
}
}
}
}
if (setCookieString && (this.options.newAuth ? ST1 && ST2 : true)) {
if (this.options.newAuth)
this.sessionDebug("ST1: %s ST2: %s", ST1, ST2);
const regexArray = this.CookieRegex.exec(setCookieString);
if (regexArray && regexArray.length >= 2) {
const sessionString: string = regexArray[1];
const session: IDrac6Session = {
port: this.port!,
host: this.host!,
ssl: this.ssl!,
username: this.options && this.options.username,
sessionId: sessionString,
...(this.options.newAuth ? { st2: ST2, st1: ST1 } : {}),
};
this.sessionDebug(
"Create session and save it: %O",
session
);
await this.saveSession(session);
return true;
}
}
}
throw new iDrac6LoginError("Invalid login data");
}
/**
*
* @param actions
*/
private async getData(actions: IDrac6DataTypes[]) {
const searchParams = new URLSearchParams();
searchParams.set("get", actions.join(","));
const response = await this.ky("data", {
searchParams,
});
const text = await response.text();
const parsed = parser.parse(text);
return parsed;
}
/**
*
* @readonly
* @private
* @memberof iDrac6
*/
private get baseKy() {
this.validateOptions();
const agent = new Agent({
rejectUnauthorized: false,
});
return ky.create({
prefixUrl: this.urlBase || undefined,
timeout: (this.options && this.options.timeout) || 6000,
// @ts-ignore
agent: this.options && this.options.validateSSL ? undefined : agent,
});
}
/**
*
* @readonly
* @private
* @memberof iDrac6
*/
private get ky() {
return this.baseKy.extend({
hooks: {
afterResponse: [
async (
request: Request,
options: NormalizedOptions,
response: Response
) => {
if (response.status === 401) {
this.sessionDebug(
"Response status is 401. Try to login."
);
await this.login();
const session = await this.getSession();
if (session) {
request.headers.set(
"Cookie",
`_appwebSessionId_=${session.sessionId}`
);
return this.baseKy(request, options);
} else {
this.sessionDebug(
"No idrac6 session found in afterResponse hook"
);
}
}
this.sessionDebug("Got idrac 6 response %j", response);
return response;
},
],
beforeRequest: [
async (request: Request) => {
const session = await this.getSession();
if (session) {
this.sessionDebug(
"Session exists. Set headers. Session: %O",
session
);
request.headers.set(
"Cookie",
`_appwebSessionId_=${session.sessionId}`
);
if (
this.options?.newAuth ||
(session.st1 && session.st2)
) {
this.sessionDebug(
"Session exists. Set ST2: %s",
session.st2
);
request.headers.set(
"ST2",
session.st2 as string
);
}
} else {
this.sessionDebug(
"No idrac6 session found in beforeRequest hook"
);
}
},
],
},
});
}
/**
*
* @readonly
* @private
* @type {(string | void)}
* @memberof iDrac6
*/
private get urlBase(): string | void {
if (!this.host || !this.port) return;
return `${this.ssl ? "https" : "http"}://${this.host}:${this.port}`;
}
}