libp2p 0.39.5 → 0.40.0
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 +5 -0
- package/dist/src/address-manager/index.d.ts +18 -2
- package/dist/src/address-manager/index.d.ts.map +1 -1
- package/dist/src/address-manager/index.js +18 -5
- package/dist/src/address-manager/index.js.map +1 -1
- package/dist/src/circuit/auto-relay.d.ts +2 -2
- package/dist/src/circuit/auto-relay.d.ts.map +1 -1
- package/dist/src/circuit/auto-relay.js +12 -12
- package/dist/src/circuit/auto-relay.js.map +1 -1
- package/dist/src/circuit/index.d.ts +13 -3
- package/dist/src/circuit/index.d.ts.map +1 -1
- package/dist/src/circuit/index.js +1 -1
- package/dist/src/circuit/index.js.map +1 -1
- package/dist/src/circuit/pb/index.d.ts.map +1 -1
- package/dist/src/circuit/pb/index.js +29 -34
- package/dist/src/circuit/pb/index.js.map +1 -1
- package/dist/src/circuit/transport.d.ts +22 -7
- package/dist/src/circuit/transport.d.ts.map +1 -1
- package/dist/src/circuit/transport.js +26 -17
- package/dist/src/circuit/transport.js.map +1 -1
- package/dist/src/components.d.ts +110 -0
- package/dist/src/components.d.ts.map +1 -0
- package/dist/src/components.js +199 -0
- package/dist/src/components.js.map +1 -0
- package/dist/src/connection-manager/auto-dialler.d.ts +9 -2
- package/dist/src/connection-manager/auto-dialler.d.ts.map +1 -1
- package/dist/src/connection-manager/auto-dialler.js +6 -6
- package/dist/src/connection-manager/auto-dialler.js.map +1 -1
- package/dist/src/connection-manager/dialer/index.d.ts +12 -5
- package/dist/src/connection-manager/dialer/index.d.ts.map +1 -1
- package/dist/src/connection-manager/dialer/index.js +9 -9
- package/dist/src/connection-manager/dialer/index.js.map +1 -1
- package/dist/src/connection-manager/index.d.ts +41 -9
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +82 -39
- package/dist/src/connection-manager/index.js.map +1 -1
- package/dist/src/content-routing/index.d.ts +7 -2
- package/dist/src/content-routing/index.d.ts.map +1 -1
- package/dist/src/content-routing/index.js +4 -4
- package/dist/src/content-routing/index.js.map +1 -1
- package/dist/src/content-routing/utils.d.ts +1 -1
- package/dist/src/content-routing/utils.d.ts.map +1 -1
- package/dist/src/errors.d.ts +2 -1
- package/dist/src/errors.d.ts.map +1 -1
- package/dist/src/errors.js +1 -0
- package/dist/src/errors.js.map +1 -1
- package/dist/src/fetch/index.d.ts +7 -3
- package/dist/src/fetch/index.d.ts.map +1 -1
- package/dist/src/fetch/index.js +3 -3
- package/dist/src/fetch/index.js.map +1 -1
- package/dist/src/fetch/pb/proto.d.ts.map +1 -1
- package/dist/src/fetch/pb/proto.js +24 -35
- package/dist/src/fetch/pb/proto.js.map +1 -1
- package/dist/src/identify/index.d.ts +13 -3
- package/dist/src/identify/index.d.ts.map +1 -1
- package/dist/src/identify/index.js +41 -41
- package/dist/src/identify/index.js.map +1 -1
- package/dist/src/identify/pb/message.d.ts.map +1 -1
- package/dist/src/identify/pb/message.js +23 -34
- package/dist/src/identify/pb/message.js.map +1 -1
- package/dist/src/index.d.ts +10 -9
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/insecure/index.d.ts +2 -8
- package/dist/src/insecure/index.d.ts.map +1 -1
- package/dist/src/insecure/index.js +5 -3
- package/dist/src/insecure/index.js.map +1 -1
- package/dist/src/insecure/pb/proto.d.ts.map +1 -1
- package/dist/src/insecure/pb/proto.js +24 -29
- package/dist/src/insecure/pb/proto.js.map +1 -1
- package/dist/src/keychain/index.d.ts +6 -2
- package/dist/src/keychain/index.d.ts.map +1 -1
- package/dist/src/keychain/index.js +19 -19
- package/dist/src/keychain/index.js.map +1 -1
- package/dist/src/libp2p.d.ts +1 -2
- package/dist/src/libp2p.d.ts.map +1 -1
- package/dist/src/libp2p.js +48 -71
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/nat-manager.d.ts +9 -2
- package/dist/src/nat-manager.d.ts.map +1 -1
- package/dist/src/nat-manager.js +3 -3
- package/dist/src/nat-manager.js.map +1 -1
- package/dist/src/peer-record-updater.d.ts +11 -2
- package/dist/src/peer-record-updater.d.ts.map +1 -1
- package/dist/src/peer-record-updater.js +10 -10
- package/dist/src/peer-record-updater.js.map +1 -1
- package/dist/src/peer-routing.d.ts +6 -2
- package/dist/src/peer-routing.d.ts.map +1 -1
- package/dist/src/peer-routing.js +4 -4
- package/dist/src/peer-routing.js.map +1 -1
- package/dist/src/ping/index.d.ts +7 -3
- package/dist/src/ping/index.d.ts.map +1 -1
- package/dist/src/ping/index.js +3 -3
- package/dist/src/ping/index.js.map +1 -1
- package/dist/src/pnet/index.d.ts +2 -17
- package/dist/src/pnet/index.d.ts.map +1 -1
- package/dist/src/pnet/index.js +4 -1
- package/dist/src/pnet/index.js.map +1 -1
- package/dist/src/pubsub/dummy-pubsub.d.ts +2 -1
- package/dist/src/pubsub/dummy-pubsub.d.ts.map +1 -1
- package/dist/src/pubsub/dummy-pubsub.js +4 -0
- package/dist/src/pubsub/dummy-pubsub.js.map +1 -1
- package/dist/src/registrar.d.ts +9 -3
- package/dist/src/registrar.d.ts.map +1 -1
- package/dist/src/registrar.js +6 -6
- package/dist/src/registrar.js.map +1 -1
- package/dist/src/transport-manager.d.ts +9 -3
- package/dist/src/transport-manager.d.ts.map +1 -1
- package/dist/src/transport-manager.js +4 -4
- package/dist/src/transport-manager.js.map +1 -1
- package/dist/src/upgrader.d.ts +17 -5
- package/dist/src/upgrader.d.ts.map +1 -1
- package/dist/src/upgrader.js +55 -43
- package/dist/src/upgrader.js.map +1 -1
- package/package.json +37 -37
- package/src/address-manager/index.ts +31 -8
- package/src/circuit/auto-relay.ts +15 -15
- package/src/circuit/index.ts +16 -5
- package/src/circuit/pb/index.ts +29 -35
- package/src/circuit/transport.ts +49 -20
- package/src/components.ts +332 -0
- package/src/connection-manager/auto-dialler.ts +17 -9
- package/src/connection-manager/dialer/index.ts +23 -15
- package/src/connection-manager/index.ts +139 -47
- package/src/content-routing/index.ts +13 -7
- package/src/errors.ts +2 -1
- package/src/fetch/index.ts +12 -7
- package/src/fetch/pb/proto.ts +24 -35
- package/src/identify/index.ts +57 -53
- package/src/identify/pb/message.ts +23 -35
- package/src/index.ts +10 -9
- package/src/insecure/index.ts +6 -3
- package/src/insecure/pb/proto.ts +24 -29
- package/src/keychain/index.ts +27 -22
- package/src/libp2p.ts +49 -77
- package/src/nat-manager.ts +14 -6
- package/src/peer-record-updater.ts +23 -13
- package/src/peer-routing.ts +12 -7
- package/src/ping/index.ts +12 -7
- package/src/pnet/index.ts +5 -1
- package/src/pubsub/dummy-pubsub.ts +3 -1
- package/src/registrar.ts +17 -10
- package/src/transport-manager.ts +15 -8
- package/src/upgrader.ts +75 -49
- package/src/version.ts +1 -1
- package/dist/src/connection-manager/dialer/auto-dialer.d.ts +0 -16
- package/dist/src/connection-manager/dialer/auto-dialer.d.ts.map +0 -1
- package/dist/src/connection-manager/dialer/auto-dialer.js +0 -43
- package/dist/src/connection-manager/dialer/auto-dialer.js.map +0 -1
- package/src/connection-manager/dialer/auto-dialer.ts +0 -65
|
@@ -3,7 +3,8 @@ import { CustomEvent, EventEmitter } from '@libp2p/interfaces/events'
|
|
|
3
3
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
4
4
|
import { multiaddr } from '@multiformats/multiaddr'
|
|
5
5
|
import { peerIdFromString } from '@libp2p/peer-id'
|
|
6
|
-
import type {
|
|
6
|
+
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
7
|
+
import type { TransportManager } from '@libp2p/interface-transport'
|
|
7
8
|
|
|
8
9
|
export interface AddressManagerInit {
|
|
9
10
|
announceFilter?: AddressFilter
|
|
@@ -24,6 +25,11 @@ export interface AddressManagerInit {
|
|
|
24
25
|
noAnnounce?: string[]
|
|
25
26
|
}
|
|
26
27
|
|
|
28
|
+
export interface DefaultAddressManagerComponents {
|
|
29
|
+
peerId: PeerId
|
|
30
|
+
transportManager: TransportManager
|
|
31
|
+
}
|
|
32
|
+
|
|
27
33
|
export interface AddressFilter {
|
|
28
34
|
(addrs: Multiaddr[]): Multiaddr[]
|
|
29
35
|
}
|
|
@@ -31,7 +37,7 @@ export interface AddressFilter {
|
|
|
31
37
|
const defaultAddressFilter = (addrs: Multiaddr[]): Multiaddr[] => addrs
|
|
32
38
|
|
|
33
39
|
export class DefaultAddressManager extends EventEmitter<AddressManagerEvents> {
|
|
34
|
-
private readonly components:
|
|
40
|
+
private readonly components: DefaultAddressManagerComponents
|
|
35
41
|
private readonly listen: Set<string>
|
|
36
42
|
private readonly announce: Set<string>
|
|
37
43
|
private readonly observed: Set<string>
|
|
@@ -43,7 +49,7 @@ export class DefaultAddressManager extends EventEmitter<AddressManagerEvents> {
|
|
|
43
49
|
* The listen addresses will be used by the libp2p transports to listen for new connections,
|
|
44
50
|
* while the announce addresses will be used for the peer addresses' to other peers in the network.
|
|
45
51
|
*/
|
|
46
|
-
constructor (components:
|
|
52
|
+
constructor (components: DefaultAddressManagerComponents, init: AddressManagerInit) {
|
|
47
53
|
super()
|
|
48
54
|
|
|
49
55
|
const { listen = [], announce = [] } = init
|
|
@@ -76,6 +82,23 @@ export class DefaultAddressManager extends EventEmitter<AddressManagerEvents> {
|
|
|
76
82
|
return Array.from(this.observed).map((a) => multiaddr(a))
|
|
77
83
|
}
|
|
78
84
|
|
|
85
|
+
/**
|
|
86
|
+
* Add peer observed addresses
|
|
87
|
+
* Signal that we have confidence an observed multiaddr is publicly dialable -
|
|
88
|
+
* this will make it appear in the output of getAddresses()
|
|
89
|
+
*/
|
|
90
|
+
confirmObservedAddr (addr: Multiaddr): void {
|
|
91
|
+
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Signal that we do not have confidence an observed multiaddr is publicly dialable -
|
|
96
|
+
* this will remove it from the output of getObservedAddrs()
|
|
97
|
+
*/
|
|
98
|
+
removeObservedAddr (addr: Multiaddr): void {
|
|
99
|
+
|
|
100
|
+
}
|
|
101
|
+
|
|
79
102
|
/**
|
|
80
103
|
* Add peer observed addresses
|
|
81
104
|
*/
|
|
@@ -88,8 +111,8 @@ export class DefaultAddressManager extends EventEmitter<AddressManagerEvents> {
|
|
|
88
111
|
const remotePeerId = peerIdFromString(remotePeer)
|
|
89
112
|
|
|
90
113
|
// use same encoding for comparison
|
|
91
|
-
if (remotePeerId.equals(this.components.
|
|
92
|
-
ma = ma.decapsulate(multiaddr(`/p2p/${this.components.
|
|
114
|
+
if (remotePeerId.equals(this.components.peerId)) {
|
|
115
|
+
ma = ma.decapsulate(multiaddr(`/p2p/${this.components.peerId.toString()}`))
|
|
93
116
|
}
|
|
94
117
|
}
|
|
95
118
|
|
|
@@ -109,7 +132,7 @@ export class DefaultAddressManager extends EventEmitter<AddressManagerEvents> {
|
|
|
109
132
|
|
|
110
133
|
if (addrs.length === 0) {
|
|
111
134
|
// no configured announce addrs, add configured listen addresses
|
|
112
|
-
addrs = this.components.
|
|
135
|
+
addrs = this.components.transportManager.getAddrs().map(ma => ma.toString())
|
|
113
136
|
}
|
|
114
137
|
|
|
115
138
|
addrs = addrs.concat(this.getObservedAddrs().map(ma => ma.toString()))
|
|
@@ -121,11 +144,11 @@ export class DefaultAddressManager extends EventEmitter<AddressManagerEvents> {
|
|
|
121
144
|
return this.announceFilter(Array.from(addrSet)
|
|
122
145
|
.map(str => multiaddr(str)))
|
|
123
146
|
.map(ma => {
|
|
124
|
-
if (ma.getPeerId() === this.components.
|
|
147
|
+
if (ma.getPeerId() === this.components.peerId.toString()) {
|
|
125
148
|
return ma
|
|
126
149
|
}
|
|
127
150
|
|
|
128
|
-
return ma.encapsulate(`/p2p/${this.components.
|
|
151
|
+
return ma.encapsulate(`/p2p/${this.components.peerId.toString()}`)
|
|
129
152
|
})
|
|
130
153
|
}
|
|
131
154
|
}
|
|
@@ -13,11 +13,11 @@ import {
|
|
|
13
13
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
14
14
|
import type { AddressSorter, PeerProtocolsChangeData } from '@libp2p/interface-peer-store'
|
|
15
15
|
import type { Connection } from '@libp2p/interface-connection'
|
|
16
|
-
import type { Components } from '@libp2p/components'
|
|
17
16
|
import sort from 'it-sort'
|
|
18
17
|
import all from 'it-all'
|
|
19
18
|
import { pipe } from 'it-pipe'
|
|
20
19
|
import { publicAddressesFirst } from '@libp2p/utils/address-sort'
|
|
20
|
+
import type { RelayComponents } from './index.js'
|
|
21
21
|
|
|
22
22
|
const log = logger('libp2p:auto-relay')
|
|
23
23
|
|
|
@@ -30,13 +30,13 @@ export interface AutoRelayInit {
|
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
export class AutoRelay {
|
|
33
|
-
private readonly components:
|
|
33
|
+
private readonly components: RelayComponents
|
|
34
34
|
private readonly addressSorter: AddressSorter
|
|
35
35
|
private readonly maxListeners: number
|
|
36
36
|
private readonly listenRelays: Set<string>
|
|
37
37
|
private readonly onError: (error: Error, msg?: string) => void
|
|
38
38
|
|
|
39
|
-
constructor (components:
|
|
39
|
+
constructor (components: RelayComponents, init: AutoRelayInit) {
|
|
40
40
|
this.components = components
|
|
41
41
|
this.addressSorter = init.addressSorter ?? publicAddressesFirst
|
|
42
42
|
this.maxListeners = init.maxListeners ?? 1
|
|
@@ -46,12 +46,12 @@ export class AutoRelay {
|
|
|
46
46
|
this._onProtocolChange = this._onProtocolChange.bind(this)
|
|
47
47
|
this._onPeerDisconnected = this._onPeerDisconnected.bind(this)
|
|
48
48
|
|
|
49
|
-
this.components.
|
|
49
|
+
this.components.peerStore.addEventListener('change:protocols', (evt) => {
|
|
50
50
|
void this._onProtocolChange(evt).catch(err => {
|
|
51
51
|
log.error(err)
|
|
52
52
|
})
|
|
53
53
|
})
|
|
54
|
-
this.components.
|
|
54
|
+
this.components.connectionManager.addEventListener('peer:disconnect', this._onPeerDisconnected)
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
/**
|
|
@@ -85,7 +85,7 @@ export class AutoRelay {
|
|
|
85
85
|
|
|
86
86
|
// If protocol, check if can hop, store info in the metadataBook and listen on it
|
|
87
87
|
try {
|
|
88
|
-
const connections = this.components.
|
|
88
|
+
const connections = this.components.connectionManager.getConnections(peerId)
|
|
89
89
|
|
|
90
90
|
if (connections.length === 0) {
|
|
91
91
|
return
|
|
@@ -102,7 +102,7 @@ export class AutoRelay {
|
|
|
102
102
|
const supportsHop = await canHop({ connection })
|
|
103
103
|
|
|
104
104
|
if (supportsHop) {
|
|
105
|
-
await this.components.
|
|
105
|
+
await this.components.peerStore.metadataBook.setValue(peerId, HOP_METADATA_KEY, uint8ArrayFromString(HOP_METADATA_VALUE))
|
|
106
106
|
await this._addListenRelay(connection, id)
|
|
107
107
|
}
|
|
108
108
|
} catch (err: any) {
|
|
@@ -140,7 +140,7 @@ export class AutoRelay {
|
|
|
140
140
|
|
|
141
141
|
// Get peer known addresses and sort them with public addresses first
|
|
142
142
|
const remoteAddrs = await pipe(
|
|
143
|
-
await this.components.
|
|
143
|
+
await this.components.peerStore.addressBook.get(connection.remotePeer),
|
|
144
144
|
(source) => sort(source, this.addressSorter),
|
|
145
145
|
async (source) => await all(source)
|
|
146
146
|
)
|
|
@@ -158,7 +158,7 @@ export class AutoRelay {
|
|
|
158
158
|
multiaddr = multiaddr.encapsulate('/p2p-circuit')
|
|
159
159
|
|
|
160
160
|
// Announce multiaddrs will update on listen success by TransportManager event being triggered
|
|
161
|
-
await this.components.
|
|
161
|
+
await this.components.transportManager.listen([multiaddr])
|
|
162
162
|
return true
|
|
163
163
|
} catch (err: any) {
|
|
164
164
|
log.error('error listening on circuit address', err)
|
|
@@ -203,7 +203,7 @@ export class AutoRelay {
|
|
|
203
203
|
}
|
|
204
204
|
|
|
205
205
|
const knownHopsToDial = []
|
|
206
|
-
const peers = await this.components.
|
|
206
|
+
const peers = await this.components.peerStore.all()
|
|
207
207
|
|
|
208
208
|
// Check if we have known hop peers to use and attempt to listen on the already connected
|
|
209
209
|
for (const { id, metadata } of peers) {
|
|
@@ -225,7 +225,7 @@ export class AutoRelay {
|
|
|
225
225
|
continue
|
|
226
226
|
}
|
|
227
227
|
|
|
228
|
-
const connections = this.components.
|
|
228
|
+
const connections = this.components.connectionManager.getConnections(id)
|
|
229
229
|
|
|
230
230
|
// If not connected, store for possible later use.
|
|
231
231
|
if (connections.length === 0) {
|
|
@@ -254,19 +254,19 @@ export class AutoRelay {
|
|
|
254
254
|
// Try to find relays to hop on the network
|
|
255
255
|
try {
|
|
256
256
|
const cid = await namespaceToCid(RELAY_RENDEZVOUS_NS)
|
|
257
|
-
for await (const provider of this.components.
|
|
257
|
+
for await (const provider of this.components.contentRouting.findProviders(cid)) {
|
|
258
258
|
if (provider.multiaddrs.length === 0) {
|
|
259
259
|
continue
|
|
260
260
|
}
|
|
261
261
|
|
|
262
262
|
const peerId = provider.id
|
|
263
263
|
|
|
264
|
-
if (peerId.equals(this.components.
|
|
264
|
+
if (peerId.equals(this.components.peerId)) {
|
|
265
265
|
// Skip the provider if it's us as dialing will fail
|
|
266
266
|
continue
|
|
267
267
|
}
|
|
268
268
|
|
|
269
|
-
await this.components.
|
|
269
|
+
await this.components.peerStore.addressBook.add(peerId, provider.multiaddrs)
|
|
270
270
|
|
|
271
271
|
await this._tryToListenOnRelay(peerId)
|
|
272
272
|
|
|
@@ -282,7 +282,7 @@ export class AutoRelay {
|
|
|
282
282
|
|
|
283
283
|
async _tryToListenOnRelay (peerId: PeerId) {
|
|
284
284
|
try {
|
|
285
|
-
const connection = await this.components.
|
|
285
|
+
const connection = await this.components.connectionManager.openConnection(peerId)
|
|
286
286
|
await this._addListenRelay(connection, peerId.toString())
|
|
287
287
|
} catch (err: any) {
|
|
288
288
|
log.error('Could not use %p as relay', peerId, err)
|
package/src/circuit/index.ts
CHANGED
|
@@ -10,10 +10,13 @@ import { namespaceToCid } from './utils.js'
|
|
|
10
10
|
import {
|
|
11
11
|
RELAY_RENDEZVOUS_NS
|
|
12
12
|
} from './constants.js'
|
|
13
|
-
import type { AddressSorter } from '@libp2p/interface-peer-store'
|
|
13
|
+
import type { AddressSorter, PeerStore } from '@libp2p/interface-peer-store'
|
|
14
14
|
import type { Startable } from '@libp2p/interfaces/startable'
|
|
15
|
-
import type { Components } from '@libp2p/components'
|
|
16
15
|
import type { RelayConfig } from '../index.js'
|
|
16
|
+
import type { ContentRouting } from '@libp2p/interface-content-routing'
|
|
17
|
+
import type { ConnectionManager } from '@libp2p/interface-connection-manager'
|
|
18
|
+
import type { TransportManager } from '@libp2p/interface-transport'
|
|
19
|
+
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
17
20
|
|
|
18
21
|
const log = logger('libp2p:relay')
|
|
19
22
|
|
|
@@ -36,8 +39,16 @@ export interface RelayInit extends RelayConfig {
|
|
|
36
39
|
addressSorter?: AddressSorter
|
|
37
40
|
}
|
|
38
41
|
|
|
42
|
+
export interface RelayComponents {
|
|
43
|
+
peerId: PeerId
|
|
44
|
+
contentRouting: ContentRouting
|
|
45
|
+
peerStore: PeerStore
|
|
46
|
+
connectionManager: ConnectionManager
|
|
47
|
+
transportManager: TransportManager
|
|
48
|
+
}
|
|
49
|
+
|
|
39
50
|
export class Relay implements Startable {
|
|
40
|
-
private readonly components:
|
|
51
|
+
private readonly components: RelayComponents
|
|
41
52
|
private readonly init: RelayInit
|
|
42
53
|
// @ts-expect-error this field isn't used anywhere?
|
|
43
54
|
private readonly autoRelay?: AutoRelay
|
|
@@ -47,7 +58,7 @@ export class Relay implements Startable {
|
|
|
47
58
|
/**
|
|
48
59
|
* Creates an instance of Relay
|
|
49
60
|
*/
|
|
50
|
-
constructor (components:
|
|
61
|
+
constructor (components: RelayComponents, init: RelayInit) {
|
|
51
62
|
this.components = components
|
|
52
63
|
// Create autoRelay if enabled
|
|
53
64
|
this.autoRelay = init.autoRelay?.enabled !== false
|
|
@@ -97,7 +108,7 @@ export class Relay implements Startable {
|
|
|
97
108
|
async _advertiseService () {
|
|
98
109
|
try {
|
|
99
110
|
const cid = await namespaceToCid(RELAY_RENDEZVOUS_NS)
|
|
100
|
-
await this.components.
|
|
111
|
+
await this.components.contentRouting.provide(cid)
|
|
101
112
|
} catch (err: any) {
|
|
102
113
|
if (err.code === codes.ERR_NO_ROUTERS_AVAILABLE) {
|
|
103
114
|
log.error('a content router, such as a DHT, must be provided in order to advertise the relay service', err)
|
package/src/circuit/pb/index.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
/* eslint-disable import/export */
|
|
2
|
+
/* eslint-disable complexity */
|
|
2
3
|
/* eslint-disable @typescript-eslint/no-namespace */
|
|
4
|
+
/* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */
|
|
3
5
|
|
|
4
6
|
import { enumeration, encodeMessage, decodeMessage, message } from 'protons-runtime'
|
|
5
7
|
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
@@ -87,32 +89,31 @@ export namespace CircuitRelay {
|
|
|
87
89
|
|
|
88
90
|
export const codec = (): Codec<Peer> => {
|
|
89
91
|
if (_codec == null) {
|
|
90
|
-
_codec = message<Peer>((obj,
|
|
92
|
+
_codec = message<Peer>((obj, w, opts = {}) => {
|
|
91
93
|
if (opts.lengthDelimited !== false) {
|
|
92
|
-
|
|
94
|
+
w.fork()
|
|
93
95
|
}
|
|
94
96
|
|
|
95
|
-
if (obj.id != null) {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
} else {
|
|
99
|
-
throw new Error('Protocol error: required field "id" was not found in object')
|
|
97
|
+
if (opts.writeDefaults === true || (obj.id != null && obj.id.byteLength > 0)) {
|
|
98
|
+
w.uint32(10)
|
|
99
|
+
w.bytes(obj.id)
|
|
100
100
|
}
|
|
101
101
|
|
|
102
102
|
if (obj.addrs != null) {
|
|
103
103
|
for (const value of obj.addrs) {
|
|
104
|
-
|
|
105
|
-
|
|
104
|
+
w.uint32(18)
|
|
105
|
+
w.bytes(value)
|
|
106
106
|
}
|
|
107
|
-
} else {
|
|
108
|
-
throw new Error('Protocol error: required field "addrs" was not found in object')
|
|
109
107
|
}
|
|
110
108
|
|
|
111
109
|
if (opts.lengthDelimited !== false) {
|
|
112
|
-
|
|
110
|
+
w.ldelim()
|
|
113
111
|
}
|
|
114
112
|
}, (reader, length) => {
|
|
115
|
-
const obj: any = {
|
|
113
|
+
const obj: any = {
|
|
114
|
+
id: new Uint8Array(0),
|
|
115
|
+
addrs: []
|
|
116
|
+
}
|
|
116
117
|
|
|
117
118
|
const end = length == null ? reader.len : reader.pos + length
|
|
118
119
|
|
|
@@ -124,7 +125,6 @@ export namespace CircuitRelay {
|
|
|
124
125
|
obj.id = reader.bytes()
|
|
125
126
|
break
|
|
126
127
|
case 2:
|
|
127
|
-
obj.addrs = obj.addrs ?? []
|
|
128
128
|
obj.addrs.push(reader.bytes())
|
|
129
129
|
break
|
|
130
130
|
default:
|
|
@@ -133,16 +133,6 @@ export namespace CircuitRelay {
|
|
|
133
133
|
}
|
|
134
134
|
}
|
|
135
135
|
|
|
136
|
-
obj.addrs = obj.addrs ?? []
|
|
137
|
-
|
|
138
|
-
if (obj.id == null) {
|
|
139
|
-
throw new Error('Protocol error: value for required field "id" was not found in protobuf')
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
if (obj.addrs == null) {
|
|
143
|
-
throw new Error('Protocol error: value for required field "addrs" was not found in protobuf')
|
|
144
|
-
}
|
|
145
|
-
|
|
146
136
|
return obj
|
|
147
137
|
})
|
|
148
138
|
}
|
|
@@ -163,33 +153,37 @@ export namespace CircuitRelay {
|
|
|
163
153
|
|
|
164
154
|
export const codec = (): Codec<CircuitRelay> => {
|
|
165
155
|
if (_codec == null) {
|
|
166
|
-
_codec = message<CircuitRelay>((obj,
|
|
156
|
+
_codec = message<CircuitRelay>((obj, w, opts = {}) => {
|
|
167
157
|
if (opts.lengthDelimited !== false) {
|
|
168
|
-
|
|
158
|
+
w.fork()
|
|
169
159
|
}
|
|
170
160
|
|
|
171
161
|
if (obj.type != null) {
|
|
172
|
-
|
|
173
|
-
CircuitRelay.Type.codec().encode(obj.type,
|
|
162
|
+
w.uint32(8)
|
|
163
|
+
CircuitRelay.Type.codec().encode(obj.type, w)
|
|
174
164
|
}
|
|
175
165
|
|
|
176
166
|
if (obj.srcPeer != null) {
|
|
177
|
-
|
|
178
|
-
CircuitRelay.Peer.codec().encode(obj.srcPeer,
|
|
167
|
+
w.uint32(18)
|
|
168
|
+
CircuitRelay.Peer.codec().encode(obj.srcPeer, w, {
|
|
169
|
+
writeDefaults: false
|
|
170
|
+
})
|
|
179
171
|
}
|
|
180
172
|
|
|
181
173
|
if (obj.dstPeer != null) {
|
|
182
|
-
|
|
183
|
-
CircuitRelay.Peer.codec().encode(obj.dstPeer,
|
|
174
|
+
w.uint32(26)
|
|
175
|
+
CircuitRelay.Peer.codec().encode(obj.dstPeer, w, {
|
|
176
|
+
writeDefaults: false
|
|
177
|
+
})
|
|
184
178
|
}
|
|
185
179
|
|
|
186
180
|
if (obj.code != null) {
|
|
187
|
-
|
|
188
|
-
CircuitRelay.Status.codec().encode(obj.code,
|
|
181
|
+
w.uint32(32)
|
|
182
|
+
CircuitRelay.Status.codec().encode(obj.code, w)
|
|
189
183
|
}
|
|
190
184
|
|
|
191
185
|
if (opts.lengthDelimited !== false) {
|
|
192
|
-
|
|
186
|
+
w.ldelim()
|
|
193
187
|
}
|
|
194
188
|
}, (reader, length) => {
|
|
195
189
|
const obj: any = {}
|
package/src/circuit/transport.ts
CHANGED
|
@@ -11,11 +11,10 @@ import { createListener } from './listener.js'
|
|
|
11
11
|
import { handleCanHop, handleHop, hop } from './circuit/hop.js'
|
|
12
12
|
import { handleStop } from './circuit/stop.js'
|
|
13
13
|
import { StreamHandler } from './circuit/stream-handler.js'
|
|
14
|
-
import { symbol } from '@libp2p/interface-transport'
|
|
14
|
+
import { symbol, Upgrader } from '@libp2p/interface-transport'
|
|
15
15
|
import { peerIdFromString } from '@libp2p/peer-id'
|
|
16
|
-
import { Components, Initializable } from '@libp2p/components'
|
|
17
16
|
import type { AbortOptions } from '@libp2p/interfaces'
|
|
18
|
-
import type { IncomingStreamData } from '@libp2p/interface-registrar'
|
|
17
|
+
import type { IncomingStreamData, Registrar } from '@libp2p/interface-registrar'
|
|
19
18
|
import type { Listener, Transport, CreateListenerOptions, ConnectionHandler } from '@libp2p/interface-transport'
|
|
20
19
|
import type { Connection } from '@libp2p/interface-connection'
|
|
21
20
|
import type { RelayConfig } from '../index.js'
|
|
@@ -24,21 +23,47 @@ import { TimeoutController } from 'timeout-abort-controller'
|
|
|
24
23
|
import { setMaxListeners } from 'events'
|
|
25
24
|
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
26
25
|
import type { Duplex } from 'it-stream-types'
|
|
26
|
+
import type { Startable } from '@libp2p/interfaces/startable'
|
|
27
|
+
import type { ConnectionManager } from '@libp2p/interface-connection-manager'
|
|
28
|
+
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
29
|
+
import type { PeerStore } from '@libp2p/interface-peer-store'
|
|
30
|
+
import type { AddressManager } from '@libp2p/interface-address-manager'
|
|
27
31
|
|
|
28
32
|
const log = logger('libp2p:circuit')
|
|
29
33
|
|
|
30
|
-
export
|
|
34
|
+
export interface CircuitComponents {
|
|
35
|
+
peerId: PeerId
|
|
36
|
+
peerStore: PeerStore
|
|
37
|
+
registrar: Registrar
|
|
38
|
+
connectionManager: ConnectionManager
|
|
39
|
+
upgrader: Upgrader
|
|
40
|
+
addressManager: AddressManager
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export class Circuit implements Transport, Startable {
|
|
31
44
|
private handler?: ConnectionHandler
|
|
32
|
-
private components:
|
|
45
|
+
private readonly components: CircuitComponents
|
|
33
46
|
private readonly _init: RelayConfig
|
|
47
|
+
private _started: boolean
|
|
34
48
|
|
|
35
|
-
constructor (init: RelayConfig) {
|
|
49
|
+
constructor (components: CircuitComponents, init: RelayConfig) {
|
|
36
50
|
this._init = init
|
|
51
|
+
this.components = components
|
|
52
|
+
this._started = false
|
|
37
53
|
}
|
|
38
54
|
|
|
39
|
-
|
|
40
|
-
this.
|
|
41
|
-
|
|
55
|
+
isStarted () {
|
|
56
|
+
return this._started
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async start (): Promise<void> {
|
|
60
|
+
if (this._started) {
|
|
61
|
+
return
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
this._started = true
|
|
65
|
+
|
|
66
|
+
await this.components.registrar.handle(RELAY_CODEC, (data) => {
|
|
42
67
|
void this._onProtocol(data).catch(err => {
|
|
43
68
|
log.error(err)
|
|
44
69
|
})
|
|
@@ -48,6 +73,10 @@ export class Circuit implements Transport, Initializable {
|
|
|
48
73
|
})
|
|
49
74
|
}
|
|
50
75
|
|
|
76
|
+
async stop () {
|
|
77
|
+
await this.components.registrar.unhandle(RELAY_CODEC)
|
|
78
|
+
}
|
|
79
|
+
|
|
51
80
|
hopEnabled () {
|
|
52
81
|
return true
|
|
53
82
|
}
|
|
@@ -108,7 +137,7 @@ export class Circuit implements Transport, Initializable {
|
|
|
108
137
|
request,
|
|
109
138
|
streamHandler,
|
|
110
139
|
circuit: this,
|
|
111
|
-
connectionManager: this.components.
|
|
140
|
+
connectionManager: this.components.connectionManager
|
|
112
141
|
})
|
|
113
142
|
break
|
|
114
143
|
}
|
|
@@ -145,7 +174,7 @@ export class Circuit implements Transport, Initializable {
|
|
|
145
174
|
const type = request.type === CircuitPB.Type.HOP ? 'relay' : 'inbound'
|
|
146
175
|
log('new %s connection %s', type, maConn.remoteAddr)
|
|
147
176
|
|
|
148
|
-
const conn = await this.components.
|
|
177
|
+
const conn = await this.components.upgrader.upgradeInbound(maConn)
|
|
149
178
|
log('%s connection %s upgraded', type, maConn.remoteAddr)
|
|
150
179
|
|
|
151
180
|
if (this.handler != null) {
|
|
@@ -178,12 +207,12 @@ export class Circuit implements Transport, Initializable {
|
|
|
178
207
|
const destinationPeer = peerIdFromString(destinationId)
|
|
179
208
|
|
|
180
209
|
let disconnectOnFailure = false
|
|
181
|
-
const relayConnections = this.components.
|
|
210
|
+
const relayConnections = this.components.connectionManager.getConnections(relayPeer)
|
|
182
211
|
let relayConnection = relayConnections[0]
|
|
183
212
|
|
|
184
213
|
if (relayConnection == null) {
|
|
185
|
-
await this.components.
|
|
186
|
-
relayConnection = await this.components.
|
|
214
|
+
await this.components.peerStore.addressBook.add(relayPeer, [relayAddr])
|
|
215
|
+
relayConnection = await this.components.connectionManager.openConnection(relayPeer, options)
|
|
187
216
|
disconnectOnFailure = true
|
|
188
217
|
}
|
|
189
218
|
|
|
@@ -194,8 +223,8 @@ export class Circuit implements Transport, Initializable {
|
|
|
194
223
|
request: {
|
|
195
224
|
type: CircuitPB.Type.HOP,
|
|
196
225
|
srcPeer: {
|
|
197
|
-
id: this.components.
|
|
198
|
-
addrs: this.components.
|
|
226
|
+
id: this.components.peerId.toBytes(),
|
|
227
|
+
addrs: this.components.addressManager.getAddresses().map(addr => addr.bytes)
|
|
199
228
|
},
|
|
200
229
|
dstPeer: {
|
|
201
230
|
id: destinationPeer.toBytes(),
|
|
@@ -204,7 +233,7 @@ export class Circuit implements Transport, Initializable {
|
|
|
204
233
|
}
|
|
205
234
|
})
|
|
206
235
|
|
|
207
|
-
const localAddr = relayAddr.encapsulate(`/p2p-circuit/p2p/${this.components.
|
|
236
|
+
const localAddr = relayAddr.encapsulate(`/p2p-circuit/p2p/${this.components.peerId.toString()}`)
|
|
208
237
|
const maConn = streamToMaConnection({
|
|
209
238
|
stream: virtualConnection,
|
|
210
239
|
remoteAddr: ma,
|
|
@@ -212,7 +241,7 @@ export class Circuit implements Transport, Initializable {
|
|
|
212
241
|
})
|
|
213
242
|
log('new outbound connection %s', maConn.remoteAddr)
|
|
214
243
|
|
|
215
|
-
return await this.components.
|
|
244
|
+
return await this.components.upgrader.upgradeOutbound(maConn)
|
|
216
245
|
} catch (err: any) {
|
|
217
246
|
log.error('Circuit relay dial failed', err)
|
|
218
247
|
disconnectOnFailure && await relayConnection.close()
|
|
@@ -228,8 +257,8 @@ export class Circuit implements Transport, Initializable {
|
|
|
228
257
|
this.handler = options.handler
|
|
229
258
|
|
|
230
259
|
return createListener({
|
|
231
|
-
connectionManager: this.components.
|
|
232
|
-
peerStore: this.components.
|
|
260
|
+
connectionManager: this.components.connectionManager,
|
|
261
|
+
peerStore: this.components.peerStore
|
|
233
262
|
})
|
|
234
263
|
}
|
|
235
264
|
|