-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathuniversalParse.ts
More file actions
515 lines (448 loc) · 17.3 KB
/
universalParse.ts
File metadata and controls
515 lines (448 loc) · 17.3 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
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
'use strict';
/**
* Universal TMOS Parser
*
* Recursive parser that converts TMOS configuration text to structured JSON.
* Ported from tmos-converter with enhancements for hierarchical output.
*
* Key features:
* - Universal recursive parsing (handles any nesting depth)
* - iRule-aware bracket matching
* - Handles edge cases: multiline strings, pseudo-arrays, empty objects
* - Outputs hierarchical structure: ltm.pool["/Common/pool1"]
*/
import logger from './logger';
export interface ParsedObject {
[key: string]: any;
}
/**
* Check if a line is the start of an iRule, GTM rule, or PEM iRule
*/
function isRule(str: string): boolean {
return str.includes('ltm rule') || str.includes('gtm rule') || str.includes('pem irule');
}
/**
* Count occurrences of a character in a string
*/
function countChar(str: string, char: string): number {
return str.split(char).length - 1;
}
/**
* Get indentation level (number of leading spaces)
*/
function countIndent(line: string): number {
const match = line.match(/^( *)/);
return match ? match[1].length : 0;
}
/**
* Remove one level of indentation from array of lines
*/
function removeIndent(arr: string[]): string[] {
return arr.map(line => line.replace(/^ /, ''));
}
/**
* Extract the object name/title from a TMOS object line
* Example: "ltm pool /Common/web_pool {" → "ltm pool /Common/web_pool"
*/
function getTitle(str: string): string {
return str.replace(/\s?\{\s?}?$/, '').trim();
}
/**
* Parse a "key value" string into an object
*/
function strToObj(line: string): Record<string, string> {
const trimmed = line.trim();
const split = trimmed.split(' ');
const key = split.shift() ?? '';
return { [key]: split.join(' ') };
}
/**
* Convert pseudo-array to actual array
* Example: "{ /Common/http /Common/tcp }" → ["/Common/http", "/Common/tcp"]
*/
function objToArr(line: string): string[] {
const match = line.match(/{\s*([\s\S]*?)\s*}/);
if (match && match[1]) {
return match[1].trim().split(/\s+/).filter(Boolean);
}
return [];
}
/**
* Handle multiline quoted strings
*/
function arrToMultilineStr(chunk: string[]): Record<string, string> {
const joined = chunk.join('\n');
const match = joined.match(/^(\S+)\s+"([\s\S]*)"$/);
if (match) {
return { [match[1]]: match[2] };
}
// Fallback: try to parse as key-value
const firstLine = chunk[0]?.trim() ?? '';
const key = firstLine.split(' ')[0] ?? '';
const value = chunk.join('\n').replace(new RegExp(`^${key}\\s*`), '').replace(/^"|"$/g, '');
return { [key]: value };
}
interface GroupResult {
group: string[][];
error: Error | null;
}
/**
* Group root-level TMOS objects from config lines
* Handles bracket matching, iRule special cases, and escaped characters
*/
function groupObjects(arr: string[]): GroupResult {
const group: string[][] = [];
let error: Error | null = null;
try {
for (let i = 0; i < arr.length; i += 1) {
const currentLine = arr[i];
if (!currentLine) continue;
// Empty object or pseudo-array on single line
if (currentLine.includes('{') && currentLine.includes('}') && currentLine[0] !== ' ') {
group.push([currentLine]);
} else if (currentLine.trim().endsWith('{') && !currentLine.startsWith(' ') && !currentLine.startsWith('#')) {
let c = 0;
let ruleLine = '';
const ruleFlag = isRule(currentLine);
if (ruleFlag) {
ruleLine = currentLine;
}
// Bracket counting for finding object end
let bracketCount = 1;
let opening = 1;
let closing = 0;
while (bracketCount !== 0) {
c += 1;
const line = arr[i + c];
if (!line) {
if ((opening !== closing) && ruleFlag) {
error = new Error(`iRule parsing error, check the following iRule: ${ruleLine}`);
}
break;
}
let subcount = 0;
// Don't count brackets in comments or special lines inside iRules
if (!((line.trim().startsWith('#') || line.trim().startsWith('set') ||
line.trim().startsWith('STREAM')) && ruleFlag)) {
// Exclude quoted parts and escaped quotes
const updatedline = line.trim().replace(/\\"/g, '').replace(/"[^"]*"/g, '');
let previousChar = '';
updatedline.split('').forEach((char) => {
if (previousChar !== '\\') {
if (char === '{') {
subcount += 1;
opening += 1;
}
if (char === '}') {
subcount -= 1;
closing += 1;
}
}
previousChar = char;
});
// Abort if we run into the next rule
if (isRule(line)) {
c -= 1;
bracketCount = 0;
break;
}
bracketCount += subcount;
}
}
group.push(arr.slice(i, i + c + 1));
i += c;
}
}
} catch (e) {
error = e as Error;
}
return { group, error };
}
/**
* Recursively parse a grouped TMOS object into JSON
*/
function orchestrate(arr: string[]): Record<string, any> {
const key = getTitle(arr[0] ?? '');
// Preserve original body for the 'line' property (excluding first/last bracket lines)
const originalBody = arr.slice(1, -1).join('\n');
// Remove opening and closing bracket lines
arr.pop();
arr.shift();
let obj: any = {};
// Edge case: iRules (multiline string, preserve as-is)
if (isRule(key)) {
obj = arr.join('\n');
}
// Edge case: monitor min X of {...}
else if (key.includes('monitor min')) {
const trimmedArr = arr.map((s) => s.trim());
obj = trimmedArr.join(' ').split(' ');
}
// Edge case: skip cli script and cert-order-manager (complex quoting)
else if (!key.includes('cli script') && !key.includes('sys crypto cert-order-manager')) {
for (let i = 0; i < arr.length; i += 1) {
const line = arr[i];
if (!line) continue;
// Nested object - RECURSIVE
if (line.endsWith('{') && arr.length !== 1) {
let c = 0;
while (arr[i + c] !== ' }') {
c += 1;
if ((i + c) >= arr.length) {
throw new Error(`Missing or mis-indented '}' for line: '${line}'`);
}
}
const subObjArr = removeIndent(arr.slice(i, i + c + 1));
// Coerce unnamed objects into indexed array
let arrIdx = 0;
const coerceArr = subObjArr.map((subLine) => {
if (subLine === ' {') {
const newLine = subLine.replace('{', `${arrIdx} {`);
arrIdx += 1;
return newLine;
}
return subLine;
});
// Recursion for subObjects
Object.assign(obj, orchestrate(coerceArr));
i += c;
}
// Empty object: "metadata { }"
else if (line.split(' ').join('').endsWith('{}')) {
obj[(line.split('{')[0] ?? '').trim()] = {};
}
// Pseudo-array: "vlans { /Common/v1 /Common/v2 }"
else if (line.includes('{') && line.includes('}') && !line.includes('"')) {
obj[(line.split('{')[0] ?? '').trim()] = objToArr(line);
}
// Single-string property (flag)
else if ((!line.trim().includes(' ') || line.trim().match(/^"[\s\S]*"$/)) && !line.includes('}')) {
obj[line.trim()] = '';
}
// Regular string property on correct indentation level
else if (countIndent(line) === 4) {
// Multiline string handling
const quoteCount = (line.match(/"/g) ?? []).length;
if (quoteCount % 2 === 1) {
let c = 1;
while (arr[i + c] && (arr[i + c]?.match(/"/g) ?? []).length % 2 !== 1) {
c += 1;
}
const chunk = arr.slice(i, i + c + 1);
const subObjArr = arrToMultilineStr(chunk);
obj = Object.assign(obj, subObjArr);
i += c;
} else {
// Standard key-value
const tmp = strToObj(line.trim());
// GTM external monitor user-defined properties
if (key.startsWith('gtm monitor external') && Object.keys(tmp).includes('user-defined')) {
if (!obj['user-defined']) obj['user-defined'] = {};
const tmpObj = strToObj(tmp['user-defined'] ?? '');
obj['user-defined'][Object.keys(tmpObj)[0] ?? ''] = Object.values(tmpObj)[0];
} else {
obj = Object.assign(obj, tmp);
}
}
} else {
logger.debug(`UNRECOGNIZED LINE: '${line}'`);
}
}
}
// If obj is an object (not a string like iRules), add the original body as 'line'
if (typeof obj === 'object' && obj !== null) {
obj._originalBody = originalBody;
}
return { [key]: obj };
}
/**
* Pre-process GTM topology records into standard format
*/
function preprocessTopology(fileArr: string[]): string[] {
const newFileArr: string[] = [];
const topologyArr: string[] = [];
let topologyCount = 0;
let longestMatchEnabled = false;
let inTopology = false;
fileArr.forEach((line) => {
if (line.includes('topology-longest-match') && line.includes('yes')) {
longestMatchEnabled = true;
}
if (line.startsWith('gtm topology ldns:')) {
inTopology = true;
if (topologyArr.length === 0) {
topologyArr.push('gtm topology /Common/Shared/topology {');
topologyArr.push(' records {');
}
const ldnsIndex = line.indexOf('ldns:');
const serverIndex = line.indexOf('server:');
const bracketIndex = line.indexOf('{');
const ldns = line.slice(ldnsIndex + 5, serverIndex).trim();
topologyArr.push(` topology_${topologyCount} {`);
topologyCount += 1;
topologyArr.push(` source ${ldns}`);
const server = line.slice(serverIndex + 7, bracketIndex).trim();
topologyArr.push(` destination ${server}`);
} else if (inTopology) {
if (line === '}') {
inTopology = false;
topologyArr.push(' }');
} else {
topologyArr.push(` ${line}`);
}
} else {
newFileArr.push(line);
}
});
if (topologyArr.length) {
topologyArr.push(` longest-match-enabled ${longestMatchEnabled}`);
topologyArr.push(' }');
topologyArr.push('}');
}
return newFileArr.concat(topologyArr);
}
/**
* Process iRule comments outside of iRules
*/
function preprocessComments(fileArr: string[]): string[] {
let iruleDepth = 0;
return fileArr.map(line => {
if (iruleDepth === 0) {
if (line.trim().startsWith('# ')) {
return line.trim().replace('# ', '#comment# ');
} else if (isRule(line)) {
iruleDepth += 1;
}
} else if (!line.trim().startsWith('#')) {
iruleDepth = iruleDepth + countChar(line, '{') - countChar(line, '}');
}
return line;
});
}
/**
* Convert flat parsed object to hierarchical structure
* "ltm pool /Common/web_pool" → ltm.pool["/Common/web_pool"]
*/
function flatToHierarchical(flat: Record<string, any>): Record<string, any> {
const result: Record<string, any> = {};
for (const [key, value] of Object.entries(flat)) {
const parts = key.split(' ');
if (parts.length < 2) {
// Single word key (shouldn't happen often)
result[key] = value;
continue;
}
const category = parts[0]; // ltm, gtm, sys, net, etc.
// Handle various object path depths
// "ltm virtual /Common/vs" → ltm.virtual["/Common/vs"]
// "ltm profile http /Common/http" → ltm.profile.http["/Common/http"]
// "gtm pool a /Common/pool" → gtm.pool.a["/Common/pool"]
if (!result[category]) result[category] = {};
// Find where the object name starts (starts with /)
let nameIndex = parts.findIndex(p => p.startsWith('/'));
if (nameIndex === -1) {
// No object name found, might be a system setting
// "sys global-settings" → sys["global-settings"]
const restKey = parts.slice(1).join(' ');
result[category][restKey] = value;
continue;
}
// Build the path between category and name
const pathParts = parts.slice(1, nameIndex);
const objectName = parts.slice(nameIndex).join(' ');
// Navigate/create nested structure
let current = result[category];
for (const part of pathParts) {
if (!current[part]) current[part] = {};
current = current[part];
}
// Assign the value with metadata
if (typeof value === 'string') {
// For rules and simple string values
current[objectName] = value;
} else {
// For objects, add metadata
const nameParts = objectName.match(/^(\/[\w\d_\-.]+(?:\/[\w\d_\-.]+)?)\/([\w\d_\-.]+)$/);
// Extract and remove the _originalBody, convert to 'line'
const { _originalBody, ...restValue } = value;
const enhanced: any = {
...restValue,
line: _originalBody || ''
};
if (nameParts) {
enhanced.partition = nameParts[1].split('/')[1];
if (nameParts[1].split('/').length > 2) {
enhanced.folder = nameParts[1].split('/')[2];
}
enhanced.name = nameParts[2];
}
current[objectName] = enhanced;
}
}
return result;
}
/**
* Recursively convert _originalBody to line in nested objects
*/
function cleanupOriginalBody(obj: any): any {
if (typeof obj !== 'object' || obj === null) {
return obj;
}
if (Array.isArray(obj)) {
return obj.map(item => cleanupOriginalBody(item));
}
const result: any = {};
for (const [key, value] of Object.entries(obj)) {
if (key === '_originalBody') {
result.line = value;
} else if (typeof value === 'object' && value !== null) {
result[key] = cleanupOriginalBody(value);
} else {
result[key] = value;
}
}
return result;
}
/**
* Parse TMOS configuration text to hierarchical JSON
*
* @param configText - Raw TMOS configuration text
* @returns Parsed hierarchical JSON object
*/
export function parseConfig(configText: string): Record<string, any> {
try {
// Normalize line endings
const normalized = configText.replace(/\r\n/g, '\n');
let fileArr = normalized.split('\n');
// Pre-process GTM topology
fileArr = preprocessTopology(fileArr);
// Pre-process comments
fileArr = preprocessComments(fileArr);
// Filter empty lines and found comments
fileArr = fileArr.filter(line => !(line === '' || line.trim().startsWith('#comment# ')));
// Group root-level objects
const groupResult = groupObjects(fileArr);
if (groupResult.error) {
logger.error(groupResult.error.message);
throw groupResult.error;
}
// Parse each group
const flatParsed: Record<string, any> = {};
for (const group of groupResult.group) {
const parsed = orchestrate(group);
Object.assign(flatParsed, parsed);
}
// Convert to hierarchical structure
const hierarchical = flatToHierarchical(flatParsed);
// Clean up nested _originalBody -> line
return cleanupOriginalBody(hierarchical);
} catch (e) {
const err = e as Error;
if (err.message.startsWith('iRule parsing error')) {
throw err;
}
err.message = `Error parsing configuration: ${err.message}`;
throw err;
}
}
export default parseConfig;