libp2p 0.42.2-593deefe → 0.42.2-75d5a36f
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 +30 -30
- package/dist/src/circuit/constants.d.ts +29 -3
- package/dist/src/circuit/constants.d.ts.map +1 -1
- package/dist/src/circuit/constants.js +32 -5
- package/dist/src/circuit/constants.js.map +1 -1
- package/dist/src/circuit/index.d.ts +16 -61
- package/dist/src/circuit/index.d.ts.map +1 -1
- package/dist/src/circuit/index.js +2 -1
- package/dist/src/circuit/index.js.map +1 -1
- package/dist/src/circuit/server/advert-service.d.ts +43 -0
- package/dist/src/circuit/server/advert-service.d.ts.map +1 -0
- package/dist/src/circuit/server/advert-service.js +69 -0
- package/dist/src/circuit/server/advert-service.js.map +1 -0
- package/dist/src/circuit/server/index.d.ts +60 -0
- package/dist/src/circuit/server/index.d.ts.map +1 -0
- package/dist/src/circuit/server/index.js +287 -0
- package/dist/src/circuit/server/index.js.map +1 -0
- package/dist/src/circuit/{reservation-store.d.ts → server/reservation-store.d.ts} +17 -10
- package/dist/src/circuit/server/reservation-store.d.ts.map +1 -0
- package/dist/src/circuit/server/reservation-store.js +57 -0
- package/dist/src/circuit/server/reservation-store.js.map +1 -0
- package/dist/src/circuit/server/reservation-voucher.d.ts.map +1 -0
- package/dist/src/circuit/{reservation-voucher.js → server/reservation-voucher.js} +1 -1
- package/dist/src/circuit/server/reservation-voucher.js.map +1 -0
- package/dist/src/circuit/transport/discovery.d.ts +45 -0
- package/dist/src/circuit/transport/discovery.d.ts.map +1 -0
- package/dist/src/circuit/transport/discovery.js +82 -0
- package/dist/src/circuit/transport/discovery.js.map +1 -0
- package/dist/src/circuit/transport/index.d.ts +33 -0
- package/dist/src/circuit/transport/index.d.ts.map +1 -0
- package/dist/src/circuit/transport/index.js +225 -0
- package/dist/src/circuit/transport/index.js.map +1 -0
- package/dist/src/circuit/transport/listener.d.ts +9 -0
- package/dist/src/circuit/transport/listener.d.ts.map +1 -0
- package/dist/src/circuit/transport/listener.js +76 -0
- package/dist/src/circuit/transport/listener.js.map +1 -0
- package/dist/src/circuit/transport/reservation-store.d.ts +60 -0
- package/dist/src/circuit/transport/reservation-store.d.ts.map +1 -0
- package/dist/src/circuit/transport/reservation-store.js +165 -0
- package/dist/src/circuit/transport/reservation-store.js.map +1 -0
- package/dist/src/circuit/utils.d.ts +4 -2
- package/dist/src/circuit/utils.d.ts.map +1 -1
- package/dist/src/circuit/utils.js +75 -102
- package/dist/src/circuit/utils.js.map +1 -1
- package/dist/src/components.d.ts +2 -1
- package/dist/src/components.d.ts.map +1 -1
- package/dist/src/components.js.map +1 -1
- package/dist/src/config.d.ts.map +1 -1
- package/dist/src/config.js +7 -18
- package/dist/src/config.js.map +1 -1
- package/dist/src/connection-manager/dialer/index.d.ts +2 -1
- package/dist/src/connection-manager/dialer/index.d.ts.map +1 -1
- package/dist/src/connection-manager/dialer/index.js +3 -3
- package/dist/src/connection-manager/dialer/index.js.map +1 -1
- package/dist/src/index.d.ts +5 -4
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/libp2p.d.ts +2 -2
- package/dist/src/libp2p.d.ts.map +1 -1
- package/dist/src/libp2p.js +7 -19
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/transport-manager.js +1 -1
- package/dist/src/transport-manager.js.map +1 -1
- package/dist/src/upgrader.d.ts +2 -1
- package/dist/src/upgrader.d.ts.map +1 -1
- package/dist/src/upgrader.js +10 -10
- package/dist/src/upgrader.js.map +1 -1
- package/package.json +11 -5
- package/src/circuit/constants.ts +40 -5
- package/src/circuit/index.ts +17 -67
- package/src/circuit/server/advert-service.ts +108 -0
- package/src/circuit/server/index.ts +420 -0
- package/src/circuit/server/reservation-store.ts +111 -0
- package/src/circuit/{reservation-voucher.ts → server/reservation-voucher.ts} +3 -3
- package/src/circuit/transport/discovery.ts +127 -0
- package/src/circuit/transport/index.ts +325 -0
- package/src/circuit/transport/listener.ts +99 -0
- package/src/circuit/transport/reservation-store.ts +249 -0
- package/src/circuit/utils.ts +78 -115
- package/src/components.ts +2 -1
- package/src/config.ts +7 -18
- package/src/connection-manager/dialer/index.ts +5 -4
- package/src/index.ts +5 -4
- package/src/libp2p.ts +11 -24
- package/src/transport-manager.ts +1 -1
- package/src/upgrader.ts +12 -11
- package/src/version.ts +1 -1
- package/dist/src/circuit/client.d.ts +0 -81
- package/dist/src/circuit/client.d.ts.map +0 -1
- package/dist/src/circuit/client.js +0 -285
- package/dist/src/circuit/client.js.map +0 -1
- package/dist/src/circuit/hop.d.ts +0 -23
- package/dist/src/circuit/hop.d.ts.map +0 -1
- package/dist/src/circuit/hop.js +0 -167
- package/dist/src/circuit/hop.js.map +0 -1
- package/dist/src/circuit/interfaces.d.ts +0 -27
- package/dist/src/circuit/interfaces.d.ts.map +0 -1
- package/dist/src/circuit/interfaces.js +0 -2
- package/dist/src/circuit/interfaces.js.map +0 -1
- package/dist/src/circuit/listener.d.ts +0 -9
- package/dist/src/circuit/listener.d.ts.map +0 -1
- package/dist/src/circuit/listener.js +0 -62
- package/dist/src/circuit/listener.js.map +0 -1
- package/dist/src/circuit/multicodec.d.ts +0 -3
- package/dist/src/circuit/multicodec.d.ts.map +0 -1
- package/dist/src/circuit/multicodec.js +0 -3
- package/dist/src/circuit/multicodec.js.map +0 -1
- package/dist/src/circuit/relay.d.ts +0 -34
- package/dist/src/circuit/relay.d.ts.map +0 -1
- package/dist/src/circuit/relay.js +0 -61
- package/dist/src/circuit/relay.js.map +0 -1
- package/dist/src/circuit/reservation-store.d.ts.map +0 -1
- package/dist/src/circuit/reservation-store.js +0 -59
- package/dist/src/circuit/reservation-store.js.map +0 -1
- package/dist/src/circuit/reservation-voucher.d.ts.map +0 -1
- package/dist/src/circuit/reservation-voucher.js.map +0 -1
- package/dist/src/circuit/stop.d.ts +0 -19
- package/dist/src/circuit/stop.d.ts.map +0 -1
- package/dist/src/circuit/stop.js +0 -67
- package/dist/src/circuit/stop.js.map +0 -1
- package/dist/src/circuit/transport.d.ts +0 -66
- package/dist/src/circuit/transport.d.ts.map +0 -1
- package/dist/src/circuit/transport.js +0 -238
- package/dist/src/circuit/transport.js.map +0 -1
- package/src/circuit/client.ts +0 -381
- package/src/circuit/hop.ts +0 -210
- package/src/circuit/interfaces.ts +0 -28
- package/src/circuit/listener.ts +0 -83
- package/src/circuit/multicodec.ts +0 -3
- package/src/circuit/relay.ts +0 -87
- package/src/circuit/reservation-store.ts +0 -106
- package/src/circuit/stop.ts +0 -92
- package/src/circuit/transport.ts +0 -313
- /package/dist/src/circuit/{reservation-voucher.d.ts → server/reservation-voucher.d.ts} +0 -0
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
import { logger } from '@libp2p/logger';
|
|
2
|
+
import { createLimitedRelay } from '../utils.js';
|
|
3
|
+
import { CIRCUIT_PROTO_CODE, DEFAULT_HOP_TIMEOUT, RELAY_SOURCE_TAG, RELAY_V2_HOP_CODEC, RELAY_V2_STOP_CODEC } from '../constants.js';
|
|
4
|
+
import { ReservationStore } from './reservation-store.js';
|
|
5
|
+
import { AdvertService } from './advert-service.js';
|
|
6
|
+
import pDefer from 'p-defer';
|
|
7
|
+
import { pbStream } from 'it-pb-stream';
|
|
8
|
+
import { HopMessage, Status, StopMessage } from '../pb/index.js';
|
|
9
|
+
import { multiaddr } from '@multiformats/multiaddr';
|
|
10
|
+
import { peerIdFromBytes } from '@libp2p/peer-id';
|
|
11
|
+
import { RecordEnvelope } from '@libp2p/peer-record';
|
|
12
|
+
import { ReservationVoucherRecord } from './reservation-voucher.js';
|
|
13
|
+
import { CustomEvent, EventEmitter } from '@libp2p/interfaces/events';
|
|
14
|
+
import { setMaxListeners } from 'events';
|
|
15
|
+
const log = logger('libp2p:circuit-relay:server');
|
|
16
|
+
const isRelayAddr = (ma) => ma.protoCodes().includes(CIRCUIT_PROTO_CODE);
|
|
17
|
+
class CircuitRelayServer extends EventEmitter {
|
|
18
|
+
/**
|
|
19
|
+
* Creates an instance of Relay
|
|
20
|
+
*/
|
|
21
|
+
constructor(components, init = {}) {
|
|
22
|
+
super();
|
|
23
|
+
this.registrar = components.registrar;
|
|
24
|
+
this.peerStore = components.peerStore;
|
|
25
|
+
this.addressManager = components.addressManager;
|
|
26
|
+
this.peerId = components.peerId;
|
|
27
|
+
this.connectionManager = components.connectionManager;
|
|
28
|
+
this.connectionGater = components.connectionGater;
|
|
29
|
+
this.started = false;
|
|
30
|
+
this.hopTimeout = init?.hopTimeout ?? DEFAULT_HOP_TIMEOUT;
|
|
31
|
+
this.shutdownController = new AbortController();
|
|
32
|
+
this.maxInboundHopStreams = init.maxInboundHopStreams;
|
|
33
|
+
this.maxOutboundHopStreams = init.maxOutboundHopStreams;
|
|
34
|
+
try {
|
|
35
|
+
// fails on node < 15.4
|
|
36
|
+
setMaxListeners?.(Infinity, this.shutdownController.signal);
|
|
37
|
+
}
|
|
38
|
+
catch { }
|
|
39
|
+
if (init.advertise != null && init.advertise !== false) {
|
|
40
|
+
this.advertService = new AdvertService(components, init.advertise === true ? undefined : init.advertise);
|
|
41
|
+
this.advertService.addEventListener('advert:success', () => {
|
|
42
|
+
this.dispatchEvent(new CustomEvent('relay:advert:success'));
|
|
43
|
+
});
|
|
44
|
+
this.advertService.addEventListener('advert:error', (evt) => {
|
|
45
|
+
this.safeDispatchEvent('relay:advert:error', { detail: evt.detail });
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
this.reservationStore = new ReservationStore(init.reservations);
|
|
49
|
+
}
|
|
50
|
+
isStarted() {
|
|
51
|
+
return this.started;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Start Relay service
|
|
55
|
+
*/
|
|
56
|
+
async start() {
|
|
57
|
+
if (this.started) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
// Advertise service if HOP enabled and advertising enabled
|
|
61
|
+
this.advertService?.start();
|
|
62
|
+
await this.registrar.handle(RELAY_V2_HOP_CODEC, (data) => {
|
|
63
|
+
void this.onHop(data).catch(err => {
|
|
64
|
+
log.error(err);
|
|
65
|
+
});
|
|
66
|
+
}, {
|
|
67
|
+
maxInboundStreams: this.maxInboundHopStreams,
|
|
68
|
+
maxOutboundStreams: this.maxOutboundHopStreams
|
|
69
|
+
});
|
|
70
|
+
this.reservationStore.start();
|
|
71
|
+
this.started = true;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Stop Relay service
|
|
75
|
+
*/
|
|
76
|
+
async stop() {
|
|
77
|
+
this.advertService?.stop();
|
|
78
|
+
this.reservationStore.stop();
|
|
79
|
+
this.shutdownController.abort();
|
|
80
|
+
await this.registrar.unhandle(RELAY_V2_HOP_CODEC);
|
|
81
|
+
this.started = false;
|
|
82
|
+
}
|
|
83
|
+
async onHop({ connection, stream }) {
|
|
84
|
+
log('received circuit v2 hop protocol stream from %s', connection.remotePeer);
|
|
85
|
+
const hopTimeoutPromise = pDefer();
|
|
86
|
+
const timeout = setTimeout(() => {
|
|
87
|
+
hopTimeoutPromise.reject('timed out');
|
|
88
|
+
}, this.hopTimeout);
|
|
89
|
+
const pbstr = pbStream(stream);
|
|
90
|
+
try {
|
|
91
|
+
const request = await Promise.race([
|
|
92
|
+
pbstr.pb(HopMessage).read(),
|
|
93
|
+
hopTimeoutPromise.promise
|
|
94
|
+
]);
|
|
95
|
+
if (request?.type == null) {
|
|
96
|
+
throw new Error('request was invalid, could not read from stream');
|
|
97
|
+
}
|
|
98
|
+
log('received', request.type);
|
|
99
|
+
await Promise.race([
|
|
100
|
+
this.handleHopProtocol({
|
|
101
|
+
connection,
|
|
102
|
+
stream: pbstr,
|
|
103
|
+
request
|
|
104
|
+
}),
|
|
105
|
+
hopTimeoutPromise.promise
|
|
106
|
+
]);
|
|
107
|
+
}
|
|
108
|
+
catch (err) {
|
|
109
|
+
log.error('error while handling hop', err);
|
|
110
|
+
pbstr.pb(HopMessage).write({
|
|
111
|
+
type: HopMessage.Type.STATUS,
|
|
112
|
+
status: Status.MALFORMED_MESSAGE
|
|
113
|
+
});
|
|
114
|
+
stream.abort(err);
|
|
115
|
+
}
|
|
116
|
+
finally {
|
|
117
|
+
clearTimeout(timeout);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
async handleHopProtocol({ stream, request, connection }) {
|
|
121
|
+
log('received hop message');
|
|
122
|
+
switch (request.type) {
|
|
123
|
+
case HopMessage.Type.RESERVE:
|
|
124
|
+
await this.handleReserve({ stream, request, connection });
|
|
125
|
+
break;
|
|
126
|
+
case HopMessage.Type.CONNECT:
|
|
127
|
+
await this.handleConnect({ stream, request, connection });
|
|
128
|
+
break;
|
|
129
|
+
default: {
|
|
130
|
+
log.error('invalid hop request type %s via peer %s', request.type, connection.remotePeer);
|
|
131
|
+
stream.pb(HopMessage).write({ type: HopMessage.Type.STATUS, status: Status.UNEXPECTED_MESSAGE });
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
async handleReserve({ stream, request, connection }) {
|
|
136
|
+
const hopstr = stream.pb(HopMessage);
|
|
137
|
+
log('hop reserve request from %s', connection.remotePeer);
|
|
138
|
+
if (isRelayAddr(connection.remoteAddr)) {
|
|
139
|
+
log.error('relay reservation over circuit connection denied for peer: %p', connection.remotePeer);
|
|
140
|
+
hopstr.write({ type: HopMessage.Type.STATUS, status: Status.PERMISSION_DENIED });
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
if ((await this.connectionGater.denyInboundRelayReservation?.(connection.remotePeer)) === true) {
|
|
144
|
+
log.error('reservation for %p denied by connection gater', connection.remotePeer);
|
|
145
|
+
hopstr.write({ type: HopMessage.Type.STATUS, status: Status.PERMISSION_DENIED });
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
const result = await this.reservationStore.reserve(connection.remotePeer, connection.remoteAddr);
|
|
149
|
+
if (result.status !== Status.OK) {
|
|
150
|
+
hopstr.write({ type: HopMessage.Type.STATUS, status: result.status });
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
try {
|
|
154
|
+
// tag relay target peer
|
|
155
|
+
// result.expire is non-null if `ReservationStore.reserve` returns with status == OK
|
|
156
|
+
if (result.expire != null) {
|
|
157
|
+
const ttl = new Date().getTime() - result.expire;
|
|
158
|
+
await this.peerStore.tagPeer(connection.remotePeer, RELAY_SOURCE_TAG, { value: 1, ttl });
|
|
159
|
+
}
|
|
160
|
+
hopstr.write({
|
|
161
|
+
type: HopMessage.Type.STATUS,
|
|
162
|
+
status: Status.OK,
|
|
163
|
+
reservation: await this.makeReservation(connection.remotePeer, BigInt(result.expire ?? 0)),
|
|
164
|
+
limit: (await this.reservationStore.get(connection.remotePeer))?.limit
|
|
165
|
+
});
|
|
166
|
+
log('sent confirmation response to %s', connection.remotePeer);
|
|
167
|
+
}
|
|
168
|
+
catch (err) {
|
|
169
|
+
log.error('failed to send confirmation response to %p', connection.remotePeer, err);
|
|
170
|
+
await this.reservationStore.removeReservation(connection.remotePeer);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
async makeReservation(remotePeer, expire) {
|
|
174
|
+
const addrs = [];
|
|
175
|
+
for (const relayAddr of this.addressManager.getAddresses()) {
|
|
176
|
+
addrs.push(relayAddr.bytes);
|
|
177
|
+
}
|
|
178
|
+
const voucher = await RecordEnvelope.seal(new ReservationVoucherRecord({
|
|
179
|
+
peer: remotePeer,
|
|
180
|
+
relay: this.peerId,
|
|
181
|
+
expiration: Number(expire)
|
|
182
|
+
}), this.peerId);
|
|
183
|
+
return {
|
|
184
|
+
addrs,
|
|
185
|
+
expire,
|
|
186
|
+
voucher: voucher.marshal()
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
async handleConnect({ stream, request, connection }) {
|
|
190
|
+
const hopstr = stream.pb(HopMessage);
|
|
191
|
+
if (isRelayAddr(connection.remoteAddr)) {
|
|
192
|
+
log.error('relay reservation over circuit connection denied for peer: %p', connection.remotePeer);
|
|
193
|
+
hopstr.write({ type: HopMessage.Type.STATUS, status: Status.PERMISSION_DENIED });
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
log('hop connect request from %s', connection.remotePeer);
|
|
197
|
+
let dstPeer;
|
|
198
|
+
try {
|
|
199
|
+
if (request.peer == null) {
|
|
200
|
+
log.error('no peer info in hop connect request');
|
|
201
|
+
throw new Error('no peer info in request');
|
|
202
|
+
}
|
|
203
|
+
request.peer.addrs.forEach(multiaddr);
|
|
204
|
+
dstPeer = peerIdFromBytes(request.peer.id);
|
|
205
|
+
}
|
|
206
|
+
catch (err) {
|
|
207
|
+
log.error('invalid hop connect request via peer %p %s', connection.remotePeer, err);
|
|
208
|
+
hopstr.write({ type: HopMessage.Type.STATUS, status: Status.MALFORMED_MESSAGE });
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
if (!await this.reservationStore.hasReservation(dstPeer)) {
|
|
212
|
+
log.error('hop connect denied for destination peer %p not having a reservation for %p with status %s', dstPeer, connection.remotePeer, Status.NO_RESERVATION);
|
|
213
|
+
hopstr.write({ type: HopMessage.Type.STATUS, status: Status.NO_RESERVATION });
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
if ((await this.connectionGater.denyOutboundRelayedConnection?.(connection.remotePeer, dstPeer)) === true) {
|
|
217
|
+
log.error('hop connect for %p to %p denied by connection gater', connection.remotePeer, dstPeer);
|
|
218
|
+
hopstr.write({ type: HopMessage.Type.STATUS, status: Status.PERMISSION_DENIED });
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
const connections = this.connectionManager.getConnections(dstPeer);
|
|
222
|
+
if (connections.length === 0) {
|
|
223
|
+
log('hop connect denied for destination peer %p not having a connection for %p as there is no destination connection', dstPeer, connection.remotePeer);
|
|
224
|
+
hopstr.write({ type: HopMessage.Type.STATUS, status: Status.NO_RESERVATION });
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
const destinationConnection = connections[0];
|
|
228
|
+
const destinationStream = await this.stopHop({
|
|
229
|
+
connection: destinationConnection,
|
|
230
|
+
request: {
|
|
231
|
+
type: StopMessage.Type.CONNECT,
|
|
232
|
+
peer: {
|
|
233
|
+
id: connection.remotePeer.toBytes(),
|
|
234
|
+
addrs: []
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
});
|
|
238
|
+
if (destinationStream == null) {
|
|
239
|
+
log.error('failed to open stream to destination peer %s', destinationConnection?.remotePeer);
|
|
240
|
+
hopstr.write({ type: HopMessage.Type.STATUS, status: Status.CONNECTION_FAILED });
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
hopstr.write({ type: HopMessage.Type.STATUS, status: Status.OK });
|
|
244
|
+
const sourceStream = stream.unwrap();
|
|
245
|
+
log('connection from %p to %p established - merging streans', connection.remotePeer, dstPeer);
|
|
246
|
+
const limit = (await this.reservationStore.get(dstPeer))?.limit;
|
|
247
|
+
// Short circuit the two streams to create the relayed connection
|
|
248
|
+
createLimitedRelay(sourceStream, destinationStream, this.shutdownController.signal, limit);
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Send a STOP request to the target peer that the dialing peer wants to contact
|
|
252
|
+
*/
|
|
253
|
+
async stopHop({ connection, request }) {
|
|
254
|
+
log('starting circuit relay v2 stop request to %s', connection.remotePeer);
|
|
255
|
+
const stream = await connection.newStream([RELAY_V2_STOP_CODEC]);
|
|
256
|
+
const pbstr = pbStream(stream);
|
|
257
|
+
const stopstr = pbstr.pb(StopMessage);
|
|
258
|
+
stopstr.write(request);
|
|
259
|
+
let response;
|
|
260
|
+
try {
|
|
261
|
+
response = await stopstr.read();
|
|
262
|
+
}
|
|
263
|
+
catch (err) {
|
|
264
|
+
log.error('error parsing stop message response from %s', connection.remotePeer);
|
|
265
|
+
}
|
|
266
|
+
if (response == null) {
|
|
267
|
+
log.error('could not read response from %s', connection.remotePeer);
|
|
268
|
+
stream.close();
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
if (response.status === Status.OK) {
|
|
272
|
+
log('stop request to %s was successful', connection.remotePeer);
|
|
273
|
+
return pbstr.unwrap();
|
|
274
|
+
}
|
|
275
|
+
log('stop request failed with code %d', response.status);
|
|
276
|
+
stream.close();
|
|
277
|
+
}
|
|
278
|
+
get reservations() {
|
|
279
|
+
return this.reservationStore.reservations;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
export function circuitRelayServer(init = {}) {
|
|
283
|
+
return (components) => {
|
|
284
|
+
return new CircuitRelayServer(components, init);
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/circuit/server/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AACvC,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAA;AAChD,OAAO,EACL,kBAAkB,EAClB,mBAAmB,EACnB,gBAAgB,EACd,kBAAkB,EAAE,mBAAmB,EAC1C,MAAM,iBAAiB,CAAA;AAGxB,OAAO,EAAE,gBAAgB,EAAwB,MAAM,wBAAwB,CAAA;AAE/E,OAAO,EAAE,aAAa,EAA8C,MAAM,qBAAqB,CAAA;AAC/F,OAAO,MAAM,MAAM,SAAS,CAAA;AAC5B,OAAO,EAAE,QAAQ,EAAkB,MAAM,cAAc,CAAA;AACvD,OAAO,EAAE,UAAU,EAAe,MAAM,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAC7E,OAAO,EAAa,SAAS,EAAE,MAAM,yBAAyB,CAAA;AAG9D,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AAEjD,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAA;AACpD,OAAO,EAAE,wBAAwB,EAAE,MAAM,0BAA0B,CAAA;AAInE,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAA;AACrE,OAAO,EAAE,eAAe,EAAE,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AA;QACrC,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,CAAA;QACtB,IAAI,QAAQ,CAAA;QAEZ,IAAI;YACF,QAAQ,GAAG,MAAM,OAAO,CAAC,IAAI,EAAE,CAAA;SAChC;QAAC,OAAO,GAAG,EAAE;YACZ,GAAG,CAAC,KAAK,CAAC,6CAA6C,EAAE,UAAU,CAAC,UAAU,CAAC,CAAA;SAChF;QAED,IAAI,QAAQ,IAAI,IAAI,EAAE;YACpB,GAAG,CAAC,KAAK,CAAC,iCAAiC,EAAE,UAAU,CAAC,UAAU,CAAC,CAAA;YACnE,MAAM,CAAC,KAAK,EAAE,CAAA;YACd,OAAM;SACP;QAED,IAAI,QAAQ,CAAC,MAAM,KAAK,MAAM,CAAC,EAAE,EAAE;YACjC,GAAG,CAAC,mCAAmC,EAAE,UAAU,CAAC,UAAU,CAAC,CAAA;YAC/D,OAAO,KAAK,CAAC,MAAM,EAAE,CAAA;SACtB;QAED,GAAG,CAAC,kCAAkC,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAA;QACxD,MAAM,CAAC,KAAK,EAAE,CAAA;IAChB,CAAC;IAED,IAAI,YAAY;QACd,OAAO,IAAI,CAAC,gBAAgB,CAAC,YAAY,CAAA;IAC3C,CAAC;CACF;AAED,MAAM,UAAU,kBAAkB,CAAE,OAA+B,EAAE;IACnE,OAAO,CAAC,UAAU,EAAE,EAAE;QACpB,OAAO,IAAI,kBAAkB,CAAC,UAAU,EAAE,IAAI,CAAC,CAAA;IACjD,CAAC,CAAA;AACH,CAAC"}
|
|
@@ -1,17 +1,19 @@
|
|
|
1
|
-
import { Limit } from '
|
|
2
|
-
import type { ReservationStore as IReservationStore, ReservationStatus, Reservation } from './interfaces.js';
|
|
1
|
+
import { Limit, Status } from '../pb/index.js';
|
|
3
2
|
import type { Multiaddr } from '@multiformats/multiaddr';
|
|
4
3
|
import type { PeerId } from '@libp2p/interface-peer-id';
|
|
5
4
|
import type { Startable } from '@libp2p/interfaces/startable';
|
|
5
|
+
import { PeerMap } from '@libp2p/peer-collections';
|
|
6
6
|
import type { RecursivePartial } from '@libp2p/interfaces';
|
|
7
|
+
import type { RelayReservation } from '../index.js';
|
|
8
|
+
export type ReservationStatus = Status.OK | Status.PERMISSION_DENIED | Status.RESERVATION_REFUSED;
|
|
7
9
|
export interface ReservationStoreInit {
|
|
8
|
-
maxReservations
|
|
9
|
-
reservationClearInterval
|
|
10
|
-
applyDefaultLimit
|
|
10
|
+
maxReservations?: number;
|
|
11
|
+
reservationClearInterval?: number;
|
|
12
|
+
applyDefaultLimit?: boolean;
|
|
11
13
|
/**
|
|
12
14
|
* reservation ttl, default: 2 hours
|
|
13
15
|
*/
|
|
14
|
-
reservationTtl
|
|
16
|
+
reservationTtl?: number;
|
|
15
17
|
/**
|
|
16
18
|
* The maximum time a relayed connection can be open for
|
|
17
19
|
*/
|
|
@@ -22,11 +24,16 @@ export interface ReservationStoreInit {
|
|
|
22
24
|
defaultDataLimit?: bigint;
|
|
23
25
|
}
|
|
24
26
|
export type ReservationStoreOptions = RecursivePartial<ReservationStoreInit>;
|
|
25
|
-
export declare class ReservationStore implements
|
|
26
|
-
|
|
27
|
+
export declare class ReservationStore implements Startable {
|
|
28
|
+
readonly reservations: PeerMap<RelayReservation>;
|
|
27
29
|
private _started;
|
|
28
30
|
private interval;
|
|
29
|
-
private readonly
|
|
31
|
+
private readonly maxReservations;
|
|
32
|
+
private readonly reservationClearInterval;
|
|
33
|
+
private readonly applyDefaultLimit;
|
|
34
|
+
private readonly reservationTtl;
|
|
35
|
+
private readonly defaultDurationLimit;
|
|
36
|
+
private readonly defaultDataLimit;
|
|
30
37
|
constructor(options?: ReservationStoreOptions);
|
|
31
38
|
isStarted(): boolean;
|
|
32
39
|
start(): void;
|
|
@@ -37,6 +44,6 @@ export declare class ReservationStore implements IReservationStore, Startable {
|
|
|
37
44
|
};
|
|
38
45
|
removeReservation(peer: PeerId): void;
|
|
39
46
|
hasReservation(dst: PeerId): boolean;
|
|
40
|
-
get(peer: PeerId):
|
|
47
|
+
get(peer: PeerId): RelayReservation | undefined;
|
|
41
48
|
}
|
|
42
49
|
//# sourceMappingURL=reservation-store.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reservation-store.d.ts","sourceRoot":"","sources":["../../../../src/circuit/server/reservation-store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAC9C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AACxD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAA;AACvD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAA;AAC7D,OAAO,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAA;AAClD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AAE1D,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAA;AAEnD,MAAM,MAAM,iBAAiB,GAAG,MAAM,CAAC,EAAE,GAAG,MAAM,CAAC,iBAAiB,GAAG,MAAM,CAAC,mBAAmB,CAAA;AAEjG,MAAM,WAAW,oBAAoB;IAInC,eAAe,CAAC,EAAE,MAAM,CAAA;IAIxB,wBAAwB,CAAC,EAAE,MAAM,CAAA;IAIjC,iBAAiB,CAAC,EAAE,OAAO,CAAA;IAC3B;;OAEG;IACH,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB;;OAEG;IACH,oBAAoB,CAAC,EAAE,MAAM,CAAA;IAC7B;;OAEG;IACH,gBAAgB,CAAC,EAAE,MAAM,CAAA;CAC1B;AAED,MAAM,MAAM,uBAAuB,GAAG,gBAAgB,CAAC,oBAAoB,CAAC,CAAA;AAE5E,qBAAa,gBAAiB,YAAW,SAAS;IAChD,SAAgB,YAAY,4BAAkC;IAC9D,OAAO,CAAC,QAAQ,CAAQ;IACxB,OAAO,CAAC,QAAQ,CAAK;IACrB,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAQ;IACxC,OAAO,CAAC,QAAQ,CAAC,wBAAwB,CAAQ;IACjD,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CAAS;IAC3C,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAQ;IACvC,OAAO,CAAC,QAAQ,CAAC,oBAAoB,CAAQ;IAC7C,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAQ;gBAE5B,OAAO,GAAE,uBAA4B;IASlD,SAAS,IAAK,OAAO;IAIrB,KAAK,IAAK,IAAI;IAkBd,IAAI,IAAK,IAAI;IAIb,OAAO,CAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,CAAC,EAAE,KAAK,GAAG;QAAE,MAAM,EAAE,iBAAiB,CAAC;QAAC,MAAM,CAAC,EAAE,MAAM,CAAA;KAAE;IAatG,iBAAiB,CAAE,IAAI,EAAE,MAAM,GAAG,IAAI;IAItC,cAAc,CAAE,GAAG,EAAE,MAAM,GAAG,OAAO;IAIrC,GAAG,CAAE,IAAI,EAAE,MAAM,GAAG,gBAAgB,GAAG,SAAS;CAGjD"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { Status } from '../pb/index.js';
|
|
2
|
+
import { PeerMap } from '@libp2p/peer-collections';
|
|
3
|
+
import { DEFAULT_DATA_LIMIT, DEFAULT_DURATION_LIMIT, DEFAULT_MAX_RESERVATION_CLEAR_INTERVAL, DEFAULT_MAX_RESERVATION_STORE_SIZE, DEFAULT_MAX_RESERVATION_TTL } from '../constants.js';
|
|
4
|
+
export class ReservationStore {
|
|
5
|
+
constructor(options = {}) {
|
|
6
|
+
this.reservations = new PeerMap();
|
|
7
|
+
this._started = false;
|
|
8
|
+
this.maxReservations = options.maxReservations ?? DEFAULT_MAX_RESERVATION_STORE_SIZE;
|
|
9
|
+
this.reservationClearInterval = options.reservationClearInterval ?? DEFAULT_MAX_RESERVATION_CLEAR_INTERVAL;
|
|
10
|
+
this.applyDefaultLimit = options.applyDefaultLimit !== false;
|
|
11
|
+
this.reservationTtl = options.reservationTtl ?? DEFAULT_MAX_RESERVATION_TTL;
|
|
12
|
+
this.defaultDurationLimit = options.defaultDurationLimit ?? DEFAULT_DURATION_LIMIT;
|
|
13
|
+
this.defaultDataLimit = options.defaultDataLimit ?? DEFAULT_DATA_LIMIT;
|
|
14
|
+
}
|
|
15
|
+
isStarted() {
|
|
16
|
+
return this._started;
|
|
17
|
+
}
|
|
18
|
+
start() {
|
|
19
|
+
if (this._started) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
this._started = true;
|
|
23
|
+
this.interval = setInterval(() => {
|
|
24
|
+
const now = (new Date()).getTime();
|
|
25
|
+
this.reservations.forEach((r, k) => {
|
|
26
|
+
if (r.expire.getTime() < now) {
|
|
27
|
+
this.reservations.delete(k);
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
}, this.reservationClearInterval);
|
|
31
|
+
}
|
|
32
|
+
stop() {
|
|
33
|
+
clearInterval(this.interval);
|
|
34
|
+
}
|
|
35
|
+
reserve(peer, addr, limit) {
|
|
36
|
+
if (this.reservations.size >= this.maxReservations && !this.reservations.has(peer)) {
|
|
37
|
+
return { status: Status.RESERVATION_REFUSED };
|
|
38
|
+
}
|
|
39
|
+
const expire = new Date(Date.now() + this.reservationTtl);
|
|
40
|
+
let checkedLimit;
|
|
41
|
+
if (this.applyDefaultLimit) {
|
|
42
|
+
checkedLimit = limit ?? { data: this.defaultDataLimit, duration: this.defaultDurationLimit };
|
|
43
|
+
}
|
|
44
|
+
this.reservations.set(peer, { addr, expire, limit: checkedLimit });
|
|
45
|
+
return { status: Status.OK, expire: expire.getTime() };
|
|
46
|
+
}
|
|
47
|
+
removeReservation(peer) {
|
|
48
|
+
this.reservations.delete(peer);
|
|
49
|
+
}
|
|
50
|
+
hasReservation(dst) {
|
|
51
|
+
return this.reservations.has(dst);
|
|
52
|
+
}
|
|
53
|
+
get(peer) {
|
|
54
|
+
return this.reservations.get(peer);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=reservation-store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reservation-store.js","sourceRoot":"","sources":["../../../../src/circuit/server/reservation-store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAS,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAI9C,OAAO,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAA;AAElD,OAAO,EAAE,kBAAkB,EAAE,sBAAsB,EAAE,sCAAsC,EAAE,kCAAkC,EAAE,2BAA2B,EAAE,MAAM,iBAAiB,CAAA;AAkCrL,MAAM,OAAO,gBAAgB;IAW3B,YAAa,UAAmC,EAAE;QAVlC,iBAAY,GAAG,IAAI,OAAO,EAAoB,CAAA;QACtD,aAAQ,GAAG,KAAK,CAAA;QAUtB,IAAI,CAAC,eAAe,GAAG,OAAO,CAAC,eAAe,IAAI,kCAAkC,CAAA;QACpF,IAAI,CAAC,wBAAwB,GAAG,OAAO,CAAC,wBAAwB,IAAI,sCAAsC,CAAA;QAC1G,IAAI,CAAC,iBAAiB,GAAG,OAAO,CAAC,iBAAiB,KAAK,KAAK,CAAA;QAC5D,IAAI,CAAC,cAAc,GAAG,OAAO,CAAC,cAAc,IAAI,2BAA2B,CAAA;QAC3E,IAAI,CAAC,oBAAoB,GAAG,OAAO,CAAC,oBAAoB,IAAI,sBAAsB,CAAA;QAClF,IAAI,CAAC,gBAAgB,GAAG,OAAO,CAAC,gBAAgB,IAAI,kBAAkB,CAAA;IACxE,CAAC;IAED,SAAS;QACP,OAAO,IAAI,CAAC,QAAQ,CAAA;IACtB,CAAC;IAED,KAAK;QACH,IAAI,IAAI,CAAC,QAAQ,EAAE;YACjB,OAAM;SACP;QACD,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAA;QACpB,IAAI,CAAC,QAAQ,GAAG,WAAW,CACzB,GAAG,EAAE;YACH,MA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CAAE,IAAY;QAC7B,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;IAChC,CAAC;IAED,cAAc,CAAE,GAAW;QACzB,OAAO,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;IACnC,CAAC;IAED,GAAG,CAAE,IAAY;QACf,OAAO,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,IAAI,CAAC,CAAA;IACpC,CAAC;CACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reservation-voucher.d.ts","sourceRoot":"","sources":["../../../../src/circuit/server/reservation-voucher.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAA;AACvD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,0BAA0B,CAAA;AAGtD,MAAM,WAAW,yBAAyB;IACxC,KAAK,EAAE,MAAM,CAAA;IACb,IAAI,EAAE,MAAM,CAAA;IACZ,UAAU,EAAE,MAAM,CAAA;CACnB;AAED,qBAAa,wBAAyB,YAAW,MAAM;IACrD,SAAgB,MAAM,uBAAsB;IAC5C,SAAgB,KAAK,aAA+B;IAEpD,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAQ;IAC9B,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAQ;IAC7B,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAQ;gBAEtB,EAAE,KAAK,EAAE,IAAI,EAAE,UAAU,EAAE,EAAE,yBAAyB;IAMnE,OAAO,IAAK,UAAU;IAQtB,MAAM,CAAE,KAAK,EAAE,MAAM,GAAG,OAAO;CAkBhC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reservation-voucher.js","sourceRoot":"","sources":["../../../../src/circuit/server/reservation-voucher.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAA;AAQnD,MAAM,OAAO,wBAAwB;IAQnC,YAAa,EAAE,KAAK,EAAE,IAAI,EAAE,UAAU,EAA6B;QAPnD,WAAM,GAAG,mBAAmB,CAAA;QAC5B,UAAK,GAAG,IAAI,UAAU,CAAC,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAA;QAOlD,IAAI,CAAC,KAAK,GAAG,KAAK,CAAA;QAClB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;QAChB,IAAI,CAAC,UAAU,GAAG,UAAU,CAAA;IAC9B,CAAC;IAED,OAAO;QACL,OAAO,kBAAkB,CAAC,MAAM,CAAC;YAC/B,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE;YAC3B,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE;YACzB,UAAU,EAAE,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC;SACpC,CAAC,CAAA;IACJ,CAAC;IAED,MAAM,CAAE,KAAa;QACnB,IAAI,CAAC,CAAC,KAAK,YAAY,wBAAwB,CAAC,EAAE;YAChD,OAAO,KAAK,CAAA;SACb;QACD,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE;YACjC,OAAO,KAAK,CAAA;SACb;QAED,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE;YACnC,OAAO,KAAK,CAAA;SACb;QAED,IAAI,IAAI,CAAC,UAAU,KAAK,KAAK,CAAC,UAAU,EAAE;YACxC,OAAO,KAAK,CAAA;SACb;QAED,OAAO,IAAI,CAAA;IACb,CAAC;CACF"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { PeerId } from '@libp2p/interface-peer-id';
|
|
2
|
+
import type { PeerStore } from '@libp2p/interface-peer-store';
|
|
3
|
+
import { EventEmitter } from '@libp2p/interfaces/events';
|
|
4
|
+
import type { Startable } from '@libp2p/interfaces/startable';
|
|
5
|
+
import type { ConnectionManager } from '@libp2p/interface-connection-manager';
|
|
6
|
+
import type { TransportManager } from '@libp2p/interface-transport';
|
|
7
|
+
import type { ContentRouting } from '@libp2p/interface-content-routing';
|
|
8
|
+
import type { Registrar } from '@libp2p/interface-registrar';
|
|
9
|
+
export interface RelayDiscoveryEvents {
|
|
10
|
+
'relay:discover': CustomEvent<PeerId>;
|
|
11
|
+
}
|
|
12
|
+
export interface RelayDiscoveryComponents {
|
|
13
|
+
peerId: PeerId;
|
|
14
|
+
peerStore: PeerStore;
|
|
15
|
+
connectionManager: ConnectionManager;
|
|
16
|
+
transportManager: TransportManager;
|
|
17
|
+
contentRouting: ContentRouting;
|
|
18
|
+
registrar: Registrar;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* ReservationManager automatically makes a circuit v2 reservation on any connected
|
|
22
|
+
* peers that support the circuit v2 HOP protocol.
|
|
23
|
+
*/
|
|
24
|
+
export declare class RelayDiscovery extends EventEmitter<RelayDiscoveryEvents> implements Startable {
|
|
25
|
+
private readonly peerId;
|
|
26
|
+
private readonly peerStore;
|
|
27
|
+
private readonly contentRouting;
|
|
28
|
+
private readonly registrar;
|
|
29
|
+
private started;
|
|
30
|
+
private topologyId?;
|
|
31
|
+
constructor(components: RelayDiscoveryComponents);
|
|
32
|
+
isStarted(): boolean;
|
|
33
|
+
start(): Promise<void>;
|
|
34
|
+
stop(): void;
|
|
35
|
+
/**
|
|
36
|
+
* Try to listen on available hop relay connections.
|
|
37
|
+
* The following order will happen while we do not have enough relays:
|
|
38
|
+
*
|
|
39
|
+
* 1. Check the metadata store for known relays, try to listen on the ones we are already connected
|
|
40
|
+
* 2. Dial and try to listen on the peers we know that support hop but are not connected
|
|
41
|
+
* 3. Search the network
|
|
42
|
+
*/
|
|
43
|
+
discover(): Promise<void>;
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=discovery.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"discovery.d.ts","sourceRoot":"","sources":["../../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,IAAI;IAQb;;;;;;;OAOG;IACG,QAAQ,IAAK,OAAO,CAAC,IAAI,CAAC;CAqCjC"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { logger } from '@libp2p/logger';
|
|
2
|
+
import { namespaceToCid } from '../utils.js';
|
|
3
|
+
import { RELAY_RENDEZVOUS_NS, RELAY_V2_HOP_CODEC } from '../constants.js';
|
|
4
|
+
import { EventEmitter } from '@libp2p/interfaces/events';
|
|
5
|
+
import { createTopology } from '@libp2p/topology';
|
|
6
|
+
const log = logger('libp2p:circuit-relay:discover-relays');
|
|
7
|
+
/**
|
|
8
|
+
* ReservationManager automatically makes a circuit v2 reservation on any connected
|
|
9
|
+
* peers that support the circuit v2 HOP protocol.
|
|
10
|
+
*/
|
|
11
|
+
export class RelayDiscovery extends EventEmitter {
|
|
12
|
+
constructor(components) {
|
|
13
|
+
super();
|
|
14
|
+
this.started = false;
|
|
15
|
+
this.peerId = components.peerId;
|
|
16
|
+
this.peerStore = components.peerStore;
|
|
17
|
+
this.contentRouting = components.contentRouting;
|
|
18
|
+
this.registrar = components.registrar;
|
|
19
|
+
}
|
|
20
|
+
isStarted() {
|
|
21
|
+
return this.started;
|
|
22
|
+
}
|
|
23
|
+
async start() {
|
|
24
|
+
// register a topology listener for when new peers are encountered
|
|
25
|
+
// that support the hop protocol
|
|
26
|
+
this.topologyId = await this.registrar.register(RELAY_V2_HOP_CODEC, createTopology({
|
|
27
|
+
onConnect: (peerId) => {
|
|
28
|
+
this.safeDispatchEvent('relay:discover', { detail: peerId });
|
|
29
|
+
}
|
|
30
|
+
}));
|
|
31
|
+
void this.discover()
|
|
32
|
+
.catch(err => {
|
|
33
|
+
log.error('error listening on relays', err);
|
|
34
|
+
});
|
|
35
|
+
this.started = true;
|
|
36
|
+
}
|
|
37
|
+
stop() {
|
|
38
|
+
if (this.topologyId != null) {
|
|
39
|
+
this.registrar.unregister(this.topologyId);
|
|
40
|
+
}
|
|
41
|
+
this.started = false;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Try to listen on available hop relay connections.
|
|
45
|
+
* The following order will happen while we do not have enough relays:
|
|
46
|
+
*
|
|
47
|
+
* 1. Check the metadata store for known relays, try to listen on the ones we are already connected
|
|
48
|
+
* 2. Dial and try to listen on the peers we know that support hop but are not connected
|
|
49
|
+
* 3. Search the network
|
|
50
|
+
*/
|
|
51
|
+
async discover() {
|
|
52
|
+
log('searching peer store for relays');
|
|
53
|
+
const peers = (await this.peerStore.all())
|
|
54
|
+
// filter by a list of peers supporting RELAY_V2_HOP and ones we are not listening on
|
|
55
|
+
.filter(({ id, protocols }) => protocols.includes(RELAY_V2_HOP_CODEC))
|
|
56
|
+
.sort(() => Math.random() - 0.5);
|
|
57
|
+
for (const peer of peers) {
|
|
58
|
+
log('found relay peer %p in content peer store', peer.id);
|
|
59
|
+
this.safeDispatchEvent('relay:discover', { detail: peer.id });
|
|
60
|
+
}
|
|
61
|
+
log('found %d relay peers in peer store', peers.length);
|
|
62
|
+
try {
|
|
63
|
+
log('searching content routing for relays');
|
|
64
|
+
const cid = await namespaceToCid(RELAY_RENDEZVOUS_NS);
|
|
65
|
+
let found = 0;
|
|
66
|
+
for await (const provider of this.contentRouting.findProviders(cid)) {
|
|
67
|
+
if (provider.multiaddrs.length > 0 && !provider.id.equals(this.peerId)) {
|
|
68
|
+
const peerId = provider.id;
|
|
69
|
+
found++;
|
|
70
|
+
log('found relay peer %p in content routing', peerId);
|
|
71
|
+
await this.peerStore.addressBook.add(peerId, provider.multiaddrs);
|
|
72
|
+
this.safeDispatchEvent('relay:discover', { detail: peerId });
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
log('found %d relay peers in content routing', found);
|
|
76
|
+
}
|
|
77
|
+
catch (err) {
|
|
78
|
+
log.error('failed when finding relays on the network', err);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
//# sourceMappingURL=discovery.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1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,IAAI,QAAQ,CAAC,UAAU,CAAC,MAAM,GAAG,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE;oBACtE,MAAM,MAAM,GAAG,QAAQ,CAAC,EAAE,CAAA;oBAE1B,KAAK,EAAE,CAAA;oBACP,GAAG,CAAC,wCAAwC,EAAE,MAAM,CAAC,CAAA;oBACrD,MAAM,IAAI,CAAC,SAAS,CAAC,WAAW,CAAC,GAAG,CAAC,MAAM,EAAE,QAAQ,CAAC,UAAU,CAAC,CAAA;oBAEjE,IAAI,CAAC,iBAAiB,CAAC,gBAAgB,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,CAAA;iBAC7D;aACF;YAED,GAAG,CAAC,yCAAyC,EAAE,KAAK,CAAC,CAAA;SACtD;QAAC,OAAO,GAAQ,EAAE;YACjB,GAAG,CAAC,KAAK,CAAC,2CAA2C,EAAE,GAAG,CAAC,CAAA;SAC5D;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Upgrader } from '@libp2p/interface-transport';
|
|
2
|
+
import type { Registrar } from '@libp2p/interface-registrar';
|
|
3
|
+
import type { Transport } from '@libp2p/interface-transport';
|
|
4
|
+
import type { ConnectionGater } from '@libp2p/interface-connection-gater';
|
|
5
|
+
import type { PeerId } from '@libp2p/interface-peer-id';
|
|
6
|
+
import type { PeerStore } from '@libp2p/interface-peer-store';
|
|
7
|
+
import type { ConnectionManager } from '@libp2p/interface-connection-manager';
|
|
8
|
+
import type { AddressManager } from '@libp2p/interface-address-manager';
|
|
9
|
+
import type { ContentRouting } from '@libp2p/interface-content-routing';
|
|
10
|
+
import { RelayStoreInit } from './reservation-store.js';
|
|
11
|
+
import { RelayDiscoveryComponents } from './discovery.js';
|
|
12
|
+
export interface CircuitRelayTransportComponents extends RelayDiscoveryComponents {
|
|
13
|
+
peerId: PeerId;
|
|
14
|
+
peerStore: PeerStore;
|
|
15
|
+
registrar: Registrar;
|
|
16
|
+
connectionManager: ConnectionManager;
|
|
17
|
+
upgrader: Upgrader;
|
|
18
|
+
addressManager: AddressManager;
|
|
19
|
+
contentRouting: ContentRouting;
|
|
20
|
+
connectionGater: ConnectionGater;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* RelayConfig configures the circuit v2 relay transport.
|
|
24
|
+
*/
|
|
25
|
+
export interface CircuitRelayTransportInit extends RelayStoreInit {
|
|
26
|
+
/**
|
|
27
|
+
* The number of peers running diable relays to search for and
|
|
28
|
+
* connect to. (default: 0)
|
|
29
|
+
*/
|
|
30
|
+
discoverRelays?: number;
|
|
31
|
+
}
|
|
32
|
+
export declare function circuitRelayTransport(init?: CircuitRelayTransportInit): (components: CircuitRelayTransportComponents) => Transport;
|
|
33
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/circuit/transport/index.ts"],"names":[],"mappings":"AAQA,OAAO,EAAU,QAAQ,EAAE,MAAM,6BAA6B,CAAA;AAG9D,OAAO,KAAK,EAAsB,SAAS,EAAE,MAAM,6BAA6B,CAAA;AAChF,OAAO,KAAK,EAAY,SAAS,EAAyB,MAAM,6BAA6B,CAAA;AAE7F,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,oCAAoC,CAAA;AACzE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAA;AAEvD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAA;AAC7D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,sCAAsC,CAAA;AAC7E,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,mCAAmC,CAAA;AAEvE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,mCAAmC,CAAA;AAEvE,OAAO,EAAE,cAAc,EAAoB,MAAM,wBAAwB,CAAA;AACzE,OAAO,EAAkB,wBA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