@libp2p/utils 5.2.2 → 5.2.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 +19 -0
- package/dist/src/multiaddr/is-private.js +2 -2
- package/dist/src/multiaddr/is-private.js.map +1 -1
- package/dist/src/peer-queue.d.ts +3 -3
- package/dist/src/peer-queue.d.ts.map +1 -1
- package/dist/src/peer-queue.js.map +1 -1
- package/dist/src/private-ip.d.ts +2 -0
- package/dist/src/private-ip.d.ts.map +1 -0
- package/dist/src/private-ip.js +60 -0
- package/dist/src/private-ip.js.map +1 -0
- package/dist/typedoc-urls.json +2 -2
- package/package.json +7 -2
- package/src/multiaddr/is-private.ts +2 -2
- package/src/peer-queue.ts +3 -3
- package/src/private-ip.ts +61 -0
package/README.md
CHANGED
|
@@ -5,6 +5,25 @@
|
|
|
5
5
|
|
|
6
6
|
> Package to aggregate shared logic and dependencies for the libp2p ecosystem
|
|
7
7
|
|
|
8
|
+
# About
|
|
9
|
+
|
|
10
|
+
The libp2p ecosystem has lots of repos with it comes several problems like:
|
|
11
|
+
|
|
12
|
+
- Domain logic dedupe - all modules shared a lot of logic like validation, streams handling, etc.
|
|
13
|
+
- Dependencies management - it's really easy with so many repos for dependencies to go out of control, they become outdated, different repos use different modules to do the same thing (like merging defaults options), browser bundles ends up with multiple versions of the same package, bumping versions is cumbersome to do because we need to go through several repos, etc.
|
|
14
|
+
|
|
15
|
+
These problems are the motivation for this package, having shared logic in this package avoids creating cyclic dependencies, centralizes common use modules/functions (exactly like aegir does for the tooling), semantic versioning for 3rd party dependencies is handled in one single place (a good example is going from streams 2 to 3) and maintainers should only care about having `libp2p-utils` updated.
|
|
16
|
+
|
|
17
|
+
## Example
|
|
18
|
+
|
|
19
|
+
Each function should be imported directly.
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
import ipAndPortToMultiaddr from '@libp2p/utils/ip-port-to-multiaddr'
|
|
23
|
+
|
|
24
|
+
const ma = ipAndPortToMultiaddr('127.0.0.1', 9000)
|
|
25
|
+
```
|
|
26
|
+
|
|
8
27
|
# Install
|
|
9
28
|
|
|
10
29
|
```console
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import
|
|
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(
|
|
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,
|
|
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"}
|
package/dist/src/peer-queue.d.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import { Queue, type QueueAddOptions } from './queue/index.js';
|
|
2
2
|
import type { Job } from './queue/job.js';
|
|
3
3
|
import type { PeerId } from '@libp2p/interface';
|
|
4
|
-
export interface
|
|
4
|
+
export interface PeerQueueJobOptions extends QueueAddOptions {
|
|
5
5
|
peerId: PeerId;
|
|
6
6
|
}
|
|
7
7
|
/**
|
|
8
8
|
* Extends Queue to add support for querying queued jobs by peer id
|
|
9
9
|
*/
|
|
10
|
-
export declare class PeerQueue<JobReturnType = void> extends Queue<JobReturnType,
|
|
10
|
+
export declare class PeerQueue<JobReturnType = void, JobOptions extends PeerQueueJobOptions = PeerQueueJobOptions> extends Queue<JobReturnType, JobOptions> {
|
|
11
11
|
has(peerId: PeerId): boolean;
|
|
12
|
-
find(peerId: PeerId): Job<
|
|
12
|
+
find(peerId: PeerId): Job<JobOptions, JobReturnType> | undefined;
|
|
13
13
|
}
|
|
14
14
|
//# sourceMappingURL=peer-queue.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"peer-queue.d.ts","sourceRoot":"","sources":["../../src/peer-queue.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,EAAE,KAAK,eAAe,EAAE,MAAM,kBAAkB,CAAA;AAC9D,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAA;AACzC,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAA;AAE/C,MAAM,WAAW,
|
|
1
|
+
{"version":3,"file":"peer-queue.d.ts","sourceRoot":"","sources":["../../src/peer-queue.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,EAAE,KAAK,eAAe,EAAE,MAAM,kBAAkB,CAAA;AAC9D,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAA;AACzC,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAA;AAE/C,MAAM,WAAW,mBAAoB,SAAQ,eAAe;IAC1D,MAAM,EAAE,MAAM,CAAA;CACf;AAED;;GAEG;AACH,qBAAa,SAAS,CAAC,aAAa,GAAG,IAAI,EAAE,UAAU,SAAS,mBAAmB,GAAG,mBAAmB,CAAE,SAAQ,KAAK,CAAC,aAAa,EAAE,UAAU,CAAC;IACjJ,GAAG,CAAE,MAAM,EAAE,MAAM,GAAG,OAAO;IAI7B,IAAI,CAAE,MAAM,EAAE,MAAM,GAAG,GAAG,CAAC,UAAU,EAAE,aAAa,CAAC,GAAG,SAAS;CAKlE"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"peer-queue.js","sourceRoot":"","sources":["../../src/peer-queue.ts"],"names":[],"mappings":"AAAA,6DAA6D;AAE7D,OAAO,EAAE,KAAK,EAAwB,MAAM,kBAAkB,CAAA;AAQ9D;;GAEG;AACH,MAAM,OAAO,
|
|
1
|
+
{"version":3,"file":"peer-queue.js","sourceRoot":"","sources":["../../src/peer-queue.ts"],"names":[],"mappings":"AAAA,6DAA6D;AAE7D,OAAO,EAAE,KAAK,EAAwB,MAAM,kBAAkB,CAAA;AAQ9D;;GAEG;AACH,MAAM,OAAO,SAA8F,SAAQ,KAAgC;IACjJ,GAAG,CAAE,MAAc;QACjB,OAAO,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,IAAI,CAAA;IAClC,CAAC;IAED,IAAI,CAAE,MAAc;QAClB,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE;YAC3B,OAAO,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,MAAM,CAAC,CAAA;QAC1C,CAAC,CAAC,CAAA;IACJ,CAAC;CACF"}
|
|
@@ -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,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAA;AACjD,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAA;AAEjC,MAAM,iBAAiB,GAAG;IACxB,WAAW;IACX,YAAY;IACZ,eAAe;IACf,aAAa;IACb,gBAAgB;IAChB,eAAe;IACf,cAAc;IACd,cAAc;IACd,cAAc;IACd,cAAc;IACd,eAAe;IACf,gBAAgB;IAChB,gBAAgB;IAChB,cAAc;IACd,iBAAiB;IACjB,iBAAiB;IACjB,gBAAgB;IAChB,gBAAgB;IAChB,iBAAiB;IACjB,eAAe;IACf,iBAAiB;IACjB,gBAAgB;IAChB,aAAa;IACb,oBAAoB;CACrB,CAAA;AAED,MAAM,cAAc,GAAG,iBAAiB,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,CAAC,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC,CAAA;AAE7E,SAAS,SAAS,CAAE,MAAc;IAChC,KAAK,MAAM,CAAC,IAAI,cAAc,EAAE,CAAC;QAC/B,IAAI,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC;YAAE,OAAO,IAAI,CAAA;IACrC,CAAC;IAED,OAAO,KAAK,CAAA;AACd,CAAC;AAED,SAAS,SAAS,CAAE,MAAc;IAChC,OAAO,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC;QACxB,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC;QACpB,iEAAiE,CAAC,IAAI,CAAC,MAAM,CAAC;QAC9E,mEAAmE,CAAC,IAAI,CAAC,MAAM,CAAC;QAChF,mEAAmE,CAAC,IAAI,CAAC,MAAM,CAAC;QAChF,uFAAuF,CAAC,IAAI,CAAC,MAAM,CAAC;QACpG,gIAAgI,CAAC,IAAI,CAAC,MAAM,CAAC;QAC7I,4IAA4I,CAAC,IAAI,CAAC,MAAM,CAAC;QACzJ,mIAAmI,CAAC,IAAI,CAAC,MAAM,CAAC;QAChJ,mJAAmJ,CAAC,IAAI,CAAC,MAAM,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/dist/typedoc-urls.json
CHANGED
|
@@ -27,8 +27,8 @@
|
|
|
27
27
|
"./multiaddr/is-private:isPrivate": "https://libp2p.github.io/js-libp2p/functions/_libp2p_utils.multiaddr_is_private.isPrivate.html",
|
|
28
28
|
"PeerQueue": "https://libp2p.github.io/js-libp2p/classes/_libp2p_utils.peer_queue.PeerQueue.html",
|
|
29
29
|
"./peer-queue:PeerQueue": "https://libp2p.github.io/js-libp2p/classes/_libp2p_utils.peer_queue.PeerQueue.html",
|
|
30
|
-
"
|
|
31
|
-
"./peer-queue:
|
|
30
|
+
"PeerQueueJobOptions": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.peer_queue.PeerQueueJobOptions.html",
|
|
31
|
+
"./peer-queue:PeerQueueJobOptions": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.peer_queue.PeerQueueJobOptions.html",
|
|
32
32
|
"Queue": "https://libp2p.github.io/js-libp2p/classes/_libp2p_utils.queue.Queue.html",
|
|
33
33
|
"./queue:Queue": "https://libp2p.github.io/js-libp2p/classes/_libp2p_utils.queue.Queue.html",
|
|
34
34
|
"JobMatcher": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_utils.queue.JobMatcher.html",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/utils",
|
|
3
|
-
"version": "5.2.
|
|
3
|
+
"version": "5.2.3",
|
|
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"
|
|
@@ -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
146
|
"@libp2p/peer-id-factory": "^4.0.5",
|
|
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
|
|
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(
|
|
11
|
+
return Boolean(isPrivateIp(address))
|
|
12
12
|
} catch {
|
|
13
13
|
return true
|
|
14
14
|
}
|
package/src/peer-queue.ts
CHANGED
|
@@ -4,19 +4,19 @@ import { Queue, type QueueAddOptions } from './queue/index.js'
|
|
|
4
4
|
import type { Job } from './queue/job.js'
|
|
5
5
|
import type { PeerId } from '@libp2p/interface'
|
|
6
6
|
|
|
7
|
-
export interface
|
|
7
|
+
export interface PeerQueueJobOptions extends QueueAddOptions {
|
|
8
8
|
peerId: PeerId
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* Extends Queue to add support for querying queued jobs by peer id
|
|
13
13
|
*/
|
|
14
|
-
export class PeerQueue<JobReturnType = void> extends Queue<JobReturnType,
|
|
14
|
+
export class PeerQueue<JobReturnType = void, JobOptions extends PeerQueueJobOptions = PeerQueueJobOptions> extends Queue<JobReturnType, JobOptions> {
|
|
15
15
|
has (peerId: PeerId): boolean {
|
|
16
16
|
return this.find(peerId) != null
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
find (peerId: PeerId): Job<
|
|
19
|
+
find (peerId: PeerId): Job<JobOptions, JobReturnType> | undefined {
|
|
20
20
|
return this.queue.find(job => {
|
|
21
21
|
return peerId.equals(job.options.peerId)
|
|
22
22
|
})
|
|
@@ -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
|
+
}
|