libp2p 0.42.2-45fc415b → 0.42.2-593deefe
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.min.js +29 -29
- package/dist/src/circuit/client.d.ts +81 -0
- package/dist/src/circuit/client.d.ts.map +1 -0
- package/dist/src/circuit/client.js +285 -0
- package/dist/src/circuit/client.js.map +1 -0
- package/dist/src/circuit/constants.d.ts +7 -8
- package/dist/src/circuit/constants.d.ts.map +1 -1
- package/dist/src/circuit/constants.js +11 -8
- package/dist/src/circuit/constants.js.map +1 -1
- package/dist/src/circuit/hop.d.ts +23 -0
- package/dist/src/circuit/hop.d.ts.map +1 -0
- package/dist/src/circuit/hop.js +167 -0
- package/dist/src/circuit/hop.js.map +1 -0
- package/dist/src/circuit/index.d.ts +47 -47
- package/dist/src/circuit/index.d.ts.map +1 -1
- package/dist/src/circuit/index.js +1 -68
- package/dist/src/circuit/index.js.map +1 -1
- package/dist/src/circuit/interfaces.d.ts +27 -0
- package/dist/src/circuit/interfaces.d.ts.map +1 -0
- package/dist/src/circuit/interfaces.js +2 -0
- package/dist/src/circuit/interfaces.js.map +1 -0
- package/dist/src/circuit/multicodec.d.ts +2 -1
- package/dist/src/circuit/multicodec.d.ts.map +1 -1
- package/dist/src/circuit/multicodec.js +2 -1
- package/dist/src/circuit/multicodec.js.map +1 -1
- package/dist/src/circuit/pb/index.d.ts +83 -43
- package/dist/src/circuit/pb/index.d.ts.map +1 -1
- package/dist/src/circuit/pb/index.js +363 -127
- package/dist/src/circuit/pb/index.js.map +1 -1
- package/dist/src/circuit/relay.d.ts +34 -0
- package/dist/src/circuit/relay.d.ts.map +1 -0
- package/dist/src/circuit/relay.js +61 -0
- package/dist/src/circuit/relay.js.map +1 -0
- package/dist/src/circuit/reservation-store.d.ts +42 -0
- package/dist/src/circuit/reservation-store.d.ts.map +1 -0
- package/dist/src/circuit/reservation-store.js +59 -0
- package/dist/src/circuit/reservation-store.js.map +1 -0
- package/dist/src/circuit/reservation-voucher.d.ts +18 -0
- package/dist/src/circuit/reservation-voucher.d.ts.map +1 -0
- package/dist/src/circuit/reservation-voucher.js +33 -0
- package/dist/src/circuit/reservation-voucher.js.map +1 -0
- package/dist/src/circuit/stop.d.ts +19 -0
- package/dist/src/circuit/stop.d.ts.map +1 -0
- package/dist/src/circuit/stop.js +67 -0
- package/dist/src/circuit/stop.js.map +1 -0
- package/dist/src/circuit/transport.d.ts +24 -9
- package/dist/src/circuit/transport.d.ts.map +1 -1
- package/dist/src/circuit/transport.js +133 -118
- package/dist/src/circuit/transport.js.map +1 -1
- package/dist/src/circuit/utils.d.ts +5 -0
- package/dist/src/circuit/utils.d.ts.map +1 -1
- package/dist/src/circuit/utils.js +114 -0
- package/dist/src/circuit/utils.js.map +1 -1
- package/dist/src/config.d.ts.map +1 -1
- package/dist/src/config.js +2 -3
- package/dist/src/config.js.map +1 -1
- package/dist/src/connection-manager/dialer/dial-request.d.ts.map +1 -1
- package/dist/src/connection-manager/dialer/dial-request.js +7 -0
- package/dist/src/connection-manager/dialer/dial-request.js.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/pb/proto.d.ts +3 -3
- package/dist/src/fetch/pb/proto.d.ts.map +1 -1
- package/dist/src/fetch/pb/proto.js +4 -3
- package/dist/src/fetch/pb/proto.js.map +1 -1
- package/dist/src/identify/pb/message.d.ts +2 -2
- package/dist/src/identify/pb/message.d.ts.map +1 -1
- package/dist/src/identify/pb/message.js +1 -0
- package/dist/src/identify/pb/message.js.map +1 -1
- package/dist/src/insecure/pb/proto.d.ts +3 -3
- package/dist/src/insecure/pb/proto.d.ts.map +1 -1
- package/dist/src/insecure/pb/proto.js +4 -5
- package/dist/src/insecure/pb/proto.js.map +1 -1
- package/dist/src/libp2p.d.ts +2 -0
- package/dist/src/libp2p.d.ts.map +1 -1
- package/dist/src/libp2p.js +10 -3
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/transport-manager.d.ts.map +1 -1
- package/dist/src/transport-manager.js +2 -3
- package/dist/src/transport-manager.js.map +1 -1
- package/dist/src/upgrader.d.ts.map +1 -1
- package/dist/src/upgrader.js.map +1 -1
- package/package.json +12 -11
- package/src/circuit/client.ts +381 -0
- package/src/circuit/constants.ts +14 -8
- package/src/circuit/hop.ts +210 -0
- package/src/circuit/index.ts +49 -111
- package/src/circuit/interfaces.ts +28 -0
- package/src/circuit/multicodec.ts +2 -1
- package/src/circuit/pb/index.proto +57 -32
- package/src/circuit/pb/index.ts +448 -138
- package/src/circuit/relay.ts +87 -0
- package/src/circuit/reservation-store.ts +106 -0
- package/src/circuit/reservation-voucher.ts +51 -0
- package/src/circuit/stop.ts +92 -0
- package/src/circuit/transport.ts +166 -131
- package/src/circuit/utils.ts +145 -0
- package/src/config.ts +2 -3
- package/src/connection-manager/dialer/dial-request.ts +7 -0
- package/src/errors.ts +2 -1
- package/src/fetch/pb/proto.ts +8 -7
- package/src/identify/pb/message.ts +3 -2
- package/src/insecure/pb/proto.ts +8 -9
- package/src/libp2p.ts +12 -3
- package/src/transport-manager.ts +2 -3
- package/src/upgrader.ts +4 -4
- package/src/version.ts +1 -1
- package/dist/src/circuit/auto-relay.d.ts +0 -46
- package/dist/src/circuit/auto-relay.d.ts.map +0 -1
- package/dist/src/circuit/auto-relay.js +0 -220
- package/dist/src/circuit/auto-relay.js.map +0 -1
- package/dist/src/circuit/circuit/hop.d.ts +0 -42
- package/dist/src/circuit/circuit/hop.d.ts.map +0 -1
- package/dist/src/circuit/circuit/hop.js +0 -142
- package/dist/src/circuit/circuit/hop.js.map +0 -1
- package/dist/src/circuit/circuit/stop.d.ts +0 -24
- package/dist/src/circuit/circuit/stop.d.ts.map +0 -1
- package/dist/src/circuit/circuit/stop.js +0 -51
- package/dist/src/circuit/circuit/stop.js.map +0 -1
- package/dist/src/circuit/circuit/stream-handler.d.ts +0 -40
- package/dist/src/circuit/circuit/stream-handler.d.ts.map +0 -1
- package/dist/src/circuit/circuit/stream-handler.js +0 -59
- package/dist/src/circuit/circuit/stream-handler.js.map +0 -1
- package/dist/src/circuit/circuit/utils.d.ts +0 -7
- package/dist/src/circuit/circuit/utils.d.ts.map +0 -1
- package/dist/src/circuit/circuit/utils.js +0 -43
- package/dist/src/circuit/circuit/utils.js.map +0 -1
- package/src/circuit/IMPLEMENTATION_NOTES.md +0 -128
- package/src/circuit/README.md +0 -111
- package/src/circuit/auto-relay.ts +0 -292
- package/src/circuit/circuit/hop.ts +0 -220
- package/src/circuit/circuit/stop.ts +0 -83
- package/src/circuit/circuit/stream-handler.ts +0 -87
- package/src/circuit/circuit/utils.ts +0 -44
package/src/circuit/transport.ts
CHANGED
|
@@ -1,36 +1,37 @@
|
|
|
1
|
+
import * as CircuitV2 from './pb/index.js'
|
|
2
|
+
import { ReservationStore } from './reservation-store.js'
|
|
1
3
|
import { logger } from '@libp2p/logger'
|
|
2
|
-
import
|
|
4
|
+
import createError from 'err-code'
|
|
3
5
|
import * as mafmt from '@multiformats/mafmt'
|
|
4
|
-
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
5
6
|
import { multiaddr } from '@multiformats/multiaddr'
|
|
6
|
-
import { CircuitRelay as CircuitPB } from './pb/index.js'
|
|
7
7
|
import { codes } from '../errors.js'
|
|
8
8
|
import { streamToMaConnection } from '@libp2p/utils/stream-to-ma-conn'
|
|
9
|
-
import {
|
|
9
|
+
import { RELAY_V2_HOP_CODEC, RELAY_V2_STOP_CODEC } from './multicodec.js'
|
|
10
10
|
import { createListener } from './listener.js'
|
|
11
|
-
import {
|
|
12
|
-
import { handleStop } from './circuit/stop.js'
|
|
13
|
-
import { StreamHandler } from './circuit/stream-handler.js'
|
|
14
|
-
import { symbol, Upgrader } from '@libp2p/interface-transport'
|
|
11
|
+
import { symbol, TransportManager, Upgrader } from '@libp2p/interface-transport'
|
|
15
12
|
import { peerIdFromString } from '@libp2p/peer-id'
|
|
16
13
|
import type { AbortOptions } from '@libp2p/interfaces'
|
|
17
14
|
import type { IncomingStreamData, Registrar } from '@libp2p/interface-registrar'
|
|
18
15
|
import type { Listener, Transport, CreateListenerOptions, ConnectionHandler } from '@libp2p/interface-transport'
|
|
19
|
-
import type { Connection } from '@libp2p/interface-connection'
|
|
16
|
+
import type { Connection, Stream } from '@libp2p/interface-connection'
|
|
20
17
|
import type { RelayConfig } from './index.js'
|
|
21
|
-
import { abortableDuplex } from 'abortable-iterator'
|
|
22
|
-
import { TimeoutController } from 'timeout-abort-controller'
|
|
23
|
-
import { setMaxListeners } from 'events'
|
|
24
|
-
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
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
18
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
19
|
+
import { handleHopProtocol } from './hop.js'
|
|
20
|
+
import { handleStop } from './stop.js'
|
|
21
|
+
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
29
22
|
import type { PeerStore } from '@libp2p/interface-peer-store'
|
|
23
|
+
import type { Startable } from '@libp2p/interfaces/dist/src/startable'
|
|
24
|
+
import type { ConnectionManager } from '@libp2p/interface-connection-manager'
|
|
30
25
|
import type { AddressManager } from '@libp2p/interface-address-manager'
|
|
26
|
+
import { pbStream } from 'it-pb-stream'
|
|
27
|
+
import pDefer from 'p-defer'
|
|
31
28
|
|
|
32
29
|
const log = logger('libp2p:circuit')
|
|
33
30
|
|
|
31
|
+
export interface CircuitOptions {
|
|
32
|
+
limit?: number
|
|
33
|
+
}
|
|
34
|
+
|
|
34
35
|
export interface CircuitComponents {
|
|
35
36
|
peerId: PeerId
|
|
36
37
|
peerStore: PeerStore
|
|
@@ -38,17 +39,33 @@ export interface CircuitComponents {
|
|
|
38
39
|
connectionManager: ConnectionManager
|
|
39
40
|
upgrader: Upgrader
|
|
40
41
|
addressManager: AddressManager
|
|
42
|
+
transportManager: TransportManager
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
interface ConnectOptions {
|
|
46
|
+
stream: Stream
|
|
47
|
+
connection: Connection
|
|
48
|
+
destinationPeer: PeerId
|
|
49
|
+
destinationAddr: Multiaddr
|
|
50
|
+
relayAddr: Multiaddr
|
|
51
|
+
ma: Multiaddr
|
|
52
|
+
disconnectOnFailure: boolean
|
|
41
53
|
}
|
|
42
54
|
|
|
43
55
|
export class Circuit implements Transport, Startable {
|
|
44
56
|
private handler?: ConnectionHandler
|
|
45
57
|
private readonly components: CircuitComponents
|
|
58
|
+
private readonly reservationStore: ReservationStore
|
|
46
59
|
private readonly _init: RelayConfig
|
|
47
60
|
private _started: boolean
|
|
48
61
|
|
|
49
|
-
constructor (components: CircuitComponents,
|
|
50
|
-
this._init = init
|
|
62
|
+
constructor (components: CircuitComponents, options: RelayConfig) {
|
|
51
63
|
this.components = components
|
|
64
|
+
this._init = options
|
|
65
|
+
this.reservationStore = new ReservationStore({
|
|
66
|
+
defaultDataLimit: options.hop?.limit?.data,
|
|
67
|
+
defaultDurationLimit: options.hop?.limit?.duration
|
|
68
|
+
})
|
|
52
69
|
this._started = false
|
|
53
70
|
}
|
|
54
71
|
|
|
@@ -63,26 +80,38 @@ export class Circuit implements Transport, Startable {
|
|
|
63
80
|
|
|
64
81
|
this._started = true
|
|
65
82
|
|
|
66
|
-
|
|
67
|
-
|
|
83
|
+
// only handle hop if enabled
|
|
84
|
+
if (this._init.hop.enabled === true) {
|
|
85
|
+
void this.components.registrar.handle(RELAY_V2_HOP_CODEC, (data) => {
|
|
86
|
+
void this.onHop(data).catch(err => {
|
|
87
|
+
log.error(err)
|
|
88
|
+
})
|
|
89
|
+
})
|
|
90
|
+
.catch(err => {
|
|
91
|
+
log.error(err)
|
|
92
|
+
})
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
void this.components.registrar.handle(RELAY_V2_STOP_CODEC, (data) => {
|
|
96
|
+
void this.onStop(data).catch(err => {
|
|
68
97
|
log.error(err)
|
|
69
98
|
})
|
|
70
|
-
}
|
|
99
|
+
})
|
|
71
100
|
.catch(err => {
|
|
72
101
|
log.error(err)
|
|
73
102
|
})
|
|
74
|
-
}
|
|
75
103
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
hopEnabled () {
|
|
81
|
-
return true
|
|
104
|
+
if (this._init.hop.enabled === true) {
|
|
105
|
+
void this.reservationStore.start()
|
|
106
|
+
}
|
|
82
107
|
}
|
|
83
108
|
|
|
84
|
-
|
|
85
|
-
|
|
109
|
+
async stop () {
|
|
110
|
+
if (this._init.hop.enabled === true) {
|
|
111
|
+
this.reservationStore.stop()
|
|
112
|
+
await this.components.registrar.unhandle(RELAY_V2_HOP_CODEC)
|
|
113
|
+
}
|
|
114
|
+
await this.components.registrar.unhandle(RELAY_V2_STOP_CODEC)
|
|
86
115
|
}
|
|
87
116
|
|
|
88
117
|
get [symbol] (): true {
|
|
@@ -90,99 +119,78 @@ export class Circuit implements Transport, Startable {
|
|
|
90
119
|
}
|
|
91
120
|
|
|
92
121
|
get [Symbol.toStringTag] () {
|
|
93
|
-
return 'libp2p/circuit-relay-
|
|
122
|
+
return 'libp2p/circuit-relay-v2'
|
|
94
123
|
}
|
|
95
124
|
|
|
96
|
-
async
|
|
97
|
-
|
|
98
|
-
const controller = new TimeoutController(this._init.hop.timeout)
|
|
125
|
+
async onHop ({ connection, stream }: IncomingStreamData) {
|
|
126
|
+
log('received circuit v2 hop protocol stream from %s', connection.remotePeer)
|
|
99
127
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
}
|
|
128
|
+
const hopTimeoutPromise = pDefer()
|
|
129
|
+
const timeout = setTimeout(() => {
|
|
130
|
+
hopTimeoutPromise.reject('timed out')
|
|
131
|
+
}, this._init.hop.timeout)
|
|
132
|
+
const pbstr = pbStream(stream)
|
|
104
133
|
|
|
105
134
|
try {
|
|
106
|
-
const
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
...source
|
|
111
|
-
}
|
|
112
|
-
})
|
|
113
|
-
const request = await streamHandler.read()
|
|
135
|
+
const request: CircuitV2.HopMessage = await Promise.race([
|
|
136
|
+
pbstr.pb(CircuitV2.HopMessage).read(),
|
|
137
|
+
hopTimeoutPromise.promise as any
|
|
138
|
+
])
|
|
114
139
|
|
|
115
|
-
if (request == null) {
|
|
116
|
-
|
|
117
|
-
streamHandler.write({
|
|
118
|
-
type: CircuitPB.Type.STATUS,
|
|
119
|
-
code: CircuitPB.Status.MALFORMED_MESSAGE
|
|
120
|
-
})
|
|
121
|
-
streamHandler.close()
|
|
122
|
-
return
|
|
140
|
+
if (request?.type == null) {
|
|
141
|
+
throw new Error('request was invalid, could not read from stream')
|
|
123
142
|
}
|
|
124
143
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
request,
|
|
149
|
-
streamHandler
|
|
150
|
-
})
|
|
151
|
-
break
|
|
152
|
-
}
|
|
153
|
-
default: {
|
|
154
|
-
log('Request of type %s not supported', request.type)
|
|
155
|
-
streamHandler.write({
|
|
156
|
-
type: CircuitPB.Type.STATUS,
|
|
157
|
-
code: CircuitPB.Status.MALFORMED_MESSAGE
|
|
158
|
-
})
|
|
159
|
-
streamHandler.close()
|
|
160
|
-
return
|
|
161
|
-
}
|
|
162
|
-
}
|
|
144
|
+
await Promise.race([
|
|
145
|
+
handleHopProtocol({
|
|
146
|
+
connection,
|
|
147
|
+
stream: pbstr,
|
|
148
|
+
connectionManager: this.components.connectionManager,
|
|
149
|
+
relayPeer: this.components.peerId,
|
|
150
|
+
relayAddrs: this.components.addressManager.getListenAddrs(),
|
|
151
|
+
reservationStore: this.reservationStore,
|
|
152
|
+
peerStore: this.components.peerStore,
|
|
153
|
+
request
|
|
154
|
+
}),
|
|
155
|
+
hopTimeoutPromise.promise
|
|
156
|
+
])
|
|
157
|
+
} catch (_err) {
|
|
158
|
+
pbstr.pb(CircuitV2.HopMessage).write({
|
|
159
|
+
type: CircuitV2.HopMessage.Type.STATUS,
|
|
160
|
+
status: CircuitV2.Status.MALFORMED_MESSAGE
|
|
161
|
+
})
|
|
162
|
+
stream.abort(_err as Error)
|
|
163
|
+
} finally {
|
|
164
|
+
clearTimeout(timeout)
|
|
165
|
+
}
|
|
166
|
+
}
|
|
163
167
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
remoteAddr,
|
|
172
|
-
localAddr
|
|
173
|
-
})
|
|
174
|
-
const type = request.type === CircuitPB.Type.HOP ? 'relay' : 'inbound'
|
|
175
|
-
log('new %s connection %s', type, maConn.remoteAddr)
|
|
168
|
+
async onStop ({ connection, stream }: IncomingStreamData) {
|
|
169
|
+
const pbstr = pbStream(stream)
|
|
170
|
+
const request = await pbstr.readPB(CircuitV2.StopMessage)
|
|
171
|
+
log('received circuit v2 stop protocol request from %s', connection.remotePeer)
|
|
172
|
+
if (request?.type === undefined) {
|
|
173
|
+
return
|
|
174
|
+
}
|
|
176
175
|
|
|
177
|
-
|
|
178
|
-
|
|
176
|
+
const mStream = await handleStop({
|
|
177
|
+
connection,
|
|
178
|
+
pbstr,
|
|
179
|
+
request
|
|
180
|
+
})
|
|
179
181
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
182
|
+
if (mStream != null) {
|
|
183
|
+
const remoteAddr = multiaddr(request.peer?.addrs?.[0])
|
|
184
|
+
const localAddr = this.components.transportManager.getAddrs()[0]
|
|
185
|
+
const maConn = streamToMaConnection({
|
|
186
|
+
stream: mStream as any,
|
|
187
|
+
remoteAddr,
|
|
188
|
+
localAddr
|
|
189
|
+
})
|
|
190
|
+
log('new inbound connection %s', maConn.remoteAddr)
|
|
191
|
+
const conn = await this.components.upgrader.upgradeInbound(maConn)
|
|
192
|
+
log('%s connection %s upgraded', 'inbound', maConn.remoteAddr)
|
|
193
|
+
this.handler?.(conn)
|
|
186
194
|
}
|
|
187
195
|
}
|
|
188
196
|
|
|
@@ -200,7 +208,7 @@ export class Circuit implements Transport, Startable {
|
|
|
200
208
|
if (relayId == null || destinationId == null) {
|
|
201
209
|
const errMsg = 'Circuit relay dial failed as addresses did not have peer id'
|
|
202
210
|
log.error(errMsg)
|
|
203
|
-
throw
|
|
211
|
+
throw createError(new Error(errMsg), codes.ERR_RELAYED_DIAL)
|
|
204
212
|
}
|
|
205
213
|
|
|
206
214
|
const relayPeer = peerIdFromString(relayId)
|
|
@@ -217,34 +225,61 @@ export class Circuit implements Transport, Startable {
|
|
|
217
225
|
}
|
|
218
226
|
|
|
219
227
|
try {
|
|
220
|
-
const
|
|
221
|
-
|
|
228
|
+
const stream = await relayConnection.newStream([RELAY_V2_HOP_CODEC])
|
|
229
|
+
return await this.connectV2({
|
|
230
|
+
stream,
|
|
222
231
|
connection: relayConnection,
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
232
|
+
destinationPeer,
|
|
233
|
+
destinationAddr,
|
|
234
|
+
relayAddr,
|
|
235
|
+
ma,
|
|
236
|
+
disconnectOnFailure
|
|
237
|
+
})
|
|
238
|
+
} catch (err: any) {
|
|
239
|
+
log.error('Circuit relay dial failed', err)
|
|
240
|
+
disconnectOnFailure && await relayConnection.close()
|
|
241
|
+
throw err
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
async connectV2 (
|
|
246
|
+
{
|
|
247
|
+
stream, connection, destinationPeer,
|
|
248
|
+
destinationAddr, relayAddr, ma,
|
|
249
|
+
disconnectOnFailure
|
|
250
|
+
}: ConnectOptions
|
|
251
|
+
) {
|
|
252
|
+
try {
|
|
253
|
+
const pbstr = pbStream(stream)
|
|
254
|
+
const hopstr = pbstr.pb(CircuitV2.HopMessage)
|
|
255
|
+
hopstr.write({
|
|
256
|
+
type: CircuitV2.HopMessage.Type.CONNECT,
|
|
257
|
+
peer: {
|
|
258
|
+
id: destinationPeer.toBytes(),
|
|
259
|
+
addrs: [multiaddr(destinationAddr).bytes]
|
|
233
260
|
}
|
|
234
261
|
})
|
|
235
262
|
|
|
236
|
-
const
|
|
263
|
+
const status = await hopstr.read()
|
|
264
|
+
if (status.status !== CircuitV2.Status.OK) {
|
|
265
|
+
throw createError(new Error(`failed to connect via relay with status ${status?.status?.toString() ?? 'undefined'}`), codes.ERR_HOP_REQUEST_FAILED)
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// TODO: do something with limit and transient connection
|
|
269
|
+
|
|
270
|
+
let localAddr = relayAddr
|
|
271
|
+
localAddr = localAddr.encapsulate(`/p2p-circuit/p2p/${this.components.peerId.toString()}`)
|
|
237
272
|
const maConn = streamToMaConnection({
|
|
238
|
-
stream:
|
|
273
|
+
stream: pbstr.unwrap(),
|
|
239
274
|
remoteAddr: ma,
|
|
240
275
|
localAddr
|
|
241
276
|
})
|
|
242
277
|
log('new outbound connection %s', maConn.remoteAddr)
|
|
243
|
-
|
|
244
|
-
return
|
|
245
|
-
} catch (err
|
|
278
|
+
const conn = await this.components.upgrader.upgradeOutbound(maConn)
|
|
279
|
+
return conn
|
|
280
|
+
} catch (err) {
|
|
246
281
|
log.error('Circuit relay dial failed', err)
|
|
247
|
-
disconnectOnFailure && await
|
|
282
|
+
disconnectOnFailure && await connection.close()
|
|
248
283
|
throw err
|
|
249
284
|
}
|
|
250
285
|
}
|
package/src/circuit/utils.ts
CHANGED
|
@@ -1,5 +1,145 @@
|
|
|
1
1
|
import { CID } from 'multiformats/cid'
|
|
2
2
|
import { sha256 } from 'multiformats/hashes/sha2'
|
|
3
|
+
import type { Source } from 'it-stream-types'
|
|
4
|
+
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
5
|
+
import type { Limit } from './pb/index.js'
|
|
6
|
+
import { logger } from '@libp2p/logger'
|
|
7
|
+
import type { Stream } from '@libp2p/interface-connection'
|
|
8
|
+
|
|
9
|
+
const log = logger('libp2p:circuit:v2:util')
|
|
10
|
+
|
|
11
|
+
const doRelay = (src: Stream, dst: Stream) => {
|
|
12
|
+
queueMicrotask(() => {
|
|
13
|
+
void dst.sink(src.source).catch(err => log.error('error while relating streams:', err))
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
queueMicrotask(() => {
|
|
17
|
+
void src.sink(dst.source).catch(err => log.error('error while relaying streams:', err))
|
|
18
|
+
})
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function createLimitedRelay (source: Stream, destination: Stream, limit?: Limit) {
|
|
22
|
+
// trivial case
|
|
23
|
+
if (limit == null) {
|
|
24
|
+
doRelay(source, destination)
|
|
25
|
+
return
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const dataLimit = limit.data ?? 0n
|
|
29
|
+
const durationLimit = limit.duration ?? 0
|
|
30
|
+
const src = durationLimitDuplex(dataLimitDuplex(source, dataLimit), durationLimit)
|
|
31
|
+
const dst = durationLimitDuplex(dataLimitDuplex(destination, dataLimit), durationLimit)
|
|
32
|
+
|
|
33
|
+
doRelay(src, dst)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const dataLimitSource = (stream: Stream, limit: bigint): Stream => {
|
|
37
|
+
if (limit === 0n) {
|
|
38
|
+
return stream
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const source = stream.source
|
|
42
|
+
|
|
43
|
+
stream.source = (async function * (): Source<Uint8ArrayList> {
|
|
44
|
+
let total = 0n
|
|
45
|
+
|
|
46
|
+
for await (const buf of source) {
|
|
47
|
+
const len = BigInt(buf.byteLength)
|
|
48
|
+
if (total + len > limit) {
|
|
49
|
+
// this is a safe downcast since len is guarantee to be in the range for a number
|
|
50
|
+
const remaining = Number(limit - total)
|
|
51
|
+
try {
|
|
52
|
+
if (remaining !== 0) {
|
|
53
|
+
yield buf
|
|
54
|
+
}
|
|
55
|
+
} finally {
|
|
56
|
+
stream.abort(new Error('data limit exceeded'))
|
|
57
|
+
}
|
|
58
|
+
return
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
yield buf
|
|
62
|
+
|
|
63
|
+
total += len
|
|
64
|
+
}
|
|
65
|
+
})()
|
|
66
|
+
|
|
67
|
+
return stream
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const dataLimitSink = (stream: Stream, limit: bigint): Stream => {
|
|
71
|
+
if (limit === 0n) {
|
|
72
|
+
return stream
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const sink = stream.sink
|
|
76
|
+
|
|
77
|
+
stream.sink = async (source: Source<Uint8ArrayList | Uint8Array>) => {
|
|
78
|
+
await sink((async function * (): Source<Uint8ArrayList | Uint8Array> {
|
|
79
|
+
let total = 0n
|
|
80
|
+
|
|
81
|
+
for await (const buf of source) {
|
|
82
|
+
const len = BigInt(buf.byteLength)
|
|
83
|
+
if (total + len > limit) {
|
|
84
|
+
// this is a safe downcast since len is guarantee to be in the range for a number
|
|
85
|
+
const remaining = Number(limit - total)
|
|
86
|
+
try {
|
|
87
|
+
if (remaining !== 0) {
|
|
88
|
+
yield buf.subarray(0, remaining)
|
|
89
|
+
}
|
|
90
|
+
} finally {
|
|
91
|
+
stream.abort(new Error('data limit exceeded'))
|
|
92
|
+
}
|
|
93
|
+
return
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
total += len
|
|
97
|
+
yield buf
|
|
98
|
+
}
|
|
99
|
+
})())
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return stream
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const dataLimitDuplex = (stream: Stream, limit: bigint): Stream => {
|
|
106
|
+
dataLimitSource(stream, limit)
|
|
107
|
+
dataLimitSink(stream, limit)
|
|
108
|
+
|
|
109
|
+
return stream
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const durationLimitDuplex = (stream: Stream, limit: number): Stream => {
|
|
113
|
+
if (limit === 0) {
|
|
114
|
+
return stream
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
let timedOut = false
|
|
118
|
+
const timeout = setTimeout(
|
|
119
|
+
() => {
|
|
120
|
+
timedOut = true
|
|
121
|
+
stream.abort(new Error('exceeded connection duration limit'))
|
|
122
|
+
},
|
|
123
|
+
limit
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
const source = stream.source
|
|
127
|
+
|
|
128
|
+
stream.source = (async function * (): Source<Uint8ArrayList> {
|
|
129
|
+
try {
|
|
130
|
+
for await (const buf of source) {
|
|
131
|
+
if (timedOut) {
|
|
132
|
+
return
|
|
133
|
+
}
|
|
134
|
+
yield buf
|
|
135
|
+
}
|
|
136
|
+
} finally {
|
|
137
|
+
clearTimeout(timeout)
|
|
138
|
+
}
|
|
139
|
+
})()
|
|
140
|
+
|
|
141
|
+
return stream
|
|
142
|
+
}
|
|
3
143
|
|
|
4
144
|
/**
|
|
5
145
|
* Convert a namespace string into a cid
|
|
@@ -10,3 +150,8 @@ export async function namespaceToCid (namespace: string): Promise<CID> {
|
|
|
10
150
|
|
|
11
151
|
return CID.createV0(hash)
|
|
12
152
|
}
|
|
153
|
+
|
|
154
|
+
/** returns number of ms beween now and expiration time */
|
|
155
|
+
export function getExpiration (expireTime: bigint): number {
|
|
156
|
+
return Number(expireTime) - new Date().getTime()
|
|
157
|
+
}
|
package/src/config.ts
CHANGED
|
@@ -58,12 +58,11 @@ const DefaultConfig: Partial<Libp2pInit> = {
|
|
|
58
58
|
},
|
|
59
59
|
hop: {
|
|
60
60
|
enabled: false,
|
|
61
|
-
active: false,
|
|
62
61
|
timeout: 30000
|
|
63
62
|
},
|
|
64
|
-
|
|
63
|
+
reservationManager: {
|
|
65
64
|
enabled: false,
|
|
66
|
-
|
|
65
|
+
maxReservations: 2
|
|
67
66
|
}
|
|
68
67
|
},
|
|
69
68
|
identify: {
|
|
@@ -124,6 +124,13 @@ export class DialRequest {
|
|
|
124
124
|
|
|
125
125
|
return conn
|
|
126
126
|
}))
|
|
127
|
+
} catch (err: any) {
|
|
128
|
+
// if we only dialed one address, unwrap the AggregateError
|
|
129
|
+
if (this.addrs.length === 1 && err.name === 'AggregateError') {
|
|
130
|
+
throw err.errors[0]
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
throw err
|
|
127
134
|
} finally {
|
|
128
135
|
// success/failure happened, abort everything else
|
|
129
136
|
dialAbortControllers.forEach(c => {
|
package/src/errors.ts
CHANGED
|
@@ -74,5 +74,6 @@ export enum codes {
|
|
|
74
74
|
ERR_NO_HANDLER_FOR_PROTOCOL = 'ERR_NO_HANDLER_FOR_PROTOCOL',
|
|
75
75
|
ERR_TOO_MANY_OUTBOUND_PROTOCOL_STREAMS = 'ERR_TOO_MANY_OUTBOUND_PROTOCOL_STREAMS',
|
|
76
76
|
ERR_TOO_MANY_INBOUND_PROTOCOL_STREAMS = 'ERR_TOO_MANY_INBOUND_PROTOCOL_STREAMS',
|
|
77
|
-
ERR_CONNECTION_DENIED = 'ERR_CONNECTION_DENIED'
|
|
77
|
+
ERR_CONNECTION_DENIED = 'ERR_CONNECTION_DENIED',
|
|
78
|
+
ERR_TRANSFER_LIMIT_EXCEEDED = 'ERR_TRANSFER_LIMIT_EXCEEDED',
|
|
78
79
|
}
|
package/src/fetch/pb/proto.ts
CHANGED
|
@@ -2,10 +2,11 @@
|
|
|
2
2
|
/* eslint-disable complexity */
|
|
3
3
|
/* eslint-disable @typescript-eslint/no-namespace */
|
|
4
4
|
/* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */
|
|
5
|
+
/* eslint-disable @typescript-eslint/no-empty-interface */
|
|
5
6
|
|
|
6
7
|
import { encodeMessage, decodeMessage, message, enumeration } from 'protons-runtime'
|
|
7
|
-
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
8
8
|
import type { Codec } from 'protons-runtime'
|
|
9
|
+
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
9
10
|
|
|
10
11
|
export interface FetchRequest {
|
|
11
12
|
identifier: string
|
|
@@ -21,7 +22,7 @@ export namespace FetchRequest {
|
|
|
21
22
|
w.fork()
|
|
22
23
|
}
|
|
23
24
|
|
|
24
|
-
if (
|
|
25
|
+
if ((obj.identifier != null && obj.identifier !== '')) {
|
|
25
26
|
w.uint32(10)
|
|
26
27
|
w.string(obj.identifier)
|
|
27
28
|
}
|
|
@@ -56,7 +57,7 @@ export namespace FetchRequest {
|
|
|
56
57
|
return _codec
|
|
57
58
|
}
|
|
58
59
|
|
|
59
|
-
export const encode = (obj: FetchRequest): Uint8Array => {
|
|
60
|
+
export const encode = (obj: Partial<FetchRequest>): Uint8Array => {
|
|
60
61
|
return encodeMessage(obj, FetchRequest.codec())
|
|
61
62
|
}
|
|
62
63
|
|
|
@@ -84,7 +85,7 @@ export namespace FetchResponse {
|
|
|
84
85
|
}
|
|
85
86
|
|
|
86
87
|
export namespace StatusCode {
|
|
87
|
-
export const codec = () => {
|
|
88
|
+
export const codec = (): Codec<StatusCode> => {
|
|
88
89
|
return enumeration<StatusCode>(__StatusCodeValues)
|
|
89
90
|
}
|
|
90
91
|
}
|
|
@@ -98,12 +99,12 @@ export namespace FetchResponse {
|
|
|
98
99
|
w.fork()
|
|
99
100
|
}
|
|
100
101
|
|
|
101
|
-
if (
|
|
102
|
+
if (obj.status != null && __StatusCodeValues[obj.status] !== 0) {
|
|
102
103
|
w.uint32(8)
|
|
103
104
|
FetchResponse.StatusCode.codec().encode(obj.status, w)
|
|
104
105
|
}
|
|
105
106
|
|
|
106
|
-
if (
|
|
107
|
+
if ((obj.data != null && obj.data.byteLength > 0)) {
|
|
107
108
|
w.uint32(18)
|
|
108
109
|
w.bytes(obj.data)
|
|
109
110
|
}
|
|
@@ -142,7 +143,7 @@ export namespace FetchResponse {
|
|
|
142
143
|
return _codec
|
|
143
144
|
}
|
|
144
145
|
|
|
145
|
-
export const encode = (obj: FetchResponse): Uint8Array => {
|
|
146
|
+
export const encode = (obj: Partial<FetchResponse>): Uint8Array => {
|
|
146
147
|
return encodeMessage(obj, FetchResponse.codec())
|
|
147
148
|
}
|
|
148
149
|
|
|
@@ -2,10 +2,11 @@
|
|
|
2
2
|
/* eslint-disable complexity */
|
|
3
3
|
/* eslint-disable @typescript-eslint/no-namespace */
|
|
4
4
|
/* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */
|
|
5
|
+
/* eslint-disable @typescript-eslint/no-empty-interface */
|
|
5
6
|
|
|
6
7
|
import { encodeMessage, decodeMessage, message } from 'protons-runtime'
|
|
7
|
-
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
8
8
|
import type { Codec } from 'protons-runtime'
|
|
9
|
+
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
9
10
|
|
|
10
11
|
export interface Identify {
|
|
11
12
|
protocolVersion?: string
|
|
@@ -115,7 +116,7 @@ export namespace Identify {
|
|
|
115
116
|
return _codec
|
|
116
117
|
}
|
|
117
118
|
|
|
118
|
-
export const encode = (obj: Identify): Uint8Array => {
|
|
119
|
+
export const encode = (obj: Partial<Identify>): Uint8Array => {
|
|
119
120
|
return encodeMessage(obj, Identify.codec())
|
|
120
121
|
}
|
|
121
122
|
|