node-ikev2 0.1.4 → 0.3.0

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.
@@ -1,4 +1,7 @@
1
1
  "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TrafficSelector = exports.TrafficSelectorType = void 0;
4
+ const ip_address_1 = require("./ip-address");
2
5
  /**
3
6
  * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
4
7
  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
@@ -17,8 +20,6 @@
17
20
 
18
21
  Traffic Selector
19
22
  */
20
- Object.defineProperty(exports, "__esModule", { value: true });
21
- exports.TrafficSelector = exports.TrafficSelectorType = void 0;
22
23
  /**
23
24
  * Traffic Selector Type
24
25
  */
@@ -39,14 +40,14 @@ var TrafficSelectorType;
39
40
  * @property {Buffer} endAddress - 4 bytes
40
41
  */
41
42
  class TrafficSelector {
42
- constructor(type, protocolId, length, startPort, endPort, startAddress, endAddress) {
43
+ constructor(type, protocolId, startPort, endPort, startAddress, endAddress, length = 0) {
43
44
  this.type = type;
44
45
  this.protocolId = protocolId;
45
- this.length = length;
46
46
  this.startPort = startPort;
47
47
  this.endPort = endPort;
48
48
  this.startAddress = startAddress;
49
49
  this.endAddress = endAddress;
50
+ this.length = length;
50
51
  }
51
52
  /**
52
53
  * Parses a Traffic Selector from a buffer
@@ -58,16 +59,42 @@ class TrafficSelector {
58
59
  static parse(buffer) {
59
60
  try {
60
61
  const type = buffer.readUInt8(0);
62
+ let expectedLength;
63
+ switch (type) {
64
+ case TrafficSelectorType.TS_IPV4_ADDR_RANGE:
65
+ expectedLength = 16;
66
+ break;
67
+ case TrafficSelectorType.TS_IPV6_ADDR_RANGE:
68
+ expectedLength = 40;
69
+ break;
70
+ default:
71
+ throw new Error("Invalid traffic selector type");
72
+ }
61
73
  const protocolId = buffer.readUInt8(1);
62
74
  const length = buffer.readUInt16BE(2);
75
+ if (length !== expectedLength) {
76
+ throw new Error("Invalid traffic selector length");
77
+ }
63
78
  const startPort = buffer.readUInt16BE(4);
64
79
  const endPort = buffer.readUInt16BE(6);
65
- const startAddress = buffer.subarray(8, 12);
66
- const endAddress = buffer.subarray(12, 16);
67
- return new TrafficSelector(type, protocolId, length, startPort, endPort, startAddress, endAddress);
80
+ let startAddress;
81
+ let endAddress;
82
+ switch (type) {
83
+ case TrafficSelectorType.TS_IPV4_ADDR_RANGE:
84
+ startAddress = buffer.subarray(8, 12);
85
+ endAddress = buffer.subarray(12, 16);
86
+ break;
87
+ case TrafficSelectorType.TS_IPV6_ADDR_RANGE:
88
+ startAddress = buffer.subarray(8, 24);
89
+ endAddress = buffer.subarray(24, 40);
90
+ break;
91
+ default:
92
+ throw new Error("Invalid traffic selector type");
93
+ }
94
+ return new TrafficSelector(type, protocolId, startPort, endPort, startAddress, endAddress, length);
68
95
  }
69
96
  catch (error) {
70
- throw new Error("Failed to parse traffic selector");
97
+ throw new Error("Failed to parse traffic selector: " + error);
71
98
  }
72
99
  }
73
100
  /**
@@ -78,14 +105,29 @@ class TrafficSelector {
78
105
  * @returns {Buffer}
79
106
  */
80
107
  static serializeJSON(json) {
81
- const buffer = Buffer.alloc(16);
108
+ let length;
109
+ let ipLength;
110
+ const startAddress = (0, ip_address_1.parseIPAddressString)(json.startAddress);
111
+ const endAddress = (0, ip_address_1.parseIPAddressString)(json.endAddress);
112
+ if (startAddress.length === 4 && endAddress.length === 4) {
113
+ length = 16;
114
+ ipLength = 4;
115
+ }
116
+ else if (startAddress.length === 16 && endAddress.length === 16) {
117
+ length = 40;
118
+ ipLength = 16;
119
+ }
120
+ else {
121
+ throw new Error("Invalid traffic selector length");
122
+ }
123
+ const buffer = Buffer.alloc(length);
82
124
  buffer.writeUInt8(json.type, 0);
83
125
  buffer.writeUInt8(json.protocolId, 1);
84
- buffer.writeUInt16BE(json.length, 2);
126
+ buffer.writeUInt16BE(length, 2);
85
127
  buffer.writeUInt16BE(json.startPort, 4);
86
128
  buffer.writeUInt16BE(json.endPort, 6);
87
- Buffer.from(json.startAddress, "hex").copy(buffer, 8);
88
- Buffer.from(json.endAddress, "hex").copy(buffer, 12);
129
+ startAddress.copy(buffer, 8);
130
+ endAddress.copy(buffer, 8 + ipLength);
89
131
  return buffer;
90
132
  }
91
133
  /**
@@ -94,14 +136,33 @@ class TrafficSelector {
94
136
  * @returns {Buffer}
95
137
  */
96
138
  serialize() {
97
- const buffer = Buffer.alloc(16);
139
+ let ipLength;
140
+ switch (this.type) {
141
+ case TrafficSelectorType.TS_IPV4_ADDR_RANGE:
142
+ if (this.startAddress.length != 4 || this.endAddress.length != 4) {
143
+ throw new Error("Invalid traffic selector length");
144
+ }
145
+ this.length = 16;
146
+ ipLength = 4;
147
+ break;
148
+ case TrafficSelectorType.TS_IPV6_ADDR_RANGE:
149
+ if (this.startAddress.length != 16 || this.endAddress.length != 16) {
150
+ throw new Error("Invalid traffic selector length");
151
+ }
152
+ this.length = 40;
153
+ ipLength = 16;
154
+ break;
155
+ default:
156
+ throw new Error("Invalid traffic selector type");
157
+ }
158
+ const buffer = Buffer.alloc(this.length);
98
159
  buffer.writeUInt8(this.type, 0);
99
160
  buffer.writeUInt8(this.protocolId, 1);
100
161
  buffer.writeUInt16BE(this.length, 2);
101
162
  buffer.writeUInt16BE(this.startPort, 4);
102
163
  buffer.writeUInt16BE(this.endPort, 6);
103
164
  this.startAddress.copy(buffer, 8);
104
- this.endAddress.copy(buffer, 12);
165
+ this.endAddress.copy(buffer, 8 + ipLength);
105
166
  return buffer;
106
167
  }
107
168
  /**
@@ -113,11 +174,10 @@ class TrafficSelector {
113
174
  return {
114
175
  type: this.type,
115
176
  protocolId: this.protocolId,
116
- length: this.length,
117
177
  startPort: this.startPort,
118
178
  endPort: this.endPort,
119
- startAddress: this.startAddress.toString("hex"),
120
- endAddress: this.endAddress.toString("hex"),
179
+ startAddress: (0, ip_address_1.formatIPAddressBuffer)(this.startAddress),
180
+ endAddress: (0, ip_address_1.formatIPAddressBuffer)(this.endAddress),
121
181
  };
122
182
  }
123
183
  /**
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "node-ikev2",
3
3
  "title": "Node-IKEV2",
4
- "version": "0.1.4",
4
+ "version": "0.3.0",
5
5
  "description": "IKEv2 parser and serializer for Node.js",
6
6
  "main": "./lib/src/index.js",
7
7
  "types": "./lib/src/index.d.ts",