libp2p 0.46.16-d5ef1c91 → 0.46.17
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 +18 -61
- package/dist/index.min.js +29 -29
- package/dist/src/autonat/index.d.ts.map +1 -1
- package/dist/src/autonat/index.js +3 -11
- package/dist/src/autonat/index.js.map +1 -1
- package/dist/src/circuit-relay/server/index.d.ts.map +1 -1
- package/dist/src/circuit-relay/server/index.js +2 -7
- package/dist/src/circuit-relay/server/index.js.map +1 -1
- package/dist/src/circuit-relay/transport/discovery.d.ts.map +1 -1
- package/dist/src/circuit-relay/transport/discovery.js +1 -0
- package/dist/src/circuit-relay/transport/discovery.js.map +1 -1
- package/dist/src/circuit-relay/transport/listener.d.ts.map +1 -1
- package/dist/src/circuit-relay/transport/listener.js +3 -17
- package/dist/src/circuit-relay/transport/listener.js.map +1 -1
- package/dist/src/connection/index.d.ts.map +1 -1
- package/dist/src/connection/index.js +5 -6
- package/dist/src/connection/index.js.map +1 -1
- package/dist/src/connection-manager/dial-queue.d.ts +4 -0
- package/dist/src/connection-manager/dial-queue.d.ts.map +1 -1
- package/dist/src/connection-manager/dial-queue.js +37 -7
- package/dist/src/connection-manager/dial-queue.js.map +1 -1
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +7 -5
- package/dist/src/connection-manager/index.js.map +1 -1
- package/dist/src/connection-manager/utils.d.ts.map +1 -1
- package/dist/src/connection-manager/utils.js +3 -11
- package/dist/src/connection-manager/utils.js.map +1 -1
- package/dist/src/dcutr/dcutr.d.ts +0 -5
- package/dist/src/dcutr/dcutr.d.ts.map +1 -1
- package/dist/src/dcutr/dcutr.js +4 -28
- package/dist/src/dcutr/dcutr.js.map +1 -1
- package/dist/src/dcutr/utils.d.ts +8 -0
- package/dist/src/dcutr/utils.d.ts.map +1 -0
- package/dist/src/dcutr/utils.js +27 -0
- package/dist/src/dcutr/utils.js.map +1 -0
- package/dist/src/fetch/index.d.ts.map +1 -1
- package/dist/src/fetch/index.js +2 -6
- package/dist/src/fetch/index.js.map +1 -1
- package/dist/src/identify/consts.d.ts +1 -1
- package/dist/src/identify/consts.d.ts.map +1 -1
- package/dist/src/identify/identify.d.ts.map +1 -1
- package/dist/src/identify/identify.js +55 -47
- package/dist/src/identify/identify.js.map +1 -1
- package/dist/src/libp2p.d.ts.map +1 -1
- package/dist/src/libp2p.js +3 -7
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/registrar.d.ts +7 -6
- package/dist/src/registrar.d.ts.map +1 -1
- package/dist/src/registrar.js +16 -41
- package/dist/src/registrar.js.map +1 -1
- package/dist/src/upgrader.d.ts.map +1 -1
- package/dist/src/upgrader.js +3 -11
- package/dist/src/upgrader.js.map +1 -1
- package/dist/src/version.d.ts +1 -1
- package/dist/src/version.d.ts.map +1 -1
- package/dist/src/version.js +1 -1
- package/dist/src/version.js.map +1 -1
- package/dist/typedoc-urls.json +71 -0
- package/package.json +23 -24
- package/src/autonat/index.ts +3 -9
- package/src/circuit-relay/server/index.ts +2 -6
- package/src/circuit-relay/transport/discovery.ts +1 -0
- package/src/circuit-relay/transport/listener.ts +3 -21
- package/src/connection/index.ts +6 -5
- package/src/connection-manager/dial-queue.ts +46 -6
- package/src/connection-manager/index.ts +7 -5
- package/src/connection-manager/utils.ts +3 -9
- package/src/dcutr/dcutr.ts +4 -35
- package/src/dcutr/utils.ts +33 -0
- package/src/fetch/index.ts +2 -5
- package/src/identify/identify.ts +63 -47
- package/src/libp2p.ts +3 -6
- package/src/registrar.ts +19 -50
- package/src/upgrader.ts +3 -9
- package/src/version.ts +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "libp2p",
|
|
3
|
-
"version": "0.46.
|
|
3
|
+
"version": "0.46.17",
|
|
4
4
|
"description": "JavaScript implementation of libp2p, a modular peer to peer network stack",
|
|
5
5
|
"license": "Apache-2.0 OR MIT",
|
|
6
6
|
"homepage": "https://github.com/libp2p/js-libp2p/tree/master/packages/libp2p#readme",
|
|
@@ -100,7 +100,7 @@
|
|
|
100
100
|
"scripts": {
|
|
101
101
|
"clean": "aegir clean",
|
|
102
102
|
"lint": "aegir lint",
|
|
103
|
-
"dep-check": "aegir dep-check
|
|
103
|
+
"dep-check": "aegir dep-check",
|
|
104
104
|
"prepublishOnly": "node scripts/update-version.js && npm run build",
|
|
105
105
|
"build": "aegir build",
|
|
106
106
|
"generate": "run-s generate:proto:*",
|
|
@@ -121,25 +121,24 @@
|
|
|
121
121
|
},
|
|
122
122
|
"dependencies": {
|
|
123
123
|
"@achingbrain/nat-port-mapper": "^1.0.9",
|
|
124
|
-
"@libp2p/crypto": "2.0.
|
|
125
|
-
"@libp2p/interface": "0.1.
|
|
126
|
-
"@libp2p/interface-internal": "0.1.
|
|
127
|
-
"@libp2p/keychain": "3.0.
|
|
128
|
-
"@libp2p/logger": "3.0.
|
|
129
|
-
"@libp2p/multistream-select": "4.0.
|
|
130
|
-
"@libp2p/peer-collections": "4.0.
|
|
131
|
-
"@libp2p/peer-id": "3.0.
|
|
132
|
-
"@libp2p/peer-id-factory": "3.0.
|
|
133
|
-
"@libp2p/peer-record": "6.0.
|
|
134
|
-
"@libp2p/peer-store": "9.0.
|
|
135
|
-
"@libp2p/utils": "4.0.
|
|
124
|
+
"@libp2p/crypto": "^2.0.7",
|
|
125
|
+
"@libp2p/interface": "^0.1.5",
|
|
126
|
+
"@libp2p/interface-internal": "^0.1.8",
|
|
127
|
+
"@libp2p/keychain": "^3.0.7",
|
|
128
|
+
"@libp2p/logger": "^3.0.5",
|
|
129
|
+
"@libp2p/multistream-select": "^4.0.5",
|
|
130
|
+
"@libp2p/peer-collections": "^4.0.7",
|
|
131
|
+
"@libp2p/peer-id": "^3.0.5",
|
|
132
|
+
"@libp2p/peer-id-factory": "^3.0.7",
|
|
133
|
+
"@libp2p/peer-record": "^6.0.8",
|
|
134
|
+
"@libp2p/peer-store": "^9.0.8",
|
|
135
|
+
"@libp2p/utils": "^4.0.6",
|
|
136
136
|
"@multiformats/mafmt": "^12.1.2",
|
|
137
137
|
"@multiformats/multiaddr": "^12.1.5",
|
|
138
138
|
"@multiformats/multiaddr-matcher": "^1.0.0",
|
|
139
139
|
"any-signal": "^4.1.1",
|
|
140
140
|
"datastore-core": "^9.0.1",
|
|
141
141
|
"delay": "^6.0.0",
|
|
142
|
-
"events": "^3.3.0",
|
|
143
142
|
"interface-datastore": "^8.2.0",
|
|
144
143
|
"it-all": "^3.0.2",
|
|
145
144
|
"it-drain": "^3.0.2",
|
|
@@ -171,17 +170,17 @@
|
|
|
171
170
|
"@chainsafe/libp2p-gossipsub": "^10.0.0",
|
|
172
171
|
"@chainsafe/libp2p-noise": "^13.0.0",
|
|
173
172
|
"@chainsafe/libp2p-yamux": "^5.0.0",
|
|
174
|
-
"@libp2p/bootstrap": "9.0.
|
|
173
|
+
"@libp2p/bootstrap": "^9.0.10",
|
|
175
174
|
"@libp2p/daemon-client": "^7.0.0",
|
|
176
175
|
"@libp2p/daemon-server": "^6.0.0",
|
|
177
|
-
"@libp2p/floodsub": "8.0.
|
|
178
|
-
"@libp2p/interface-compliance-tests": "4.1.
|
|
176
|
+
"@libp2p/floodsub": "^8.0.11",
|
|
177
|
+
"@libp2p/interface-compliance-tests": "^4.1.3",
|
|
179
178
|
"@libp2p/interop": "^9.0.0",
|
|
180
|
-
"@libp2p/kad-dht": "10.0.
|
|
181
|
-
"@libp2p/mdns": "9.0.
|
|
182
|
-
"@libp2p/mplex": "9.0.
|
|
183
|
-
"@libp2p/tcp": "8.0.
|
|
184
|
-
"@libp2p/websockets": "7.0.
|
|
179
|
+
"@libp2p/kad-dht": "^10.0.12",
|
|
180
|
+
"@libp2p/mdns": "^9.0.12",
|
|
181
|
+
"@libp2p/mplex": "^9.0.10",
|
|
182
|
+
"@libp2p/tcp": "^8.0.11",
|
|
183
|
+
"@libp2p/websockets": "^7.0.11",
|
|
185
184
|
"@types/xsalsa20": "^1.1.0",
|
|
186
185
|
"aegir": "^41.0.2",
|
|
187
186
|
"execa": "^8.0.1",
|
|
@@ -194,7 +193,7 @@
|
|
|
194
193
|
"p-wait-for": "^5.0.2",
|
|
195
194
|
"protons": "^7.0.2",
|
|
196
195
|
"sinon": "^17.0.0",
|
|
197
|
-
"sinon-ts": "^
|
|
196
|
+
"sinon-ts": "^2.0.0"
|
|
198
197
|
},
|
|
199
198
|
"browser": {
|
|
200
199
|
"./dist/src/connection-manager/constants.js": "./dist/src/connection-manager/constants.browser.js",
|
package/src/autonat/index.ts
CHANGED
|
@@ -19,8 +19,8 @@
|
|
|
19
19
|
* ```
|
|
20
20
|
*/
|
|
21
21
|
|
|
22
|
-
import { setMaxListeners } from 'events'
|
|
23
22
|
import { CodeError } from '@libp2p/interface/errors'
|
|
23
|
+
import { setMaxListeners } from '@libp2p/interface/events'
|
|
24
24
|
import { logger } from '@libp2p/logger'
|
|
25
25
|
import { peerIdFromBytes } from '@libp2p/peer-id'
|
|
26
26
|
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
|
|
@@ -164,10 +164,7 @@ class DefaultAutoNATService implements Startable {
|
|
|
164
164
|
|
|
165
165
|
// this controller may be used while dialing lots of peers so prevent MaxListenersExceededWarning
|
|
166
166
|
// appearing in the console
|
|
167
|
-
|
|
168
|
-
// fails on node < 15.4
|
|
169
|
-
setMaxListeners?.(Infinity, signal)
|
|
170
|
-
} catch {}
|
|
167
|
+
setMaxListeners(Infinity, signal)
|
|
171
168
|
|
|
172
169
|
const ourHosts = this.components.addressManager.getAddresses()
|
|
173
170
|
.map(ma => ma.toOptions().host)
|
|
@@ -432,10 +429,7 @@ class DefaultAutoNATService implements Startable {
|
|
|
432
429
|
|
|
433
430
|
// this controller may be used while dialing lots of peers so prevent MaxListenersExceededWarning
|
|
434
431
|
// appearing in the console
|
|
435
|
-
|
|
436
|
-
// fails on node < 15.4
|
|
437
|
-
setMaxListeners?.(Infinity, signal)
|
|
438
|
-
} catch {}
|
|
432
|
+
setMaxListeners(Infinity, signal)
|
|
439
433
|
|
|
440
434
|
const self = this
|
|
441
435
|
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { setMaxListeners } from 'events'
|
|
2
|
-
import { TypedEventEmitter } from '@libp2p/interface/events'
|
|
1
|
+
import { TypedEventEmitter, setMaxListeners } from '@libp2p/interface/events'
|
|
3
2
|
import { logger } from '@libp2p/logger'
|
|
4
3
|
import { peerIdFromBytes } from '@libp2p/peer-id'
|
|
5
4
|
import { RecordEnvelope } from '@libp2p/peer-record'
|
|
@@ -134,10 +133,7 @@ class CircuitRelayServer extends TypedEventEmitter<RelayServerEvents> implements
|
|
|
134
133
|
this.maxOutboundHopStreams = init.maxOutboundHopStreams
|
|
135
134
|
this.maxOutboundStopStreams = init.maxOutboundStopStreams ?? defaults.maxOutboundStopStreams
|
|
136
135
|
|
|
137
|
-
|
|
138
|
-
// fails on node < 15.4
|
|
139
|
-
setMaxListeners?.(Infinity, this.shutdownController.signal)
|
|
140
|
-
} catch { }
|
|
136
|
+
setMaxListeners(Infinity, this.shutdownController.signal)
|
|
141
137
|
|
|
142
138
|
if (init.advertise != null && init.advertise !== false) {
|
|
143
139
|
this.advertService = new AdvertService(components, init.advertise === true ? undefined : init.advertise)
|
|
@@ -57,6 +57,7 @@ export class RelayDiscovery extends TypedEventEmitter<RelayDiscoveryEvents> impl
|
|
|
57
57
|
// register a topology listener for when new peers are encountered
|
|
58
58
|
// that support the hop protocol
|
|
59
59
|
this.topologyId = await this.registrar.register(RELAY_V2_HOP_CODEC, {
|
|
60
|
+
notifyOnTransient: true,
|
|
60
61
|
onConnect: (peerId) => {
|
|
61
62
|
this.safeDispatchEvent('relay:discover', { detail: peerId })
|
|
62
63
|
}
|
|
@@ -2,10 +2,8 @@ import { CodeError } from '@libp2p/interface/errors'
|
|
|
2
2
|
import { TypedEventEmitter } from '@libp2p/interface/events'
|
|
3
3
|
import { logger } from '@libp2p/logger'
|
|
4
4
|
import { PeerMap } from '@libp2p/peer-collections'
|
|
5
|
-
import { peerIdFromString } from '@libp2p/peer-id'
|
|
6
5
|
import { multiaddr } from '@multiformats/multiaddr'
|
|
7
6
|
import type { ReservationStore } from './reservation-store.js'
|
|
8
|
-
import type { Connection } from '@libp2p/interface/connection'
|
|
9
7
|
import type { PeerId } from '@libp2p/interface/peer-id'
|
|
10
8
|
import type { Listener, ListenerEvents } from '@libp2p/interface/transport'
|
|
11
9
|
import type { ConnectionManager } from '@libp2p/interface-internal/connection-manager'
|
|
@@ -41,25 +39,9 @@ class CircuitRelayTransportListener extends TypedEventEmitter<ListenerEvents> im
|
|
|
41
39
|
async listen (addr: Multiaddr): Promise<void> {
|
|
42
40
|
log('listen on %a', addr)
|
|
43
41
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
// check if we already have a connection to the relay
|
|
48
|
-
if (relayPeerStr != null) {
|
|
49
|
-
const relayPeer = peerIdFromString(relayPeerStr)
|
|
50
|
-
const connections = this.connectionManager.getConnectionsMap().get(relayPeer) ?? []
|
|
51
|
-
|
|
52
|
-
if (connections.length > 0) {
|
|
53
|
-
relayConn = connections[0]
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
// open a new connection as we don't already have one
|
|
58
|
-
if (relayConn == null) {
|
|
59
|
-
const addrString = addr.toString().split('/p2p-circuit').find(a => a !== '')
|
|
60
|
-
const ma = multiaddr(addrString)
|
|
61
|
-
relayConn = await this.connectionManager.openConnection(ma)
|
|
62
|
-
}
|
|
42
|
+
// remove the circuit part to get the peer id of the relay
|
|
43
|
+
const relayAddr = addr.decapsulate('/p2p-circuit')
|
|
44
|
+
const relayConn = await this.connectionManager.openConnection(relayAddr)
|
|
63
45
|
|
|
64
46
|
if (!this.relayStore.hasReservation(relayConn.remotePeer)) {
|
|
65
47
|
// addRelay calls transportManager.listen which calls this listen method
|
package/src/connection/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { setMaxListeners } from 'events'
|
|
2
1
|
import { symbol } from '@libp2p/interface/connection'
|
|
3
2
|
import { CodeError } from '@libp2p/interface/errors'
|
|
3
|
+
import { setMaxListeners } from '@libp2p/interface/events'
|
|
4
4
|
import { logger } from '@libp2p/logger'
|
|
5
5
|
import type { AbortOptions } from '@libp2p/interface'
|
|
6
6
|
import type { Direction, Connection, Stream, ConnectionTimeline, ConnectionStatus, NewStreamOptions } from '@libp2p/interface/connection'
|
|
@@ -93,6 +93,10 @@ export class ConnectionImpl implements Connection {
|
|
|
93
93
|
this.encryption = init.encryption
|
|
94
94
|
this.transient = init.transient ?? false
|
|
95
95
|
|
|
96
|
+
if (this.remoteAddr.getPeerId() == null) {
|
|
97
|
+
this.remoteAddr = this.remoteAddr.encapsulate(`/p2p/${this.remotePeer}`)
|
|
98
|
+
}
|
|
99
|
+
|
|
96
100
|
this._newStream = newStream
|
|
97
101
|
this._close = close
|
|
98
102
|
this._abort = abort
|
|
@@ -152,10 +156,7 @@ export class ConnectionImpl implements Connection {
|
|
|
152
156
|
|
|
153
157
|
options.signal = options?.signal ?? AbortSignal.timeout(CLOSE_TIMEOUT)
|
|
154
158
|
|
|
155
|
-
|
|
156
|
-
// fails on node < 15.4
|
|
157
|
-
setMaxListeners?.(Infinity, options.signal)
|
|
158
|
-
} catch { }
|
|
159
|
+
setMaxListeners(Infinity, options.signal)
|
|
159
160
|
|
|
160
161
|
try {
|
|
161
162
|
log.trace('closing all streams')
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { setMaxListeners } from 'events'
|
|
2
1
|
import { AbortError, CodeError } from '@libp2p/interface/errors'
|
|
2
|
+
import { setMaxListeners } from '@libp2p/interface/events'
|
|
3
3
|
import { logger } from '@libp2p/logger'
|
|
4
|
+
import { PeerMap } from '@libp2p/peer-collections'
|
|
4
5
|
import { defaultAddressSort } from '@libp2p/utils/address-sort'
|
|
5
6
|
import { type Multiaddr, type Resolver, resolvers } from '@multiformats/multiaddr'
|
|
6
7
|
import { dnsaddrResolver } from '@multiformats/multiaddr/resolvers'
|
|
@@ -35,6 +36,7 @@ export interface PendingDialTarget {
|
|
|
35
36
|
|
|
36
37
|
export interface DialOptions extends AbortOptions {
|
|
37
38
|
priority?: number
|
|
39
|
+
force?: boolean
|
|
38
40
|
}
|
|
39
41
|
|
|
40
42
|
interface PendingDialInternal extends PendingDial {
|
|
@@ -48,6 +50,7 @@ interface DialerInit {
|
|
|
48
50
|
maxParallelDialsPerPeer?: number
|
|
49
51
|
dialTimeout?: number
|
|
50
52
|
resolvers?: Record<string, Resolver>
|
|
53
|
+
connections?: PeerMap<Connection[]>
|
|
51
54
|
}
|
|
52
55
|
|
|
53
56
|
const defaultOptions = {
|
|
@@ -83,12 +86,14 @@ export class DialQueue {
|
|
|
83
86
|
private readonly inProgressDialCount?: Metric
|
|
84
87
|
private readonly pendingDialCount?: Metric
|
|
85
88
|
private readonly shutDownController: AbortController
|
|
89
|
+
private readonly connections: PeerMap<Connection[]>
|
|
86
90
|
|
|
87
91
|
constructor (components: DialQueueComponents, init: DialerInit = {}) {
|
|
88
92
|
this.addressSorter = init.addressSorter ?? defaultOptions.addressSorter
|
|
89
93
|
this.maxPeerAddrsToDial = init.maxPeerAddrsToDial ?? defaultOptions.maxPeerAddrsToDial
|
|
90
94
|
this.maxParallelDialsPerPeer = init.maxParallelDialsPerPeer ?? defaultOptions.maxParallelDialsPerPeer
|
|
91
95
|
this.dialTimeout = init.dialTimeout ?? defaultOptions.dialTimeout
|
|
96
|
+
this.connections = init.connections ?? new PeerMap()
|
|
92
97
|
|
|
93
98
|
this.peerId = components.peerId
|
|
94
99
|
this.peerStore = components.peerStore
|
|
@@ -96,10 +101,7 @@ export class DialQueue {
|
|
|
96
101
|
this.transportManager = components.transportManager
|
|
97
102
|
this.shutDownController = new AbortController()
|
|
98
103
|
|
|
99
|
-
|
|
100
|
-
// This emitter gets listened to a lot
|
|
101
|
-
setMaxListeners?.(Infinity, this.shutDownController.signal)
|
|
102
|
-
} catch {}
|
|
104
|
+
setMaxListeners(Infinity, this.shutDownController.signal)
|
|
103
105
|
|
|
104
106
|
this.pendingDialCount = components.metrics?.registerMetric('libp2p_dialler_pending_dials')
|
|
105
107
|
this.inProgressDialCount = components.metrics?.registerMetric('libp2p_dialler_in_progress_dials')
|
|
@@ -190,6 +192,23 @@ export class DialQueue {
|
|
|
190
192
|
throw err
|
|
191
193
|
}
|
|
192
194
|
|
|
195
|
+
// make sure we don't have an existing connection to any of the addresses we
|
|
196
|
+
// are about to dial
|
|
197
|
+
let existingConnection = Array.from(this.connections.values()).flat().find(conn => {
|
|
198
|
+
if (options.force === true) {
|
|
199
|
+
return false
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
return addrsToDial.find(addr => {
|
|
203
|
+
return addr.multiaddr.equals(conn.remoteAddr)
|
|
204
|
+
})
|
|
205
|
+
})
|
|
206
|
+
|
|
207
|
+
if (existingConnection != null) {
|
|
208
|
+
log('already connected to %a', existingConnection.remoteAddr)
|
|
209
|
+
return existingConnection
|
|
210
|
+
}
|
|
211
|
+
|
|
193
212
|
// ready to dial, all async work finished - make sure we don't have any
|
|
194
213
|
// pending dials in progress for this peer or set of multiaddrs
|
|
195
214
|
const existingDial = this.pendingDials.find(dial => {
|
|
@@ -260,7 +279,28 @@ export class DialQueue {
|
|
|
260
279
|
// let other dials join this one
|
|
261
280
|
this.pendingDials.push(pendingDial)
|
|
262
281
|
|
|
263
|
-
|
|
282
|
+
const connection = await pendingDial.promise
|
|
283
|
+
|
|
284
|
+
// we may have been dialing a multiaddr without a peer id attached but by
|
|
285
|
+
// this point we have upgraded the connection so the remote peer information
|
|
286
|
+
// should be available - check again that we don't already have a connection
|
|
287
|
+
// to the remote multiaddr
|
|
288
|
+
existingConnection = Array.from(this.connections.values()).flat().find(conn => {
|
|
289
|
+
if (options.force === true) {
|
|
290
|
+
return false
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
return conn.id !== connection.id && conn.remoteAddr.equals(connection.remoteAddr)
|
|
294
|
+
})
|
|
295
|
+
|
|
296
|
+
if (existingConnection != null) {
|
|
297
|
+
log('already connected to %a', existingConnection.remoteAddr)
|
|
298
|
+
await connection.close()
|
|
299
|
+
return existingConnection
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
log('connection opened to %a', connection.remoteAddr)
|
|
303
|
+
return connection
|
|
264
304
|
}
|
|
265
305
|
|
|
266
306
|
private createDialAbortControllers (userSignal?: AbortSignal): ClearableSignal {
|
|
@@ -261,7 +261,8 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
|
|
|
261
261
|
dialTimeout: init.dialTimeout ?? DIAL_TIMEOUT,
|
|
262
262
|
resolvers: init.resolvers ?? {
|
|
263
263
|
dnsaddr: dnsaddrResolver
|
|
264
|
-
}
|
|
264
|
+
},
|
|
265
|
+
connections: this.connections
|
|
265
266
|
})
|
|
266
267
|
}
|
|
267
268
|
|
|
@@ -505,12 +506,13 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
|
|
|
505
506
|
|
|
506
507
|
if (peerId != null && options.force !== true) {
|
|
507
508
|
log('dial %p', peerId)
|
|
508
|
-
const
|
|
509
|
+
const existingConnection = this.getConnections(peerId)
|
|
510
|
+
.find(conn => !conn.transient)
|
|
509
511
|
|
|
510
|
-
if (
|
|
511
|
-
log('had an existing connection to %p', peerId)
|
|
512
|
+
if (existingConnection != null) {
|
|
513
|
+
log('had an existing non-transient connection to %p', peerId)
|
|
512
514
|
|
|
513
|
-
return
|
|
515
|
+
return existingConnection
|
|
514
516
|
}
|
|
515
517
|
}
|
|
516
518
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { setMaxListeners } from 'events'
|
|
1
|
+
import { setMaxListeners } from '@libp2p/interface/events'
|
|
2
2
|
import { logger } from '@libp2p/logger'
|
|
3
3
|
import { type AbortOptions, multiaddr, type Multiaddr } from '@multiformats/multiaddr'
|
|
4
4
|
import { type ClearableSignal, anySignal } from 'any-signal'
|
|
@@ -55,10 +55,7 @@ export function combineSignals (...signals: Array<AbortSignal | undefined>): Cle
|
|
|
55
55
|
|
|
56
56
|
for (const sig of signals) {
|
|
57
57
|
if (sig != null) {
|
|
58
|
-
|
|
59
|
-
// fails on node < 15.4
|
|
60
|
-
setMaxListeners?.(Infinity, sig)
|
|
61
|
-
} catch { }
|
|
58
|
+
setMaxListeners(Infinity, sig)
|
|
62
59
|
sigs.push(sig)
|
|
63
60
|
}
|
|
64
61
|
}
|
|
@@ -66,10 +63,7 @@ export function combineSignals (...signals: Array<AbortSignal | undefined>): Cle
|
|
|
66
63
|
// let any signal abort the dial
|
|
67
64
|
const signal = anySignal(sigs)
|
|
68
65
|
|
|
69
|
-
|
|
70
|
-
// fails on node < 15.4
|
|
71
|
-
setMaxListeners?.(Infinity, signal)
|
|
72
|
-
} catch {}
|
|
66
|
+
setMaxListeners(Infinity, signal)
|
|
73
67
|
|
|
74
68
|
return signal
|
|
75
69
|
}
|
package/src/dcutr/dcutr.ts
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import { CodeError } from '@libp2p/interface/errors'
|
|
2
2
|
import { logger } from '@libp2p/logger'
|
|
3
3
|
import { type Multiaddr, multiaddr } from '@multiformats/multiaddr'
|
|
4
|
-
import { Circuit, IP, DNS } from '@multiformats/multiaddr-matcher'
|
|
5
4
|
import delay from 'delay'
|
|
6
5
|
import { pbStream } from 'it-protobuf-stream'
|
|
7
|
-
import isPrivate from 'private-ip'
|
|
8
6
|
import { codes } from '../errors.js'
|
|
9
7
|
import { HolePunch } from './pb/message.js'
|
|
8
|
+
import { isPublicAndDialable } from './utils.js'
|
|
10
9
|
import { multicodec } from './index.js'
|
|
11
10
|
import type { DCUtRServiceComponents, DCUtRServiceInit } from './index.js'
|
|
12
11
|
import type { Connection, Stream } from '@libp2p/interface/connection'
|
|
@@ -72,6 +71,7 @@ export class DefaultDCUtRService implements Startable {
|
|
|
72
71
|
// register for notifications of when peers that support DCUtR connect
|
|
73
72
|
// nb. requires the identify service to be enabled
|
|
74
73
|
this.topologyId = await this.registrar.register(multicodec, {
|
|
74
|
+
notifyOnTransient: true,
|
|
75
75
|
onConnect: (peerId, connection) => {
|
|
76
76
|
if (!connection.transient) {
|
|
77
77
|
// the connection is already direct, no upgrade is required
|
|
@@ -241,7 +241,7 @@ export class DefaultDCUtRService implements Startable {
|
|
|
241
241
|
return ma
|
|
242
242
|
})
|
|
243
243
|
.filter(ma => {
|
|
244
|
-
return
|
|
244
|
+
return isPublicAndDialable(ma, this.transportManager)
|
|
245
245
|
})
|
|
246
246
|
|
|
247
247
|
if (publicAddresses.length > 0) {
|
|
@@ -365,7 +365,7 @@ export class DefaultDCUtRService implements Startable {
|
|
|
365
365
|
try {
|
|
366
366
|
const ma = multiaddr(addr)
|
|
367
367
|
|
|
368
|
-
if (!
|
|
368
|
+
if (!isPublicAndDialable(ma, this.transportManager)) {
|
|
369
369
|
continue
|
|
370
370
|
}
|
|
371
371
|
|
|
@@ -375,35 +375,4 @@ export class DefaultDCUtRService implements Startable {
|
|
|
375
375
|
|
|
376
376
|
return output
|
|
377
377
|
}
|
|
378
|
-
|
|
379
|
-
/**
|
|
380
|
-
* Returns true if the passed multiaddr is public, not relayed and we have a
|
|
381
|
-
* transport that can dial it
|
|
382
|
-
*/
|
|
383
|
-
isPublicAndDialable (ma: Multiaddr): boolean {
|
|
384
|
-
// ignore circuit relay
|
|
385
|
-
if (Circuit.matches(ma)) {
|
|
386
|
-
return false
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
// dns addresses are probably public?
|
|
390
|
-
if (DNS.matches(ma)) {
|
|
391
|
-
return true
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
// ensure we have only IPv4/IPv6 addresses
|
|
395
|
-
if (!IP.matches(ma)) {
|
|
396
|
-
return false
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
const transport = this.transportManager.transportForMultiaddr(ma)
|
|
400
|
-
|
|
401
|
-
if (transport == null) {
|
|
402
|
-
return false
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
const options = ma.toOptions()
|
|
406
|
-
|
|
407
|
-
return isPrivate(options.host) === false
|
|
408
|
-
}
|
|
409
378
|
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { type Multiaddr } from '@multiformats/multiaddr'
|
|
2
|
+
import { Circuit, IP, DNS } from '@multiformats/multiaddr-matcher'
|
|
3
|
+
import isPrivate from 'private-ip'
|
|
4
|
+
import type { TransportManager } from '@libp2p/interface-internal/src/transport-manager'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Returns true if the passed multiaddr is public, not relayed and we have a
|
|
8
|
+
* transport that can dial it
|
|
9
|
+
*/
|
|
10
|
+
export function isPublicAndDialable (ma: Multiaddr, transportManager: TransportManager): boolean {
|
|
11
|
+
// ignore circuit relay
|
|
12
|
+
if (Circuit.matches(ma)) {
|
|
13
|
+
return false
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const transport = transportManager.transportForMultiaddr(ma)
|
|
17
|
+
|
|
18
|
+
if (transport == null) {
|
|
19
|
+
return false
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// dns addresses are probably public?
|
|
23
|
+
if (DNS.matches(ma)) {
|
|
24
|
+
return true
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// ensure we have only IPv4/IPv6 addresses
|
|
28
|
+
if (!IP.matches(ma)) {
|
|
29
|
+
return false
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return isPrivate(ma.toOptions().host) === false
|
|
33
|
+
}
|
package/src/fetch/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { setMaxListeners } from 'events'
|
|
2
1
|
import { CodeError } from '@libp2p/interface/errors'
|
|
2
|
+
import { setMaxListeners } from '@libp2p/interface/events'
|
|
3
3
|
import { logger } from '@libp2p/logger'
|
|
4
4
|
import first from 'it-first'
|
|
5
5
|
import * as lp from 'it-length-prefixed'
|
|
@@ -146,10 +146,7 @@ class DefaultFetchService implements Startable, FetchService {
|
|
|
146
146
|
log('using default timeout of %d ms', this.init.timeout)
|
|
147
147
|
signal = AbortSignal.timeout(this.init.timeout ?? DEFAULT_TIMEOUT)
|
|
148
148
|
|
|
149
|
-
|
|
150
|
-
// fails on node < 15.4
|
|
151
|
-
setMaxListeners?.(Infinity, signal)
|
|
152
|
-
} catch {}
|
|
149
|
+
setMaxListeners(Infinity, signal)
|
|
153
150
|
}
|
|
154
151
|
|
|
155
152
|
try {
|