forked from thinhhoangpham/tcp_analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfolder_loader.js
More file actions
549 lines (480 loc) · 21.1 KB
/
folder_loader.js
File metadata and controls
549 lines (480 loc) · 21.1 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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/**
* Folder-based data loader for TCP TimeArcs
* Handles loading of split file structure:
* - manifest.json: Dataset metadata
* - packets.csv: Packet data for timearcs
* - flows_index.json: Flow summaries
* - flows/*.json: Individual flow files
* - ip_stats.json: IP statistics
* - flag_stats.json: Flag statistics
*/
export class FolderLoader {
constructor() {
this.folderHandle = null;
this.manifest = null;
this.packets = null;
this.flowsIndex = null;
this.ipStats = null;
this.flagStats = null;
this.loadedFlows = new Map(); // Cache for loaded flow files
}
/**
* Open folder picker and load manifest
*/
async openFolder() {
try {
// Use File System Access API to open folder
this.folderHandle = await window.showDirectoryPicker({
mode: 'read'
});
console.log(`Opened folder: ${this.folderHandle.name}`);
// Verify we have read permission (especially important for external drives)
const permission = await this.folderHandle.queryPermission({ mode: 'read' });
console.log(`[FolderLoader] Initial permission status: ${permission}`);
if (permission !== 'granted') {
console.log('[FolderLoader] Requesting read permission...');
const newPermission = await this.folderHandle.requestPermission({ mode: 'read' });
console.log(`[FolderLoader] Permission after request: ${newPermission}`);
if (newPermission !== 'granted') {
throw new Error('Read permission not granted for folder');
}
}
// Debug: List what's accessible in the folder
await this.listFolderContents();
// Load manifest first
await this.loadManifest();
return {
success: true,
folderName: this.folderHandle.name,
manifest: this.manifest
};
} catch (err) {
if (err.name === 'AbortError') {
console.log('Folder selection cancelled');
return { success: false, cancelled: true };
}
console.error('Error opening folder:', err);
throw err;
}
}
/**
* Debug helper: List accessible entries in folder
*/
async listFolderContents(dirHandle = null) {
const handle = dirHandle || this.folderHandle;
const entries = [];
try {
for await (const entry of handle.values()) {
entries.push({
name: entry.name,
kind: entry.kind
});
}
console.log('[FolderLoader] Accessible entries:', entries);
return entries;
} catch (err) {
console.error('[FolderLoader] Error listing folder contents:', err);
return [];
}
}
/**
* Helper: Get subdirectory handle with better error reporting
*/
async getSubdirectory(dirName) {
try {
console.log(`[FolderLoader] Attempting to access subdirectory: ${dirName}`);
const dirHandle = await this.folderHandle.getDirectoryHandle(dirName);
console.log(`[FolderLoader] Successfully accessed subdirectory: ${dirName}`);
// Verify we can read from it by listing contents
const entries = await this.listFolderContents(dirHandle);
console.log(`[FolderLoader] Subdirectory ${dirName} contains ${entries.length} entries`);
return dirHandle;
} catch (err) {
console.error(`[FolderLoader] Failed to access subdirectory ${dirName}:`, err.name, err.message);
throw new Error(`Cannot access subdirectory '${dirName}': ${err.message}`);
}
}
/**
* Load manifest.json from folder
*/
async loadManifest() {
try {
const manifestFile = await this.folderHandle.getFileHandle('manifest.json');
const file = await manifestFile.getFile();
const text = await file.text();
this.manifest = JSON.parse(text);
console.log('Loaded manifest:', this.manifest);
return this.manifest;
} catch (err) {
console.error('Error loading manifest:', err);
throw new Error('Could not load manifest.json from folder. Please ensure you selected a valid data folder.');
}
}
/**
* Load packets.csv for timearcs visualization
*/
async loadPackets(onProgress = null) {
try {
const packetsFile = await this.folderHandle.getFileHandle('packets.csv');
const file = await packetsFile.getFile();
const text = await file.text();
// Parse CSV with progress tracking
this.packets = await this.parseCSVAsync(text, onProgress);
console.log(`Loaded ${this.packets.length} packets`);
return this.packets;
} catch (err) {
console.error('Error loading packets:', err);
throw new Error('Could not load packets.csv from folder.');
}
}
/**
* Load flows_index.json
* Supports both flows/flows_index.json (v2.0) and flows_index.json (v1.0)
*/
async loadFlowsIndex() {
try {
// Try v3.0 chunked index first (best for large datasets)
try {
const flowsDir = await this.getSubdirectory('flows');
const indexChunksDir = await flowsDir.getDirectoryHandle('index_chunks');
const masterFile = await indexChunksDir.getFileHandle('flows_index_master.json');
const file = await masterFile.getFile();
const text = await file.text();
const master = JSON.parse(text);
console.log(`[FolderLoader] Found chunked index (v3.0): ${master.num_chunks} chunks, ${master.total_flows.toLocaleString()} flows`);
console.log(`[FolderLoader] Loading flows progressively from ${master.num_chunks} chunks (${master.chunk_size.toLocaleString()} flows/chunk)...`);
// Load all chunks progressively
this.flowsIndex = [];
const batchSize = 10; // Load 10 chunks at a time
for (let i = 0; i < master.num_chunks; i += batchSize) {
const endIdx = Math.min(i + batchSize, master.num_chunks);
const chunkPromises = [];
for (let j = i; j < endIdx; j++) {
const chunkInfo = master.chunks[j];
const promise = (async () => {
const chunkFile = await indexChunksDir.getFileHandle(chunkInfo.file);
const f = await chunkFile.getFile();
const t = await f.text();
return JSON.parse(t);
})();
chunkPromises.push(promise);
}
const chunks = await Promise.all(chunkPromises);
for (const chunk of chunks) {
this.flowsIndex.push(...chunk);
}
console.log(`[FolderLoader] Loaded ${this.flowsIndex.length.toLocaleString()}/${master.total_flows.toLocaleString()} flows (${Math.round(this.flowsIndex.length / master.total_flows * 100)}%)`);
}
console.log(`[FolderLoader] ✓ Loaded all ${this.flowsIndex.length.toLocaleString()} flow summaries (v3.0 chunked index)`);
return this.flowsIndex;
} catch (err) {
console.warn('[FolderLoader] v3.0 chunked index not found:', err.message);
}
// Fall back to v2.0 monolithic index
try {
const flowsDir = await this.getSubdirectory('flows');
const indexFile = await flowsDir.getFileHandle('flows_index.json');
console.log('[FolderLoader] Successfully got flows_index.json file handle');
const file = await indexFile.getFile();
const fileSizeMB = (file.size / (1024 * 1024)).toFixed(1);
console.log(`[FolderLoader] Reading flows_index.json (${fileSizeMB} MB)...`);
// For large files, add progress indicator
if (file.size > 50 * 1024 * 1024) { // > 50MB
console.warn(`[FolderLoader] Large file detected (${fileSizeMB} MB). This may take a moment...`);
}
const text = await file.text();
console.log(`[FolderLoader] File read complete, parsing JSON...`);
this.flowsIndex = JSON.parse(text);
console.log(`[FolderLoader] Loaded ${this.flowsIndex.length} flow summaries (v2.0 monolithic format)`);
return this.flowsIndex;
} catch (err) {
console.warn('[FolderLoader] v2.0 location failed:', err.message);
// Check if it's a JSON parse error on a large file
if (err.message.includes('JSON') || err.name === 'SyntaxError') {
throw new Error(`flows_index.json is too large or corrupted. Error: ${err.message}. Run create_chunked_flows_index.py to create chunked version.`);
}
console.log('[FolderLoader] Attempting fallback to v1.0 location (root)...');
// Fall back to v1.0 location (flows_index.json at root)
const indexFile = await this.folderHandle.getFileHandle('flows_index.json');
const file = await indexFile.getFile();
const text = await file.text();
this.flowsIndex = JSON.parse(text);
console.log(`[FolderLoader] Loaded ${this.flowsIndex.length} flow summaries (v1.0 individual format)`);
return this.flowsIndex;
}
} catch (err) {
console.error('[FolderLoader] All attempts failed:', err);
throw new Error(`Could not load flows_index.json from folder. Error: ${err.message}`);
}
}
/**
* Load a specific flow by ID
* Supports both chunked (v2.0) and individual (v1.0) formats
*/
async loadFlow(flowId) {
// Check cache first
if (this.loadedFlows.has(flowId)) {
return this.loadedFlows.get(flowId);
}
try {
const flowSummary = this.flowsIndex.find(f => f.id === flowId);
if (!flowSummary) {
throw new Error(`Flow ${flowId} not found in index`);
}
// Check format version
const isChunked = this.manifest?.format === 'chunked' || flowSummary.chunk_file;
if (isChunked) {
// v2.0 chunked format: load from chunk file
return await this.loadFlowFromChunk(flowSummary);
} else {
// v1.0 individual format: load individual file
return await this.loadFlowIndividual(flowId);
}
} catch (err) {
console.error(`Error loading flow ${flowId}:`, err);
throw err;
}
}
/**
* Load flow from chunk file (v2.0 format)
*/
async loadFlowFromChunk(flowSummary) {
const chunkFile = flowSummary.chunk_file;
const chunkIndex = flowSummary.chunk_index;
// Check if chunk is already cached
const cacheKey = `chunk:${chunkFile}`;
if (this.loadedFlows.has(cacheKey)) {
const chunk = this.loadedFlows.get(cacheKey);
const flow = chunk[chunkIndex];
this.loadedFlows.set(flowSummary.id, flow);
return flow;
}
// Load chunk file
try {
const flowsDir = await this.folderHandle.getDirectoryHandle('flows');
const file = await flowsDir.getFileHandle(chunkFile);
const fileObj = await file.getFile();
const text = await fileObj.text();
const chunk = JSON.parse(text);
// Cache the entire chunk
this.loadedFlows.set(cacheKey, chunk);
// Get specific flow from chunk
const flow = chunk[chunkIndex];
this.loadedFlows.set(flowSummary.id, flow);
return flow;
} catch (err) {
console.error(`Error loading chunk ${chunkFile}:`, err);
throw new Error(`Could not load chunk file: ${chunkFile}`);
}
}
/**
* Load individual flow file (v1.0 format - backward compatibility)
*/
async loadFlowIndividual(flowId) {
try {
const flowsDir = await this.folderHandle.getDirectoryHandle('flows');
const flowFile = await flowsDir.getFileHandle(`${flowId}.json`);
const file = await flowFile.getFile();
const text = await file.text();
const flow = JSON.parse(text);
// Cache the flow
this.loadedFlows.set(flowId, flow);
return flow;
} catch (err) {
console.error(`Error loading flow ${flowId}:`, err);
throw new Error(`Could not load flow file: ${flowId}.json`);
}
}
/**
* Load multiple flows by IDs
*/
async loadFlows(flowIds) {
const flows = [];
for (const flowId of flowIds) {
try {
const flow = await this.loadFlow(flowId);
flows.push(flow);
} catch (err) {
console.warn(`Failed to load flow ${flowId}:`, err);
}
}
return flows;
}
/**
* Load IP statistics
* Supports both ips/ip_stats.json (v2.0) and ip_stats.json (v1.0)
*/
async loadIPStats() {
try {
// Try v2.0 location first (ips/ip_stats.json)
try {
const ipsDir = await this.folderHandle.getDirectoryHandle('ips');
const statsFile = await ipsDir.getFileHandle('ip_stats.json');
const file = await statsFile.getFile();
const text = await file.text();
this.ipStats = JSON.parse(text);
console.log(`Loaded IP statistics for ${Object.keys(this.ipStats).length} IPs`);
return this.ipStats;
} catch (err) {
// Fall back to v1.0 location (ip_stats.json at root)
const statsFile = await this.folderHandle.getFileHandle('ip_stats.json');
const file = await statsFile.getFile();
const text = await file.text();
this.ipStats = JSON.parse(text);
console.log(`Loaded IP statistics for ${Object.keys(this.ipStats).length} IPs`);
return this.ipStats;
}
} catch (err) {
console.error('Error loading IP stats:', err);
throw new Error('Could not load ip_stats.json from folder.');
}
}
/**
* Load flag statistics
* Supports both ips/flag_stats.json (v2.0) and flag_stats.json (v1.0)
*/
async loadFlagStats() {
try {
// Try v2.0 location first (ips/flag_stats.json)
try {
const ipsDir = await this.folderHandle.getDirectoryHandle('ips');
const statsFile = await ipsDir.getFileHandle('flag_stats.json');
const file = await statsFile.getFile();
const text = await file.text();
this.flagStats = JSON.parse(text);
console.log('Loaded flag statistics:', this.flagStats);
return this.flagStats;
} catch (err) {
// Fall back to v1.0 location (flag_stats.json at root)
const statsFile = await this.folderHandle.getFileHandle('flag_stats.json');
const file = await statsFile.getFile();
const text = await file.text();
this.flagStats = JSON.parse(text);
console.log('Loaded flag statistics:', this.flagStats);
return this.flagStats;
}
} catch (err) {
console.error('Error loading flag stats:', err);
throw new Error('Could not load flag_stats.json from folder.');
}
}
/**
* Filter flows index by selected IPs
*/
filterFlowsByIPs(selectedIPs) {
if (!this.flowsIndex) {
return [];
}
const ipSet = new Set(selectedIPs);
return this.flowsIndex.filter(flow => {
return ipSet.has(flow.initiator) && ipSet.has(flow.responder);
});
}
/**
* Get flows within a time range
*/
filterFlowsByTimeRange(startTime, endTime) {
if (!this.flowsIndex) {
return [];
}
return this.flowsIndex.filter(flow => {
return flow.startTime >= startTime && flow.endTime <= endTime;
});
}
/**
* Parse CSV asynchronously with progress tracking
*/
async parseCSVAsync(csvText, onProgress = null) {
return new Promise((resolve, reject) => {
try {
const lines = csvText.split('\n');
const headers = lines[0].split(',').map(h => h.trim());
const records = [];
const totalLines = lines.length - 1;
let processedLines = 0;
// Process in chunks to allow UI updates
const CHUNK_SIZE = 1000;
let currentLine = 1;
const processChunk = () => {
const endLine = Math.min(currentLine + CHUNK_SIZE, lines.length);
for (let i = currentLine; i < endLine; i++) {
const line = lines[i].trim();
if (!line) continue;
const values = line.split(',');
const record = {};
headers.forEach((header, idx) => {
const value = values[idx] ? values[idx].trim() : '';
// Parse specific fields
if (['timestamp', 'src_port', 'dst_port', 'flags', 'seq_num', 'ack_num', 'length', 'protocol'].includes(header)) {
record[header] = value ? parseInt(value) : 0;
} else if (header === 'flags') {
// Parse flag bits
const flagBits = parseInt(value) || 0;
record.flags = {
fin: (flagBits & 0x01) !== 0,
syn: (flagBits & 0x02) !== 0,
rst: (flagBits & 0x04) !== 0,
psh: (flagBits & 0x08) !== 0,
ack: (flagBits & 0x10) !== 0,
urg: (flagBits & 0x20) !== 0,
ece: (flagBits & 0x40) !== 0,
cwr: (flagBits & 0x80) !== 0
};
} else {
record[header] = value;
}
});
records.push(record);
processedLines++;
}
// Report progress
if (onProgress) {
const progress = (processedLines / totalLines) * 100;
onProgress(progress, processedLines, totalLines);
}
currentLine = endLine;
if (currentLine < lines.length) {
// Schedule next chunk
setTimeout(processChunk, 0);
} else {
// Done
resolve(records);
}
};
// Start processing
processChunk();
} catch (err) {
reject(err);
}
});
}
/**
* Clear all cached data
*/
clear() {
this.folderHandle = null;
this.manifest = null;
this.packets = null;
this.flowsIndex = null;
this.ipStats = null;
this.flagStats = null;
this.loadedFlows.clear();
}
/**
* Get summary information
*/
getSummary() {
return {
folderOpen: this.folderHandle !== null,
folderName: this.folderHandle?.name || null,
manifest: this.manifest,
packetsLoaded: this.packets?.length || 0,
flowsIndexLoaded: this.flowsIndex?.length || 0,
flowsCached: this.loadedFlows.size,
ipStatsLoaded: this.ipStats ? Object.keys(this.ipStats).length : 0,
flagStatsLoaded: this.flagStats ? Object.keys(this.flagStats).length : 0
};
}
}
// Export singleton instance
export const folderLoader = new FolderLoader();