@cto.af/pcap-ng-parser 3.0.2 → 3.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -11
- package/lib/blocks.d.ts +148 -0
- package/lib/blocks.js +77 -0
- package/lib/events.d.ts +108 -0
- package/lib/events.js +61 -0
- package/lib/index.d.ts +117 -0
- package/lib/index.js +705 -0
- package/lib/linkTypes.d.ts +6 -0
- package/lib/linkTypes.js +223 -0
- package/lib/options.d.ts +13 -0
- package/lib/options.js +90 -0
- package/lib/utils.d.ts +14 -0
- package/lib/utils.js +20 -0
- package/package.json +7 -7
- package/lib/blocks.mjs +0 -1
- package/lib/events.d.mts +0 -97
- package/lib/events.mjs +0 -1
- package/lib/index.d.mts +0 -119
- package/lib/index.mjs +0 -1
- package/lib/linkTypes.mjs +0 -1
- package/lib/options.d.mts +0 -4
- package/lib/options.mjs +0 -1
- package/lib/utils.mjs +0 -1
package/README.md
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
|
|
2
2
|
# Overview
|
|
3
|
+
|
|
3
4
|
@cto.af/pcap-ng-parser is a stream-based module to decode, print and analyze
|
|
4
5
|
network traffic packets. With this module, you can read from an existing
|
|
5
6
|
.pcap or .pcapng file or connect it to an active stream.
|
|
6
7
|
|
|
7
8
|
Implements:
|
|
9
|
+
|
|
8
10
|
- [draft-ietf-opsawg-pcap-06](https://www.ietf.org/archive/id/draft-ietf-opsawg-pcap-06.html)
|
|
9
11
|
- [draft-ietf-opsawg-pcapng-04](https://www.ietf.org/archive/id/draft-ietf-opsawg-pcapng-04.html)
|
|
10
12
|
|
|
11
|
-
|
|
13
|
+
## Installation
|
|
12
14
|
|
|
13
15
|
This module is available through the [npm registry](https://www.npmjs.com/).
|
|
14
16
|
|
|
@@ -16,13 +18,14 @@ This module is available through the [npm registry](https://www.npmjs.com/).
|
|
|
16
18
|
$ npm install @cto.af/pcap-ng-parser
|
|
17
19
|
```
|
|
18
20
|
|
|
19
|
-
|
|
21
|
+
## Usage
|
|
20
22
|
|
|
21
23
|
Note that the package on its own should work just fine in any modern JS
|
|
22
24
|
runtime (including browsers). However, reading from a file requires some
|
|
23
25
|
runtime-dependent code.
|
|
24
26
|
|
|
25
|
-
|
|
27
|
+
### Via .pcapng File for Node.js
|
|
28
|
+
|
|
26
29
|
Here is a quick example of how to log out packets to the console from a valid .pcapng file named `myfile.pcapng`.
|
|
27
30
|
|
|
28
31
|
```javascript
|
|
@@ -44,7 +47,7 @@ file.readableWebStream().pipeTo(parser);
|
|
|
44
47
|
In the example above, we create a new ReadableStream from our file and pipe to
|
|
45
48
|
the instance `parser` which will cause various events to fire.
|
|
46
49
|
|
|
47
|
-
|
|
50
|
+
### Via .pcapng file in Deno
|
|
48
51
|
|
|
49
52
|
```js
|
|
50
53
|
import {PCAPNGParser} from '@cto.af/pcap-ng-parser';
|
|
@@ -59,7 +62,7 @@ const f = await Deno.open('examples/res/myfile.pcapng');
|
|
|
59
62
|
f.readable.pipeTo(pcapNgParser);
|
|
60
63
|
```
|
|
61
64
|
|
|
62
|
-
|
|
65
|
+
### Via .pcapng file in Bun
|
|
63
66
|
|
|
64
67
|
```js
|
|
65
68
|
import {PCAPNGParser} from './src/index.ts';
|
|
@@ -77,7 +80,7 @@ const f = Bun.file('examples/res/myfile.pcapng');
|
|
|
77
80
|
f.stream().pipeTo(pcapNgParser);
|
|
78
81
|
```
|
|
79
82
|
|
|
80
|
-
|
|
83
|
+
### Via TCPDump in Node.JS
|
|
81
84
|
|
|
82
85
|
You can also pipe from TCPDump using `process.stdin` for a command line interaction.
|
|
83
86
|
|
|
@@ -113,7 +116,7 @@ It allows you to read from a sample capture file (the default), a file (if you
|
|
|
113
116
|
specify a file name on the command line), or stdin (if you specify "-" as the
|
|
114
117
|
input file).
|
|
115
118
|
|
|
116
|
-
|
|
119
|
+
## Class PCAPNGParser
|
|
117
120
|
|
|
118
121
|
PCAPNGParser is an extension of the [stream.Transform
|
|
119
122
|
class](https://nodejs.org/api/stream.html#stream_class_stream_transform). The
|
|
@@ -131,17 +134,17 @@ PCAPNGParser class fires the following events:
|
|
|
131
134
|
See the full [API documentation](https://cto-af.github.io/pcap-ng-parser/) for
|
|
132
135
|
the types of the event parameters.
|
|
133
136
|
|
|
134
|
-
|
|
137
|
+
## Contribution
|
|
135
138
|
|
|
136
139
|
Refer to the the [Contribution Guide](/docs/CONTRIBUTING.md) for details on how to contribute.
|
|
137
140
|
|
|
138
|
-
|
|
141
|
+
## License
|
|
139
142
|
|
|
140
143
|
This module is covered under the BSD-3 Open Software License. Review the [License Documention](/docs/LICENSE.md) for more information.
|
|
141
144
|
|
|
142
|
-
|
|
145
|
+
## Provenance
|
|
143
146
|
|
|
144
|
-
This code was forked from https://github.com/CollinearGroup/pcap-ng-parser due
|
|
147
|
+
This code was forked from [pcap-ng-parser](https://github.com/CollinearGroup/pcap-ng-parser) due
|
|
145
148
|
to lack of maintenance. To simplify matters, copyright remains with the
|
|
146
149
|
original authors, including all changes made in this repository. If the
|
|
147
150
|
original authors contact me (easiest would be to file an issue here), all of
|
package/lib/blocks.d.ts
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @see documentation at https://www.ietf.org/archive/id/draft-ietf-opsawg-pcapng-04.html#name-section-header-block
|
|
3
|
+
*/
|
|
4
|
+
export declare const sectionHeaderBlock: {
|
|
5
|
+
readonly byteOrderMagic: {
|
|
6
|
+
readonly read: 'u32';
|
|
7
|
+
};
|
|
8
|
+
readonly majorVersion: {
|
|
9
|
+
readonly read: 'u16';
|
|
10
|
+
};
|
|
11
|
+
readonly minorVersion: {
|
|
12
|
+
readonly read: 'u16';
|
|
13
|
+
};
|
|
14
|
+
readonly sectionLength: {
|
|
15
|
+
readonly read: 'i64';
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* @see definition at https://www.ietf.org/archive/id/draft-ietf-opsawg-pcapng-04.html#name-enhanced-packet-block
|
|
20
|
+
*/
|
|
21
|
+
export declare const blockType: {
|
|
22
|
+
readonly blockType: {
|
|
23
|
+
readonly read: 'i32';
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* @see definition at https://www.ietf.org/archive/id/draft-ietf-opsawg-pcapng-04.html#name-interface-description-block
|
|
28
|
+
*/
|
|
29
|
+
export declare const interfaceDescriptionBlockFormat: {
|
|
30
|
+
readonly linkType: {
|
|
31
|
+
readonly read: 'u16';
|
|
32
|
+
};
|
|
33
|
+
readonly reserved: {
|
|
34
|
+
readonly read: 'u16';
|
|
35
|
+
};
|
|
36
|
+
readonly snapLen: {
|
|
37
|
+
readonly read: 'u32';
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
export declare const nameResolutionFormat: {
|
|
41
|
+
readonly recordType: {
|
|
42
|
+
readonly read: 'u16';
|
|
43
|
+
};
|
|
44
|
+
readonly recordValueLength: {
|
|
45
|
+
readonly read: 'u16';
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* @see definition at https://www.ietf.org/archive/id/draft-ietf-opsawg-pcapng-04.html#name-enhanced-packet-block
|
|
50
|
+
*/
|
|
51
|
+
export declare const enhancedPacketBlockFormat: {
|
|
52
|
+
readonly interfaceId: {
|
|
53
|
+
readonly read: 'u32';
|
|
54
|
+
};
|
|
55
|
+
readonly timestampHigh: {
|
|
56
|
+
readonly read: 'u32';
|
|
57
|
+
};
|
|
58
|
+
readonly timestampLow: {
|
|
59
|
+
readonly read: 'u32';
|
|
60
|
+
};
|
|
61
|
+
readonly capturedPacketLength: {
|
|
62
|
+
readonly read: 'u32';
|
|
63
|
+
};
|
|
64
|
+
readonly originalPacketLength: {
|
|
65
|
+
readonly read: 'u32';
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* @see definition at https://www.ietf.org/archive/id/draft-ietf-opsawg-pcapng-04.html#name-options
|
|
70
|
+
*/
|
|
71
|
+
export declare const optionBlock: {
|
|
72
|
+
readonly optionType: {
|
|
73
|
+
readonly read: 'u16';
|
|
74
|
+
};
|
|
75
|
+
readonly dataLength: {
|
|
76
|
+
readonly read: 'u16';
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
export declare const interfaceStatisticsFormat: {
|
|
80
|
+
readonly interfaceId: {
|
|
81
|
+
readonly read: 'u32';
|
|
82
|
+
};
|
|
83
|
+
readonly timestampHigh: {
|
|
84
|
+
readonly read: 'u32';
|
|
85
|
+
};
|
|
86
|
+
readonly timestampLow: {
|
|
87
|
+
readonly read: 'u32';
|
|
88
|
+
};
|
|
89
|
+
};
|
|
90
|
+
export declare const decryptionSecretsFormat: {
|
|
91
|
+
readonly secretsType: {
|
|
92
|
+
readonly read: 'u32';
|
|
93
|
+
};
|
|
94
|
+
readonly secretsLength: {
|
|
95
|
+
readonly read: 'u32';
|
|
96
|
+
};
|
|
97
|
+
};
|
|
98
|
+
export declare const ifTsOffsetFormat: {
|
|
99
|
+
tsoffset: {
|
|
100
|
+
size: number;
|
|
101
|
+
sign: boolean;
|
|
102
|
+
};
|
|
103
|
+
};
|
|
104
|
+
export declare const timestampFormat: {
|
|
105
|
+
readonly timestampHigh: {
|
|
106
|
+
readonly read: 'u32';
|
|
107
|
+
};
|
|
108
|
+
readonly timestampLow: {
|
|
109
|
+
readonly read: 'u32';
|
|
110
|
+
};
|
|
111
|
+
};
|
|
112
|
+
export declare const pcapHeaderFormat: {
|
|
113
|
+
readonly byteOrderMagic: {
|
|
114
|
+
readonly read: 'u32';
|
|
115
|
+
};
|
|
116
|
+
readonly majorVersion: {
|
|
117
|
+
readonly read: 'u16';
|
|
118
|
+
};
|
|
119
|
+
readonly minorVersion: {
|
|
120
|
+
readonly read: 'u16';
|
|
121
|
+
};
|
|
122
|
+
readonly _reserved1: {
|
|
123
|
+
readonly read: 'u32';
|
|
124
|
+
};
|
|
125
|
+
readonly _reserved2: {
|
|
126
|
+
readonly read: 'u32';
|
|
127
|
+
};
|
|
128
|
+
readonly snapLen: {
|
|
129
|
+
readonly read: 'u32';
|
|
130
|
+
};
|
|
131
|
+
readonly linkType: {
|
|
132
|
+
readonly read: 'u32';
|
|
133
|
+
};
|
|
134
|
+
};
|
|
135
|
+
export declare const pcapPacketFormat: {
|
|
136
|
+
readonly timestampHigh: {
|
|
137
|
+
readonly read: 'u32';
|
|
138
|
+
};
|
|
139
|
+
readonly timestampLow: {
|
|
140
|
+
readonly read: 'u32';
|
|
141
|
+
};
|
|
142
|
+
readonly capturedPacketLength: {
|
|
143
|
+
readonly read: 'u32';
|
|
144
|
+
};
|
|
145
|
+
readonly originalPacketLength: {
|
|
146
|
+
readonly read: 'u32';
|
|
147
|
+
};
|
|
148
|
+
};
|
package/lib/blocks.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @see documentation at https://www.ietf.org/archive/id/draft-ietf-opsawg-pcapng-04.html#name-section-header-block
|
|
3
|
+
*/
|
|
4
|
+
export const sectionHeaderBlock = {
|
|
5
|
+
byteOrderMagic: { read: 'u32' },
|
|
6
|
+
majorVersion: { read: 'u16' },
|
|
7
|
+
minorVersion: { read: 'u16' },
|
|
8
|
+
sectionLength: { read: 'i64' },
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* @see definition at https://www.ietf.org/archive/id/draft-ietf-opsawg-pcapng-04.html#name-enhanced-packet-block
|
|
12
|
+
*/
|
|
13
|
+
export const blockType = {
|
|
14
|
+
blockType: { read: 'i32' },
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* @see definition at https://www.ietf.org/archive/id/draft-ietf-opsawg-pcapng-04.html#name-interface-description-block
|
|
18
|
+
*/
|
|
19
|
+
export const interfaceDescriptionBlockFormat = {
|
|
20
|
+
linkType: { read: 'u16' },
|
|
21
|
+
reserved: { read: 'u16' },
|
|
22
|
+
snapLen: { read: 'u32' },
|
|
23
|
+
};
|
|
24
|
+
export const nameResolutionFormat = {
|
|
25
|
+
recordType: { read: 'u16' },
|
|
26
|
+
recordValueLength: { read: 'u16' },
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* @see definition at https://www.ietf.org/archive/id/draft-ietf-opsawg-pcapng-04.html#name-enhanced-packet-block
|
|
30
|
+
*/
|
|
31
|
+
export const enhancedPacketBlockFormat = {
|
|
32
|
+
interfaceId: { read: 'u32' },
|
|
33
|
+
timestampHigh: { read: 'u32' },
|
|
34
|
+
timestampLow: { read: 'u32' },
|
|
35
|
+
capturedPacketLength: { read: 'u32' },
|
|
36
|
+
originalPacketLength: { read: 'u32' },
|
|
37
|
+
/* Packet Data */
|
|
38
|
+
/* Options */
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* @see definition at https://www.ietf.org/archive/id/draft-ietf-opsawg-pcapng-04.html#name-options
|
|
42
|
+
*/
|
|
43
|
+
export const optionBlock = {
|
|
44
|
+
optionType: { read: 'u16' },
|
|
45
|
+
dataLength: { read: 'u16' },
|
|
46
|
+
};
|
|
47
|
+
export const interfaceStatisticsFormat = {
|
|
48
|
+
interfaceId: { read: 'u32' },
|
|
49
|
+
timestampHigh: { read: 'u32' },
|
|
50
|
+
timestampLow: { read: 'u32' },
|
|
51
|
+
};
|
|
52
|
+
export const decryptionSecretsFormat = {
|
|
53
|
+
secretsType: { read: 'u32' },
|
|
54
|
+
secretsLength: { read: 'u32' },
|
|
55
|
+
};
|
|
56
|
+
export const ifTsOffsetFormat = {
|
|
57
|
+
tsoffset: { size: 8, sign: true },
|
|
58
|
+
};
|
|
59
|
+
export const timestampFormat = {
|
|
60
|
+
timestampHigh: { read: 'u32' },
|
|
61
|
+
timestampLow: { read: 'u32' },
|
|
62
|
+
};
|
|
63
|
+
export const pcapHeaderFormat = {
|
|
64
|
+
byteOrderMagic: { read: 'u32' },
|
|
65
|
+
majorVersion: { read: 'u16' },
|
|
66
|
+
minorVersion: { read: 'u16' },
|
|
67
|
+
_reserved1: { read: 'u32' },
|
|
68
|
+
_reserved2: { read: 'u32' },
|
|
69
|
+
snapLen: { read: 'u32' },
|
|
70
|
+
linkType: { read: 'u32' },
|
|
71
|
+
};
|
|
72
|
+
export const pcapPacketFormat = {
|
|
73
|
+
timestampHigh: { read: 'u32' },
|
|
74
|
+
timestampLow: { read: 'u32' },
|
|
75
|
+
capturedPacketLength: { read: 'u32' },
|
|
76
|
+
originalPacketLength: { read: 'u32' },
|
|
77
|
+
};
|
package/lib/events.d.ts
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
export declare const EPB_FLAGS_DIRECTIONS: readonly ['notAvailable', 'inbound', 'outbound', 'invalid'];
|
|
2
|
+
export declare const EPB_FLAGS_RECEPTION: readonly ['notSpecified', 'unicast', 'multicast', 'broadcast', 'promiscuous', 'invalid', 'invalid', 'invalid'];
|
|
3
|
+
export declare const EPB_FLAGS_LINK_LAYER_ERRORS: readonly ['symbol', 'preamble', 'startFrameDelimiter', 'unalignedFrame', 'wrongInterFrameGap', 'packetTooShort', 'packetTooLong', 'CRC', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'];
|
|
4
|
+
export interface GenericOption {
|
|
5
|
+
optionType: number;
|
|
6
|
+
dataLength?: number;
|
|
7
|
+
data?: Uint8Array;
|
|
8
|
+
str?: string;
|
|
9
|
+
bigint?: bigint;
|
|
10
|
+
date?: Date;
|
|
11
|
+
name?: string;
|
|
12
|
+
private?: boolean;
|
|
13
|
+
}
|
|
14
|
+
export interface PrivateEnterpriseNumber {
|
|
15
|
+
pen?: number;
|
|
16
|
+
}
|
|
17
|
+
export type Option = GenericOption & PrivateEnterpriseNumber;
|
|
18
|
+
export interface PacketFlags {
|
|
19
|
+
direction?: typeof EPB_FLAGS_DIRECTIONS[number];
|
|
20
|
+
reception?: typeof EPB_FLAGS_RECEPTION[number];
|
|
21
|
+
FCSlen?: number;
|
|
22
|
+
noChecksum?: boolean;
|
|
23
|
+
checksumValid?: boolean;
|
|
24
|
+
TCPsegmentationOffload?: boolean;
|
|
25
|
+
linkLayerErrors?: typeof EPB_FLAGS_LINK_LAYER_ERRORS[number][];
|
|
26
|
+
}
|
|
27
|
+
export interface Packet {
|
|
28
|
+
interfaceId: number;
|
|
29
|
+
timestamp?: Date;
|
|
30
|
+
flags?: PacketFlags;
|
|
31
|
+
originalPacketLength: number;
|
|
32
|
+
data: Uint8Array;
|
|
33
|
+
options: Option[];
|
|
34
|
+
}
|
|
35
|
+
export interface Interface {
|
|
36
|
+
/**
|
|
37
|
+
* @see See https://datatracker.ietf.org/doc/html/draft-ietf-opsawg-pcaplinktype for info.
|
|
38
|
+
*/
|
|
39
|
+
linkType: number;
|
|
40
|
+
linkTypeName?: string;
|
|
41
|
+
snapLen: number;
|
|
42
|
+
name?: string;
|
|
43
|
+
tsoffset: bigint;
|
|
44
|
+
tsresol: bigint;
|
|
45
|
+
options: Option[];
|
|
46
|
+
}
|
|
47
|
+
export type Endianess = 'BE' | 'LE';
|
|
48
|
+
export interface SectionHeader {
|
|
49
|
+
endianess: Endianess;
|
|
50
|
+
majorVersion: number;
|
|
51
|
+
minorVersion: number;
|
|
52
|
+
sectionLength: bigint;
|
|
53
|
+
options: Option[];
|
|
54
|
+
}
|
|
55
|
+
export interface IPv4ResolutionRecord {
|
|
56
|
+
name: 'nrb_record_ipv4';
|
|
57
|
+
ipv4: string;
|
|
58
|
+
entries: string[];
|
|
59
|
+
}
|
|
60
|
+
export interface IPv6ResolutionRecord {
|
|
61
|
+
name: 'nrb_record_ipv6';
|
|
62
|
+
ipv6: string;
|
|
63
|
+
entries: string[];
|
|
64
|
+
}
|
|
65
|
+
export interface EUI48ResolutionRecord {
|
|
66
|
+
name: 'nrb_record_eui48';
|
|
67
|
+
eui48: string;
|
|
68
|
+
entries: string[];
|
|
69
|
+
}
|
|
70
|
+
export interface EUI64ResolutionRecord {
|
|
71
|
+
name: 'nrb_record_eui64';
|
|
72
|
+
eui64: string;
|
|
73
|
+
entries: string[];
|
|
74
|
+
}
|
|
75
|
+
export type NameResolutionRecord = IPv4ResolutionRecord | IPv6ResolutionRecord | EUI48ResolutionRecord | EUI64ResolutionRecord;
|
|
76
|
+
export interface NameResolution {
|
|
77
|
+
records: NameResolutionRecord[];
|
|
78
|
+
options: Option[];
|
|
79
|
+
}
|
|
80
|
+
export interface InterfaceStatistics {
|
|
81
|
+
interfaceId: number;
|
|
82
|
+
timestamp: Date;
|
|
83
|
+
options: Option[];
|
|
84
|
+
}
|
|
85
|
+
export interface DecryptionSecrets {
|
|
86
|
+
secretsType: number;
|
|
87
|
+
data: Uint8Array;
|
|
88
|
+
options: Option[];
|
|
89
|
+
}
|
|
90
|
+
export interface CustomBlock {
|
|
91
|
+
pen: number;
|
|
92
|
+
data: Uint8Array;
|
|
93
|
+
copy: boolean;
|
|
94
|
+
}
|
|
95
|
+
export declare class ErrorEventPolyfill extends Event implements ErrorEvent {
|
|
96
|
+
#private;
|
|
97
|
+
constructor(type: string, eventInitDict?: ErrorEventInit);
|
|
98
|
+
get message(): string;
|
|
99
|
+
get filename(): string;
|
|
100
|
+
get lineno(): number;
|
|
101
|
+
get colno(): number;
|
|
102
|
+
get error(): any;
|
|
103
|
+
}
|
|
104
|
+
declare const LocalErrorEvent: {
|
|
105
|
+
new (type: string, eventInitDict?: ErrorEventInit): ErrorEvent;
|
|
106
|
+
prototype: ErrorEvent;
|
|
107
|
+
};
|
|
108
|
+
export { LocalErrorEvent as ErrorEvent, };
|
package/lib/events.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
export const EPB_FLAGS_DIRECTIONS = [
|
|
2
|
+
'notAvailable',
|
|
3
|
+
'inbound',
|
|
4
|
+
'outbound',
|
|
5
|
+
'invalid',
|
|
6
|
+
];
|
|
7
|
+
export const EPB_FLAGS_RECEPTION = [
|
|
8
|
+
'notSpecified',
|
|
9
|
+
'unicast',
|
|
10
|
+
'multicast',
|
|
11
|
+
'broadcast',
|
|
12
|
+
'promiscuous',
|
|
13
|
+
'invalid',
|
|
14
|
+
'invalid',
|
|
15
|
+
'invalid',
|
|
16
|
+
];
|
|
17
|
+
export const EPB_FLAGS_LINK_LAYER_ERRORS = [
|
|
18
|
+
'symbol',
|
|
19
|
+
'preamble',
|
|
20
|
+
'startFrameDelimiter',
|
|
21
|
+
'unalignedFrame',
|
|
22
|
+
'wrongInterFrameGap',
|
|
23
|
+
'packetTooShort',
|
|
24
|
+
'packetTooLong',
|
|
25
|
+
'CRC',
|
|
26
|
+
'invalid',
|
|
27
|
+
'invalid',
|
|
28
|
+
'invalid',
|
|
29
|
+
'invalid',
|
|
30
|
+
'invalid',
|
|
31
|
+
'invalid',
|
|
32
|
+
'invalid',
|
|
33
|
+
'invalid',
|
|
34
|
+
];
|
|
35
|
+
// Needed before node 25.0.0.
|
|
36
|
+
// eslint-disable-next-line n/no-unsupported-features/node-builtins
|
|
37
|
+
export class ErrorEventPolyfill extends Event {
|
|
38
|
+
#eventInit;
|
|
39
|
+
constructor(type, eventInitDict = {}) {
|
|
40
|
+
super(type, eventInitDict);
|
|
41
|
+
this.#eventInit = eventInitDict;
|
|
42
|
+
}
|
|
43
|
+
get message() {
|
|
44
|
+
return this.#eventInit.message ?? '';
|
|
45
|
+
}
|
|
46
|
+
get filename() {
|
|
47
|
+
return this.#eventInit.filename ?? '';
|
|
48
|
+
}
|
|
49
|
+
get lineno() {
|
|
50
|
+
return this.#eventInit.lineno ?? 0;
|
|
51
|
+
}
|
|
52
|
+
get colno() {
|
|
53
|
+
return this.#eventInit.colno ?? 0;
|
|
54
|
+
}
|
|
55
|
+
get error() {
|
|
56
|
+
return this.#eventInit.error;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
// eslint-disable-next-line n/no-unsupported-features/node-builtins
|
|
60
|
+
const LocalErrorEvent = globalThis.ErrorEvent ?? ErrorEventPolyfill;
|
|
61
|
+
export { LocalErrorEvent as ErrorEvent, };
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { type OptionType } from './options.ts';
|
|
2
|
+
import { type CustomBlock, type DecryptionSecrets, type EUI48ResolutionRecord, type EUI64ResolutionRecord, type Endianess, type GenericOption, type IPv4ResolutionRecord, type IPv6ResolutionRecord, type Interface, type InterfaceStatistics, type NameResolution, type NameResolutionRecord, type Option, type Packet, type PacketFlags, type PrivateEnterpriseNumber, type SectionHeader } from './events.ts';
|
|
3
|
+
export type { CustomBlock, DecryptionSecrets, EUI48ResolutionRecord, EUI64ResolutionRecord, GenericOption, Endianess, Interface, IPv4ResolutionRecord, IPv6ResolutionRecord, InterfaceStatistics, NameResolution, NameResolutionRecord, Option, OptionType, Packet, PacketFlags, PrivateEnterpriseNumber, SectionHeader, };
|
|
4
|
+
export type EventCallback<T> = ((evt: T) => void) | {
|
|
5
|
+
handleEvent(evt: T): void;
|
|
6
|
+
} | null;
|
|
7
|
+
/**
|
|
8
|
+
* An error has occured when parsing inside a block. This error can be
|
|
9
|
+
* ignored and parsing can continue within the next block safely.
|
|
10
|
+
*/
|
|
11
|
+
export declare class RecoverableError extends Error {
|
|
12
|
+
constructor(err: unknown);
|
|
13
|
+
}
|
|
14
|
+
export interface ParseEvents {
|
|
15
|
+
close: Event;
|
|
16
|
+
custom: CustomEvent<CustomBlock>;
|
|
17
|
+
error: ErrorEvent;
|
|
18
|
+
interface: CustomEvent<Interface>;
|
|
19
|
+
names: CustomEvent<NameResolution>;
|
|
20
|
+
data: CustomEvent<Packet>;
|
|
21
|
+
section: CustomEvent<SectionHeader>;
|
|
22
|
+
secrets: CustomEvent<DecryptionSecrets>;
|
|
23
|
+
stats: CustomEvent<InterfaceStatistics>;
|
|
24
|
+
unknown: CustomEvent<number>;
|
|
25
|
+
}
|
|
26
|
+
export type ParseEventsDetail<K extends keyof ParseEvents> = ParseEvents[K] extends CustomEvent<infer T> ? T : ParseEvents[K] extends ErrorEvent ? any : void;
|
|
27
|
+
export interface ParserOptions {
|
|
28
|
+
/** If true, don't allow old PCAP format. */
|
|
29
|
+
rejectOld?: boolean;
|
|
30
|
+
/** If true, don't allow new PCAPng format. */
|
|
31
|
+
rejectNG?: boolean;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Parse a PCAPng or old-style PCAP file from a stream.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```js
|
|
38
|
+
* import fs from 'node:fs/promises';
|
|
39
|
+
* import {PCAPNGParser} from '@cto.af/pcap-ng-parser';
|
|
40
|
+
*
|
|
41
|
+
* const parser = new PCAPNGParser();
|
|
42
|
+
* parser.on('data', pkt => console.log(pkt.data)); // Uint8Array
|
|
43
|
+
* const file = await fs.open('examples/res/myfile.pcapng');
|
|
44
|
+
* file.readableWebStream().pipeTo(parser);
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
export declare class PCAPNGParser extends WritableStream<Uint8Array> implements EventTarget {
|
|
48
|
+
#private;
|
|
49
|
+
/**
|
|
50
|
+
* Interface information receieved for the current section, in order of
|
|
51
|
+
* reception.
|
|
52
|
+
*/
|
|
53
|
+
interfaces: Interface[];
|
|
54
|
+
constructor(opts?: ParserOptions);
|
|
55
|
+
/**
|
|
56
|
+
* Is this file in the pcap-ng format?
|
|
57
|
+
*/
|
|
58
|
+
get ng(): boolean;
|
|
59
|
+
/**
|
|
60
|
+
* Endianness of the current section.
|
|
61
|
+
* Little-endian => LE.
|
|
62
|
+
* Big-endian => BE.
|
|
63
|
+
*/
|
|
64
|
+
get endianess(): Endianess;
|
|
65
|
+
/**
|
|
66
|
+
* The **`addEventListener()`** method of the EventTarget interface sets up
|
|
67
|
+
* a function that will be called whenever the specified event is delivered
|
|
68
|
+
* to the target.
|
|
69
|
+
*
|
|
70
|
+
* @see https://developer.mozilla.org/docs/Web/API/EventTarget/addEventListener
|
|
71
|
+
*/
|
|
72
|
+
addEventListener<E extends keyof ParseEvents>(type: E, callback: EventCallback<ParseEvents[E]>, options?: AddEventListenerOptions | boolean): void;
|
|
73
|
+
/**
|
|
74
|
+
* The **`dispatchEvent()`** method of the EventTarget sends an Event to the
|
|
75
|
+
* object, (synchronously) invoking the affected event listeners in the
|
|
76
|
+
* appropriate order.
|
|
77
|
+
*
|
|
78
|
+
* @see https://developer.mozilla.org/docs/Web/API/EventTarget/dispatchEvent
|
|
79
|
+
*/
|
|
80
|
+
dispatchEvent(event: ParseEvents[keyof ParseEvents]): boolean;
|
|
81
|
+
/**
|
|
82
|
+
* The **`removeEventListener()`** method of the EventTarget interface
|
|
83
|
+
* removes an event listener previously registered with
|
|
84
|
+
* EventTarget.addEventListener() from the target.
|
|
85
|
+
*
|
|
86
|
+
* @see https://developer.mozilla.org/docs/Web/API/EventTarget/removeEventListener
|
|
87
|
+
*/
|
|
88
|
+
removeEventListener<E extends keyof ParseEvents>(event: E, calback: EventCallback<ParseEvents[E]>, options?: EventListenerOptions | boolean): void;
|
|
89
|
+
/**
|
|
90
|
+
* Listen for an event, unwrapping the detailor error in the event so that
|
|
91
|
+
* it is easier to process.
|
|
92
|
+
*
|
|
93
|
+
* @param eventName Event name.
|
|
94
|
+
* @param listener Handler.
|
|
95
|
+
* @returns This, for chaining.
|
|
96
|
+
*/
|
|
97
|
+
on<E extends keyof ParseEvents>(eventName: E, listener: (detail: ParseEventsDetail<E>) => void): this;
|
|
98
|
+
/**
|
|
99
|
+
* Listen for the first occurence of a given event. Does the same
|
|
100
|
+
* unwrapping as `on`.
|
|
101
|
+
*
|
|
102
|
+
* @param eventName Event name.
|
|
103
|
+
* @param listener Handler.
|
|
104
|
+
* @returns This, for chaining.
|
|
105
|
+
*/
|
|
106
|
+
once<E extends keyof ParseEvents>(eventName: E, listener: (detail: ParseEventsDetail<E>) => void): this;
|
|
107
|
+
/**
|
|
108
|
+
* Stop listening for the given event.
|
|
109
|
+
*
|
|
110
|
+
* @param eventName Event name.
|
|
111
|
+
* @param listener Handler to remove.
|
|
112
|
+
* @returns This, for chaining.
|
|
113
|
+
* @throws On unknown listener.
|
|
114
|
+
*/
|
|
115
|
+
off<E extends keyof ParseEvents>(eventName: E, listener: (detail: ParseEventsDetail<E>) => void): this;
|
|
116
|
+
}
|
|
117
|
+
export default PCAPNGParser;
|