@libp2p/utils 7.4.5-fab409544 → 7.4.5-ff95dc546
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/dist/index.min.js +4 -4
- package/dist/index.min.js.map +4 -4
- package/dist/src/private-ip.d.ts.map +1 -1
- package/dist/src/private-ip.js +33 -62
- package/dist/src/private-ip.js.map +1 -1
- package/package.json +5 -7
- package/src/private-ip.ts +34 -73
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"private-ip.d.ts","sourceRoot":"","sources":["../../src/private-ip.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"private-ip.d.ts","sourceRoot":"","sources":["../../src/private-ip.ts"],"names":[],"mappings":"AAmDA,wBAAgB,WAAW,CAAE,EAAE,EAAE,MAAM,GAAG,OAAO,GAAG,SAAS,CAgB5D"}
|
package/dist/src/private-ip.js
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
import { isIPv4, isIPv6 } from '@chainsafe/is-ip';
|
|
2
|
-
import {
|
|
2
|
+
import { parseIP } from '@chainsafe/is-ip/parse';
|
|
3
|
+
import { IpNet, iPv4FromIPv6, isIPv4mappedIPv6 } from '@chainsafe/netmask';
|
|
4
|
+
/**
|
|
5
|
+
* Private, reserved or special-purpose IP ranges
|
|
6
|
+
*
|
|
7
|
+
* Addresses are matched by their parsed bytes so the result does not depend on
|
|
8
|
+
* how an address is spelled (IPv6 compression, leading zeros, case).
|
|
9
|
+
*
|
|
10
|
+
* @see https://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml
|
|
11
|
+
* @see https://www.iana.org/assignments/iana-ipv6-special-registry/iana-ipv6-special-registry.xhtml
|
|
12
|
+
*/
|
|
3
13
|
const PRIVATE_IP_RANGES = [
|
|
4
14
|
'0.0.0.0/8',
|
|
5
15
|
'10.0.0.0/8',
|
|
@@ -24,69 +34,30 @@ const PRIVATE_IP_RANGES = [
|
|
|
24
34
|
'198.51.100.0/24',
|
|
25
35
|
'203.0.113.0/24',
|
|
26
36
|
'240.0.0.0/4',
|
|
27
|
-
'255.255.255.255/32'
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* @see https://datatracker.ietf.org/doc/html/rfc4291#section-2.5.5.2
|
|
43
|
-
*/
|
|
44
|
-
function ipv4MappedIpv6Check(ipAddr) {
|
|
45
|
-
const parts = ipAddr.split(':');
|
|
46
|
-
if (parts.length < 2) {
|
|
47
|
-
return false;
|
|
48
|
-
}
|
|
49
|
-
const octet34 = parts[parts.length - 1].padStart(4, '0');
|
|
50
|
-
const octet12 = parts[parts.length - 2].padStart(4, '0');
|
|
51
|
-
const ip4 = `${parseInt(octet12.substring(0, 2), 16)}.${parseInt(octet12.substring(2), 16)}.${parseInt(octet34.substring(0, 2), 16)}.${parseInt(octet34.substring(2), 16)}`;
|
|
52
|
-
return ipv4Check(ip4);
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* @see https://datatracker.ietf.org/doc/html/rfc4291#section-2.2 example 3
|
|
56
|
-
*/
|
|
57
|
-
function isIpv4EmbeddedIpv6(ipAddr) {
|
|
58
|
-
return /^::ffff:([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/.test(ipAddr);
|
|
59
|
-
}
|
|
60
|
-
function ipv4EmbeddedIpv6Check(ipAddr) {
|
|
61
|
-
const parts = ipAddr.split(':');
|
|
62
|
-
const ip4 = parts[parts.length - 1];
|
|
63
|
-
return ipv4Check(ip4);
|
|
64
|
-
}
|
|
65
|
-
function ipv6Check(ipAddr) {
|
|
66
|
-
return /^::$/.test(ipAddr) ||
|
|
67
|
-
/^::1$/.test(ipAddr) ||
|
|
68
|
-
/^64:ff9b::([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/.test(ipAddr) ||
|
|
69
|
-
/^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) ||
|
|
70
|
-
/^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) ||
|
|
71
|
-
/^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) ||
|
|
72
|
-
/^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) ||
|
|
73
|
-
/^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) ||
|
|
74
|
-
/^f[c-d]([0-9a-fA-F]{2,2}):/i.test(ipAddr) ||
|
|
75
|
-
/^fe[8-9a-bA-B][0-9a-fA-F]:/i.test(ipAddr) ||
|
|
76
|
-
/^ff([0-9a-fA-F]{2,2}):/i.test(ipAddr);
|
|
77
|
-
}
|
|
37
|
+
'255.255.255.255/32',
|
|
38
|
+
'::/128', // unspecified (RFC 4291)
|
|
39
|
+
'::1/128', // loopback (RFC 4291)
|
|
40
|
+
'64:ff9b::/96', // IPv4/IPv6 translation (RFC 6052)
|
|
41
|
+
'100::/64', // discard-only (RFC 6666)
|
|
42
|
+
'2001::/32', // Teredo (RFC 4380)
|
|
43
|
+
'2001:20::/28', // ORCHIDv2 (RFC 7343)
|
|
44
|
+
'2001:db8::/32', // documentation (RFC 3849)
|
|
45
|
+
'2002::/16', // 6to4 (RFC 3056)
|
|
46
|
+
'fc00::/7', // unique local (RFC 4193)
|
|
47
|
+
'fe80::/10', // link-local unicast (RFC 4291)
|
|
48
|
+
'ff00::/8' // multicast (RFC 4291)
|
|
49
|
+
].map(cidr => new IpNet(cidr));
|
|
78
50
|
export function isPrivateIp(ip) {
|
|
79
|
-
if (isIPv4(ip)) {
|
|
80
|
-
return
|
|
81
|
-
}
|
|
82
|
-
if (isIpv4MappedIpv6(ip)) {
|
|
83
|
-
return ipv4MappedIpv6Check(ip);
|
|
84
|
-
}
|
|
85
|
-
if (isIpv4EmbeddedIpv6(ip)) {
|
|
86
|
-
return ipv4EmbeddedIpv6Check(ip);
|
|
51
|
+
if (!isIPv4(ip) && !isIPv6(ip)) {
|
|
52
|
+
return;
|
|
87
53
|
}
|
|
88
|
-
|
|
89
|
-
|
|
54
|
+
const parsed = parseIP(ip);
|
|
55
|
+
if (parsed == null) {
|
|
56
|
+
return;
|
|
90
57
|
}
|
|
58
|
+
// check IPv4-mapped IPv6 (::ffff:0:0/96) against the IPv4 ranges, the length
|
|
59
|
+
// check matters as isIPv4mappedIPv6 also matches 0.0.0.0
|
|
60
|
+
const addr = parsed.length === 16 && isIPv4mappedIPv6(parsed) ? iPv4FromIPv6(parsed) : parsed;
|
|
61
|
+
return PRIVATE_IP_RANGES.some(net => net.contains(addr));
|
|
91
62
|
}
|
|
92
63
|
//# sourceMappingURL=private-ip.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"private-ip.js","sourceRoot":"","sources":["../../src/private-ip.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAA;AACjD,OAAO,EAAE,OA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AI,MAAM,IAAI,IAAI,EAAE,CAAC;QACnB,OAAM;IACR,CAAC;IAED,6EAA6E;IAC7E,yDAAyD;IACzD,MAAM,IAAI,GAAG,MAAM,CAAC,MAAM,KAAK,EAAE,IAAI,gBAAgB,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAA;IAE7F,OAAO,iBAAiB,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAA;AAC1D,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/utils",
|
|
3
|
-
"version": "7.4.5-
|
|
3
|
+
"version": "7.4.5-ff95dc546",
|
|
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",
|
|
@@ -47,8 +47,8 @@
|
|
|
47
47
|
"dependencies": {
|
|
48
48
|
"@chainsafe/is-ip": "^2.1.0",
|
|
49
49
|
"@chainsafe/netmask": "^2.0.0",
|
|
50
|
-
"@libp2p/interface": "3.4.0-
|
|
51
|
-
"@libp2p/logger": "6.2.16-
|
|
50
|
+
"@libp2p/interface": "3.4.0-ff95dc546",
|
|
51
|
+
"@libp2p/logger": "6.2.16-ff95dc546",
|
|
52
52
|
"@multiformats/multiaddr": "^13.0.3",
|
|
53
53
|
"@multiformats/multiaddr-matcher": "^3.0.2",
|
|
54
54
|
"@sindresorhus/fnv1a": "^3.1.0",
|
|
@@ -61,7 +61,6 @@
|
|
|
61
61
|
"it-pushable": "^3.2.3",
|
|
62
62
|
"it-stream-types": "^2.0.2",
|
|
63
63
|
"main-event": "^1.0.5",
|
|
64
|
-
"netmask": "^2.0.2",
|
|
65
64
|
"p-defer": "^4.0.1",
|
|
66
65
|
"p-event": "^8.0.1",
|
|
67
66
|
"progress-events": "^1.1.0",
|
|
@@ -72,9 +71,8 @@
|
|
|
72
71
|
"uint8arrays": "^6.1.1"
|
|
73
72
|
},
|
|
74
73
|
"devDependencies": {
|
|
75
|
-
"@libp2p/crypto": "5.2.0-
|
|
76
|
-
"@libp2p/peer-id": "6.0.18-
|
|
77
|
-
"@types/netmask": "^2.0.5",
|
|
74
|
+
"@libp2p/crypto": "5.2.0-ff95dc546",
|
|
75
|
+
"@libp2p/peer-id": "6.0.18-ff95dc546",
|
|
78
76
|
"aegir": "^48.1.1",
|
|
79
77
|
"benchmark": "^2.1.4",
|
|
80
78
|
"it-all": "^3.0.9",
|
package/src/private-ip.ts
CHANGED
|
@@ -1,6 +1,16 @@
|
|
|
1
1
|
import { isIPv4, isIPv6 } from '@chainsafe/is-ip'
|
|
2
|
-
import {
|
|
2
|
+
import { parseIP } from '@chainsafe/is-ip/parse'
|
|
3
|
+
import { IpNet, iPv4FromIPv6, isIPv4mappedIPv6 } from '@chainsafe/netmask'
|
|
3
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Private, reserved or special-purpose IP ranges
|
|
7
|
+
*
|
|
8
|
+
* Addresses are matched by their parsed bytes so the result does not depend on
|
|
9
|
+
* how an address is spelled (IPv6 compression, leading zeros, case).
|
|
10
|
+
*
|
|
11
|
+
* @see https://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml
|
|
12
|
+
* @see https://www.iana.org/assignments/iana-ipv6-special-registry/iana-ipv6-special-registry.xhtml
|
|
13
|
+
*/
|
|
4
14
|
const PRIVATE_IP_RANGES = [
|
|
5
15
|
'0.0.0.0/8',
|
|
6
16
|
'10.0.0.0/8',
|
|
@@ -25,83 +35,34 @@ const PRIVATE_IP_RANGES = [
|
|
|
25
35
|
'198.51.100.0/24',
|
|
26
36
|
'203.0.113.0/24',
|
|
27
37
|
'240.0.0.0/4',
|
|
28
|
-
'255.255.255.255/32'
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
function isIpv4MappedIpv6 (ipAddr: string): boolean {
|
|
42
|
-
return /^::ffff:([0-9a-fA-F]{1,4}):([0-9a-fA-F]{1,4})$/.test(ipAddr)
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* @see https://datatracker.ietf.org/doc/html/rfc4291#section-2.5.5.2
|
|
47
|
-
*/
|
|
48
|
-
function ipv4MappedIpv6Check (ipAddr: string): boolean {
|
|
49
|
-
const parts = ipAddr.split(':')
|
|
50
|
-
|
|
51
|
-
if (parts.length < 2) {
|
|
52
|
-
return false
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
const octet34 = parts[parts.length - 1].padStart(4, '0')
|
|
56
|
-
const octet12 = parts[parts.length - 2].padStart(4, '0')
|
|
57
|
-
|
|
58
|
-
const ip4 = `${parseInt(octet12.substring(0, 2), 16)}.${parseInt(octet12.substring(2), 16)}.${parseInt(octet34.substring(0, 2), 16)}.${parseInt(octet34.substring(2), 16)}`
|
|
59
|
-
|
|
60
|
-
return ipv4Check(ip4)
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* @see https://datatracker.ietf.org/doc/html/rfc4291#section-2.2 example 3
|
|
65
|
-
*/
|
|
66
|
-
function isIpv4EmbeddedIpv6 (ipAddr: string): boolean {
|
|
67
|
-
return /^::ffff:([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/.test(ipAddr)
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
function ipv4EmbeddedIpv6Check (ipAddr: string): boolean {
|
|
71
|
-
const parts = ipAddr.split(':')
|
|
72
|
-
const ip4 = parts[parts.length - 1]
|
|
73
|
-
|
|
74
|
-
return ipv4Check(ip4)
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
function ipv6Check (ipAddr: string): boolean {
|
|
78
|
-
return /^::$/.test(ipAddr) ||
|
|
79
|
-
/^::1$/.test(ipAddr) ||
|
|
80
|
-
/^64:ff9b::([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/.test(ipAddr) ||
|
|
81
|
-
/^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) ||
|
|
82
|
-
/^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) ||
|
|
83
|
-
/^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) ||
|
|
84
|
-
/^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) ||
|
|
85
|
-
/^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) ||
|
|
86
|
-
/^f[c-d]([0-9a-fA-F]{2,2}):/i.test(ipAddr) ||
|
|
87
|
-
/^fe[8-9a-bA-B][0-9a-fA-F]:/i.test(ipAddr) ||
|
|
88
|
-
/^ff([0-9a-fA-F]{2,2}):/i.test(ipAddr)
|
|
89
|
-
}
|
|
38
|
+
'255.255.255.255/32',
|
|
39
|
+
'::/128', // unspecified (RFC 4291)
|
|
40
|
+
'::1/128', // loopback (RFC 4291)
|
|
41
|
+
'64:ff9b::/96', // IPv4/IPv6 translation (RFC 6052)
|
|
42
|
+
'100::/64', // discard-only (RFC 6666)
|
|
43
|
+
'2001::/32', // Teredo (RFC 4380)
|
|
44
|
+
'2001:20::/28', // ORCHIDv2 (RFC 7343)
|
|
45
|
+
'2001:db8::/32', // documentation (RFC 3849)
|
|
46
|
+
'2002::/16', // 6to4 (RFC 3056)
|
|
47
|
+
'fc00::/7', // unique local (RFC 4193)
|
|
48
|
+
'fe80::/10', // link-local unicast (RFC 4291)
|
|
49
|
+
'ff00::/8' // multicast (RFC 4291)
|
|
50
|
+
].map(cidr => new IpNet(cidr))
|
|
90
51
|
|
|
91
52
|
export function isPrivateIp (ip: string): boolean | undefined {
|
|
92
|
-
if (isIPv4(ip)) {
|
|
93
|
-
return
|
|
53
|
+
if (!isIPv4(ip) && !isIPv6(ip)) {
|
|
54
|
+
return
|
|
94
55
|
}
|
|
95
56
|
|
|
96
|
-
|
|
97
|
-
return ipv4MappedIpv6Check(ip)
|
|
98
|
-
}
|
|
57
|
+
const parsed = parseIP(ip)
|
|
99
58
|
|
|
100
|
-
if (
|
|
101
|
-
return
|
|
59
|
+
if (parsed == null) {
|
|
60
|
+
return
|
|
102
61
|
}
|
|
103
62
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
63
|
+
// check IPv4-mapped IPv6 (::ffff:0:0/96) against the IPv4 ranges, the length
|
|
64
|
+
// check matters as isIPv4mappedIPv6 also matches 0.0.0.0
|
|
65
|
+
const addr = parsed.length === 16 && isIPv4mappedIPv6(parsed) ? iPv4FromIPv6(parsed) : parsed
|
|
66
|
+
|
|
67
|
+
return PRIVATE_IP_RANGES.some(net => net.contains(addr))
|
|
107
68
|
}
|