This repository was archived by the owner on Nov 8, 2024. It is now read-only.
forked from frankie-zeng/node_npcap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpcap.js
More file actions
118 lines (94 loc) · 3.96 KB
/
pcap.js
File metadata and controls
118 lines (94 loc) · 3.96 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
var util = require("util");
var events = require("events");
var binding = require("./build/Release/pcap_binding");
var decode = require("./decode").decode;
var tcp_tracker = require("./tcp_tracker");
var DNSCache = require("./dns_cache");
exports.decode = decode;
exports.TCPTracker = tcp_tracker.TCPTracker;
exports.TCPSession = tcp_tracker.TCPSession;
exports.DNSCache = DNSCache;
// This may be overriden by the user
exports.warningHandler = function warningHandler(x) {
console.warn('warning: %s - this may not actually work', x);
};
function PcapSession(is_live, device_name, filter, buffer_size, snap_length, outfile, is_monitor, buffer_timeout, promiscuous) {
this.is_live = is_live;
this.device_name = device_name;
this.filter = filter || "";
this.buffer_size = buffer_size;
this.snap_length = snap_length;
this.outfile = outfile || "";
this.is_monitor = Boolean(is_monitor);
this.buffer_timeout = buffer_timeout;
this.promiscuous = typeof promiscuous === 'undefined' ? true : promiscuous;
this.link_type = null;
this.opened = null;
this.buf = null;
this.header = null;
this.empty_reads = 0;
this.packets_read = null;
this.session = new binding.PcapSession();
if (typeof this.buffer_size === "number" && !isNaN(this.buffer_size)) {
this.buffer_size = Math.round(this.buffer_size);
} else {
this.buffer_size = 10 * 1024 * 1024; // Default buffer size is 10MB
}
if (typeof this.snap_length === "number" && !isNaN(this.snap_length)) {
this.snap_length = Math.round(this.snap_length);
} else {
this.snap_length = 65535; // Default snap length is 65535
}
if (typeof this.buffer_timeout === "number" && !isNaN(this.buffer_timeout)) {
this.buffer_timeout = Math.round(this.buffer_timeout);
} else {
this.buffer_timeout = 1000; // Default buffer timeout is 1s
}
this.buf = Buffer.alloc(this.snap_length);
this.header = Buffer.alloc(16);
this.session.load_buffer(this.buf, this.header);
const packet_ready = this.on_packet_ready.bind(this);
if (this.is_live) {
this.device_name = this.device_name || binding.default_device();
this.link_type = this.session.open_live(this.device_name, this.filter, this.buffer_size, this.snap_length, this.outfile, packet_ready, this.is_monitor, this.buffer_timeout, exports.warningHandler, this.promiscuous);
} else {
this.link_type = this.session.open_offline(this.device_name, this.filter, this.buffer_size, this.snap_length, this.outfile, packet_ready, this.is_monitor, this.buffer_timeout, exports.warningHandler, this.promiscuous);
}
this.opened = true;
events.EventEmitter.call(this);
}
util.inherits(PcapSession, events.EventEmitter);
exports.lib_version = binding.lib_version();
exports.findalldevs = function () {
return binding.findalldevs();
};
function PacketWithHeader(buf, header, link_type) {
this.buf = buf;
this.header = header;
this.link_type = link_type;
}
PcapSession.prototype.on_packet_ready = function () {
var full_packet = new PacketWithHeader(this.buf, this.header, this.link_type);
this.emit("packet", full_packet);
};
PcapSession.prototype.close = function () {
this.opened = false;
this.removeAllListeners();
this.session.close();
};
PcapSession.prototype.stats = function () {
return this.session.stats();
};
PcapSession.prototype.inject = function (data) {
return this.session.inject(data);
};
exports.Pcap = PcapSession;
exports.PcapSession = PcapSession;
exports.createSession = function (device, options) {
options = options || {};
return new PcapSession(true, device, options.filter, options.buffer_size, options.snap_length, null, options.monitor, options.buffer_timeout, options.promiscuous);
};
exports.createOfflineSession = function (path, options) {
options = options || {};
return new PcapSession(false, path, options.filter, 0, null, null);
};