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
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
2
|
+
import { RecordEnvelope } from '@libp2p/peer-record'
|
|
3
|
+
import { logger } from '@libp2p/logger'
|
|
4
|
+
import type { Connection, Stream } from '@libp2p/interface-connection'
|
|
5
|
+
import { HopMessage, Limit, Reservation, Status, StopMessage } from './pb/index.js'
|
|
6
|
+
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
7
|
+
import { multiaddr } from '@multiformats/multiaddr'
|
|
8
|
+
import type { Acl, ReservationStore } from './interfaces.js'
|
|
9
|
+
import { RELAY_V2_HOP_CODEC } from './multicodec.js'
|
|
10
|
+
import { stop } from './stop.js'
|
|
11
|
+
import { ReservationVoucherRecord } from './reservation-voucher.js'
|
|
12
|
+
import { peerIdFromBytes } from '@libp2p/peer-id'
|
|
13
|
+
import type { ConnectionManager } from '@libp2p/interface-connection-manager'
|
|
14
|
+
import type { ProtobufStream } from 'it-pb-stream'
|
|
15
|
+
import { pbStream } from 'it-pb-stream'
|
|
16
|
+
import { CIRCUIT_PROTO_CODE, RELAY_DESTINATION_TAG } from './constants.js'
|
|
17
|
+
import type { PeerStore } from '@libp2p/interface-peer-store'
|
|
18
|
+
import { createLimitedRelay } from './utils.js'
|
|
19
|
+
|
|
20
|
+
const log = logger('libp2p:circuit:v2:hop')
|
|
21
|
+
|
|
22
|
+
export interface HopProtocolOptions {
|
|
23
|
+
connection: Connection
|
|
24
|
+
request: HopMessage
|
|
25
|
+
stream: ProtobufStream<Stream>
|
|
26
|
+
relayPeer: PeerId
|
|
27
|
+
relayAddrs: Multiaddr[]
|
|
28
|
+
limit?: Limit
|
|
29
|
+
acl?: Acl
|
|
30
|
+
reservationStore: ReservationStore
|
|
31
|
+
connectionManager: ConnectionManager
|
|
32
|
+
peerStore: PeerStore
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export async function handleHopProtocol (options: HopProtocolOptions): Promise<void> {
|
|
36
|
+
const { stream, request } = options
|
|
37
|
+
log('received hop message')
|
|
38
|
+
switch (request.type) {
|
|
39
|
+
case HopMessage.Type.RESERVE: await handleReserve(options); break
|
|
40
|
+
case HopMessage.Type.CONNECT: await handleConnect(options); break
|
|
41
|
+
default: {
|
|
42
|
+
log.error('invalid hop request type %s via peer %s', options.request.type, options.connection.remotePeer)
|
|
43
|
+
stream.pb(HopMessage).write({ type: HopMessage.Type.STATUS, status: Status.UNEXPECTED_MESSAGE })
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export async function reserve (connection: Connection): Promise<Reservation> {
|
|
49
|
+
log('requesting reservation from %s', connection.remotePeer)
|
|
50
|
+
const stream = await connection.newStream([RELAY_V2_HOP_CODEC])
|
|
51
|
+
const pbstr = pbStream(stream)
|
|
52
|
+
const hopstr = pbstr.pb(HopMessage)
|
|
53
|
+
hopstr.write({ type: HopMessage.Type.RESERVE })
|
|
54
|
+
|
|
55
|
+
let response: HopMessage
|
|
56
|
+
try {
|
|
57
|
+
response = await hopstr.read()
|
|
58
|
+
} catch (err: any) {
|
|
59
|
+
log.error('error passing reserve message response from %s because', connection.remotePeer, err.message)
|
|
60
|
+
stream.close()
|
|
61
|
+
throw err
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (response.status === Status.OK && (response.reservation != null)) {
|
|
65
|
+
return response.reservation
|
|
66
|
+
}
|
|
67
|
+
const errMsg = `reservation failed with status ${response.status ?? 'undefined'}`
|
|
68
|
+
log.error(errMsg)
|
|
69
|
+
throw new Error(errMsg)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const isRelayAddr = (ma: Multiaddr): boolean => ma.protoCodes().includes(CIRCUIT_PROTO_CODE)
|
|
73
|
+
|
|
74
|
+
async function handleReserve ({ connection, stream: pbstr, relayPeer, relayAddrs, acl, reservationStore, peerStore }: HopProtocolOptions): Promise<void> {
|
|
75
|
+
const hopstr = pbstr.pb(HopMessage)
|
|
76
|
+
log('hop reserve request from %s', connection.remotePeer)
|
|
77
|
+
|
|
78
|
+
if (isRelayAddr(connection.remoteAddr)) {
|
|
79
|
+
log.error('relay reservation over circuit connection denied for peer: %p', connection.remotePeer)
|
|
80
|
+
hopstr.write({ type: HopMessage.Type.STATUS, status: Status.PERMISSION_DENIED })
|
|
81
|
+
return
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if ((await acl?.allowReserve?.(connection.remotePeer, connection.remoteAddr)) === false) {
|
|
85
|
+
log.error('acl denied reservation to %s', connection.remotePeer)
|
|
86
|
+
hopstr.write({ type: HopMessage.Type.STATUS, status: Status.PERMISSION_DENIED })
|
|
87
|
+
return
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const result = await reservationStore.reserve(connection.remotePeer, connection.remoteAddr)
|
|
91
|
+
|
|
92
|
+
if (result.status !== Status.OK) {
|
|
93
|
+
hopstr.write({ type: HopMessage.Type.STATUS, status: result.status })
|
|
94
|
+
return
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
try {
|
|
98
|
+
// tag relayed peer
|
|
99
|
+
// result.expire is non-null if `ReservationStore.reserve` returns with status == OK
|
|
100
|
+
if (result.expire != null) {
|
|
101
|
+
const ttl = new Date().getTime() - result.expire
|
|
102
|
+
await peerStore.tagPeer(relayPeer, RELAY_DESTINATION_TAG, { value: 1, ttl })
|
|
103
|
+
}
|
|
104
|
+
hopstr.write({
|
|
105
|
+
type: HopMessage.Type.STATUS,
|
|
106
|
+
status: Status.OK,
|
|
107
|
+
reservation: await makeReservation(relayAddrs, relayPeer, connection.remotePeer, BigInt(result.expire ?? 0)),
|
|
108
|
+
limit: (await reservationStore.get(relayPeer))?.limit
|
|
109
|
+
})
|
|
110
|
+
log('sent confirmation response to %s', connection.remotePeer)
|
|
111
|
+
} catch (err) {
|
|
112
|
+
log.error('failed to send confirmation response to %s', connection.remotePeer)
|
|
113
|
+
await reservationStore.removeReservation(connection.remotePeer)
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
async function handleConnect (options: HopProtocolOptions): Promise<void> {
|
|
118
|
+
const { connection, stream, request, reservationStore, connectionManager, acl } = options
|
|
119
|
+
const hopstr = stream.pb(HopMessage)
|
|
120
|
+
|
|
121
|
+
log('hop connect request from %s', connection.remotePeer)
|
|
122
|
+
|
|
123
|
+
let dstPeer: PeerId
|
|
124
|
+
try {
|
|
125
|
+
if (request.peer == null) {
|
|
126
|
+
log.error('no peer info in hop connect request')
|
|
127
|
+
throw new Error('no peer info in request')
|
|
128
|
+
}
|
|
129
|
+
request.peer.addrs.forEach(multiaddr)
|
|
130
|
+
dstPeer = peerIdFromBytes(request.peer.id)
|
|
131
|
+
} catch (err) {
|
|
132
|
+
log.error('invalid hop connect request via peer %p %s', connection.remotePeer, err)
|
|
133
|
+
hopstr.write({ type: HopMessage.Type.STATUS, status: Status.MALFORMED_MESSAGE })
|
|
134
|
+
return
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (acl?.allowConnect !== undefined) {
|
|
138
|
+
const status = await acl.allowConnect(connection.remotePeer, connection.remoteAddr, dstPeer)
|
|
139
|
+
if (status !== Status.OK) {
|
|
140
|
+
log.error('hop connect denied for %s with status %s', connection.remotePeer, status)
|
|
141
|
+
hopstr.write({ type: HopMessage.Type.STATUS, status: status })
|
|
142
|
+
return
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (!await reservationStore.hasReservation(dstPeer)) {
|
|
147
|
+
log.error('hop connect denied for %s with status %s', connection.remotePeer, Status.NO_RESERVATION)
|
|
148
|
+
hopstr.write({ type: HopMessage.Type.STATUS, status: Status.NO_RESERVATION })
|
|
149
|
+
return
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const connections = connectionManager.getConnections(dstPeer)
|
|
153
|
+
if (connections.length === 0) {
|
|
154
|
+
log('hop connect denied for %s as there is no destination connection', connection.remotePeer)
|
|
155
|
+
hopstr.write({ type: HopMessage.Type.STATUS, status: Status.NO_RESERVATION })
|
|
156
|
+
return
|
|
157
|
+
}
|
|
158
|
+
const destinationConnection = connections[0]
|
|
159
|
+
log('hop connect request from %s to %s is valid', connection.remotePeer, dstPeer)
|
|
160
|
+
|
|
161
|
+
const destinationStream = await stop({
|
|
162
|
+
connection: destinationConnection,
|
|
163
|
+
request: {
|
|
164
|
+
type: StopMessage.Type.CONNECT,
|
|
165
|
+
peer: {
|
|
166
|
+
id: connection.remotePeer.toBytes(),
|
|
167
|
+
addrs: [multiaddr('/p2p/' + connection.remotePeer.toString()).bytes]
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
})
|
|
171
|
+
|
|
172
|
+
if (destinationStream == null) {
|
|
173
|
+
log.error('failed to open stream to destination peer %s', destinationConnection?.remotePeer)
|
|
174
|
+
hopstr.write({ type: HopMessage.Type.STATUS, status: Status.CONNECTION_FAILED })
|
|
175
|
+
return
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
hopstr.write({ type: HopMessage.Type.STATUS, status: Status.OK })
|
|
179
|
+
const sourceStream = stream.unwrap()
|
|
180
|
+
|
|
181
|
+
log('connection to destination established, short circuiting streams...')
|
|
182
|
+
const limit = (await reservationStore.get(dstPeer))?.limit
|
|
183
|
+
// Short circuit the two streams to create the relayed connection
|
|
184
|
+
return createLimitedRelay(sourceStream, destinationStream, limit)
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
async function makeReservation (
|
|
188
|
+
relayAddrs: Multiaddr[],
|
|
189
|
+
relayPeerId: PeerId,
|
|
190
|
+
remotePeer: PeerId,
|
|
191
|
+
expire: bigint
|
|
192
|
+
): Promise<Reservation> {
|
|
193
|
+
const addrs = []
|
|
194
|
+
|
|
195
|
+
for (const relayAddr of relayAddrs) {
|
|
196
|
+
addrs.push(relayAddr.bytes)
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const voucher = await RecordEnvelope.seal(new ReservationVoucherRecord({
|
|
200
|
+
peer: remotePeer,
|
|
201
|
+
relay: relayPeerId,
|
|
202
|
+
expiration: Number(expire)
|
|
203
|
+
}), relayPeerId)
|
|
204
|
+
|
|
205
|
+
return {
|
|
206
|
+
addrs,
|
|
207
|
+
expire,
|
|
208
|
+
voucher: voucher.marshal()
|
|
209
|
+
}
|
|
210
|
+
}
|
package/src/circuit/index.ts
CHANGED
|
@@ -1,135 +1,73 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
import { namespaceToCid } from './utils.js'
|
|
10
|
-
import {
|
|
11
|
-
RELAY_RENDEZVOUS_NS
|
|
12
|
-
} from './constants.js'
|
|
13
|
-
import type { AddressSorter, PeerStore } from '@libp2p/interface-peer-store'
|
|
14
|
-
import type { Startable } from '@libp2p/interfaces/startable'
|
|
15
|
-
import type { ContentRouting } from '@libp2p/interface-content-routing'
|
|
16
|
-
import type { ConnectionManager } from '@libp2p/interface-connection-manager'
|
|
17
|
-
import type { TransportManager } from '@libp2p/interface-transport'
|
|
18
|
-
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
19
|
-
import type { StreamHandlerOptions } from '@libp2p/interface-registrar'
|
|
20
|
-
|
|
21
|
-
export interface RelayConfig extends StreamHandlerOptions {
|
|
1
|
+
/**
|
|
2
|
+
* RelayConfig configures the circuit v2 relay transport.
|
|
3
|
+
*/
|
|
4
|
+
export interface RelayConfig {
|
|
5
|
+
/**
|
|
6
|
+
* Enable dialing a client over a relay and receiving relayed connections.
|
|
7
|
+
* This in itself does not enable the node to act as a relay.
|
|
8
|
+
*/
|
|
22
9
|
enabled: boolean
|
|
23
10
|
advertise: RelayAdvertiseConfig
|
|
24
11
|
hop: HopConfig
|
|
25
|
-
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export interface HopConfig {
|
|
29
|
-
enabled?: boolean
|
|
30
|
-
active?: boolean
|
|
31
|
-
timeout: number
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const log = logger('libp2p:relay')
|
|
35
|
-
|
|
36
|
-
export interface RelayAdvertiseConfig {
|
|
37
|
-
bootDelay?: number
|
|
38
|
-
enabled?: boolean
|
|
39
|
-
ttl?: number
|
|
12
|
+
reservationManager: RelayReservationManagerConfig
|
|
40
13
|
}
|
|
41
14
|
|
|
42
|
-
|
|
15
|
+
/**
|
|
16
|
+
* RelayReservationManagerConfig allows the node to automatically listen
|
|
17
|
+
* on any discovered relays upto a specified maximum.
|
|
18
|
+
*/
|
|
19
|
+
export interface RelayReservationManagerConfig {
|
|
20
|
+
/**
|
|
21
|
+
* enable or disable autorelay (default: false)
|
|
22
|
+
*/
|
|
43
23
|
enabled?: boolean
|
|
44
24
|
|
|
45
25
|
/**
|
|
46
|
-
* maximum number of relays to listen
|
|
26
|
+
* maximum number of relays to listen (default: 1)
|
|
47
27
|
*/
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export interface RelayInit extends RelayConfig {
|
|
52
|
-
addressSorter?: AddressSorter
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export interface RelayComponents {
|
|
56
|
-
peerId: PeerId
|
|
57
|
-
contentRouting: ContentRouting
|
|
58
|
-
peerStore: PeerStore
|
|
59
|
-
connectionManager: ConnectionManager
|
|
60
|
-
transportManager: TransportManager
|
|
28
|
+
maxReservations?: number
|
|
61
29
|
}
|
|
62
30
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
private readonly autoRelay?: AutoRelay
|
|
68
|
-
private timeout?: any
|
|
69
|
-
private started: boolean
|
|
70
|
-
|
|
31
|
+
/**
|
|
32
|
+
* Configures using the node as a HOP relay
|
|
33
|
+
*/
|
|
34
|
+
export interface HopConfig {
|
|
71
35
|
/**
|
|
72
|
-
*
|
|
36
|
+
* If true this node will function as a limited relay (default: false)
|
|
73
37
|
*/
|
|
74
|
-
|
|
75
|
-
this.components = components
|
|
76
|
-
// Create autoRelay if enabled
|
|
77
|
-
this.autoRelay = init.autoRelay?.enabled !== false
|
|
78
|
-
? new AutoRelay(components, {
|
|
79
|
-
addressSorter: init.addressSorter,
|
|
80
|
-
...init.autoRelay
|
|
81
|
-
})
|
|
82
|
-
: undefined
|
|
83
|
-
|
|
84
|
-
this.started = false
|
|
85
|
-
this.init = init
|
|
86
|
-
this._advertiseService = this._advertiseService.bind(this)
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
isStarted () {
|
|
90
|
-
return this.started
|
|
91
|
-
}
|
|
38
|
+
enabled?: boolean
|
|
92
39
|
|
|
93
40
|
/**
|
|
94
|
-
*
|
|
41
|
+
* timeout for hop requests to complete
|
|
95
42
|
*/
|
|
96
|
-
|
|
97
|
-
// Advertise service if HOP enabled
|
|
98
|
-
if (this.init.hop.enabled !== false && this.init.advertise.enabled !== false) {
|
|
99
|
-
this.timeout = setDelayedInterval(
|
|
100
|
-
this._advertiseService, this.init.advertise.ttl, this.init.advertise.bootDelay
|
|
101
|
-
)
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
this.started = true
|
|
105
|
-
}
|
|
43
|
+
timeout: number
|
|
106
44
|
|
|
107
45
|
/**
|
|
108
|
-
*
|
|
46
|
+
* If false, no connection limits will be applied to relayed connections (default: true)
|
|
109
47
|
*/
|
|
110
|
-
|
|
111
|
-
if (this.timeout != null) {
|
|
112
|
-
clearDelayedInterval(this.timeout)
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
this.started = false
|
|
116
|
-
}
|
|
48
|
+
applyConnectionLimits?: boolean
|
|
117
49
|
|
|
118
50
|
/**
|
|
119
|
-
*
|
|
51
|
+
* Limits to apply to incoming relay connections - relayed connections will be closed if
|
|
52
|
+
* these limits are exceeded.
|
|
120
53
|
*/
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
log.error(err)
|
|
132
|
-
}
|
|
133
|
-
}
|
|
54
|
+
limit?: {
|
|
55
|
+
/**
|
|
56
|
+
* How long to relay a connection for in milliseconds (default: 2m)
|
|
57
|
+
*/
|
|
58
|
+
duration?: number
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* How many bytes to allow to be transferred over a relayed connection (default: 128k)
|
|
62
|
+
*/
|
|
63
|
+
data?: bigint
|
|
134
64
|
}
|
|
135
65
|
}
|
|
66
|
+
|
|
67
|
+
export interface RelayAdvertiseConfig {
|
|
68
|
+
bootDelay?: number
|
|
69
|
+
enabled?: boolean
|
|
70
|
+
ttl?: number
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export { Relay } from './relay.js'
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
2
|
+
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
3
|
+
import type { Limit, Status } from './pb/index.js'
|
|
4
|
+
|
|
5
|
+
export type ReservationStatus = Status.OK | Status.PERMISSION_DENIED | Status.RESERVATION_REFUSED
|
|
6
|
+
|
|
7
|
+
export interface Reservation {
|
|
8
|
+
addr: Multiaddr
|
|
9
|
+
expire: Date
|
|
10
|
+
limit?: Limit
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface ReservationStore {
|
|
14
|
+
reserve: (peer: PeerId, addr: Multiaddr, limit?: Limit) => {status: ReservationStatus, expire?: number}
|
|
15
|
+
removeReservation: (peer: PeerId) => void
|
|
16
|
+
hasReservation: (dst: PeerId) => boolean
|
|
17
|
+
get: (peer: PeerId) => Reservation | undefined
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export type AclStatus = Status.OK | Status.RESOURCE_LIMIT_EXCEEDED | Status.PERMISSION_DENIED
|
|
21
|
+
|
|
22
|
+
export interface Acl {
|
|
23
|
+
allowReserve: (peer: PeerId, addr: Multiaddr) => Promise<boolean>
|
|
24
|
+
/**
|
|
25
|
+
* Checks if connection should be allowed
|
|
26
|
+
*/
|
|
27
|
+
allowConnect: (src: PeerId, addr: Multiaddr, dst: PeerId) => Promise<AclStatus>
|
|
28
|
+
}
|
|
@@ -1,42 +1,67 @@
|
|
|
1
1
|
syntax = "proto3";
|
|
2
2
|
|
|
3
|
-
message
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
HOP_DST_ADDR_TOO_LONG = 221;
|
|
9
|
-
HOP_SRC_MULTIADDR_INVALID = 250;
|
|
10
|
-
HOP_DST_MULTIADDR_INVALID = 251;
|
|
11
|
-
HOP_NO_CONN_TO_DST = 260;
|
|
12
|
-
HOP_CANT_DIAL_DST = 261;
|
|
13
|
-
HOP_CANT_OPEN_DST_STREAM = 262;
|
|
14
|
-
HOP_CANT_SPEAK_RELAY = 270;
|
|
15
|
-
HOP_CANT_RELAY_TO_SELF = 280;
|
|
16
|
-
STOP_SRC_ADDR_TOO_LONG = 320;
|
|
17
|
-
STOP_DST_ADDR_TOO_LONG = 321;
|
|
18
|
-
STOP_SRC_MULTIADDR_INVALID = 350;
|
|
19
|
-
STOP_DST_MULTIADDR_INVALID = 351;
|
|
20
|
-
STOP_RELAY_REFUSED = 390;
|
|
21
|
-
MALFORMED_MESSAGE = 400;
|
|
3
|
+
message HopMessage {
|
|
4
|
+
enum Type {
|
|
5
|
+
RESERVE = 0;
|
|
6
|
+
CONNECT = 1;
|
|
7
|
+
STATUS = 2;
|
|
22
8
|
}
|
|
23
9
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
10
|
+
// the presence of this field is enforced at application level
|
|
11
|
+
optional Type type = 1;
|
|
12
|
+
|
|
13
|
+
optional Peer peer = 2;
|
|
14
|
+
optional Reservation reservation = 3;
|
|
15
|
+
optional Limit limit = 4;
|
|
16
|
+
|
|
17
|
+
optional Status status = 5;
|
|
18
|
+
}
|
|
30
19
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
20
|
+
message StopMessage {
|
|
21
|
+
enum Type {
|
|
22
|
+
CONNECT = 0;
|
|
23
|
+
STATUS = 1;
|
|
34
24
|
}
|
|
35
25
|
|
|
36
|
-
|
|
26
|
+
// the presence of this field is enforced at application level
|
|
27
|
+
optional Type type = 1;
|
|
28
|
+
|
|
29
|
+
optional Peer peer = 2;
|
|
30
|
+
optional Limit limit = 3;
|
|
31
|
+
|
|
32
|
+
optional Status status = 4;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
message Peer {
|
|
36
|
+
bytes id = 1;
|
|
37
|
+
repeated bytes addrs = 2;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
message Reservation {
|
|
41
|
+
uint64 expire = 1; // Unix expiration time (UTC)
|
|
42
|
+
repeated bytes addrs = 2; // relay addrs for reserving peer
|
|
43
|
+
optional bytes voucher = 3; // reservation voucher
|
|
44
|
+
}
|
|
37
45
|
|
|
38
|
-
|
|
39
|
-
optional
|
|
46
|
+
message Limit {
|
|
47
|
+
optional uint32 duration = 1; // seconds
|
|
48
|
+
optional uint64 data = 2; // bytes
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
enum Status {
|
|
52
|
+
UNUSED = 0;
|
|
53
|
+
OK = 100;
|
|
54
|
+
RESERVATION_REFUSED = 200;
|
|
55
|
+
RESOURCE_LIMIT_EXCEEDED = 201;
|
|
56
|
+
PERMISSION_DENIED = 202;
|
|
57
|
+
CONNECTION_FAILED = 203;
|
|
58
|
+
NO_RESERVATION = 204;
|
|
59
|
+
MALFORMED_MESSAGE = 400;
|
|
60
|
+
UNEXPECTED_MESSAGE = 401;
|
|
61
|
+
}
|
|
40
62
|
|
|
41
|
-
|
|
63
|
+
message ReservationVoucher {
|
|
64
|
+
bytes relay = 1;
|
|
65
|
+
bytes peer = 2;
|
|
66
|
+
uint64 expiration = 3;
|
|
42
67
|
}
|