@libp2p/utils 5.2.2-9376e61a1 → 5.2.2-f27138ca1

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,11 +1,11 @@
1
- import isIpPrivate from 'private-ip';
1
+ import { isPrivateIp } from '../private-ip.js';
2
2
  /**
3
3
  * Check if a given multiaddr has a private address.
4
4
  */
5
5
  export function isPrivate(ma) {
6
6
  try {
7
7
  const { address } = ma.nodeAddress();
8
- return Boolean(isIpPrivate(address));
8
+ return Boolean(isPrivateIp(address));
9
9
  }
10
10
  catch {
11
11
  return true;
@@ -1 +1 @@
1
- {"version":3,"file":"is-private.js","sourceRoot":"","sources":["../../../src/multiaddr/is-private.ts"],"names":[],"mappings":"AAAA,OAAO,WAAW,MAAM,YAAY,CAAA;AAGpC;;GAEG;AACH,MAAM,UAAU,SAAS,CAAE,EAAa;IACtC,IAAI,CAAC;QACH,MAAM,EAAE,OAAO,EAAE,GAAG,EAAE,CAAC,WAAW,EAAE,CAAA;QAEpC,OAAO,OAAO,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC,CAAA;IACtC,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAA;IACb,CAAC;AACH,CAAC"}
1
+ {"version":3,"file":"is-private.js","sourceRoot":"","sources":["../../../src/multiaddr/is-private.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAG9C;;GAEG;AACH,MAAM,UAAU,SAAS,CAAE,EAAa;IACtC,IAAI,CAAC;QACH,MAAM,EAAE,OAAO,EAAE,GAAG,EAAE,CAAC,WAAW,EAAE,CAAA;QAEpC,OAAO,OAAO,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC,CAAA;IACtC,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAA;IACb,CAAC;AACH,CAAC"}
@@ -0,0 +1,2 @@
1
+ export declare function isPrivateIp(ip: string): boolean | undefined;
2
+ //# sourceMappingURL=private-ip.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"private-ip.d.ts","sourceRoot":"","sources":["../../src/private-ip.ts"],"names":[],"mappings":"AAwDA,wBAAgB,WAAW,CAAE,EAAE,EAAE,MAAM,GAAG,OAAO,GAAG,SAAS,CAI5D"}
@@ -0,0 +1,60 @@
1
+ import { isIPv4, isIPv6 } from '@chainsafe/is-ip';
2
+ import { Netmask } from 'netmask';
3
+ const PRIVATE_IP_RANGES = [
4
+ '0.0.0.0/8',
5
+ '10.0.0.0/8',
6
+ '100.64.0.0/10',
7
+ '127.0.0.0/8',
8
+ '169.254.0.0/16',
9
+ '172.16.0.0/12',
10
+ '192.0.0.0/24',
11
+ '192.0.0.0/29',
12
+ '192.0.0.8/32',
13
+ '192.0.0.9/32',
14
+ '192.0.0.10/32',
15
+ '192.0.0.170/32',
16
+ '192.0.0.171/32',
17
+ '192.0.2.0/24',
18
+ '192.31.196.0/24',
19
+ '192.52.193.0/24',
20
+ '192.88.99.0/24',
21
+ '192.168.0.0/16',
22
+ '192.175.48.0/24',
23
+ '198.18.0.0/15',
24
+ '198.51.100.0/24',
25
+ '203.0.113.0/24',
26
+ '240.0.0.0/4',
27
+ '255.255.255.255/32'
28
+ ];
29
+ const NETMASK_RANGES = PRIVATE_IP_RANGES.map(ipRange => new Netmask(ipRange));
30
+ function ipv4Check(ipAddr) {
31
+ for (const r of NETMASK_RANGES) {
32
+ if (r.contains(ipAddr))
33
+ return true;
34
+ }
35
+ return false;
36
+ }
37
+ function ipv6Check(ipAddr) {
38
+ return /^::$/.test(ipAddr) ||
39
+ /^::1$/.test(ipAddr) ||
40
+ /^::f{4}:([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/.test(ipAddr) ||
41
+ /^::f{4}:0.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/.test(ipAddr) ||
42
+ /^64:ff9b::([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/.test(ipAddr) ||
43
+ /^100::([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4})$/.test(ipAddr) ||
44
+ /^2001::([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4})$/.test(ipAddr) ||
45
+ /^2001:2[0-9a-fA-F]:([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4})$/.test(ipAddr) ||
46
+ /^2001:db8:([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4})$/.test(ipAddr) ||
47
+ /^2002:([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4})$/.test(ipAddr) ||
48
+ /^f[c-d]([0-9a-fA-F]{2,2}):/i.test(ipAddr) ||
49
+ /^fe[8-9a-bA-B][0-9a-fA-F]:/i.test(ipAddr) ||
50
+ /^ff([0-9a-fA-F]{2,2}):/i.test(ipAddr);
51
+ }
52
+ export function isPrivateIp(ip) {
53
+ if (isIPv4(ip))
54
+ return ipv4Check(ip);
55
+ else if (isIPv6(ip))
56
+ return ipv6Check(ip);
57
+ else
58
+ return undefined;
59
+ }
60
+ //# sourceMappingURL=private-ip.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"private-ip.js","sourceRoot":"","sources":["../../src/private-ip.ts"],"names":[],"mappings":"AAAA,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AM,CAAC;QAChK,6BAA6B,CAAC,IAAI,CAAC,MAAM,CAAC;QAC1C,6BAA6B,CAAC,IAAI,CAAC,MAAM,CAAC;QAC1C,yBAAyB,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;AAC1C,CAAC;AAED,MAAM,UAAU,WAAW,CAAE,EAAU;IACrC,IAAI,MAAM,CAAC,EAAE,CAAC;QAAE,OAAO,SAAS,CAAC,EAAE,CAAC,CAAA;SAC/B,IAAI,MAAM,CAAC,EAAE,CAAC;QAAE,OAAO,SAAS,CAAC,EAAE,CAAC,CAAA;;QACpC,OAAO,SAAS,CAAA;AACvB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@libp2p/utils",
3
- "version": "5.2.2-9376e61a1",
3
+ "version": "5.2.2-f27138ca1",
4
4
  "description": "Package to aggregate shared logic and dependencies for the libp2p ecosystem",
5
5
  "license": "Apache-2.0 OR MIT",
6
6
  "homepage": "https://github.com/libp2p/js-libp2p/tree/main/packages/utils#readme",
@@ -80,6 +80,10 @@
80
80
  "types": "./dist/src/peer-queue.d.ts",
81
81
  "import": "./dist/src/peer-queue.js"
82
82
  },
83
+ "./private-ip": {
84
+ "types": "./dist/src/private-ip.d.ts",
85
+ "import": "./dist/src/private-ip.js"
86
+ },
83
87
  "./queue": {
84
88
  "types": "./dist/src/queue/index.d.ts",
85
89
  "import": "./dist/src/queue/index.js"
@@ -123,8 +127,8 @@
123
127
  },
124
128
  "dependencies": {
125
129
  "@chainsafe/is-ip": "^2.0.2",
126
- "@libp2p/interface": "1.1.2-9376e61a1",
127
- "@libp2p/logger": "4.0.5-9376e61a1",
130
+ "@libp2p/interface": "1.1.2-f27138ca1",
131
+ "@libp2p/logger": "4.0.5-f27138ca1",
128
132
  "@multiformats/multiaddr": "^12.1.10",
129
133
  "@multiformats/multiaddr-matcher": "^1.1.0",
130
134
  "delay": "^6.0.0",
@@ -132,14 +136,15 @@
132
136
  "is-loopback-addr": "^2.0.1",
133
137
  "it-pushable": "^3.2.3",
134
138
  "it-stream-types": "^2.0.1",
139
+ "netmask": "^2.0.2",
135
140
  "p-defer": "^4.0.0",
136
- "private-ip": "^3.0.1",
137
141
  "race-event": "^1.1.0",
138
142
  "race-signal": "^1.0.2",
139
143
  "uint8arraylist": "^2.4.7"
140
144
  },
141
145
  "devDependencies": {
142
- "@libp2p/peer-id-factory": "4.0.5-9376e61a1",
146
+ "@libp2p/peer-id-factory": "4.0.5-f27138ca1",
147
+ "@types/netmask": "^2.0.5",
143
148
  "aegir": "^42.0.0",
144
149
  "delay": "^6.0.0",
145
150
  "it-all": "^3.0.3",
@@ -1,4 +1,4 @@
1
- import isIpPrivate from 'private-ip'
1
+ import { isPrivateIp } from '../private-ip.js'
2
2
  import type { Multiaddr } from '@multiformats/multiaddr'
3
3
 
4
4
  /**
@@ -8,7 +8,7 @@ export function isPrivate (ma: Multiaddr): boolean {
8
8
  try {
9
9
  const { address } = ma.nodeAddress()
10
10
 
11
- return Boolean(isIpPrivate(address))
11
+ return Boolean(isPrivateIp(address))
12
12
  } catch {
13
13
  return true
14
14
  }
@@ -0,0 +1,61 @@
1
+ import { isIPv4, isIPv6 } from '@chainsafe/is-ip'
2
+ import { Netmask } from 'netmask'
3
+
4
+ const PRIVATE_IP_RANGES = [
5
+ '0.0.0.0/8',
6
+ '10.0.0.0/8',
7
+ '100.64.0.0/10',
8
+ '127.0.0.0/8',
9
+ '169.254.0.0/16',
10
+ '172.16.0.0/12',
11
+ '192.0.0.0/24',
12
+ '192.0.0.0/29',
13
+ '192.0.0.8/32',
14
+ '192.0.0.9/32',
15
+ '192.0.0.10/32',
16
+ '192.0.0.170/32',
17
+ '192.0.0.171/32',
18
+ '192.0.2.0/24',
19
+ '192.31.196.0/24',
20
+ '192.52.193.0/24',
21
+ '192.88.99.0/24',
22
+ '192.168.0.0/16',
23
+ '192.175.48.0/24',
24
+ '198.18.0.0/15',
25
+ '198.51.100.0/24',
26
+ '203.0.113.0/24',
27
+ '240.0.0.0/4',
28
+ '255.255.255.255/32'
29
+ ]
30
+
31
+ const NETMASK_RANGES = PRIVATE_IP_RANGES.map(ipRange => new Netmask(ipRange))
32
+
33
+ function ipv4Check (ipAddr: string): boolean {
34
+ for (const r of NETMASK_RANGES) {
35
+ if (r.contains(ipAddr)) return true
36
+ }
37
+
38
+ return false
39
+ }
40
+
41
+ function ipv6Check (ipAddr: string): boolean {
42
+ return /^::$/.test(ipAddr) ||
43
+ /^::1$/.test(ipAddr) ||
44
+ /^::f{4}:([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/.test(ipAddr) ||
45
+ /^::f{4}:0.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/.test(ipAddr) ||
46
+ /^64:ff9b::([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/.test(ipAddr) ||
47
+ /^100::([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4})$/.test(ipAddr) ||
48
+ /^2001::([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4})$/.test(ipAddr) ||
49
+ /^2001:2[0-9a-fA-F]:([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4})$/.test(ipAddr) ||
50
+ /^2001:db8:([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4})$/.test(ipAddr) ||
51
+ /^2002:([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4})$/.test(ipAddr) ||
52
+ /^f[c-d]([0-9a-fA-F]{2,2}):/i.test(ipAddr) ||
53
+ /^fe[8-9a-bA-B][0-9a-fA-F]:/i.test(ipAddr) ||
54
+ /^ff([0-9a-fA-F]{2,2}):/i.test(ipAddr)
55
+ }
56
+
57
+ export function isPrivateIp (ip: string): boolean | undefined {
58
+ if (isIPv4(ip)) return ipv4Check(ip)
59
+ else if (isIPv6(ip)) return ipv6Check(ip)
60
+ else return undefined
61
+ }