libp2p 0.36.2-64bfcee → 0.36.2-a15254f
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/src/circuit/auto-relay.d.ts.map +1 -1
- package/dist/src/circuit/auto-relay.js +7 -6
- package/dist/src/circuit/auto-relay.js.map +1 -1
- package/dist/src/circuit/circuit/hop.d.ts +4 -4
- package/dist/src/circuit/circuit/hop.d.ts.map +1 -1
- package/dist/src/circuit/circuit/hop.js +5 -5
- package/dist/src/circuit/circuit/hop.js.map +1 -1
- package/dist/src/circuit/circuit/stop.d.ts +3 -3
- package/dist/src/circuit/circuit/stop.d.ts.map +1 -1
- package/dist/src/circuit/circuit/stop.js.map +1 -1
- package/dist/src/circuit/circuit/stream-handler.d.ts +4 -4
- package/dist/src/circuit/circuit/stream-handler.d.ts.map +1 -1
- package/dist/src/circuit/circuit/stream-handler.js +2 -3
- package/dist/src/circuit/circuit/stream-handler.js.map +1 -1
- package/dist/src/circuit/circuit/utils.d.ts +2 -2
- package/dist/src/circuit/circuit/utils.d.ts.map +1 -1
- package/dist/src/circuit/circuit/utils.js.map +1 -1
- package/dist/src/circuit/listener.d.ts +3 -3
- package/dist/src/circuit/listener.d.ts.map +1 -1
- package/dist/src/circuit/listener.js +9 -1
- package/dist/src/circuit/listener.js.map +1 -1
- package/dist/src/circuit/pb/index.d.ts +42 -164
- package/dist/src/circuit/pb/index.d.ts.map +1 -0
- package/dist/src/circuit/pb/index.js +67 -523
- package/dist/src/circuit/pb/index.js.map +1 -0
- package/dist/src/circuit/transport.d.ts.map +1 -1
- package/dist/src/circuit/transport.js +6 -4
- package/dist/src/circuit/transport.js.map +1 -1
- package/dist/src/config.d.ts.map +1 -1
- package/dist/src/config.js +5 -7
- package/dist/src/config.js.map +1 -1
- package/dist/src/connection-manager/auto-dialler.js +4 -4
- package/dist/src/connection-manager/auto-dialler.js.map +1 -1
- package/dist/src/{dialer → connection-manager/dialer}/auto-dialer.d.ts +2 -0
- package/dist/src/connection-manager/dialer/auto-dialer.d.ts.map +1 -0
- package/dist/src/connection-manager/dialer/auto-dialer.js +37 -0
- package/dist/src/connection-manager/dialer/auto-dialer.js.map +1 -0
- package/dist/src/{dialer → connection-manager/dialer}/dial-request.d.ts +2 -2
- package/dist/src/connection-manager/dialer/dial-request.d.ts.map +1 -0
- package/dist/src/{dialer → connection-manager/dialer}/dial-request.js +1 -1
- package/dist/src/connection-manager/dialer/dial-request.js.map +1 -0
- package/dist/src/{dialer → connection-manager/dialer}/index.d.ts +35 -7
- package/dist/src/connection-manager/dialer/index.d.ts.map +1 -0
- package/dist/src/{dialer → connection-manager/dialer}/index.js +10 -24
- package/dist/src/connection-manager/dialer/index.js.map +1 -0
- package/dist/src/connection-manager/index.d.ts +55 -26
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +78 -40
- package/dist/src/connection-manager/index.js.map +1 -1
- package/dist/src/dht/dummy-dht.d.ts +17 -0
- package/dist/src/dht/dummy-dht.d.ts.map +1 -0
- package/dist/src/dht/dummy-dht.js +39 -0
- package/dist/src/dht/dummy-dht.js.map +1 -0
- package/dist/src/errors.d.ts +2 -0
- package/dist/src/errors.d.ts.map +1 -1
- package/dist/src/errors.js +2 -0
- package/dist/src/errors.js.map +1 -1
- package/dist/src/fetch/index.d.ts.map +1 -1
- package/dist/src/fetch/index.js +6 -7
- package/dist/src/fetch/index.js.map +1 -1
- package/dist/src/fetch/pb/proto.d.ts +20 -128
- package/dist/src/fetch/pb/proto.d.ts.map +1 -0
- package/dist/src/fetch/pb/proto.js +44 -331
- package/dist/src/fetch/pb/proto.js.map +1 -0
- package/dist/src/identify/index.d.ts +4 -2
- package/dist/src/identify/index.d.ts.map +1 -1
- package/dist/src/identify/index.js +12 -12
- package/dist/src/identify/index.js.map +1 -1
- package/dist/src/identify/pb/message.d.ts +13 -108
- package/dist/src/identify/pb/message.d.ts.map +1 -0
- package/dist/src/identify/pb/message.js +21 -366
- package/dist/src/identify/pb/message.js.map +1 -0
- package/dist/src/index.d.ts +11 -25
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/insecure/index.d.ts.map +1 -1
- package/dist/src/insecure/index.js +5 -2
- package/dist/src/insecure/index.js.map +1 -1
- package/dist/src/insecure/pb/proto.d.ts +24 -130
- package/dist/src/insecure/pb/proto.d.ts.map +1 -0
- package/dist/src/insecure/pb/proto.js +46 -388
- package/dist/src/insecure/pb/proto.js.map +1 -0
- package/dist/src/keychain/index.d.ts +3 -3
- package/dist/src/keychain/index.js +2 -2
- package/dist/src/keychain/util.d.ts +1 -1
- package/dist/src/keychain/util.d.ts.map +1 -1
- package/dist/src/libp2p.d.ts +5 -4
- package/dist/src/libp2p.d.ts.map +1 -1
- package/dist/src/libp2p.js +69 -45
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/metrics/stats.d.ts +1 -5
- package/dist/src/metrics/stats.d.ts.map +1 -1
- package/dist/src/metrics/stats.js.map +1 -1
- package/dist/src/ping/index.d.ts.map +1 -1
- package/dist/src/ping/index.js +2 -1
- package/dist/src/ping/index.js.map +1 -1
- package/dist/src/pubsub/dummy-pubsub.d.ts +17 -0
- package/dist/src/pubsub/dummy-pubsub.d.ts.map +1 -0
- package/dist/src/pubsub/dummy-pubsub.js +37 -0
- package/dist/src/pubsub/dummy-pubsub.js.map +1 -0
- package/dist/src/registrar.js +1 -1
- package/dist/src/registrar.js.map +1 -1
- package/package.json +46 -44
- package/src/circuit/auto-relay.ts +8 -6
- package/src/circuit/circuit/hop.ts +9 -9
- package/src/circuit/circuit/stop.ts +3 -3
- package/src/circuit/circuit/stream-handler.ts +5 -6
- package/src/circuit/circuit/utils.ts +2 -2
- package/src/circuit/listener.ts +17 -4
- package/src/circuit/pb/index.proto +1 -1
- package/src/circuit/pb/index.ts +88 -0
- package/src/circuit/transport.ts +7 -4
- package/src/config.ts +5 -7
- package/src/connection-manager/auto-dialler.ts +4 -4
- package/src/{dialer → connection-manager/dialer}/auto-dialer.ts +23 -4
- package/src/{dialer → connection-manager/dialer}/dial-request.ts +4 -4
- package/src/{dialer → connection-manager/dialer}/index.ts +46 -39
- package/src/connection-manager/index.ts +143 -59
- package/src/dht/dummy-dht.ts +51 -0
- package/src/errors.ts +2 -0
- package/src/fetch/index.ts +7 -8
- package/src/fetch/pb/proto.ts +58 -0
- package/src/identify/index.ts +14 -14
- package/src/identify/pb/message.ts +36 -0
- package/src/index.ts +12 -30
- package/src/insecure/index.ts +8 -4
- package/src/insecure/pb/proto.ts +61 -0
- package/src/keychain/index.ts +2 -2
- package/src/libp2p.ts +81 -54
- package/src/metrics/stats.ts +1 -6
- package/src/ping/index.ts +2 -1
- package/src/pubsub/dummy-pubsub.ts +51 -0
- package/src/registrar.ts +1 -1
- package/dist/src/dialer/auto-dialer.d.ts.map +0 -1
- package/dist/src/dialer/auto-dialer.js +0 -24
- package/dist/src/dialer/auto-dialer.js.map +0 -1
- package/dist/src/dialer/dial-request.d.ts.map +0 -1
- package/dist/src/dialer/dial-request.js.map +0 -1
- package/dist/src/dialer/index.d.ts.map +0 -1
- package/dist/src/dialer/index.js.map +0 -1
- package/src/circuit/pb/index.d.ts +0 -173
- package/src/circuit/pb/index.js +0 -528
- package/src/fetch/pb/proto.d.ts +0 -134
- package/src/fetch/pb/proto.js +0 -331
- package/src/identify/pb/message.d.ts +0 -110
- package/src/identify/pb/message.js +0 -369
- package/src/insecure/pb/proto.d.ts +0 -134
- package/src/insecure/pb/proto.js +0 -388
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "libp2p",
|
|
3
|
-
"version": "0.36.2-
|
|
3
|
+
"version": "0.36.2-a15254f",
|
|
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#readme",
|
|
@@ -76,38 +76,38 @@
|
|
|
76
76
|
]
|
|
77
77
|
},
|
|
78
78
|
"scripts": {
|
|
79
|
+
"clean": "aegir clean",
|
|
79
80
|
"lint": "aegir lint",
|
|
80
|
-
"
|
|
81
|
-
"
|
|
82
|
-
"generate": "run-s generate:proto:*
|
|
83
|
-
"generate:proto:circuit": "
|
|
84
|
-
"generate:proto:fetch": "
|
|
85
|
-
"generate:proto:identify": "
|
|
86
|
-
"generate:proto:plaintext": "
|
|
87
|
-
"generate:proto-types:circuit": "pbts -o src/circuit/protocol/index.d.ts src/circuit/protocol/index.js",
|
|
88
|
-
"generate:proto-types:fetch": "pbts -o src/fetch/proto.d.ts src/fetch/proto.js",
|
|
89
|
-
"generate:proto-types:identify": "pbts -o src/identify/message.d.ts src/identify/message.js",
|
|
90
|
-
"generate:proto-types:plaintext": "pbts -o src/insecure/proto.d.ts src/insecure/proto.js",
|
|
91
|
-
"pretest": "npm run build",
|
|
81
|
+
"dep-check": "aegir dep-check",
|
|
82
|
+
"build": "aegir build",
|
|
83
|
+
"generate": "run-s generate:proto:*",
|
|
84
|
+
"generate:proto:circuit": "protons ./src/circuit/pb/index.proto",
|
|
85
|
+
"generate:proto:fetch": "protons ./src/fetch/pb/proto.proto",
|
|
86
|
+
"generate:proto:identify": "protons ./src/identify/pb/message.proto",
|
|
87
|
+
"generate:proto:plaintext": "protons ./src/insecure/pb/proto.proto",
|
|
92
88
|
"test": "aegir test",
|
|
93
|
-
"test:node": "
|
|
94
|
-
"test:chrome": "
|
|
95
|
-
"test:chrome-webworker": "
|
|
96
|
-
"test:firefox": "
|
|
97
|
-
"test:firefox-webworker": "
|
|
89
|
+
"test:node": "aegir test -t node -f \"./dist/test/**/*.{node,spec}.js\" --cov",
|
|
90
|
+
"test:chrome": "aegir test -t browser -f \"./dist/test/**/*.spec.js\" --cov",
|
|
91
|
+
"test:chrome-webworker": "aegir test -t webworker -f \"./dist/test/**/*.spec.js\"",
|
|
92
|
+
"test:firefox": "aegir test -t browser -f \"./dist/test/**/*.spec.js\" -- --browser firefox",
|
|
93
|
+
"test:firefox-webworker": "aegir test -t webworker -f \"./dist/test/**/*.spec.js\" -- --browser firefox",
|
|
98
94
|
"test:examples": "cd examples && npm run test:all",
|
|
99
|
-
"test:interop": "
|
|
95
|
+
"test:interop": "aegir test -t node -f dist/test/interop.js"
|
|
100
96
|
},
|
|
101
97
|
"dependencies": {
|
|
102
98
|
"@achingbrain/nat-port-mapper": "^1.0.0",
|
|
103
|
-
"@libp2p/connection": "^1.1.
|
|
104
|
-
"@libp2p/crypto": "^0.22.
|
|
105
|
-
"@libp2p/interfaces": "^1.3.
|
|
106
|
-
"@libp2p/
|
|
107
|
-
"@libp2p/
|
|
108
|
-
"@libp2p/peer-
|
|
109
|
-
"@libp2p/peer-
|
|
110
|
-
"@libp2p/
|
|
99
|
+
"@libp2p/connection": "^1.1.5",
|
|
100
|
+
"@libp2p/crypto": "^0.22.11",
|
|
101
|
+
"@libp2p/interfaces": "^1.3.30",
|
|
102
|
+
"@libp2p/logger": "^1.1.4",
|
|
103
|
+
"@libp2p/multistream-select": "^1.0.4",
|
|
104
|
+
"@libp2p/peer-collections": "^1.0.2",
|
|
105
|
+
"@libp2p/peer-id": "^1.1.10",
|
|
106
|
+
"@libp2p/peer-id-factory": "^1.0.9",
|
|
107
|
+
"@libp2p/peer-record": "^1.0.8",
|
|
108
|
+
"@libp2p/peer-store": "^1.0.10",
|
|
109
|
+
"@libp2p/tracked-map": "^1.0.5",
|
|
110
|
+
"@libp2p/utils": "^1.0.10",
|
|
111
111
|
"@multiformats/mafmt": "^11.0.2",
|
|
112
112
|
"@multiformats/multiaddr": "^10.1.8",
|
|
113
113
|
"abortable-iterator": "^4.0.2",
|
|
@@ -130,6 +130,7 @@
|
|
|
130
130
|
"it-length-prefixed": "^7.0.1",
|
|
131
131
|
"it-map": "^1.0.6",
|
|
132
132
|
"it-merge": "^1.0.3",
|
|
133
|
+
"it-pair": "^2.0.2",
|
|
133
134
|
"it-pipe": "^2.0.3",
|
|
134
135
|
"it-sort": "^1.0.1",
|
|
135
136
|
"it-stream-types": "^1.0.4",
|
|
@@ -144,7 +145,7 @@
|
|
|
144
145
|
"p-retry": "^5.0.0",
|
|
145
146
|
"p-settle": "^5.0.0",
|
|
146
147
|
"private-ip": "^2.3.3",
|
|
147
|
-
"
|
|
148
|
+
"protons-runtime": "^1.0.2",
|
|
148
149
|
"retimer": "^3.0.0",
|
|
149
150
|
"sanitize-filename": "^1.6.3",
|
|
150
151
|
"set-delayed-interval": "^1.0.0",
|
|
@@ -156,37 +157,37 @@
|
|
|
156
157
|
"xsalsa20": "^1.1.0"
|
|
157
158
|
},
|
|
158
159
|
"devDependencies": {
|
|
159
|
-
"@achingbrain/libp2p-gossipsub": "^0.13.5",
|
|
160
160
|
"@chainsafe/libp2p-noise": "^6.0.1",
|
|
161
|
-
"@libp2p/bootstrap": "^1.0.
|
|
162
|
-
"@libp2p/daemon-client": "^1.0.
|
|
163
|
-
"@libp2p/daemon-server": "^1.0.
|
|
161
|
+
"@libp2p/bootstrap": "^1.0.3",
|
|
162
|
+
"@libp2p/daemon-client": "^1.0.2",
|
|
163
|
+
"@libp2p/daemon-server": "^1.0.2",
|
|
164
164
|
"@libp2p/delegated-content-routing": "^1.0.2",
|
|
165
165
|
"@libp2p/delegated-peer-routing": "^1.0.2",
|
|
166
|
-
"@libp2p/floodsub": "^1.0.
|
|
167
|
-
"@libp2p/interface-compliance-tests": "^1.1.
|
|
168
|
-
"@libp2p/interop": "^1.0.
|
|
169
|
-
"@libp2p/kad-dht": "^1.0.
|
|
170
|
-
"@libp2p/mdns": "^1.0.
|
|
171
|
-
"@libp2p/mplex": "^1.0.
|
|
172
|
-
"@libp2p/
|
|
173
|
-
"@libp2p/
|
|
174
|
-
"@libp2p/
|
|
175
|
-
"@libp2p/
|
|
166
|
+
"@libp2p/floodsub": "^1.0.6",
|
|
167
|
+
"@libp2p/interface-compliance-tests": "^1.1.31",
|
|
168
|
+
"@libp2p/interop": "^1.0.3",
|
|
169
|
+
"@libp2p/kad-dht": "^1.0.8",
|
|
170
|
+
"@libp2p/mdns": "^1.0.4",
|
|
171
|
+
"@libp2p/mplex": "^1.0.3",
|
|
172
|
+
"@libp2p/pubsub": "^1.2.18",
|
|
173
|
+
"@libp2p/tcp": "^1.0.8",
|
|
174
|
+
"@libp2p/topology": "^1.1.7",
|
|
175
|
+
"@libp2p/webrtc-star": "^1.0.7",
|
|
176
|
+
"@libp2p/websockets": "^1.0.6",
|
|
176
177
|
"@nodeutils/defaults-deep": "^1.1.0",
|
|
177
178
|
"@types/node": "^16.11.26",
|
|
178
179
|
"@types/node-forge": "^1.0.0",
|
|
179
180
|
"@types/p-fifo": "^1.0.0",
|
|
180
181
|
"@types/varint": "^6.0.0",
|
|
181
182
|
"@types/xsalsa20": "^1.1.0",
|
|
182
|
-
"aegir": "^
|
|
183
|
+
"aegir": "^37.0.9",
|
|
183
184
|
"buffer": "^6.0.3",
|
|
184
185
|
"cborg": "^1.8.1",
|
|
185
186
|
"delay": "^5.0.0",
|
|
187
|
+
"execa": "^6.1.0",
|
|
186
188
|
"go-libp2p": "^0.0.6",
|
|
187
189
|
"into-stream": "^7.0.0",
|
|
188
190
|
"ipfs-http-client": "^56.0.1",
|
|
189
|
-
"it-pair": "^2.0.2",
|
|
190
191
|
"it-pushable": "^2.0.1",
|
|
191
192
|
"nock": "^13.0.3",
|
|
192
193
|
"npm-run-all": "^4.1.5",
|
|
@@ -194,6 +195,7 @@
|
|
|
194
195
|
"p-event": "^5.0.1",
|
|
195
196
|
"p-times": "^4.0.0",
|
|
196
197
|
"p-wait-for": "^4.1.0",
|
|
198
|
+
"protons": "^3.0.2",
|
|
197
199
|
"rimraf": "^3.0.2",
|
|
198
200
|
"sinon": "^13.0.1",
|
|
199
201
|
"ts-sinon": "^2.0.2"
|
|
@@ -85,12 +85,14 @@ 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
|
|
88
|
+
const connections = this.components.getConnectionManager().getConnections(peerId)
|
|
89
89
|
|
|
90
|
-
if (
|
|
90
|
+
if (connections.length === 0) {
|
|
91
91
|
return
|
|
92
92
|
}
|
|
93
93
|
|
|
94
|
+
const connection = connections[0]
|
|
95
|
+
|
|
94
96
|
// Do not hop on a relayed connection
|
|
95
97
|
if (connection.remoteAddr.protoCodes().includes(CIRCUIT_PROTO_CODE)) {
|
|
96
98
|
log(`relayed connection to ${id} will not be used to hop on`)
|
|
@@ -223,15 +225,15 @@ export class AutoRelay {
|
|
|
223
225
|
continue
|
|
224
226
|
}
|
|
225
227
|
|
|
226
|
-
const
|
|
228
|
+
const connections = this.components.getConnectionManager().getConnections(id)
|
|
227
229
|
|
|
228
230
|
// If not connected, store for possible later use.
|
|
229
|
-
if (
|
|
231
|
+
if (connections.length === 0) {
|
|
230
232
|
knownHopsToDial.push(id)
|
|
231
233
|
continue
|
|
232
234
|
}
|
|
233
235
|
|
|
234
|
-
await this._addListenRelay(
|
|
236
|
+
await this._addListenRelay(connections[0], idStr)
|
|
235
237
|
|
|
236
238
|
// Check if already listening on enough relays
|
|
237
239
|
if (this.listenRelays.size >= this.maxListeners) {
|
|
@@ -274,7 +276,7 @@ export class AutoRelay {
|
|
|
274
276
|
|
|
275
277
|
async _tryToListenOnRelay (peerId: PeerId) {
|
|
276
278
|
try {
|
|
277
|
-
const connection = await this.components.
|
|
279
|
+
const connection = await this.components.getConnectionManager().openConnection(peerId)
|
|
278
280
|
await this._addListenRelay(connection, peerId.toString())
|
|
279
281
|
} catch (err: any) {
|
|
280
282
|
log.error('Could not use %p as relay', peerId, err)
|
|
@@ -2,7 +2,7 @@ import { logger } from '@libp2p/logger'
|
|
|
2
2
|
import errCode from 'err-code'
|
|
3
3
|
import { validateAddrs } from './utils.js'
|
|
4
4
|
import { StreamHandler } from './stream-handler.js'
|
|
5
|
-
import { CircuitRelay as CircuitPB
|
|
5
|
+
import { CircuitRelay as CircuitPB } from '../pb/index.js'
|
|
6
6
|
import { pipe } from 'it-pipe'
|
|
7
7
|
import { codes as Errors } from '../../errors.js'
|
|
8
8
|
import { stop } from './stop.js'
|
|
@@ -11,13 +11,13 @@ import type { Connection } from '@libp2p/interfaces/connection'
|
|
|
11
11
|
import { peerIdFromBytes } from '@libp2p/peer-id'
|
|
12
12
|
import type { Duplex } from 'it-stream-types'
|
|
13
13
|
import type { Circuit } from '../transport.js'
|
|
14
|
-
import type { ConnectionManager } from '@libp2p/interfaces/
|
|
14
|
+
import type { ConnectionManager } from '@libp2p/interfaces/connection-manager'
|
|
15
15
|
|
|
16
16
|
const log = logger('libp2p:circuit:hop')
|
|
17
17
|
|
|
18
18
|
export interface HopRequest {
|
|
19
19
|
connection: Connection
|
|
20
|
-
request:
|
|
20
|
+
request: CircuitPB
|
|
21
21
|
streamHandler: StreamHandler
|
|
22
22
|
circuit: Circuit
|
|
23
23
|
connectionManager: ConnectionManager
|
|
@@ -58,8 +58,8 @@ export async function handleHop (hopRequest: HopRequest) {
|
|
|
58
58
|
// Get the connection to the destination (stop) peer
|
|
59
59
|
const destinationPeer = peerIdFromBytes(request.dstPeer.id)
|
|
60
60
|
|
|
61
|
-
const
|
|
62
|
-
if (
|
|
61
|
+
const destinationConnections = connectionManager.getConnections(destinationPeer)
|
|
62
|
+
if (destinationConnections.length === 0 && !circuit.hopActive()) {
|
|
63
63
|
log('HOP request received but we are not connected to the destination peer')
|
|
64
64
|
return streamHandler.end({
|
|
65
65
|
type: CircuitPB.Type.STATUS,
|
|
@@ -68,7 +68,7 @@ export async function handleHop (hopRequest: HopRequest) {
|
|
|
68
68
|
}
|
|
69
69
|
|
|
70
70
|
// TODO: Handle being an active relay
|
|
71
|
-
if (
|
|
71
|
+
if (destinationConnections.length === 0) {
|
|
72
72
|
log('did not have connection to remote peer')
|
|
73
73
|
return streamHandler.end({
|
|
74
74
|
type: CircuitPB.Type.STATUS,
|
|
@@ -87,7 +87,7 @@ export async function handleHop (hopRequest: HopRequest) {
|
|
|
87
87
|
try {
|
|
88
88
|
log('performing STOP request')
|
|
89
89
|
const result = await stop({
|
|
90
|
-
connection:
|
|
90
|
+
connection: destinationConnections[0],
|
|
91
91
|
request: stopRequest
|
|
92
92
|
})
|
|
93
93
|
|
|
@@ -120,7 +120,7 @@ export async function handleHop (hopRequest: HopRequest) {
|
|
|
120
120
|
|
|
121
121
|
export interface HopConfig {
|
|
122
122
|
connection: Connection
|
|
123
|
-
request:
|
|
123
|
+
request: CircuitPB
|
|
124
124
|
}
|
|
125
125
|
|
|
126
126
|
/**
|
|
@@ -153,7 +153,7 @@ export async function hop (options: HopConfig): Promise<Duplex<Uint8Array>> {
|
|
|
153
153
|
log('hop request failed with code %d, closing stream', response.code)
|
|
154
154
|
streamHandler.close()
|
|
155
155
|
|
|
156
|
-
throw errCode(new Error(`HOP request failed with code ${response.code}`), Errors.ERR_HOP_REQUEST_FAILED)
|
|
156
|
+
throw errCode(new Error(`HOP request failed with code "${response.code ?? 'unknown'}"`), Errors.ERR_HOP_REQUEST_FAILED)
|
|
157
157
|
}
|
|
158
158
|
|
|
159
159
|
export interface CanHopOptions {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { logger } from '@libp2p/logger'
|
|
2
|
-
import { CircuitRelay as CircuitPB
|
|
2
|
+
import { CircuitRelay as CircuitPB } from '../pb/index.js'
|
|
3
3
|
import { RELAY_CODEC } from '../multicodec.js'
|
|
4
4
|
import { StreamHandler } from './stream-handler.js'
|
|
5
5
|
import { validateAddrs } from './utils.js'
|
|
@@ -10,7 +10,7 @@ const log = logger('libp2p:circuit:stop')
|
|
|
10
10
|
|
|
11
11
|
export interface HandleStopOptions {
|
|
12
12
|
connection: Connection
|
|
13
|
-
request:
|
|
13
|
+
request: CircuitPB
|
|
14
14
|
streamHandler: StreamHandler
|
|
15
15
|
}
|
|
16
16
|
|
|
@@ -44,7 +44,7 @@ export function handleStop (options: HandleStopOptions): Duplex<Uint8Array> | un
|
|
|
44
44
|
|
|
45
45
|
export interface StopOptions {
|
|
46
46
|
connection: Connection
|
|
47
|
-
request:
|
|
47
|
+
request: CircuitPB
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
/**
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { logger } from '@libp2p/logger'
|
|
2
2
|
import * as lp from 'it-length-prefixed'
|
|
3
3
|
import { Handshake, handshake } from 'it-handshake'
|
|
4
|
-
import { CircuitRelay
|
|
4
|
+
import { CircuitRelay } from '../pb/index.js'
|
|
5
5
|
import type { Stream } from '@libp2p/interfaces/connection'
|
|
6
6
|
import type { Source } from 'it-stream-types'
|
|
7
7
|
|
|
@@ -53,10 +53,9 @@ export class StreamHandler {
|
|
|
53
53
|
/**
|
|
54
54
|
* Encode and write array of buffers
|
|
55
55
|
*/
|
|
56
|
-
write (msg:
|
|
56
|
+
write (msg: CircuitRelay) {
|
|
57
57
|
log('write message type %s', msg.type)
|
|
58
|
-
|
|
59
|
-
this.shake.write(lp.encode.single(CircuitRelay.encode(msg).finish()))
|
|
58
|
+
this.shake.write(lp.encode.single(CircuitRelay.encode(msg)).slice())
|
|
60
59
|
}
|
|
61
60
|
|
|
62
61
|
/**
|
|
@@ -68,9 +67,9 @@ export class StreamHandler {
|
|
|
68
67
|
}
|
|
69
68
|
|
|
70
69
|
/**
|
|
71
|
-
* @param {
|
|
70
|
+
* @param {CircuitRelay} msg - An unencoded CircuitRelay protobuf message
|
|
72
71
|
*/
|
|
73
|
-
end (msg:
|
|
72
|
+
end (msg: CircuitRelay) {
|
|
74
73
|
this.write(msg)
|
|
75
74
|
this.close()
|
|
76
75
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Multiaddr } from '@multiformats/multiaddr'
|
|
2
|
-
import { CircuitRelay
|
|
2
|
+
import { CircuitRelay } from '../pb/index.js'
|
|
3
3
|
import type { StreamHandler } from './stream-handler.js'
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -15,7 +15,7 @@ function writeResponse (streamHandler: StreamHandler, status: CircuitRelay.Statu
|
|
|
15
15
|
/**
|
|
16
16
|
* Validate incomming HOP/STOP message
|
|
17
17
|
*/
|
|
18
|
-
export function validateAddrs (msg:
|
|
18
|
+
export function validateAddrs (msg: CircuitRelay, streamHandler: StreamHandler) {
|
|
19
19
|
try {
|
|
20
20
|
if (msg.dstPeer?.addrs != null) {
|
|
21
21
|
msg.dstPeer.addrs.forEach((addr) => {
|
package/src/circuit/listener.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { CustomEvent, EventEmitter } from '@libp2p/interfaces'
|
|
2
|
-
import type { ConnectionManager } from '@libp2p/interfaces/
|
|
3
|
-
import type {
|
|
2
|
+
import type { ConnectionManager } from '@libp2p/interfaces/connection-manager'
|
|
3
|
+
import type { PeerStore } from '@libp2p/interfaces/peer-store'
|
|
4
4
|
import type { Listener } from '@libp2p/interfaces/transport'
|
|
5
|
+
import { peerIdFromString } from '@libp2p/peer-id'
|
|
5
6
|
import { Multiaddr } from '@multiformats/multiaddr'
|
|
6
7
|
|
|
7
8
|
export interface ListenerOptions {
|
|
8
|
-
|
|
9
|
+
peerStore: PeerStore
|
|
9
10
|
connectionManager: ConnectionManager
|
|
10
11
|
}
|
|
11
12
|
|
|
@@ -17,7 +18,19 @@ export function createListener (options: ListenerOptions): Listener {
|
|
|
17
18
|
*/
|
|
18
19
|
async function listen (addr: Multiaddr): Promise<void> {
|
|
19
20
|
const addrString = addr.toString().split('/p2p-circuit').find(a => a !== '')
|
|
20
|
-
const
|
|
21
|
+
const ma = new Multiaddr(addrString)
|
|
22
|
+
|
|
23
|
+
const relayPeerStr = ma.getPeerId()
|
|
24
|
+
|
|
25
|
+
if (relayPeerStr == null) {
|
|
26
|
+
throw new Error('Could not determine relay peer from multiaddr')
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const relayPeerId = peerIdFromString(relayPeerStr)
|
|
30
|
+
|
|
31
|
+
await options.peerStore.addressBook.add(relayPeerId, [ma])
|
|
32
|
+
|
|
33
|
+
const relayConn = await options.connectionManager.openConnection(relayPeerId)
|
|
21
34
|
const relayedAddr = relayConn.remoteAddr.encapsulate('/p2p-circuit')
|
|
22
35
|
|
|
23
36
|
listeningAddrs.set(relayConn.remotePeer.toString(), relayedAddr)
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/* eslint-disable import/export */
|
|
2
|
+
/* eslint-disable @typescript-eslint/no-namespace */
|
|
3
|
+
|
|
4
|
+
import { enumeration, encodeMessage, decodeMessage, message, bytes } from 'protons-runtime'
|
|
5
|
+
|
|
6
|
+
export interface CircuitRelay {
|
|
7
|
+
type?: CircuitRelay.Type
|
|
8
|
+
srcPeer?: CircuitRelay.Peer
|
|
9
|
+
dstPeer?: CircuitRelay.Peer
|
|
10
|
+
code?: CircuitRelay.Status
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export namespace CircuitRelay {
|
|
14
|
+
export enum Status {
|
|
15
|
+
SUCCESS = 'SUCCESS',
|
|
16
|
+
HOP_SRC_ADDR_TOO_LONG = 'HOP_SRC_ADDR_TOO_LONG',
|
|
17
|
+
HOP_DST_ADDR_TOO_LONG = 'HOP_DST_ADDR_TOO_LONG',
|
|
18
|
+
HOP_SRC_MULTIADDR_INVALID = 'HOP_SRC_MULTIADDR_INVALID',
|
|
19
|
+
HOP_DST_MULTIADDR_INVALID = 'HOP_DST_MULTIADDR_INVALID',
|
|
20
|
+
HOP_NO_CONN_TO_DST = 'HOP_NO_CONN_TO_DST',
|
|
21
|
+
HOP_CANT_DIAL_DST = 'HOP_CANT_DIAL_DST',
|
|
22
|
+
HOP_CANT_OPEN_DST_STREAM = 'HOP_CANT_OPEN_DST_STREAM',
|
|
23
|
+
HOP_CANT_SPEAK_RELAY = 'HOP_CANT_SPEAK_RELAY',
|
|
24
|
+
HOP_CANT_RELAY_TO_SELF = 'HOP_CANT_RELAY_TO_SELF',
|
|
25
|
+
STOP_SRC_ADDR_TOO_LONG = 'STOP_SRC_ADDR_TOO_LONG',
|
|
26
|
+
STOP_DST_ADDR_TOO_LONG = 'STOP_DST_ADDR_TOO_LONG',
|
|
27
|
+
STOP_SRC_MULTIADDR_INVALID = 'STOP_SRC_MULTIADDR_INVALID',
|
|
28
|
+
STOP_DST_MULTIADDR_INVALID = 'STOP_DST_MULTIADDR_INVALID',
|
|
29
|
+
STOP_RELAY_REFUSED = 'STOP_RELAY_REFUSED',
|
|
30
|
+
MALFORMED_MESSAGE = 'MALFORMED_MESSAGE'
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export namespace Status {
|
|
34
|
+
export const codec = () => {
|
|
35
|
+
return enumeration<typeof Status>(Status)
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
export enum Type {
|
|
39
|
+
HOP = 'HOP',
|
|
40
|
+
STOP = 'STOP',
|
|
41
|
+
STATUS = 'STATUS',
|
|
42
|
+
CAN_HOP = 'CAN_HOP'
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export namespace Type {
|
|
46
|
+
export const codec = () => {
|
|
47
|
+
return enumeration<typeof Type>(Type)
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
export interface Peer {
|
|
51
|
+
id: Uint8Array
|
|
52
|
+
addrs: Uint8Array[]
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export namespace Peer {
|
|
56
|
+
export const codec = () => {
|
|
57
|
+
return message<Peer>({
|
|
58
|
+
1: { name: 'id', codec: bytes },
|
|
59
|
+
2: { name: 'addrs', codec: bytes, repeats: true }
|
|
60
|
+
})
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export const encode = (obj: Peer): Uint8Array => {
|
|
64
|
+
return encodeMessage(obj, Peer.codec())
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export const decode = (buf: Uint8Array): Peer => {
|
|
68
|
+
return decodeMessage(buf, Peer.codec())
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export const codec = () => {
|
|
73
|
+
return message<CircuitRelay>({
|
|
74
|
+
1: { name: 'type', codec: CircuitRelay.Type.codec(), optional: true },
|
|
75
|
+
2: { name: 'srcPeer', codec: CircuitRelay.Peer.codec(), optional: true },
|
|
76
|
+
3: { name: 'dstPeer', codec: CircuitRelay.Peer.codec(), optional: true },
|
|
77
|
+
4: { name: 'code', codec: CircuitRelay.Status.codec(), optional: true }
|
|
78
|
+
})
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export const encode = (obj: CircuitRelay): Uint8Array => {
|
|
82
|
+
return encodeMessage(obj, CircuitRelay.codec())
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export const decode = (buf: Uint8Array): CircuitRelay => {
|
|
86
|
+
return decodeMessage(buf, CircuitRelay.codec())
|
|
87
|
+
}
|
|
88
|
+
}
|
package/src/circuit/transport.ts
CHANGED
|
@@ -149,9 +149,12 @@ export class Circuit implements Transport, Initializable {
|
|
|
149
149
|
const destinationPeer = peerIdFromString(destinationId)
|
|
150
150
|
|
|
151
151
|
let disconnectOnFailure = false
|
|
152
|
-
|
|
152
|
+
const relayConnections = this.components.getConnectionManager().getConnections(relayPeer)
|
|
153
|
+
let relayConnection = relayConnections[0]
|
|
154
|
+
|
|
153
155
|
if (relayConnection == null) {
|
|
154
|
-
|
|
156
|
+
await this.components.getPeerStore().addressBook.add(relayPeer, [relayAddr])
|
|
157
|
+
relayConnection = await this.components.getConnectionManager().openConnection(relayPeer, options)
|
|
155
158
|
disconnectOnFailure = true
|
|
156
159
|
}
|
|
157
160
|
|
|
@@ -195,8 +198,8 @@ export class Circuit implements Transport, Initializable {
|
|
|
195
198
|
this.handler = options.handler
|
|
196
199
|
|
|
197
200
|
return createListener({
|
|
198
|
-
|
|
199
|
-
|
|
201
|
+
connectionManager: this.components.getConnectionManager(),
|
|
202
|
+
peerStore: this.components.getPeerStore()
|
|
200
203
|
})
|
|
201
204
|
}
|
|
202
205
|
|
package/src/config.ts
CHANGED
|
@@ -21,14 +21,8 @@ const DefaultConfig: Partial<Libp2pInit> = {
|
|
|
21
21
|
connectionManager: {
|
|
22
22
|
maxConnections: 300,
|
|
23
23
|
minConnections: 50,
|
|
24
|
+
autoDial: true,
|
|
24
25
|
autoDialInterval: 10000,
|
|
25
|
-
autoDial: true
|
|
26
|
-
},
|
|
27
|
-
connectionGater: {},
|
|
28
|
-
transportManager: {
|
|
29
|
-
faultTolerance: FaultTolerance.FATAL_ALL
|
|
30
|
-
},
|
|
31
|
-
dialer: {
|
|
32
26
|
maxParallelDials: Constants.MAX_PARALLEL_DIALS,
|
|
33
27
|
maxDialsPerPeer: Constants.MAX_PER_PEER_DIALS,
|
|
34
28
|
dialTimeout: Constants.DIAL_TIMEOUT,
|
|
@@ -37,6 +31,10 @@ const DefaultConfig: Partial<Libp2pInit> = {
|
|
|
37
31
|
},
|
|
38
32
|
addressSorter: publicAddressesFirst
|
|
39
33
|
},
|
|
34
|
+
connectionGater: {},
|
|
35
|
+
transportManager: {
|
|
36
|
+
faultTolerance: FaultTolerance.FATAL_ALL
|
|
37
|
+
},
|
|
40
38
|
host: {
|
|
41
39
|
agentVersion: AGENT_VERSION
|
|
42
40
|
},
|
|
@@ -102,7 +102,7 @@ export class AutoDialler implements Startable {
|
|
|
102
102
|
const minConnections = this.options.minConnections
|
|
103
103
|
|
|
104
104
|
// Already has enough connections
|
|
105
|
-
if (this.components.getConnectionManager().
|
|
105
|
+
if (this.components.getConnectionManager().getConnections().length >= minConnections) {
|
|
106
106
|
this.autoDialTimeout = retimer(this._autoDial, this.options.autoDialInterval)
|
|
107
107
|
|
|
108
108
|
return
|
|
@@ -126,7 +126,7 @@ export class AutoDialler implements Startable {
|
|
|
126
126
|
async (source) => await all(source)
|
|
127
127
|
)
|
|
128
128
|
|
|
129
|
-
for (let i = 0; this.running && i < peers.length && this.components.getConnectionManager().
|
|
129
|
+
for (let i = 0; this.running && i < peers.length && this.components.getConnectionManager().getConnections().length < minConnections; i++) {
|
|
130
130
|
// Connection Manager was stopped during async dial
|
|
131
131
|
if (!this.running) {
|
|
132
132
|
return
|
|
@@ -134,10 +134,10 @@ export class AutoDialler implements Startable {
|
|
|
134
134
|
|
|
135
135
|
const peer = peers[i]
|
|
136
136
|
|
|
137
|
-
if (this.components.getConnectionManager().
|
|
137
|
+
if (this.components.getConnectionManager().getConnections(peer.id).length === 0) {
|
|
138
138
|
log('connecting to a peerStore stored peer %p', peer.id)
|
|
139
139
|
try {
|
|
140
|
-
await this.components.
|
|
140
|
+
await this.components.getConnectionManager().openConnection(peer.id)
|
|
141
141
|
} catch (err: any) {
|
|
142
142
|
log.error('could not connect to peerStore stored peer', err)
|
|
143
143
|
}
|
|
@@ -1,39 +1,58 @@
|
|
|
1
1
|
import type { PeerInfo } from '@libp2p/interfaces/peer-info'
|
|
2
2
|
import { logger } from '@libp2p/logger'
|
|
3
3
|
import type { Components } from '@libp2p/interfaces/components'
|
|
4
|
+
import { TimeoutController } from 'timeout-abort-controller'
|
|
4
5
|
|
|
5
6
|
const log = logger('libp2p:dialer:auto-dialer')
|
|
6
7
|
|
|
7
8
|
export interface AutoDialerInit {
|
|
8
9
|
enabled: boolean
|
|
9
10
|
minConnections: number
|
|
11
|
+
dialTimeout: number
|
|
10
12
|
}
|
|
11
13
|
|
|
12
14
|
export class AutoDialer {
|
|
13
15
|
private readonly components: Components
|
|
14
16
|
private readonly enabled: boolean
|
|
15
17
|
private readonly minConnections: number
|
|
18
|
+
private readonly dialTimeout: number
|
|
16
19
|
|
|
17
20
|
constructor (components: Components, init: AutoDialerInit) {
|
|
18
21
|
this.components = components
|
|
19
22
|
this.enabled = init.enabled
|
|
20
23
|
this.minConnections = init.minConnections
|
|
24
|
+
this.dialTimeout = init.dialTimeout
|
|
21
25
|
}
|
|
22
26
|
|
|
23
27
|
public handle (evt: CustomEvent<PeerInfo>) {
|
|
24
28
|
const { detail: peer } = evt
|
|
25
29
|
|
|
30
|
+
if (!this.enabled) {
|
|
31
|
+
return
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const connections = this.components.getConnectionManager().getConnections(peer.id)
|
|
35
|
+
|
|
26
36
|
// If auto dialing is on and we have no connection to the peer, check if we should dial
|
|
27
|
-
if (
|
|
37
|
+
if (connections.length === 0) {
|
|
28
38
|
const minConnections = this.minConnections ?? 0
|
|
29
39
|
|
|
30
|
-
|
|
31
|
-
log('auto-dialing discovered peer %p', peer.id)
|
|
40
|
+
const allConnections = this.components.getConnectionManager().getConnections()
|
|
32
41
|
|
|
33
|
-
|
|
42
|
+
if (minConnections > allConnections.length) {
|
|
43
|
+
log('auto-dialing discovered peer %p with timeout %d', peer.id, this.dialTimeout)
|
|
44
|
+
|
|
45
|
+
const controller = new TimeoutController(this.dialTimeout)
|
|
46
|
+
|
|
47
|
+
void this.components.getConnectionManager().openConnection(peer.id, {
|
|
48
|
+
signal: controller.signal
|
|
49
|
+
})
|
|
34
50
|
.catch(err => {
|
|
35
51
|
log.error('could not connect to discovered peer %p with %o', peer.id, err)
|
|
36
52
|
})
|
|
53
|
+
.finally(() => {
|
|
54
|
+
controller.clear()
|
|
55
|
+
})
|
|
37
56
|
}
|
|
38
57
|
}
|
|
39
58
|
}
|
|
@@ -3,12 +3,12 @@ import { anySignal } from 'any-signal'
|
|
|
3
3
|
import FIFO from 'p-fifo'
|
|
4
4
|
// @ts-expect-error setMaxListeners is missing from the node 16 types
|
|
5
5
|
import { setMaxListeners } from 'events'
|
|
6
|
-
import { codes } from '
|
|
6
|
+
import { codes } from '../../errors.js'
|
|
7
7
|
import { logger } from '@libp2p/logger'
|
|
8
8
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
9
9
|
import type { Connection } from '@libp2p/interfaces/connection'
|
|
10
10
|
import type { AbortOptions } from '@libp2p/interfaces'
|
|
11
|
-
import type {
|
|
11
|
+
import type { Dialer } from './index.js'
|
|
12
12
|
|
|
13
13
|
const log = logger('libp2p:dialer:dial-request')
|
|
14
14
|
|
|
@@ -19,12 +19,12 @@ export interface DialAction {
|
|
|
19
19
|
export interface DialRequestOptions {
|
|
20
20
|
addrs: Multiaddr[]
|
|
21
21
|
dialAction: DialAction
|
|
22
|
-
dialer:
|
|
22
|
+
dialer: Dialer
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
export class DialRequest {
|
|
26
26
|
private readonly addrs: Multiaddr[]
|
|
27
|
-
private readonly dialer:
|
|
27
|
+
private readonly dialer: Dialer
|
|
28
28
|
private readonly dialAction: DialAction
|
|
29
29
|
|
|
30
30
|
/**
|