-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtshark.js
91 lines (80 loc) · 2.91 KB
/
tshark.js
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
const { CommandExecutor } = require('./index.js');
async function listInterfaces() {
const executor = new CommandExecutor();
try {
const output = await executor.execute('"C:\\Program Files\\Wireshark\\tshark" -D');
console.log('Available Interfaces:');
console.log(output);
} catch (error) {
console.error('Failed to list interfaces:', error);
}
}
async function capturePackets() {
const executor = new CommandExecutor();
// Setup event listeners
executor.on('error', (error) => console.error('TShark Error:', error));
executor.on('warning', (warning) => console.warn('TShark Warning:', warning));
try {
// First list interfaces
await listInterfaces();
console.log('\nStarting packet capture...');
const capture = await executor.realtimeExecution(
'"C:\\Program Files\\Wireshark\\tshark"',
[
'-i', '5', // Interface number (change as needed)
'-T', 'fields', // Output format
'-E', 'header=y', // Include headers
'-e', 'frame.time', // Timestamp
'-e', 'ip.src', // Source IP
'-e', 'ip.dst', // Destination IP
'-e', 'ip.proto', // Protocol
'-l' // Line-buffered mode
],
{
timeout: 0, // No timeout
shell: true,
encoding: 'utf8'
},
async (data, type) => {
if (type === 'stdout') {
// Process and display packet data
const packetData = data.trim();
if (packetData) {
console.log('Packet:', packetData);
}
} else if (type === 'stderr') {
// Handle errors
console.error('TShark stderr:', data.trim());
}
}
);
console.log('Capture started successfully. Will run for 30 seconds...');
// Stop capture after 30 seconds
setTimeout(async () => {
try {
await capture.stop();
console.log('Capture stopped successfully');
} catch (error) {
console.error('Error stopping capture:', error);
}
}, 10000);
return capture;
} catch (error) {
console.error('Failed to start capture:', error);
throw error;
}
}
async function main() {
try {
console.log('Starting TShark packet capture...');
await capturePackets();
} catch (error) {
console.error('Main execution error:', error);
}
}
// Export functions
module.exports = {
capturePackets,
listInterfaces,
main
};