forked from thinhhoangpham/tcp_timearcs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfolder_loader.js
More file actions
426 lines (379 loc) · 15.3 KB
/
folder_loader.js
File metadata and controls
426 lines (379 loc) · 15.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
/**
* 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}`);
// 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;
}
}
/**
* 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 v2.0 location first (flows/flows_index.json)
try {
const flowsDir = await this.folderHandle.getDirectoryHandle('flows');
const indexFile = await flowsDir.getFileHandle('flows_index.json');
const file = await indexFile.getFile();
const text = await file.text();
this.flowsIndex = JSON.parse(text);
console.log(`Loaded ${this.flowsIndex.length} flow summaries (v2.0 chunked format)`);
return this.flowsIndex;
} catch (err) {
// 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(`Loaded ${this.flowsIndex.length} flow summaries (v1.0 individual format)`);
return this.flowsIndex;
}
} catch (err) {
console.error('Error loading flows index:', err);
throw new Error('Could not load flows_index.json from folder.');
}
}
/**
* 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();