whatap 0.4.79 → 0.4.80

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.
Files changed (216) hide show
  1. package/.vscode/keep-context.json +3 -3
  2. package/README.md +51 -51
  3. package/bindings/darwin/x64/whatap.node +0 -0
  4. package/bindings/linux/ia32/whatap.node +0 -0
  5. package/bindings/linux/x64/whatap.node +0 -0
  6. package/help.txt +10 -10
  7. package/index.js +8 -8
  8. package/lib/conf/conf-sys-mon.js +100 -100
  9. package/lib/conf/config-default.js +238 -238
  10. package/lib/conf/configure.js +423 -423
  11. package/lib/conf/license.js +41 -41
  12. package/lib/control/cmd-config.js +23 -23
  13. package/lib/control/control-handler.js +344 -344
  14. package/lib/control/packagectr-helper.js +149 -149
  15. package/lib/core/agent.js +329 -328
  16. package/lib/core/interceptor.js +142 -142
  17. package/lib/core/request-agent.js +26 -26
  18. package/lib/counter/counter-manager.js +127 -127
  19. package/lib/counter/meter/meter-activex.js +66 -66
  20. package/lib/counter/meter/meter-httpc.js +57 -57
  21. package/lib/counter/meter/meter-resource.js +9 -9
  22. package/lib/counter/meter/meter-service.js +167 -167
  23. package/lib/counter/meter/meter-socket.io.js +50 -50
  24. package/lib/counter/meter/meter-sql.js +70 -70
  25. package/lib/counter/meter/meter-users.js +57 -57
  26. package/lib/counter/meter.js +183 -183
  27. package/lib/counter/task/activetransaction.js +93 -93
  28. package/lib/counter/task/agentinfo.js +105 -105
  29. package/lib/counter/task/counter-task.js +9 -9
  30. package/lib/counter/task/gcstat.js +34 -34
  31. package/lib/counter/task/heapmem.js +24 -24
  32. package/lib/counter/task/httpc.js +75 -75
  33. package/lib/counter/task/proc-cpu.js +28 -28
  34. package/lib/counter/task/realtimeuser.js +30 -30
  35. package/lib/counter/task/res/systemECSTask.js +54 -54
  36. package/lib/counter/task/res/systemKubeTask.js +52 -52
  37. package/lib/counter/task/res/util/awsEcsClientThread.js +166 -166
  38. package/lib/counter/task/res/util/linuxProcStatUtil.js +13 -13
  39. package/lib/counter/task/res-sys-cpu.js +61 -61
  40. package/lib/counter/task/service.js +201 -201
  41. package/lib/counter/task/socketio.js +29 -29
  42. package/lib/counter/task/sql.js +104 -104
  43. package/lib/counter/task/systemperf.js +42 -42
  44. package/lib/data/datapack-sender.js +256 -256
  45. package/lib/data/dataprofile-agent.js +136 -136
  46. package/lib/data/datatext-agent.js +135 -135
  47. package/lib/data/event-level.js +15 -15
  48. package/lib/data/test.js +48 -48
  49. package/lib/env/constants.js +20 -20
  50. package/lib/kube/kube-client.js +143 -143
  51. package/lib/lang/text-types.js +58 -58
  52. package/lib/logger.js +319 -319
  53. package/lib/net/netflag.js +54 -54
  54. package/lib/net/paramdef.js +40 -40
  55. package/lib/net/receiver.js +65 -65
  56. package/lib/net/security-master.js +182 -173
  57. package/lib/net/sender.js +140 -140
  58. package/lib/net/tcp-return.js +17 -17
  59. package/lib/net/tcp-session.js +285 -285
  60. package/lib/net/tcpreq-client-proxy.js +69 -69
  61. package/lib/net/tcprequest-mgr.js +57 -57
  62. package/lib/observers/cluster-observer.js +21 -21
  63. package/lib/observers/express-observer.js +214 -214
  64. package/lib/observers/file-observer.js +184 -184
  65. package/lib/observers/global-observer.js +30 -30
  66. package/lib/observers/http-observer.js +704 -704
  67. package/lib/observers/maria-observer.js +382 -382
  68. package/lib/observers/memcached-observer.js +55 -55
  69. package/lib/observers/mongo-observer.js +262 -265
  70. package/lib/observers/mongodb-observer.js +197 -197
  71. package/lib/observers/mongoose-observer.js +83 -83
  72. package/lib/observers/mssql-observer.js +205 -205
  73. package/lib/observers/mysql-observer.js +436 -394
  74. package/lib/observers/net-observer.js +72 -72
  75. package/lib/observers/pgsql-observer.js +295 -295
  76. package/lib/observers/process-observer.js +25 -25
  77. package/lib/observers/promise-observer.js +31 -31
  78. package/lib/observers/redis-observer.js +156 -109
  79. package/lib/observers/schedule-observer.js +66 -66
  80. package/lib/observers/socket.io-observer.js +54 -54
  81. package/lib/observers/stream-observer.js +19 -19
  82. package/lib/observers/thrift-observer.js +196 -196
  83. package/lib/pack/activestack-pack.js +54 -54
  84. package/lib/pack/counter-pack.js +649 -649
  85. package/lib/pack/errorsnap-pack.js +68 -68
  86. package/lib/pack/event-pack.js +53 -53
  87. package/lib/pack/hitmap-pack.js +62 -62
  88. package/lib/pack/hitmap-pack1.js +146 -146
  89. package/lib/pack/netstat.js +14 -14
  90. package/lib/pack/pack.js +49 -49
  91. package/lib/pack/packenum.js +60 -60
  92. package/lib/pack/param-pack.js +213 -213
  93. package/lib/pack/profile-pack.js +48 -48
  94. package/lib/pack/realtimeuser-pack.js +40 -40
  95. package/lib/pack/stat-general-pack.js +95 -95
  96. package/lib/pack/staterror-pack.js +119 -119
  97. package/lib/pack/stathttpc-pack.js +66 -66
  98. package/lib/pack/stathttpc-rec.js +78 -78
  99. package/lib/pack/statremote-pack.js +45 -45
  100. package/lib/pack/statservice-pack.js +62 -62
  101. package/lib/pack/statservice-pack1.js +87 -87
  102. package/lib/pack/statservice-rec.js +292 -292
  103. package/lib/pack/statservice-rec_dep.js +151 -151
  104. package/lib/pack/statsql-pack.js +69 -69
  105. package/lib/pack/statsql-rec.js +100 -100
  106. package/lib/pack/statuseragent-pack.js +43 -43
  107. package/lib/pack/tagcount-pack.js +398 -398
  108. package/lib/pack/tagctr.js +14 -14
  109. package/lib/pack/text-pack.js +49 -49
  110. package/lib/pack/time-count.js +25 -25
  111. package/lib/pack/websocket.js +14 -14
  112. package/lib/plugin/plugin-loadermanager.js +56 -56
  113. package/lib/plugin/plugin.js +75 -75
  114. package/lib/requestlog.js +326 -326
  115. package/lib/service/tx-record.js +288 -288
  116. package/lib/stat/stat-error.js +116 -116
  117. package/lib/stat/stat-httpc.js +97 -97
  118. package/lib/stat/stat-remoteip.js +46 -46
  119. package/lib/stat/stat-sql.js +112 -112
  120. package/lib/stat/stat-tranx.js +57 -57
  121. package/lib/stat/stat-tx-caller.js +159 -159
  122. package/lib/stat/stat-tx-domain.js +110 -110
  123. package/lib/stat/stat-tx-referer.js +111 -111
  124. package/lib/stat/stat-useragent.js +48 -48
  125. package/lib/stat/timingsender.js +72 -72
  126. package/lib/step/activestack-step.js +37 -37
  127. package/lib/step/dbc-step.js +35 -35
  128. package/lib/step/http-stepx.js +66 -66
  129. package/lib/step/message-step.js +39 -39
  130. package/lib/step/method-stepx.js +46 -46
  131. package/lib/step/resultset-step.js +39 -39
  132. package/lib/step/securemsg-step.js +43 -43
  133. package/lib/step/socket-step.js +46 -46
  134. package/lib/step/sql-stepx.js +67 -67
  135. package/lib/step/sqlxtype.js +15 -15
  136. package/lib/step/step.js +65 -65
  137. package/lib/step/stepenum.js +53 -53
  138. package/lib/trace/local-context.js +13 -13
  139. package/lib/trace/parsed-sql.js +13 -13
  140. package/lib/trace/profile-collector.js +70 -70
  141. package/lib/trace/serviceurl-pattern-detector.js +117 -117
  142. package/lib/trace/trace-context-manager.js +195 -195
  143. package/lib/trace/trace-context.js +134 -134
  144. package/lib/util/anylist.js +102 -102
  145. package/lib/util/array-util.js +100 -100
  146. package/lib/util/bitutil.js +27 -27
  147. package/lib/util/cardinality/hyperloglog.js +105 -105
  148. package/lib/util/cardinality/murmurhash.js +31 -31
  149. package/lib/util/cardinality/registerset.js +74 -74
  150. package/lib/util/config-util.js +17 -17
  151. package/lib/util/cypher.js +89 -89
  152. package/lib/util/datetimehelper.js +237 -229
  153. package/lib/util/dateutil.js +110 -106
  154. package/lib/util/errordata.js +20 -20
  155. package/lib/util/escape-literal-sql.js +342 -342
  156. package/lib/util/hashutil2.js +126 -126
  157. package/lib/util/hexa32.js +57 -57
  158. package/lib/util/index.js +78 -78
  159. package/lib/util/intint-map.js +47 -47
  160. package/lib/util/intkey-linkedmap.js +26 -26
  161. package/lib/util/intkey-map.js +25 -25
  162. package/lib/util/intset.js +82 -82
  163. package/lib/util/iputil.js +119 -119
  164. package/lib/util/iputil_x.js +526 -526
  165. package/lib/util/keygen.js +17 -17
  166. package/lib/util/kube-util.js +72 -72
  167. package/lib/util/longint-linkedmap.js +35 -35
  168. package/lib/util/longkey-linkedmap.js +25 -25
  169. package/lib/util/longlong-linkedmap.js +37 -37
  170. package/lib/util/nodeutil.js +67 -67
  171. package/lib/util/oidutil.js +96 -93
  172. package/lib/util/paramsecurity.js +78 -78
  173. package/lib/util/pathtree.js +172 -172
  174. package/lib/util/pre-process.js +13 -13
  175. package/lib/util/process-seq.js +165 -165
  176. package/lib/util/property-util.js +35 -35
  177. package/lib/util/request-queue.js +42 -42
  178. package/lib/util/requestdouble-queue.js +72 -72
  179. package/lib/util/resourceprofile.js +156 -156
  180. package/lib/util/seedrandom.js +242 -242
  181. package/lib/util/stop-watch.js +29 -29
  182. package/lib/util/string-util.js +9 -9
  183. package/lib/util/stringkey-linkedmap.js +28 -28
  184. package/lib/util/stringnum-linkedmap.js +31 -31
  185. package/lib/util/stringset.js +65 -65
  186. package/lib/util/system-util.js +9 -9
  187. package/lib/util/userid-util.js +57 -57
  188. package/lib/util/utils.js +67 -67
  189. package/lib/value/blob-value.js +61 -61
  190. package/lib/value/boolean-value.js +51 -51
  191. package/lib/value/decimal-value.js +71 -71
  192. package/lib/value/double-summary.js +105 -105
  193. package/lib/value/double-value.js +50 -50
  194. package/lib/value/float-array.js +59 -59
  195. package/lib/value/float-value.js +64 -64
  196. package/lib/value/int-array.js +59 -59
  197. package/lib/value/int-map-value.js +151 -151
  198. package/lib/value/int-value.js +64 -64
  199. package/lib/value/ip4-value.js +83 -83
  200. package/lib/value/list-value.js +136 -136
  201. package/lib/value/long-array.js +59 -59
  202. package/lib/value/long-summary.js +105 -105
  203. package/lib/value/map-value.js +175 -175
  204. package/lib/value/metric-value.js +157 -157
  205. package/lib/value/null-value.js +42 -42
  206. package/lib/value/number-value.js +34 -34
  207. package/lib/value/summary-value.js +29 -29
  208. package/lib/value/text-array.js +117 -117
  209. package/lib/value/text-value.js +59 -59
  210. package/lib/value/texthash-value.js +49 -49
  211. package/lib/value/value.js +14 -14
  212. package/lib/value/valueenum.js +99 -99
  213. package/logs/whatap-20230906.log +54 -0
  214. package/package.json +28 -28
  215. package/whatap.conf +1 -1
  216. package/logs/whatap-20230616.log +0 -13
@@ -1,527 +1,527 @@
1
- /**
2
- * Copyright 2016 the WHATAP project authors. All rights reserved.
3
- * Use of this source code is governed by a license that
4
- * can be found in the LICENSE file.
5
- */
6
-
7
- const empty = '0.0.0.0';
8
- const ipv6Part = "(?:[0-9a-f]+::?)+";
9
- const ipv4Part = "(0?\\d+|0x[a-f0-9]+)";
10
- const ipv4Regexes = {
11
- fourOctet: new RegExp("^" + ipv4Part + "\\." + ipv4Part + "\\." + ipv4Part + "\\." + ipv4Part + "$", 'i'),
12
- longValue: new RegExp("^" + ipv4Part + "$", 'i')
13
- };
14
- const ipv6Regexes = {
15
- "native": new RegExp("^(::)?(" + ipv6Part + ")?([0-9a-f]+)?(::)?$", 'i'),
16
- transitional: new RegExp(("^((?:" + ipv6Part + ")|(?:::)(?:" + ipv6Part + ")?)") + ("" + ipv4Part + "\\." + ipv4Part + "\\." + ipv4Part + "\\." + ipv4Part + "$"), 'i')
17
- };
18
-
19
- var DataOutputX = require('../io/data-outputx');
20
- var expandIPv6 = function(string, parts) {
21
- var colonCount, lastColon, part, replacement, replacementCount;
22
- if (string.indexOf('::') !== string.lastIndexOf('::')) {
23
- return null;
24
- }
25
- colonCount = 0;
26
- lastColon = -1;
27
- while ((lastColon = string.indexOf(':', lastColon + 1)) >= 0) {
28
- colonCount++;
29
- }
30
- if (string.substr(0, 2) === '::') {
31
- colonCount--;
32
- }
33
- if (string.substr(-2, 2) === '::') {
34
- colonCount--;
35
- }
36
- if (colonCount > parts) {
37
- return null;
38
- }
39
- replacementCount = parts - colonCount;
40
- replacement = ':';
41
- while (replacementCount--) {
42
- replacement += '0:';
43
- }
44
- string = string.replace('::', replacement);
45
- if (string[0] === ':') {
46
- string = string.slice(1);
47
- }
48
- if (string[string.length - 1] === ':') {
49
- string = string.slice(0, -1);
50
- }
51
- return (function() {
52
- var _i, _len, _ref, _results;
53
- _ref = string.split(":");
54
- _results = [];
55
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
56
- part = _ref[_i];
57
- _results.push(parseInt(part, 16));
58
- }
59
- return _results;
60
- })();
61
- };
62
- var matchCIDR = function(first, second, partSize, cidrBits) {
63
- var part, shift;
64
- if (first.length !== second.length) {
65
- throw new Error("ipaddr: cannot match CIDR for objects with different lengths");
66
- }
67
- part = 0;
68
- while (cidrBits > 0) {
69
- shift = partSize - cidrBits;
70
- if (shift < 0) {
71
- shift = 0;
72
- }
73
- if (first[part] >> shift !== second[part] >> shift) {
74
- return false;
75
- }
76
- cidrBits -= partSize;
77
- part += 1;
78
- }
79
- return true;
80
- };
81
-
82
- function IPUtilX() {
83
-
84
- };
85
-
86
- IPUtilX.IPv6 = (function() {
87
- function IPv6(parts) {
88
- var i, part, _i, _j, _len, _ref;
89
- if (parts.length === 16) {
90
- this.parts = [];
91
- for (i = _i = 0; _i <= 14; i = _i += 2) {
92
- this.parts.push((parts[i] << 8) | parts[i + 1]);
93
- }
94
- } else if (parts.length === 8) {
95
- this.parts = parts;
96
- } else {
97
- throw new Error("ipaddr: ipv6 part count should be 8 or 16");
98
- }
99
- _ref = this.parts;
100
- for (_j = 0, _len = _ref.length; _j < _len; _j++) {
101
- part = _ref[_j];
102
- if (!((0 <= part && part <= 0xffff))) {
103
- throw new Error("ipaddr: ipv6 part should fit in 16 bits");
104
- }
105
- }
106
- }
107
- IPv6.prototype.kind = function() {
108
- return 'ipv6';
109
- };
110
- IPv6.prototype.toString = function() {
111
- var compactStringParts, part, pushPart, state, stringParts, _i, _len;
112
- stringParts = (function() {
113
- var _i, _len, _ref, _results;
114
- _ref = this.parts;
115
- _results = [];
116
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
117
- part = _ref[_i];
118
- _results.push(part.toString(16));
119
- }
120
- return _results;
121
- }).call(this);
122
- compactStringParts = [];
123
- pushPart = function(part) {
124
- return compactStringParts.push(part);
125
- };
126
- state = 0;
127
- for (_i = 0, _len = stringParts.length; _i < _len; _i++) {
128
- part = stringParts[_i];
129
- switch (state) {
130
- case 0:
131
- if (part === '0') {
132
- pushPart('');
133
- } else {
134
- pushPart(part);
135
- }
136
- state = 1;
137
- break;
138
- case 1:
139
- if (part === '0') {
140
- state = 2;
141
- } else {
142
- pushPart(part);
143
- }
144
- break;
145
- case 2:
146
- if (part !== '0') {
147
- pushPart('');
148
- pushPart(part);
149
- state = 3;
150
- }
151
- break;
152
- case 3:
153
- pushPart(part);
154
- }
155
- }
156
- if (state === 2) {
157
- pushPart('');
158
- pushPart('');
159
- }
160
- return compactStringParts.join(":");
161
- };
162
- IPv6.prototype.toByteArray = function() {
163
- var bytes, part, _i, _len, _ref;
164
- bytes = [];
165
- _ref = this.parts;
166
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
167
- part = _ref[_i];
168
- bytes.push(part >> 8);
169
- bytes.push(part & 0xff);
170
- }
171
- return bytes;
172
- };
173
- IPv6.prototype.toNormalizedString = function() {
174
- var part;
175
- return ((function() {
176
- var _i, _len, _ref, _results;
177
- _ref = this.parts;
178
- _results = [];
179
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
180
- part = _ref[_i];
181
- _results.push(part.toString(16));
182
- }
183
- return _results;
184
- }).call(this)).join(":");
185
- };
186
- IPv6.prototype.match = function(other, cidrRange) {
187
- var _ref;
188
- if (cidrRange === void 0) {
189
- _ref = other, other = _ref[0], cidrRange = _ref[1];
190
- }
191
- if (other.kind() !== 'ipv6') {
192
- throw new Error("ipaddr: cannot match ipv6 address with non-ipv6 one");
193
- }
194
- return matchCIDR(this.parts, other.parts, 16, cidrRange);
195
- };
196
- IPv6.prototype.SpecialRanges = {
197
- unspecified: [new IPv6([0, 0, 0, 0, 0, 0, 0, 0]), 128],
198
- linkLocal: [new IPv6([0xfe80, 0, 0, 0, 0, 0, 0, 0]), 10],
199
- multicast: [new IPv6([0xff00, 0, 0, 0, 0, 0, 0, 0]), 8],
200
- loopback: [new IPv6([0, 0, 0, 0, 0, 0, 0, 1]), 128],
201
- uniqueLocal: [new IPv6([0xfc00, 0, 0, 0, 0, 0, 0, 0]), 7],
202
- ipv4Mapped: [new IPv6([0, 0, 0, 0, 0, 0xffff, 0, 0]), 96],
203
- rfc6145: [new IPv6([0, 0, 0, 0, 0xffff, 0, 0, 0]), 96],
204
- rfc6052: [new IPv6([0x64, 0xff9b, 0, 0, 0, 0, 0, 0]), 96],
205
- '6to4': [new IPv6([0x2002, 0, 0, 0, 0, 0, 0, 0]), 16],
206
- teredo: [new IPv6([0x2001, 0, 0, 0, 0, 0, 0, 0]), 32],
207
- reserved: [[new IPv6([0x2001, 0xdb8, 0, 0, 0, 0, 0, 0]), 32]]
208
- };
209
- IPv6.prototype.range = function() {
210
- return IPUtilX.subnetMatch(this, this.SpecialRanges);
211
- };
212
- IPv6.prototype.isIPv4MappedAddress = function() {
213
- return this.range() === 'ipv4Mapped';
214
- };
215
- IPv6.prototype.toIPv4Address = function() {
216
- var high, low, _ref;
217
- if (!this.isIPv4MappedAddress()) {
218
- throw new Error("ipaddr: trying to convert a generic ipv6 address to ipv4");
219
- }
220
- _ref = this.parts.slice(-2), high = _ref[0], low = _ref[1];
221
- return new IPUtilX.IPv4([high >> 8, high & 0xff, low >> 8, low & 0xff]);
222
- };
223
- return IPv6;
224
- })();
225
- IPUtilX.IPv6.isValid = function(string) {
226
- var e;
227
- if (typeof string === "string" && string.indexOf(":") === -1) {
228
- return false;
229
- }
230
- try {
231
- new this(this.parser(string));
232
- return true;
233
- } catch (_error) {
234
- e = _error;
235
- return false;
236
- }
237
- };
238
- IPUtilX.IPv6.parser = function(string) {
239
- var match, octet, octets, parts, _i, _len;
240
- if (string.match(ipv6Regexes['native'])) {
241
- return expandIPv6(string, 8);
242
- } else if (match = string.match(ipv6Regexes['transitional'])) {
243
- parts = expandIPv6(match[1].slice(0, -1), 6);
244
- if (parts) {
245
- octets = [parseInt(match[2]), parseInt(match[3]), parseInt(match[4]), parseInt(match[5])];
246
- for (_i = 0, _len = octets.length; _i < _len; _i++) {
247
- octet = octets[_i];
248
- if (!((0 <= octet && octet <= 255))) {
249
- return null;
250
- }
251
- }
252
- parts.push(octets[0] << 8 | octets[1]);
253
- parts.push(octets[2] << 8 | octets[3]);
254
- return parts;
255
- }
256
- }
257
- return null;
258
- };
259
-
260
- IPUtilX.IPv4 = (function() {
261
- function IPv4(octets) {
262
- var octet, _i, _len;
263
- if (octets.length !== 4) {
264
- throw new Error("ipaddr: ipv4 octet count should be 4");
265
- }
266
- for (_i = 0, _len = octets.length; _i < _len; _i++) {
267
- octet = octets[_i];
268
- if (!((0 <= octet && octet <= 255))) {
269
- throw new Error("ipaddr: ipv4 octet should fit in 8 bits");
270
- }
271
- }
272
- this.octets = octets;
273
- }
274
-
275
- IPv4.prototype.kind = function() {
276
- return 'ipv4';
277
- };
278
- IPv4.prototype.toString = function() {
279
- return this.octets.join(".");
280
- };
281
- IPv4.prototype.toByteArray = function() {
282
- return this.octets.slice(0);
283
- };
284
- IPv4.prototype.match = function(other, cidrRange) {
285
- var _ref;
286
- if (cidrRange === void 0) {
287
- _ref = other, other = _ref[0], cidrRange = _ref[1];
288
- }
289
- if (other.kind() !== 'ipv4') {
290
- throw new Error("ipaddr: cannot match ipv4 address with non-ipv4 one");
291
- }
292
- return matchCIDR(this.octets, other.octets, 8, cidrRange);
293
- };
294
- IPv4.prototype.SpecialRanges = {
295
- unspecified: [[new IPv4([0, 0, 0, 0]), 8]],
296
- broadcast: [[new IPv4([255, 255, 255, 255]), 32]],
297
- multicast: [[new IPv4([224, 0, 0, 0]), 4]],
298
- linkLocal: [[new IPv4([169, 254, 0, 0]), 16]],
299
- loopback: [[new IPv4([127, 0, 0, 0]), 8]],
300
- "private": [[new IPv4([10, 0, 0, 0]), 8], [new IPv4([172, 16, 0, 0]), 12], [new IPv4([192, 168, 0, 0]), 16]],
301
- reserved: [[new IPv4([192, 0, 0, 0]), 24], [new IPv4([192, 0, 2, 0]), 24], [new IPv4([192, 88, 99, 0]), 24], [new IPv4([198, 51, 100, 0]), 24], [new IPv4([203, 0, 113, 0]), 24], [new IPv4([240, 0, 0, 0]), 4]]
302
- };
303
- IPv4.prototype.range = function() {
304
- return IPUtilX.subnetMatch(this, this.SpecialRanges);
305
- };
306
- IPv4.prototype.toIPv4MappedAddress = function() {
307
- return IPUtilX.IPv6.parse("::ffff:" + (this.toString()));
308
- };
309
- IPv4.prototype.prefixLengthFromSubnetMask = function() {
310
- var cidr, i, octet, stop, zeros, zerotable, _i;
311
- zerotable = {
312
- 0: 8,
313
- 128: 7,
314
- 192: 6,
315
- 224: 5,
316
- 240: 4,
317
- 248: 3,
318
- 252: 2,
319
- 254: 1,
320
- 255: 0
321
- };
322
- cidr = 0;
323
- stop = false;
324
- for (i = _i = 3; _i >= 0; i = _i += -1) {
325
- octet = this.octets[i];
326
- if (octet in zerotable) {
327
- zeros = zerotable[octet];
328
- if (stop && zeros !== 0) {
329
- return null;
330
- }
331
- if (zeros !== 8) {
332
- stop = true;
333
- }
334
- cidr += zeros;
335
- } else {
336
- return null;
337
- }
338
- }
339
- return 32 - cidr;
340
- };
341
- IPv4.prototype.isValid = function(string) {
342
- var e;
343
- try {
344
- new this(this.parser(string));
345
- return true;
346
- } catch (_error) {
347
- e = _error;
348
- return false;
349
- }
350
- };
351
- return IPv4;
352
-
353
- })();
354
- IPUtilX.IPv4.isValid = function(string) {
355
- var e;
356
- try {
357
- new this(this.parser(string));
358
- return true;
359
- } catch (_error) {
360
- e = _error;
361
- return false;
362
- }
363
- };
364
- IPUtilX.IPv4.parser = function(string) {
365
- var match, parseIntAuto, part, shift, value;
366
- parseIntAuto = function(string) {
367
- if (string[0] === "0" && string[1] !== "x") {
368
- return parseInt(string, 8);
369
- } else {
370
- return parseInt(string);
371
- }
372
- };
373
- if (match = string.match(ipv4Regexes.fourOctet)) {
374
- return (function() {
375
- var _i, _len, _ref, _results;
376
- _ref = match.slice(1, 6);
377
- _results = [];
378
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
379
- part = _ref[_i];
380
- _results.push(parseIntAuto(part));
381
- }
382
- return _results;
383
- })();
384
- } else if (match = string.match(ipv4Regexes.longValue)) {
385
- value = parseIntAuto(match[1]);
386
- if (value > 0xffffffff || value < 0) {
387
- throw new Error("ipaddr: address outside defined range");
388
- }
389
- return ((function() {
390
- var _i, _results;
391
- _results = [];
392
- for (shift = _i = 0; _i <= 24; shift = _i += 8) {
393
- _results.push((value >> shift) & 0xff);
394
- }
395
- return _results;
396
- })()).reverse();
397
- } else {
398
- return null;
399
- }
400
- };
401
- IPUtilX.IPv4.parse = IPUtilX.IPv6.parse = function(string) {
402
- var parts;
403
- parts = this.parser(string);
404
- if (parts == null) {
405
- throw new Error("ipaddr: string is not formatted like ip address");
406
- }
407
- return new this(parts);
408
- };
409
-
410
- IPUtilX.toString = function(ip) {
411
- if(ip == null) {
412
- return empty;
413
- }
414
-
415
- try{
416
- if(typeof ip == 'number') {
417
- ip = DataOutputX.toBytes(ip);
418
- }
419
-
420
- if(typeof ip == 'object' && ip.length == 4) {
421
- var str = ip[0] & 0xff;
422
- str += '.';
423
- str += ip[1] & 0xff;
424
- str += '.';
425
- str += ip[2] & 0xff;
426
- str += '.';
427
- str += ip[3] & 0xff;
428
- }
429
- return str;
430
- } catch(e) {
431
- return empty;
432
- };
433
- };
434
- IPUtilX.toBytes = function(ip) {
435
- if(ip == null) {
436
- return [0, 0, 0, 0];
437
- }
438
-
439
- if(typeof ip == 'number') {
440
- return DataOutputX.toBytes(ip);
441
- }
442
-
443
- var result = new Array(4),
444
- s = ip.split('.');
445
-
446
- try {
447
- if(s.length != 4) {
448
- return [0, 0, 0, 0];
449
- }
450
- var val;
451
- for(var i=0; i<4; i++) {
452
- val = parseInt(s[i]);
453
- if(val < 0 || val > 0xff) {
454
- return [0, 0, 0, 0];
455
- }
456
- result[i] = (val & 0xff);
457
- }
458
- } catch(exeption) {
459
- return [0, 0, 0, 0];
460
- }
461
- return result;
462
- };
463
- IPUtilX.isOK = function(ip) {
464
- try {
465
- return ip != null && ip.length == 4;
466
- } catch(e) {
467
- return false;
468
- }
469
- };
470
- IPUtilX.isNotLocal = function(ip) {
471
- return this.isOK(ip) && (ip[0] & 0xff) != 127;
472
- };
473
- IPUtilX.getIp = function(){
474
- var interfaces = require('os').networkInterfaces();
475
- var results = [];
476
- Object
477
- .keys(interfaces)
478
- .map(function(key){
479
- results = results.concat(interfaces[key]);
480
- });
481
-
482
- results = results
483
- .filter(function(iface){
484
- return iface.family === 'IPv4' && !iface.internal;
485
- })
486
- .map(function(iface){ return iface.address });
487
- return (results[0] || "127.0.0.1");
488
- }
489
- IPUtilX.parse = function(string) {
490
- if (IPUtilX.IPv6.isValid(string)) {
491
- return IPUtilX.IPv6.parse(string);
492
- } else if (IPUtilX.IPv4.isValid(string)) {
493
- return IPUtilX.IPv4.parse(string);
494
- } else {
495
- throw new Error("ipaddr: the address has neither IPv6 nor IPv4 format");
496
- }
497
- };
498
- IPUtilX.process = function(string) {
499
- var addr;
500
- addr = this.parse(string);
501
- if (addr.kind() === 'ipv6' && addr.isIPv4MappedAddress()) {
502
- return addr.toIPv4Address();
503
- } else {
504
- return addr;
505
- }
506
- };
507
- IPUtilX.subnetMatch = function(address, rangeList, defaultName) {
508
- var rangeName, rangeSubnets, subnet, _i, _len;
509
- if (defaultName == null) {
510
- defaultName = 'unicast';
511
- }
512
- for (rangeName in rangeList) {
513
- rangeSubnets = rangeList[rangeName];
514
- if (rangeSubnets[0] && !(rangeSubnets[0] instanceof Array)) {
515
- rangeSubnets = [rangeSubnets];
516
- }
517
- for (_i = 0, _len = rangeSubnets.length; _i < _len; _i++) {
518
- subnet = rangeSubnets[_i];
519
- if (address.match.apply(address, subnet)) {
520
- return rangeName;
521
- }
522
- }
523
- }
524
- return defaultName;
525
- };
526
-
1
+ /**
2
+ * Copyright 2016 the WHATAP project authors. All rights reserved.
3
+ * Use of this source code is governed by a license that
4
+ * can be found in the LICENSE file.
5
+ */
6
+
7
+ const empty = '0.0.0.0';
8
+ const ipv6Part = "(?:[0-9a-f]+::?)+";
9
+ const ipv4Part = "(0?\\d+|0x[a-f0-9]+)";
10
+ const ipv4Regexes = {
11
+ fourOctet: new RegExp("^" + ipv4Part + "\\." + ipv4Part + "\\." + ipv4Part + "\\." + ipv4Part + "$", 'i'),
12
+ longValue: new RegExp("^" + ipv4Part + "$", 'i')
13
+ };
14
+ const ipv6Regexes = {
15
+ "native": new RegExp("^(::)?(" + ipv6Part + ")?([0-9a-f]+)?(::)?$", 'i'),
16
+ transitional: new RegExp(("^((?:" + ipv6Part + ")|(?:::)(?:" + ipv6Part + ")?)") + ("" + ipv4Part + "\\." + ipv4Part + "\\." + ipv4Part + "\\." + ipv4Part + "$"), 'i')
17
+ };
18
+
19
+ var DataOutputX = require('../io/data-outputx');
20
+ var expandIPv6 = function(string, parts) {
21
+ var colonCount, lastColon, part, replacement, replacementCount;
22
+ if (string.indexOf('::') !== string.lastIndexOf('::')) {
23
+ return null;
24
+ }
25
+ colonCount = 0;
26
+ lastColon = -1;
27
+ while ((lastColon = string.indexOf(':', lastColon + 1)) >= 0) {
28
+ colonCount++;
29
+ }
30
+ if (string.substr(0, 2) === '::') {
31
+ colonCount--;
32
+ }
33
+ if (string.substr(-2, 2) === '::') {
34
+ colonCount--;
35
+ }
36
+ if (colonCount > parts) {
37
+ return null;
38
+ }
39
+ replacementCount = parts - colonCount;
40
+ replacement = ':';
41
+ while (replacementCount--) {
42
+ replacement += '0:';
43
+ }
44
+ string = string.replace('::', replacement);
45
+ if (string[0] === ':') {
46
+ string = string.slice(1);
47
+ }
48
+ if (string[string.length - 1] === ':') {
49
+ string = string.slice(0, -1);
50
+ }
51
+ return (function() {
52
+ var _i, _len, _ref, _results;
53
+ _ref = string.split(":");
54
+ _results = [];
55
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
56
+ part = _ref[_i];
57
+ _results.push(parseInt(part, 16));
58
+ }
59
+ return _results;
60
+ })();
61
+ };
62
+ var matchCIDR = function(first, second, partSize, cidrBits) {
63
+ var part, shift;
64
+ if (first.length !== second.length) {
65
+ throw new Error("ipaddr: cannot match CIDR for objects with different lengths");
66
+ }
67
+ part = 0;
68
+ while (cidrBits > 0) {
69
+ shift = partSize - cidrBits;
70
+ if (shift < 0) {
71
+ shift = 0;
72
+ }
73
+ if (first[part] >> shift !== second[part] >> shift) {
74
+ return false;
75
+ }
76
+ cidrBits -= partSize;
77
+ part += 1;
78
+ }
79
+ return true;
80
+ };
81
+
82
+ function IPUtilX() {
83
+
84
+ };
85
+
86
+ IPUtilX.IPv6 = (function() {
87
+ function IPv6(parts) {
88
+ var i, part, _i, _j, _len, _ref;
89
+ if (parts.length === 16) {
90
+ this.parts = [];
91
+ for (i = _i = 0; _i <= 14; i = _i += 2) {
92
+ this.parts.push((parts[i] << 8) | parts[i + 1]);
93
+ }
94
+ } else if (parts.length === 8) {
95
+ this.parts = parts;
96
+ } else {
97
+ throw new Error("ipaddr: ipv6 part count should be 8 or 16");
98
+ }
99
+ _ref = this.parts;
100
+ for (_j = 0, _len = _ref.length; _j < _len; _j++) {
101
+ part = _ref[_j];
102
+ if (!((0 <= part && part <= 0xffff))) {
103
+ throw new Error("ipaddr: ipv6 part should fit in 16 bits");
104
+ }
105
+ }
106
+ }
107
+ IPv6.prototype.kind = function() {
108
+ return 'ipv6';
109
+ };
110
+ IPv6.prototype.toString = function() {
111
+ var compactStringParts, part, pushPart, state, stringParts, _i, _len;
112
+ stringParts = (function() {
113
+ var _i, _len, _ref, _results;
114
+ _ref = this.parts;
115
+ _results = [];
116
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
117
+ part = _ref[_i];
118
+ _results.push(part.toString(16));
119
+ }
120
+ return _results;
121
+ }).call(this);
122
+ compactStringParts = [];
123
+ pushPart = function(part) {
124
+ return compactStringParts.push(part);
125
+ };
126
+ state = 0;
127
+ for (_i = 0, _len = stringParts.length; _i < _len; _i++) {
128
+ part = stringParts[_i];
129
+ switch (state) {
130
+ case 0:
131
+ if (part === '0') {
132
+ pushPart('');
133
+ } else {
134
+ pushPart(part);
135
+ }
136
+ state = 1;
137
+ break;
138
+ case 1:
139
+ if (part === '0') {
140
+ state = 2;
141
+ } else {
142
+ pushPart(part);
143
+ }
144
+ break;
145
+ case 2:
146
+ if (part !== '0') {
147
+ pushPart('');
148
+ pushPart(part);
149
+ state = 3;
150
+ }
151
+ break;
152
+ case 3:
153
+ pushPart(part);
154
+ }
155
+ }
156
+ if (state === 2) {
157
+ pushPart('');
158
+ pushPart('');
159
+ }
160
+ return compactStringParts.join(":");
161
+ };
162
+ IPv6.prototype.toByteArray = function() {
163
+ var bytes, part, _i, _len, _ref;
164
+ bytes = [];
165
+ _ref = this.parts;
166
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
167
+ part = _ref[_i];
168
+ bytes.push(part >> 8);
169
+ bytes.push(part & 0xff);
170
+ }
171
+ return bytes;
172
+ };
173
+ IPv6.prototype.toNormalizedString = function() {
174
+ var part;
175
+ return ((function() {
176
+ var _i, _len, _ref, _results;
177
+ _ref = this.parts;
178
+ _results = [];
179
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
180
+ part = _ref[_i];
181
+ _results.push(part.toString(16));
182
+ }
183
+ return _results;
184
+ }).call(this)).join(":");
185
+ };
186
+ IPv6.prototype.match = function(other, cidrRange) {
187
+ var _ref;
188
+ if (cidrRange === void 0) {
189
+ _ref = other, other = _ref[0], cidrRange = _ref[1];
190
+ }
191
+ if (other.kind() !== 'ipv6') {
192
+ throw new Error("ipaddr: cannot match ipv6 address with non-ipv6 one");
193
+ }
194
+ return matchCIDR(this.parts, other.parts, 16, cidrRange);
195
+ };
196
+ IPv6.prototype.SpecialRanges = {
197
+ unspecified: [new IPv6([0, 0, 0, 0, 0, 0, 0, 0]), 128],
198
+ linkLocal: [new IPv6([0xfe80, 0, 0, 0, 0, 0, 0, 0]), 10],
199
+ multicast: [new IPv6([0xff00, 0, 0, 0, 0, 0, 0, 0]), 8],
200
+ loopback: [new IPv6([0, 0, 0, 0, 0, 0, 0, 1]), 128],
201
+ uniqueLocal: [new IPv6([0xfc00, 0, 0, 0, 0, 0, 0, 0]), 7],
202
+ ipv4Mapped: [new IPv6([0, 0, 0, 0, 0, 0xffff, 0, 0]), 96],
203
+ rfc6145: [new IPv6([0, 0, 0, 0, 0xffff, 0, 0, 0]), 96],
204
+ rfc6052: [new IPv6([0x64, 0xff9b, 0, 0, 0, 0, 0, 0]), 96],
205
+ '6to4': [new IPv6([0x2002, 0, 0, 0, 0, 0, 0, 0]), 16],
206
+ teredo: [new IPv6([0x2001, 0, 0, 0, 0, 0, 0, 0]), 32],
207
+ reserved: [[new IPv6([0x2001, 0xdb8, 0, 0, 0, 0, 0, 0]), 32]]
208
+ };
209
+ IPv6.prototype.range = function() {
210
+ return IPUtilX.subnetMatch(this, this.SpecialRanges);
211
+ };
212
+ IPv6.prototype.isIPv4MappedAddress = function() {
213
+ return this.range() === 'ipv4Mapped';
214
+ };
215
+ IPv6.prototype.toIPv4Address = function() {
216
+ var high, low, _ref;
217
+ if (!this.isIPv4MappedAddress()) {
218
+ throw new Error("ipaddr: trying to convert a generic ipv6 address to ipv4");
219
+ }
220
+ _ref = this.parts.slice(-2), high = _ref[0], low = _ref[1];
221
+ return new IPUtilX.IPv4([high >> 8, high & 0xff, low >> 8, low & 0xff]);
222
+ };
223
+ return IPv6;
224
+ })();
225
+ IPUtilX.IPv6.isValid = function(string) {
226
+ var e;
227
+ if (typeof string === "string" && string.indexOf(":") === -1) {
228
+ return false;
229
+ }
230
+ try {
231
+ new this(this.parser(string));
232
+ return true;
233
+ } catch (_error) {
234
+ e = _error;
235
+ return false;
236
+ }
237
+ };
238
+ IPUtilX.IPv6.parser = function(string) {
239
+ var match, octet, octets, parts, _i, _len;
240
+ if (string.match(ipv6Regexes['native'])) {
241
+ return expandIPv6(string, 8);
242
+ } else if (match = string.match(ipv6Regexes['transitional'])) {
243
+ parts = expandIPv6(match[1].slice(0, -1), 6);
244
+ if (parts) {
245
+ octets = [parseInt(match[2]), parseInt(match[3]), parseInt(match[4]), parseInt(match[5])];
246
+ for (_i = 0, _len = octets.length; _i < _len; _i++) {
247
+ octet = octets[_i];
248
+ if (!((0 <= octet && octet <= 255))) {
249
+ return null;
250
+ }
251
+ }
252
+ parts.push(octets[0] << 8 | octets[1]);
253
+ parts.push(octets[2] << 8 | octets[3]);
254
+ return parts;
255
+ }
256
+ }
257
+ return null;
258
+ };
259
+
260
+ IPUtilX.IPv4 = (function() {
261
+ function IPv4(octets) {
262
+ var octet, _i, _len;
263
+ if (octets.length !== 4) {
264
+ throw new Error("ipaddr: ipv4 octet count should be 4");
265
+ }
266
+ for (_i = 0, _len = octets.length; _i < _len; _i++) {
267
+ octet = octets[_i];
268
+ if (!((0 <= octet && octet <= 255))) {
269
+ throw new Error("ipaddr: ipv4 octet should fit in 8 bits");
270
+ }
271
+ }
272
+ this.octets = octets;
273
+ }
274
+
275
+ IPv4.prototype.kind = function() {
276
+ return 'ipv4';
277
+ };
278
+ IPv4.prototype.toString = function() {
279
+ return this.octets.join(".");
280
+ };
281
+ IPv4.prototype.toByteArray = function() {
282
+ return this.octets.slice(0);
283
+ };
284
+ IPv4.prototype.match = function(other, cidrRange) {
285
+ var _ref;
286
+ if (cidrRange === void 0) {
287
+ _ref = other, other = _ref[0], cidrRange = _ref[1];
288
+ }
289
+ if (other.kind() !== 'ipv4') {
290
+ throw new Error("ipaddr: cannot match ipv4 address with non-ipv4 one");
291
+ }
292
+ return matchCIDR(this.octets, other.octets, 8, cidrRange);
293
+ };
294
+ IPv4.prototype.SpecialRanges = {
295
+ unspecified: [[new IPv4([0, 0, 0, 0]), 8]],
296
+ broadcast: [[new IPv4([255, 255, 255, 255]), 32]],
297
+ multicast: [[new IPv4([224, 0, 0, 0]), 4]],
298
+ linkLocal: [[new IPv4([169, 254, 0, 0]), 16]],
299
+ loopback: [[new IPv4([127, 0, 0, 0]), 8]],
300
+ "private": [[new IPv4([10, 0, 0, 0]), 8], [new IPv4([172, 16, 0, 0]), 12], [new IPv4([192, 168, 0, 0]), 16]],
301
+ reserved: [[new IPv4([192, 0, 0, 0]), 24], [new IPv4([192, 0, 2, 0]), 24], [new IPv4([192, 88, 99, 0]), 24], [new IPv4([198, 51, 100, 0]), 24], [new IPv4([203, 0, 113, 0]), 24], [new IPv4([240, 0, 0, 0]), 4]]
302
+ };
303
+ IPv4.prototype.range = function() {
304
+ return IPUtilX.subnetMatch(this, this.SpecialRanges);
305
+ };
306
+ IPv4.prototype.toIPv4MappedAddress = function() {
307
+ return IPUtilX.IPv6.parse("::ffff:" + (this.toString()));
308
+ };
309
+ IPv4.prototype.prefixLengthFromSubnetMask = function() {
310
+ var cidr, i, octet, stop, zeros, zerotable, _i;
311
+ zerotable = {
312
+ 0: 8,
313
+ 128: 7,
314
+ 192: 6,
315
+ 224: 5,
316
+ 240: 4,
317
+ 248: 3,
318
+ 252: 2,
319
+ 254: 1,
320
+ 255: 0
321
+ };
322
+ cidr = 0;
323
+ stop = false;
324
+ for (i = _i = 3; _i >= 0; i = _i += -1) {
325
+ octet = this.octets[i];
326
+ if (octet in zerotable) {
327
+ zeros = zerotable[octet];
328
+ if (stop && zeros !== 0) {
329
+ return null;
330
+ }
331
+ if (zeros !== 8) {
332
+ stop = true;
333
+ }
334
+ cidr += zeros;
335
+ } else {
336
+ return null;
337
+ }
338
+ }
339
+ return 32 - cidr;
340
+ };
341
+ IPv4.prototype.isValid = function(string) {
342
+ var e;
343
+ try {
344
+ new this(this.parser(string));
345
+ return true;
346
+ } catch (_error) {
347
+ e = _error;
348
+ return false;
349
+ }
350
+ };
351
+ return IPv4;
352
+
353
+ })();
354
+ IPUtilX.IPv4.isValid = function(string) {
355
+ var e;
356
+ try {
357
+ new this(this.parser(string));
358
+ return true;
359
+ } catch (_error) {
360
+ e = _error;
361
+ return false;
362
+ }
363
+ };
364
+ IPUtilX.IPv4.parser = function(string) {
365
+ var match, parseIntAuto, part, shift, value;
366
+ parseIntAuto = function(string) {
367
+ if (string[0] === "0" && string[1] !== "x") {
368
+ return parseInt(string, 8);
369
+ } else {
370
+ return parseInt(string);
371
+ }
372
+ };
373
+ if (match = string.match(ipv4Regexes.fourOctet)) {
374
+ return (function() {
375
+ var _i, _len, _ref, _results;
376
+ _ref = match.slice(1, 6);
377
+ _results = [];
378
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
379
+ part = _ref[_i];
380
+ _results.push(parseIntAuto(part));
381
+ }
382
+ return _results;
383
+ })();
384
+ } else if (match = string.match(ipv4Regexes.longValue)) {
385
+ value = parseIntAuto(match[1]);
386
+ if (value > 0xffffffff || value < 0) {
387
+ throw new Error("ipaddr: address outside defined range");
388
+ }
389
+ return ((function() {
390
+ var _i, _results;
391
+ _results = [];
392
+ for (shift = _i = 0; _i <= 24; shift = _i += 8) {
393
+ _results.push((value >> shift) & 0xff);
394
+ }
395
+ return _results;
396
+ })()).reverse();
397
+ } else {
398
+ return null;
399
+ }
400
+ };
401
+ IPUtilX.IPv4.parse = IPUtilX.IPv6.parse = function(string) {
402
+ var parts;
403
+ parts = this.parser(string);
404
+ if (parts == null) {
405
+ throw new Error("ipaddr: string is not formatted like ip address");
406
+ }
407
+ return new this(parts);
408
+ };
409
+
410
+ IPUtilX.toString = function(ip) {
411
+ if(ip == null) {
412
+ return empty;
413
+ }
414
+
415
+ try{
416
+ if(typeof ip == 'number') {
417
+ ip = DataOutputX.toBytes(ip);
418
+ }
419
+
420
+ if(typeof ip == 'object' && ip.length == 4) {
421
+ var str = ip[0] & 0xff;
422
+ str += '.';
423
+ str += ip[1] & 0xff;
424
+ str += '.';
425
+ str += ip[2] & 0xff;
426
+ str += '.';
427
+ str += ip[3] & 0xff;
428
+ }
429
+ return str;
430
+ } catch(e) {
431
+ return empty;
432
+ };
433
+ };
434
+ IPUtilX.toBytes = function(ip) {
435
+ if(ip == null) {
436
+ return [0, 0, 0, 0];
437
+ }
438
+
439
+ if(typeof ip == 'number') {
440
+ return DataOutputX.toBytes(ip);
441
+ }
442
+
443
+ var result = new Array(4),
444
+ s = ip.split('.');
445
+
446
+ try {
447
+ if(s.length != 4) {
448
+ return [0, 0, 0, 0];
449
+ }
450
+ var val;
451
+ for(var i=0; i<4; i++) {
452
+ val = parseInt(s[i]);
453
+ if(val < 0 || val > 0xff) {
454
+ return [0, 0, 0, 0];
455
+ }
456
+ result[i] = (val & 0xff);
457
+ }
458
+ } catch(exeption) {
459
+ return [0, 0, 0, 0];
460
+ }
461
+ return result;
462
+ };
463
+ IPUtilX.isOK = function(ip) {
464
+ try {
465
+ return ip != null && ip.length == 4;
466
+ } catch(e) {
467
+ return false;
468
+ }
469
+ };
470
+ IPUtilX.isNotLocal = function(ip) {
471
+ return this.isOK(ip) && (ip[0] & 0xff) != 127;
472
+ };
473
+ IPUtilX.getIp = function(){
474
+ var interfaces = require('os').networkInterfaces();
475
+ var results = [];
476
+ Object
477
+ .keys(interfaces)
478
+ .map(function(key){
479
+ results = results.concat(interfaces[key]);
480
+ });
481
+
482
+ results = results
483
+ .filter(function(iface){
484
+ return iface.family === 'IPv4' && !iface.internal;
485
+ })
486
+ .map(function(iface){ return iface.address });
487
+ return (results[0] || "127.0.0.1");
488
+ }
489
+ IPUtilX.parse = function(string) {
490
+ if (IPUtilX.IPv6.isValid(string)) {
491
+ return IPUtilX.IPv6.parse(string);
492
+ } else if (IPUtilX.IPv4.isValid(string)) {
493
+ return IPUtilX.IPv4.parse(string);
494
+ } else {
495
+ throw new Error("ipaddr: the address has neither IPv6 nor IPv4 format");
496
+ }
497
+ };
498
+ IPUtilX.process = function(string) {
499
+ var addr;
500
+ addr = this.parse(string);
501
+ if (addr.kind() === 'ipv6' && addr.isIPv4MappedAddress()) {
502
+ return addr.toIPv4Address();
503
+ } else {
504
+ return addr;
505
+ }
506
+ };
507
+ IPUtilX.subnetMatch = function(address, rangeList, defaultName) {
508
+ var rangeName, rangeSubnets, subnet, _i, _len;
509
+ if (defaultName == null) {
510
+ defaultName = 'unicast';
511
+ }
512
+ for (rangeName in rangeList) {
513
+ rangeSubnets = rangeList[rangeName];
514
+ if (rangeSubnets[0] && !(rangeSubnets[0] instanceof Array)) {
515
+ rangeSubnets = [rangeSubnets];
516
+ }
517
+ for (_i = 0, _len = rangeSubnets.length; _i < _len; _i++) {
518
+ subnet = rangeSubnets[_i];
519
+ if (address.match.apply(address, subnet)) {
520
+ return rangeName;
521
+ }
522
+ }
523
+ }
524
+ return defaultName;
525
+ };
526
+
527
527
  module.exports = IPUtilX;