libp2p 2.10.0-8484de8a2 → 2.10.0-87bc8d4fb
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 +17 -13
- package/dist/index.min.js.map +4 -4
- package/dist/src/address-manager/dns-mappings.d.ts +2 -1
- package/dist/src/address-manager/dns-mappings.d.ts.map +1 -1
- package/dist/src/address-manager/dns-mappings.js +43 -39
- package/dist/src/address-manager/dns-mappings.js.map +1 -1
- package/dist/src/address-manager/index.d.ts.map +1 -1
- package/dist/src/address-manager/index.js +14 -31
- package/dist/src/address-manager/index.js.map +1 -1
- package/dist/src/address-manager/ip-mappings.d.ts +0 -1
- package/dist/src/address-manager/ip-mappings.d.ts.map +1 -1
- package/dist/src/address-manager/ip-mappings.js +40 -51
- package/dist/src/address-manager/ip-mappings.js.map +1 -1
- package/dist/src/address-manager/observed-addresses.d.ts.map +1 -1
- package/dist/src/address-manager/observed-addresses.js +3 -1
- package/dist/src/address-manager/observed-addresses.js.map +1 -1
- package/dist/src/address-manager/transport-addresses.d.ts.map +1 -1
- package/dist/src/address-manager/transport-addresses.js +8 -6
- package/dist/src/address-manager/transport-addresses.js.map +1 -1
- package/dist/src/config/connection-gater.browser.d.ts.map +1 -1
- package/dist/src/config/connection-gater.browser.js +9 -3
- package/dist/src/config/connection-gater.browser.js.map +1 -1
- package/dist/src/config.js +1 -1
- package/dist/src/config.js.map +1 -1
- package/dist/src/connection-manager/address-sorter.d.ts.map +1 -1
- package/dist/src/connection-manager/address-sorter.js +2 -1
- package/dist/src/connection-manager/address-sorter.js.map +1 -1
- package/dist/src/connection-manager/connection-pruner.d.ts.map +1 -1
- package/dist/src/connection-manager/connection-pruner.js +3 -7
- package/dist/src/connection-manager/connection-pruner.js.map +1 -1
- package/dist/src/connection-manager/constants.defaults.d.ts +0 -4
- package/dist/src/connection-manager/constants.defaults.d.ts.map +1 -1
- package/dist/src/connection-manager/constants.defaults.js +0 -4
- package/dist/src/connection-manager/constants.defaults.js.map +1 -1
- package/dist/src/connection-manager/dial-queue.d.ts +2 -2
- package/dist/src/connection-manager/dial-queue.d.ts.map +1 -1
- package/dist/src/connection-manager/dial-queue.js +23 -11
- package/dist/src/connection-manager/dial-queue.js.map +1 -1
- package/dist/src/connection-manager/index.d.ts +2 -10
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +26 -44
- package/dist/src/connection-manager/index.js.map +1 -1
- package/dist/src/connection-manager/reconnect-queue.js +1 -1
- package/dist/src/connection-manager/reconnect-queue.js.map +1 -1
- package/dist/src/connection-manager/utils.d.ts +3 -31
- package/dist/src/connection-manager/utils.d.ts.map +1 -1
- package/dist/src/connection-manager/utils.js +18 -99
- package/dist/src/connection-manager/utils.js.map +1 -1
- package/dist/src/connection-monitor.d.ts +1 -1
- package/dist/src/connection-monitor.d.ts.map +1 -1
- package/dist/src/connection-monitor.js +3 -2
- package/dist/src/connection-monitor.js.map +1 -1
- package/dist/src/connection.d.ts +9 -13
- package/dist/src/connection.d.ts.map +1 -1
- package/dist/src/connection.js +138 -87
- package/dist/src/connection.js.map +1 -1
- package/dist/src/get-peer.js +3 -3
- package/dist/src/get-peer.js.map +1 -1
- package/dist/src/index.d.ts +2 -2
- package/dist/src/index.js +2 -2
- package/dist/src/libp2p.d.ts.map +1 -1
- package/dist/src/libp2p.js +5 -5
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/peer-routing.js +1 -1
- package/dist/src/peer-routing.js.map +1 -1
- package/dist/src/random-walk.d.ts.map +1 -1
- package/dist/src/random-walk.js +3 -13
- package/dist/src/random-walk.js.map +1 -1
- package/dist/src/registrar.d.ts +3 -3
- package/dist/src/registrar.d.ts.map +1 -1
- package/dist/src/registrar.js +41 -50
- package/dist/src/registrar.js.map +1 -1
- package/dist/src/transport-manager.js +2 -15
- package/dist/src/transport-manager.js.map +1 -1
- package/dist/src/upgrader.d.ts +16 -26
- package/dist/src/upgrader.d.ts.map +1 -1
- package/dist/src/upgrader.js +122 -88
- package/dist/src/upgrader.js.map +1 -1
- package/dist/src/version.d.ts +1 -1
- package/dist/src/version.js +1 -1
- package/package.json +30 -27
- package/src/address-manager/dns-mappings.ts +50 -50
- package/src/address-manager/index.ts +17 -37
- package/src/address-manager/ip-mappings.ts +44 -64
- package/src/address-manager/observed-addresses.ts +3 -1
- package/src/address-manager/transport-addresses.ts +9 -7
- package/src/config/connection-gater.browser.ts +12 -3
- package/src/config.ts +1 -1
- package/src/connection-manager/address-sorter.ts +2 -1
- package/src/connection-manager/connection-pruner.ts +3 -8
- package/src/connection-manager/constants.defaults.ts +0 -5
- package/src/connection-manager/dial-queue.ts +27 -12
- package/src/connection-manager/index.ts +28 -61
- package/src/connection-manager/reconnect-queue.ts +1 -1
- package/src/connection-manager/utils.ts +21 -129
- package/src/connection-monitor.ts +4 -3
- package/src/connection.ts +185 -111
- package/src/get-peer.ts +3 -3
- package/src/index.ts +2 -2
- package/src/libp2p.ts +6 -5
- package/src/peer-routing.ts +1 -1
- package/src/random-walk.ts +3 -13
- package/src/registrar.ts +54 -67
- package/src/transport-manager.ts +2 -18
- package/src/upgrader.ts +149 -132
- package/src/version.ts +1 -1
- package/dist/src/utils.d.ts +0 -3
- package/dist/src/utils.d.ts.map +0 -1
- package/dist/src/utils.js +0 -25
- package/dist/src/utils.js.map +0 -1
- package/src/utils.ts +0 -31
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { isPrivateIp } from '@libp2p/utils/private-ip'
|
|
2
|
+
import { trackedMap } from '@libp2p/utils/tracked-map'
|
|
3
|
+
import { multiaddr, protocols } from '@multiformats/multiaddr'
|
|
3
4
|
import type { AddressManagerComponents, AddressManagerInit } from './index.js'
|
|
4
5
|
import type { Logger } from '@libp2p/interface'
|
|
5
6
|
import type { NodeAddress } from '@libp2p/interface-internal'
|
|
6
|
-
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
7
|
+
import type { Multiaddr, StringTuple } from '@multiformats/multiaddr'
|
|
7
8
|
|
|
8
9
|
const MAX_DATE = 8_640_000_000_000_000
|
|
9
10
|
|
|
@@ -18,6 +19,13 @@ interface DNSMapping {
|
|
|
18
19
|
lastVerified?: number
|
|
19
20
|
}
|
|
20
21
|
|
|
22
|
+
const CODEC_TLS = 0x01c0
|
|
23
|
+
const CODEC_SNI = 0x01c1
|
|
24
|
+
const CODEC_DNS = 0x35
|
|
25
|
+
const CODEC_DNS4 = 0x36
|
|
26
|
+
const CODEC_DNS6 = 0x37
|
|
27
|
+
const CODEC_DNSADDR = 0x38
|
|
28
|
+
|
|
21
29
|
export class DNSMappings {
|
|
22
30
|
private readonly log: Logger
|
|
23
31
|
private readonly mappings: Map<string, DNSMapping>
|
|
@@ -31,12 +39,7 @@ export class DNSMappings {
|
|
|
31
39
|
}
|
|
32
40
|
|
|
33
41
|
has (ma: Multiaddr): boolean {
|
|
34
|
-
const
|
|
35
|
-
let host = config.host
|
|
36
|
-
|
|
37
|
-
if ((config.type === 'ip4' || config.type === 'ip6') && config.sni != null) {
|
|
38
|
-
host = config.sni
|
|
39
|
-
}
|
|
42
|
+
const host = this.findHost(ma)
|
|
40
43
|
|
|
41
44
|
for (const mapping of this.mappings.values()) {
|
|
42
45
|
if (mapping.domain === host) {
|
|
@@ -64,16 +67,11 @@ export class DNSMappings {
|
|
|
64
67
|
}
|
|
65
68
|
|
|
66
69
|
remove (ma: Multiaddr): boolean {
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
if (config.type !== 'ip4' && config.type !== 'ip6') {
|
|
70
|
-
return false
|
|
71
|
-
}
|
|
72
|
-
|
|
70
|
+
const host = this.findHost(ma)
|
|
73
71
|
let wasConfident = false
|
|
74
72
|
|
|
75
73
|
for (const [ip, mapping] of this.mappings.entries()) {
|
|
76
|
-
if (mapping.domain ===
|
|
74
|
+
if (mapping.domain === host) {
|
|
77
75
|
this.log('removing %s to %s DNS mapping %e', ip, mapping.domain)
|
|
78
76
|
this.mappings.delete(ip)
|
|
79
77
|
wasConfident = wasConfident || mapping.verified
|
|
@@ -87,30 +85,37 @@ export class DNSMappings {
|
|
|
87
85
|
const dnsMappedAddresses: NodeAddress[] = []
|
|
88
86
|
|
|
89
87
|
for (let i = 0; i < addresses.length; i++) {
|
|
90
|
-
const address = addresses[i]
|
|
88
|
+
const address = addresses[i]
|
|
89
|
+
const tuples = address.multiaddr.stringTuples()
|
|
90
|
+
const host = tuples[0][1]
|
|
91
91
|
|
|
92
|
-
if (
|
|
92
|
+
if (host == null) {
|
|
93
93
|
continue
|
|
94
94
|
}
|
|
95
95
|
|
|
96
|
-
const config = getNetConfig(address)
|
|
97
|
-
|
|
98
96
|
for (const [ip, mapping] of this.mappings.entries()) {
|
|
99
|
-
if (
|
|
97
|
+
if (host !== ip) {
|
|
100
98
|
continue
|
|
101
99
|
}
|
|
102
100
|
|
|
103
101
|
// insert SNI tuple after TLS tuple, if one is present
|
|
104
|
-
const
|
|
102
|
+
const mappedIp = this.maybeAddSNITuple(tuples, mapping.domain)
|
|
105
103
|
|
|
106
|
-
if (
|
|
104
|
+
if (mappedIp) {
|
|
107
105
|
// remove the address and replace it with the version that includes
|
|
108
106
|
// the SNI tuple
|
|
109
107
|
addresses.splice(i, 1)
|
|
110
108
|
i--
|
|
111
109
|
|
|
112
110
|
dnsMappedAddresses.push({
|
|
113
|
-
multiaddr:
|
|
111
|
+
multiaddr: multiaddr(`/${
|
|
112
|
+
tuples.map(tuple => {
|
|
113
|
+
return [
|
|
114
|
+
protocols(tuple[0]).name,
|
|
115
|
+
tuple[1]
|
|
116
|
+
].join('/')
|
|
117
|
+
}).join('/')
|
|
118
|
+
}`),
|
|
114
119
|
verified: mapping.verified,
|
|
115
120
|
type: 'dns-mapping',
|
|
116
121
|
expires: mapping.expires,
|
|
@@ -123,30 +128,19 @@ export class DNSMappings {
|
|
|
123
128
|
return dnsMappedAddresses
|
|
124
129
|
}
|
|
125
130
|
|
|
126
|
-
private
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
components.splice(j + 1, 0, {
|
|
132
|
-
name: 'sni',
|
|
133
|
-
code: CODE_SNI,
|
|
134
|
-
value: domain
|
|
135
|
-
})
|
|
136
|
-
|
|
137
|
-
return multiaddr(components)
|
|
131
|
+
private maybeAddSNITuple (tuples: StringTuple[], domain: string): boolean {
|
|
132
|
+
for (let j = 0; j < tuples.length; j++) {
|
|
133
|
+
if (tuples[j][0] === CODEC_TLS && tuples[j + 1]?.[0] !== CODEC_SNI) {
|
|
134
|
+
tuples.splice(j + 1, 0, [CODEC_SNI, domain])
|
|
135
|
+
return true
|
|
138
136
|
}
|
|
139
137
|
}
|
|
138
|
+
|
|
139
|
+
return false
|
|
140
140
|
}
|
|
141
141
|
|
|
142
142
|
confirm (ma: Multiaddr, ttl: number): boolean {
|
|
143
|
-
const
|
|
144
|
-
let host = config.host
|
|
145
|
-
|
|
146
|
-
if ((config.type === 'ip4' || config.type === 'ip6') && config.sni != null) {
|
|
147
|
-
host = config.sni
|
|
148
|
-
}
|
|
149
|
-
|
|
143
|
+
const host = this.findHost(ma)
|
|
150
144
|
let startingConfidence = false
|
|
151
145
|
|
|
152
146
|
for (const [ip, mapping] of this.mappings.entries()) {
|
|
@@ -163,13 +157,7 @@ export class DNSMappings {
|
|
|
163
157
|
}
|
|
164
158
|
|
|
165
159
|
unconfirm (ma: Multiaddr, ttl: number): boolean {
|
|
166
|
-
const
|
|
167
|
-
|
|
168
|
-
if (config.type !== 'ip4' && config.type !== 'ip6') {
|
|
169
|
-
return false
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
const host = config.sni ?? config.host
|
|
160
|
+
const host = this.findHost(ma)
|
|
173
161
|
let wasConfident = false
|
|
174
162
|
|
|
175
163
|
for (const [ip, mapping] of this.mappings.entries()) {
|
|
@@ -183,4 +171,16 @@ export class DNSMappings {
|
|
|
183
171
|
|
|
184
172
|
return wasConfident
|
|
185
173
|
}
|
|
174
|
+
|
|
175
|
+
private findHost (ma: Multiaddr): string | undefined {
|
|
176
|
+
for (const tuple of ma.stringTuples()) {
|
|
177
|
+
if (tuple[0] === CODEC_SNI) {
|
|
178
|
+
return tuple[1]
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (tuple[0] === CODEC_DNS || tuple[0] === CODEC_DNS4 || tuple[0] === CODEC_DNS6 || tuple[0] === CODEC_DNSADDR) {
|
|
182
|
+
return tuple[1]
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
186
|
}
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
/* eslint-disable complexity */
|
|
2
2
|
import { isIPv4 } from '@chainsafe/is-ip'
|
|
3
3
|
import { peerIdFromString } from '@libp2p/peer-id'
|
|
4
|
-
import { debounce
|
|
5
|
-
import {
|
|
4
|
+
import { debounce } from '@libp2p/utils/debounce'
|
|
5
|
+
import { createScalableCuckooFilter } from '@libp2p/utils/filters'
|
|
6
|
+
import { isPrivateIp } from '@libp2p/utils/private-ip'
|
|
7
|
+
import { multiaddr } from '@multiformats/multiaddr'
|
|
6
8
|
import { QUIC_V1, TCP, WebSockets, WebSocketsSecure } from '@multiformats/multiaddr-matcher'
|
|
7
9
|
import { DNSMappings } from './dns-mappings.js'
|
|
8
10
|
import { IPMappings } from './ip-mappings.js'
|
|
@@ -10,7 +12,7 @@ import { ObservedAddresses } from './observed-addresses.js'
|
|
|
10
12
|
import { TransportAddresses } from './transport-addresses.js'
|
|
11
13
|
import type { ComponentLogger, Libp2pEvents, Logger, PeerId, PeerStore, Metrics } from '@libp2p/interface'
|
|
12
14
|
import type { AddressManager as AddressManagerInterface, TransportManager, NodeAddress, ConfirmAddressOptions } from '@libp2p/interface-internal'
|
|
13
|
-
import type { Filter } from '@libp2p/utils'
|
|
15
|
+
import type { Filter } from '@libp2p/utils/filters'
|
|
14
16
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
15
17
|
import type { TypedEventTarget } from 'main-event'
|
|
16
18
|
|
|
@@ -99,7 +101,7 @@ const defaultAddressFilter = (addrs: Multiaddr[]): Multiaddr[] => addrs
|
|
|
99
101
|
* If the passed multiaddr contains the passed peer id, remove it
|
|
100
102
|
*/
|
|
101
103
|
function stripPeerId (ma: Multiaddr, peerId: PeerId): Multiaddr {
|
|
102
|
-
const observedPeerIdStr = ma.
|
|
104
|
+
const observedPeerIdStr = ma.getPeerId()
|
|
103
105
|
|
|
104
106
|
// strip our peer id if it has been passed
|
|
105
107
|
if (observedPeerIdStr != null) {
|
|
@@ -175,7 +177,7 @@ export class AddressManager implements AddressManagerInterface {
|
|
|
175
177
|
const addrs = this.getAddresses()
|
|
176
178
|
.map(ma => {
|
|
177
179
|
// strip our peer id if it is present
|
|
178
|
-
if (ma.
|
|
180
|
+
if (ma.getPeerId() === this.components.peerId.toString()) {
|
|
179
181
|
return ma.decapsulate(`/p2p/${this.components.peerId.toString()}`)
|
|
180
182
|
}
|
|
181
183
|
|
|
@@ -222,22 +224,8 @@ export class AddressManager implements AddressManagerInterface {
|
|
|
222
224
|
* Add peer observed addresses
|
|
223
225
|
*/
|
|
224
226
|
addObservedAddr (addr: Multiaddr): void {
|
|
225
|
-
const
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
switch (config.type) {
|
|
229
|
-
case 'ip4': {
|
|
230
|
-
socketAddress = `${config.host}:${config.port}`
|
|
231
|
-
break
|
|
232
|
-
}
|
|
233
|
-
case 'ip6': {
|
|
234
|
-
socketAddress = `[${config.host}]:${config.port}`
|
|
235
|
-
break
|
|
236
|
-
}
|
|
237
|
-
default: {
|
|
238
|
-
return
|
|
239
|
-
}
|
|
240
|
-
}
|
|
227
|
+
const tuples = addr.stringTuples()
|
|
228
|
+
const socketAddress = `${tuples[0][1]}:${tuples[1][1]}`
|
|
241
229
|
|
|
242
230
|
// ignore if this address if it's been observed before
|
|
243
231
|
if (this.observedAddressFilter.has(socketAddress)) {
|
|
@@ -489,14 +477,10 @@ export class AddressManager implements AddressManagerInterface {
|
|
|
489
477
|
return false
|
|
490
478
|
}
|
|
491
479
|
|
|
492
|
-
|
|
493
|
-
return false
|
|
494
|
-
}
|
|
495
|
-
|
|
496
|
-
const config = getNetConfig(ma)
|
|
480
|
+
const maOptions = ma.toOptions()
|
|
497
481
|
|
|
498
482
|
// only public IPv4 addresses
|
|
499
|
-
if (
|
|
483
|
+
if (maOptions.family === 6 || maOptions.host === '127.0.0.1' || isPrivateIp(maOptions.host) === true) {
|
|
500
484
|
return false
|
|
501
485
|
}
|
|
502
486
|
|
|
@@ -518,7 +502,7 @@ export class AddressManager implements AddressManagerInterface {
|
|
|
518
502
|
const transportListeners = listeners.filter(listener => {
|
|
519
503
|
return listener.getAddrs().filter(ma => {
|
|
520
504
|
// only IPv4 addresses of the matcher type
|
|
521
|
-
return
|
|
505
|
+
return ma.toOptions().family === 4 && matcher(ma)
|
|
522
506
|
}).length > 0
|
|
523
507
|
})
|
|
524
508
|
|
|
@@ -532,27 +516,23 @@ export class AddressManager implements AddressManagerInterface {
|
|
|
532
516
|
// we have one listener which listens on one port so whatever the external
|
|
533
517
|
// NAT port mapping is, it should be for this listener
|
|
534
518
|
const linkLocalAddr = transportListeners[0].getAddrs().filter(ma => {
|
|
535
|
-
return
|
|
519
|
+
return ma.toOptions().host !== '127.0.0.1'
|
|
536
520
|
}).pop()
|
|
537
521
|
|
|
538
522
|
if (linkLocalAddr == null) {
|
|
539
523
|
continue
|
|
540
524
|
}
|
|
541
525
|
|
|
542
|
-
const linkLocalOptions =
|
|
543
|
-
|
|
544
|
-
if (linkLocalOptions.port == null) {
|
|
545
|
-
return false
|
|
546
|
-
}
|
|
526
|
+
const linkLocalOptions = linkLocalAddr.toOptions()
|
|
547
527
|
|
|
548
528
|
// upgrade observed address to IP mapping
|
|
549
529
|
this.observed.remove(ma)
|
|
550
530
|
this.ipMappings.add(
|
|
551
531
|
linkLocalOptions.host,
|
|
552
532
|
linkLocalOptions.port,
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
533
|
+
maOptions.host,
|
|
534
|
+
maOptions.port,
|
|
535
|
+
maOptions.transport
|
|
556
536
|
)
|
|
557
537
|
|
|
558
538
|
return true
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { isIPv4 } from '@chainsafe/is-ip'
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { trackedMap } from '@libp2p/utils/tracked-map'
|
|
3
|
+
import { multiaddr, protocols } from '@multiformats/multiaddr'
|
|
4
4
|
import type { AddressManagerComponents, AddressManagerInit } from './index.js'
|
|
5
5
|
import type { Logger } from '@libp2p/interface'
|
|
6
6
|
import type { NodeAddress } from '@libp2p/interface-internal'
|
|
@@ -22,6 +22,11 @@ interface PublicAddressMapping {
|
|
|
22
22
|
lastVerified?: number
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
+
const CODEC_IP4 = 0x04
|
|
26
|
+
const CODEC_IP6 = 0x29
|
|
27
|
+
const CODEC_TCP = 0x06
|
|
28
|
+
const CODEC_UDP = 0x0111
|
|
29
|
+
|
|
25
30
|
export class IPMappings {
|
|
26
31
|
private readonly log: Logger
|
|
27
32
|
private readonly mappings: Map<string, PublicAddressMapping[]>
|
|
@@ -35,15 +40,11 @@ export class IPMappings {
|
|
|
35
40
|
}
|
|
36
41
|
|
|
37
42
|
has (ma: Multiaddr): boolean {
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
if (config.type !== 'ip4' && config.type !== 'ip6') {
|
|
41
|
-
return false
|
|
42
|
-
}
|
|
43
|
+
const tuples = ma.stringTuples()
|
|
43
44
|
|
|
44
45
|
for (const mappings of this.mappings.values()) {
|
|
45
46
|
for (const mapping of mappings) {
|
|
46
|
-
if (mapping.externalIp ===
|
|
47
|
+
if (mapping.externalIp === tuples[0][1]) {
|
|
47
48
|
return true
|
|
48
49
|
}
|
|
49
50
|
}
|
|
@@ -71,20 +72,18 @@ export class IPMappings {
|
|
|
71
72
|
}
|
|
72
73
|
|
|
73
74
|
remove (ma: Multiaddr): boolean {
|
|
74
|
-
const
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
}
|
|
79
|
-
|
|
75
|
+
const tuples = ma.stringTuples()
|
|
76
|
+
const host = tuples[0][1] ?? ''
|
|
77
|
+
const protocol = tuples[1][0] === CODEC_TCP ? 'tcp' : 'udp'
|
|
78
|
+
const port = parseInt(tuples[1][1] ?? '0')
|
|
80
79
|
let wasConfident = false
|
|
81
80
|
|
|
82
81
|
for (const [key, mappings] of this.mappings.entries()) {
|
|
83
82
|
for (let i = 0; i < mappings.length; i++) {
|
|
84
83
|
const mapping = mappings[i]
|
|
85
84
|
|
|
86
|
-
if (mapping.externalIp ===
|
|
87
|
-
this.log('removing %s:%s to %s:%s %s IP mapping', mapping.externalIp, mapping.externalPort,
|
|
85
|
+
if (mapping.externalIp === host && mapping.externalPort === port && mapping.protocol === protocol) {
|
|
86
|
+
this.log('removing %s:%s to %s:%s %s IP mapping', mapping.externalIp, mapping.externalPort, host, port, protocol)
|
|
88
87
|
|
|
89
88
|
wasConfident = wasConfident || mapping.verified
|
|
90
89
|
mappings.splice(i, 1)
|
|
@@ -104,38 +103,40 @@ export class IPMappings {
|
|
|
104
103
|
const ipMappedAddresses: NodeAddress[] = []
|
|
105
104
|
|
|
106
105
|
for (const { multiaddr: ma } of addresses) {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
const config = getNetConfig(ma)
|
|
112
|
-
|
|
113
|
-
if (config.type !== 'ip4' && config.type !== 'ip6') {
|
|
114
|
-
continue
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
let key: string | undefined
|
|
106
|
+
const tuples = ma.stringTuples()
|
|
107
|
+
let tuple: string | undefined
|
|
118
108
|
|
|
119
109
|
// see if the internal host/port/protocol tuple has been mapped externally
|
|
120
|
-
if (
|
|
121
|
-
|
|
122
|
-
} else if (
|
|
123
|
-
|
|
110
|
+
if ((tuples[0][0] === CODEC_IP4 || tuples[0][0] === CODEC_IP6) && tuples[1][0] === CODEC_TCP) {
|
|
111
|
+
tuple = `${tuples[0][1]}-${tuples[1][1]}-tcp`
|
|
112
|
+
} else if ((tuples[0][0] === CODEC_IP4 || tuples[0][0] === CODEC_IP6) && tuples[1][0] === CODEC_UDP) {
|
|
113
|
+
tuple = `${tuples[0][1]}-${tuples[1][1]}-udp`
|
|
124
114
|
}
|
|
125
115
|
|
|
126
|
-
if (
|
|
116
|
+
if (tuple == null) {
|
|
127
117
|
continue
|
|
128
118
|
}
|
|
129
119
|
|
|
130
|
-
const mappings = this.mappings.get(
|
|
120
|
+
const mappings = this.mappings.get(tuple)
|
|
131
121
|
|
|
132
122
|
if (mappings == null) {
|
|
133
123
|
continue
|
|
134
124
|
}
|
|
135
125
|
|
|
136
126
|
for (const mapping of mappings) {
|
|
127
|
+
tuples[0][0] = mapping.externalFamily === 4 ? CODEC_IP4 : CODEC_IP6
|
|
128
|
+
tuples[0][1] = mapping.externalIp
|
|
129
|
+
tuples[1][1] = `${mapping.externalPort}`
|
|
130
|
+
|
|
137
131
|
ipMappedAddresses.push({
|
|
138
|
-
multiaddr:
|
|
132
|
+
multiaddr: multiaddr(`/${
|
|
133
|
+
tuples.map(tuple => {
|
|
134
|
+
return [
|
|
135
|
+
protocols(tuple[0]).name,
|
|
136
|
+
tuple[1]
|
|
137
|
+
].join('/')
|
|
138
|
+
}).join('/')
|
|
139
|
+
}`),
|
|
139
140
|
verified: mapping.verified,
|
|
140
141
|
type: 'ip-mapping',
|
|
141
142
|
expires: mapping.expires,
|
|
@@ -147,34 +148,14 @@ export class IPMappings {
|
|
|
147
148
|
return ipMappedAddresses
|
|
148
149
|
}
|
|
149
150
|
|
|
150
|
-
private maybeOverrideIp (ma: Multiaddr, externalIp: string, externalFamily: number, protocol: 'tcp' | 'udp', externalPort: number): Multiaddr {
|
|
151
|
-
const components = ma.getComponents()
|
|
152
|
-
|
|
153
|
-
const ipIndex = components.findIndex(c => c.code === CODE_IP4 || c.code === CODE_IP6)
|
|
154
|
-
const portIndex = components.findIndex(c => c.name === protocol)
|
|
155
|
-
|
|
156
|
-
if (ipIndex > -1 && portIndex > -1) {
|
|
157
|
-
components[ipIndex].value = externalIp
|
|
158
|
-
components[ipIndex].code = externalFamily === 4 ? CODE_IP4 : CODE_IP6
|
|
159
|
-
components[portIndex].value = `${externalPort}`
|
|
160
|
-
|
|
161
|
-
return multiaddr(components)
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
return ma
|
|
165
|
-
}
|
|
166
|
-
|
|
167
151
|
confirm (ma: Multiaddr, ttl: number): boolean {
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
const config = getNetConfig(ma)
|
|
152
|
+
const tuples = ma.stringTuples()
|
|
153
|
+
const host = tuples[0][1]
|
|
173
154
|
let startingConfidence = false
|
|
174
155
|
|
|
175
156
|
for (const mappings of this.mappings.values()) {
|
|
176
157
|
for (const mapping of mappings) {
|
|
177
|
-
if (mapping.externalIp ===
|
|
158
|
+
if (mapping.externalIp === host) {
|
|
178
159
|
this.log('marking %s to %s IP mapping as verified', mapping.internalIp, mapping.externalIp)
|
|
179
160
|
startingConfidence = mapping.verified
|
|
180
161
|
mapping.verified = true
|
|
@@ -188,19 +169,18 @@ export class IPMappings {
|
|
|
188
169
|
}
|
|
189
170
|
|
|
190
171
|
unconfirm (ma: Multiaddr, ttl: number): boolean {
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
const config = getNetConfig(ma)
|
|
172
|
+
const tuples = ma.stringTuples()
|
|
173
|
+
const host = tuples[0][1] ?? ''
|
|
174
|
+
const protocol = tuples[1][0] === CODEC_TCP ? 'tcp' : 'udp'
|
|
175
|
+
const port = parseInt(tuples[1][1] ?? '0')
|
|
196
176
|
let wasConfident = false
|
|
197
177
|
|
|
198
178
|
for (const mappings of this.mappings.values()) {
|
|
199
179
|
for (let i = 0; i < mappings.length; i++) {
|
|
200
180
|
const mapping = mappings[i]
|
|
201
181
|
|
|
202
|
-
if (mapping.externalIp ===
|
|
203
|
-
this.log('removing verification of %s:%s to %s:%s %s IP mapping', mapping.externalIp, mapping.externalPort,
|
|
182
|
+
if (mapping.externalIp === host && mapping.externalPort === port && mapping.protocol === protocol) {
|
|
183
|
+
this.log('removing verification of %s:%s to %s:%s %s IP mapping', mapping.externalIp, mapping.externalPort, host, port, protocol)
|
|
204
184
|
|
|
205
185
|
wasConfident = wasConfident || mapping.verified
|
|
206
186
|
mapping.verified = false
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import { isLinkLocal
|
|
1
|
+
import { isLinkLocal } from '@libp2p/utils/multiaddr/is-link-local'
|
|
2
|
+
import { isPrivate } from '@libp2p/utils/multiaddr/is-private'
|
|
3
|
+
import { trackedMap } from '@libp2p/utils/tracked-map'
|
|
2
4
|
import { multiaddr } from '@multiformats/multiaddr'
|
|
3
5
|
import type { AddressManagerComponents, AddressManagerInit } from './index.js'
|
|
4
6
|
import type { Logger } from '@libp2p/interface'
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { isNetworkAddress } from '@libp2p/utils/multiaddr/is-network-address'
|
|
2
|
+
import { isPrivate } from '@libp2p/utils/multiaddr/is-private'
|
|
3
|
+
import { trackedMap } from '@libp2p/utils/tracked-map'
|
|
2
4
|
import type { AddressManagerComponents, AddressManagerInit } from './index.js'
|
|
3
5
|
import type { Logger } from '@libp2p/interface'
|
|
4
6
|
import type { NodeAddress } from '@libp2p/interface-internal'
|
|
@@ -112,13 +114,13 @@ export class TransportAddresses {
|
|
|
112
114
|
}
|
|
113
115
|
|
|
114
116
|
private toKey (ma: Multiaddr): string {
|
|
115
|
-
if (
|
|
116
|
-
|
|
117
|
-
|
|
117
|
+
if (isNetworkAddress(ma)) {
|
|
118
|
+
// only works for dns/ip based addresses
|
|
119
|
+
const options = ma.toOptions()
|
|
118
120
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
+
return `${options.host}-${options.port}-${options.transport}`
|
|
122
|
+
}
|
|
121
123
|
|
|
122
|
-
return
|
|
124
|
+
return ma.toString()
|
|
123
125
|
}
|
|
124
126
|
}
|
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { isPrivateIp } from '@libp2p/utils/private-ip'
|
|
2
2
|
import { WebSockets } from '@multiformats/multiaddr-matcher'
|
|
3
3
|
import type { ConnectionGater } from '@libp2p/interface'
|
|
4
4
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
5
5
|
|
|
6
|
+
const CODEC_IP4 = 0x04
|
|
7
|
+
const CODEC_IP6 = 0x29
|
|
8
|
+
|
|
6
9
|
/**
|
|
7
10
|
* Returns a connection gater that disallows dialling private addresses or
|
|
8
11
|
* insecure websockets by default.
|
|
@@ -18,11 +21,17 @@ export function connectionGater (gater: ConnectionGater = {}): ConnectionGater {
|
|
|
18
21
|
denyDialMultiaddr: async (multiaddr: Multiaddr) => {
|
|
19
22
|
// do not connect to insecure websockets by default
|
|
20
23
|
if (WebSockets.matches(multiaddr)) {
|
|
21
|
-
return
|
|
24
|
+
return false
|
|
22
25
|
}
|
|
23
26
|
|
|
27
|
+
const tuples = multiaddr.stringTuples()
|
|
28
|
+
|
|
24
29
|
// do not connect to private addresses by default
|
|
25
|
-
|
|
30
|
+
if (tuples[0][0] === CODEC_IP4 || tuples[0][0] === CODEC_IP6) {
|
|
31
|
+
return Boolean(isPrivateIp(`${tuples[0][1]}`))
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return false
|
|
26
35
|
},
|
|
27
36
|
denyInboundConnection: async () => false,
|
|
28
37
|
denyOutboundConnection: async () => false,
|
package/src/config.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { FaultTolerance, InvalidParametersError } from '@libp2p/interface'
|
|
2
|
-
import { mergeOptions } from '@libp2p/utils'
|
|
2
|
+
import { mergeOptions } from '@libp2p/utils/merge-options'
|
|
3
3
|
import { dnsaddrResolver } from './connection-manager/resolvers/dnsaddr.ts'
|
|
4
4
|
import type { Libp2pInit } from './index.js'
|
|
5
5
|
import type { ServiceMap } from '@libp2p/interface'
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { isLoopback
|
|
1
|
+
import { isLoopback } from '@libp2p/utils/multiaddr/is-loopback'
|
|
2
|
+
import { isPrivate } from '@libp2p/utils/multiaddr/is-private'
|
|
2
3
|
import { Circuit, WebSockets, WebSocketsSecure, WebRTC, WebRTCDirect, WebTransport, TCP } from '@multiformats/multiaddr-matcher'
|
|
3
4
|
import type { Address } from '@libp2p/interface'
|
|
4
5
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { PeerMap } from '@libp2p/peer-collections'
|
|
2
|
-
import {
|
|
3
|
-
import { multiaddrToIpNet
|
|
2
|
+
import { safelyCloseConnectionIfUnused } from '@libp2p/utils/close'
|
|
3
|
+
import { multiaddrToIpNet } from './utils.js'
|
|
4
4
|
import type { IpNet } from '@chainsafe/netmask'
|
|
5
5
|
import type { Libp2pEvents, Logger, ComponentLogger, PeerStore, Connection } from '@libp2p/interface'
|
|
6
6
|
import type { ConnectionManager } from '@libp2p/interface-internal'
|
|
@@ -103,12 +103,7 @@ export class ConnectionPruner {
|
|
|
103
103
|
this.log('too many connections open - closing a connection to %p', connection.remotePeer)
|
|
104
104
|
// check allow list
|
|
105
105
|
const connectionInAllowList = this.allow.some((ipNet) => {
|
|
106
|
-
|
|
107
|
-
const config = getNetConfig(connection.remoteAddr)
|
|
108
|
-
return ipNet.contains(config.host)
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
return true
|
|
106
|
+
return ipNet.contains(connection.remoteAddr.nodeAddress().address)
|
|
112
107
|
})
|
|
113
108
|
|
|
114
109
|
// Connections in the allow list should be excluded from pruning
|
|
@@ -3,11 +3,6 @@
|
|
|
3
3
|
*/
|
|
4
4
|
export const DIAL_TIMEOUT = 10_000
|
|
5
5
|
|
|
6
|
-
/**
|
|
7
|
-
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#connectionCloseTimeout
|
|
8
|
-
*/
|
|
9
|
-
export const CONNECTION_CLOSE_TIMEOUT = 1_000
|
|
10
|
-
|
|
11
6
|
/**
|
|
12
7
|
* @see https://libp2p.github.io/js-libp2p/interfaces/index._internal_.ConnectionManagerConfig.html#inboundUpgradeTimeout
|
|
13
8
|
*/
|