@libp2p/circuit-relay-v2 0.0.0-05b52d69c

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.
Files changed (65) hide show
  1. package/LICENSE +4 -0
  2. package/README.md +69 -0
  3. package/dist/index.min.js +45 -0
  4. package/dist/src/constants.d.ts +55 -0
  5. package/dist/src/constants.d.ts.map +1 -0
  6. package/dist/src/constants.js +61 -0
  7. package/dist/src/constants.js.map +1 -0
  8. package/dist/src/index.d.ts +56 -0
  9. package/dist/src/index.d.ts.map +1 -0
  10. package/dist/src/index.js +39 -0
  11. package/dist/src/index.js.map +1 -0
  12. package/dist/src/pb/index.d.ts +93 -0
  13. package/dist/src/pb/index.d.ts.map +1 -0
  14. package/dist/src/pb/index.js +425 -0
  15. package/dist/src/pb/index.js.map +1 -0
  16. package/dist/src/server/advert-service.d.ts +46 -0
  17. package/dist/src/server/advert-service.d.ts.map +1 -0
  18. package/dist/src/server/advert-service.js +72 -0
  19. package/dist/src/server/advert-service.js.map +1 -0
  20. package/dist/src/server/index.d.ts +67 -0
  21. package/dist/src/server/index.d.ts.map +1 -0
  22. package/dist/src/server/index.js +313 -0
  23. package/dist/src/server/index.js.map +1 -0
  24. package/dist/src/server/reservation-store.d.ts +49 -0
  25. package/dist/src/server/reservation-store.d.ts.map +1 -0
  26. package/dist/src/server/reservation-store.js +65 -0
  27. package/dist/src/server/reservation-store.js.map +1 -0
  28. package/dist/src/server/reservation-voucher.d.ts +18 -0
  29. package/dist/src/server/reservation-voucher.d.ts.map +1 -0
  30. package/dist/src/server/reservation-voucher.js +36 -0
  31. package/dist/src/server/reservation-voucher.js.map +1 -0
  32. package/dist/src/transport/discovery.d.ts +48 -0
  33. package/dist/src/transport/discovery.d.ts.map +1 -0
  34. package/dist/src/transport/discovery.js +97 -0
  35. package/dist/src/transport/discovery.js.map +1 -0
  36. package/dist/src/transport/index.d.ts +58 -0
  37. package/dist/src/transport/index.d.ts.map +1 -0
  38. package/dist/src/transport/index.js +279 -0
  39. package/dist/src/transport/index.js.map +1 -0
  40. package/dist/src/transport/listener.d.ts +11 -0
  41. package/dist/src/transport/listener.d.ts.map +1 -0
  42. package/dist/src/transport/listener.js +66 -0
  43. package/dist/src/transport/listener.js.map +1 -0
  44. package/dist/src/transport/reservation-store.d.ts +74 -0
  45. package/dist/src/transport/reservation-store.d.ts.map +1 -0
  46. package/dist/src/transport/reservation-store.js +209 -0
  47. package/dist/src/transport/reservation-store.js.map +1 -0
  48. package/dist/src/utils.d.ts +14 -0
  49. package/dist/src/utils.d.ts.map +1 -0
  50. package/dist/src/utils.js +106 -0
  51. package/dist/src/utils.js.map +1 -0
  52. package/package.json +83 -0
  53. package/src/constants.ts +79 -0
  54. package/src/index.ts +64 -0
  55. package/src/pb/index.proto +67 -0
  56. package/src/pb/index.ts +539 -0
  57. package/src/server/advert-service.ts +109 -0
  58. package/src/server/index.ts +446 -0
  59. package/src/server/reservation-store.ts +116 -0
  60. package/src/server/reservation-voucher.ts +51 -0
  61. package/src/transport/discovery.ts +138 -0
  62. package/src/transport/index.ts +399 -0
  63. package/src/transport/listener.ts +98 -0
  64. package/src/transport/reservation-store.ts +312 -0
  65. package/src/utils.ts +134 -0
@@ -0,0 +1,313 @@
1
+ import { TypedEventEmitter, setMaxListeners } from '@libp2p/interface/events';
2
+ import { peerIdFromBytes } from '@libp2p/peer-id';
3
+ import { RecordEnvelope } from '@libp2p/peer-record';
4
+ import { multiaddr } from '@multiformats/multiaddr';
5
+ import { pbStream } from 'it-protobuf-stream';
6
+ import pDefer from 'p-defer';
7
+ import { CIRCUIT_PROTO_CODE, DEFAULT_HOP_TIMEOUT, MAX_CONNECTIONS, RELAY_SOURCE_TAG, RELAY_V2_HOP_CODEC, RELAY_V2_STOP_CODEC } from '../constants.js';
8
+ import { HopMessage, Status, StopMessage } from '../pb/index.js';
9
+ import { createLimitedRelay } from '../utils.js';
10
+ import { AdvertService } from './advert-service.js';
11
+ import { ReservationStore } from './reservation-store.js';
12
+ import { ReservationVoucherRecord } from './reservation-voucher.js';
13
+ const isRelayAddr = (ma) => ma.protoCodes().includes(CIRCUIT_PROTO_CODE);
14
+ const defaults = {
15
+ maxOutboundStopStreams: MAX_CONNECTIONS
16
+ };
17
+ class CircuitRelayServer extends TypedEventEmitter {
18
+ registrar;
19
+ peerStore;
20
+ addressManager;
21
+ peerId;
22
+ connectionManager;
23
+ connectionGater;
24
+ reservationStore;
25
+ advertService;
26
+ started;
27
+ hopTimeout;
28
+ shutdownController;
29
+ maxInboundHopStreams;
30
+ maxOutboundHopStreams;
31
+ maxOutboundStopStreams;
32
+ log;
33
+ /**
34
+ * Creates an instance of Relay
35
+ */
36
+ constructor(components, init = {}) {
37
+ super();
38
+ this.log = components.logger.forComponent('libp2p:circuit-relay:server');
39
+ this.registrar = components.registrar;
40
+ this.peerStore = components.peerStore;
41
+ this.addressManager = components.addressManager;
42
+ this.peerId = components.peerId;
43
+ this.connectionManager = components.connectionManager;
44
+ this.connectionGater = components.connectionGater;
45
+ this.started = false;
46
+ this.hopTimeout = init?.hopTimeout ?? DEFAULT_HOP_TIMEOUT;
47
+ this.shutdownController = new AbortController();
48
+ this.maxInboundHopStreams = init.maxInboundHopStreams;
49
+ this.maxOutboundHopStreams = init.maxOutboundHopStreams;
50
+ this.maxOutboundStopStreams = init.maxOutboundStopStreams ?? defaults.maxOutboundStopStreams;
51
+ setMaxListeners(Infinity, this.shutdownController.signal);
52
+ if (init.advertise != null && init.advertise !== false) {
53
+ this.advertService = new AdvertService(components, init.advertise === true ? undefined : init.advertise);
54
+ this.advertService.addEventListener('advert:success', () => {
55
+ this.safeDispatchEvent('relay:advert:success', {});
56
+ });
57
+ this.advertService.addEventListener('advert:error', (evt) => {
58
+ this.safeDispatchEvent('relay:advert:error', { detail: evt.detail });
59
+ });
60
+ }
61
+ this.reservationStore = new ReservationStore(init.reservations);
62
+ }
63
+ isStarted() {
64
+ return this.started;
65
+ }
66
+ /**
67
+ * Start Relay service
68
+ */
69
+ async start() {
70
+ if (this.started) {
71
+ return;
72
+ }
73
+ // Advertise service if HOP enabled and advertising enabled
74
+ this.advertService?.start();
75
+ await this.registrar.handle(RELAY_V2_HOP_CODEC, (data) => {
76
+ void this.onHop(data).catch(err => {
77
+ this.log.error(err);
78
+ });
79
+ }, {
80
+ maxInboundStreams: this.maxInboundHopStreams,
81
+ maxOutboundStreams: this.maxOutboundHopStreams,
82
+ runOnTransientConnection: true
83
+ });
84
+ this.reservationStore.start();
85
+ this.started = true;
86
+ }
87
+ /**
88
+ * Stop Relay service
89
+ */
90
+ async stop() {
91
+ this.advertService?.stop();
92
+ this.reservationStore.stop();
93
+ this.shutdownController.abort();
94
+ await this.registrar.unhandle(RELAY_V2_HOP_CODEC);
95
+ this.started = false;
96
+ }
97
+ async onHop({ connection, stream }) {
98
+ this.log('received circuit v2 hop protocol stream from %p', connection.remotePeer);
99
+ const hopTimeoutPromise = pDefer();
100
+ const timeout = setTimeout(() => {
101
+ hopTimeoutPromise.reject('timed out');
102
+ }, this.hopTimeout);
103
+ const pbstr = pbStream(stream);
104
+ try {
105
+ const request = await Promise.race([
106
+ pbstr.pb(HopMessage).read(),
107
+ hopTimeoutPromise.promise
108
+ ]);
109
+ if (request?.type == null) {
110
+ throw new Error('request was invalid, could not read from stream');
111
+ }
112
+ this.log('received', request.type);
113
+ await Promise.race([
114
+ this.handleHopProtocol({
115
+ connection,
116
+ stream: pbstr,
117
+ request
118
+ }),
119
+ hopTimeoutPromise.promise
120
+ ]);
121
+ }
122
+ catch (err) {
123
+ this.log.error('error while handling hop', err);
124
+ await pbstr.pb(HopMessage).write({
125
+ type: HopMessage.Type.STATUS,
126
+ status: Status.MALFORMED_MESSAGE
127
+ });
128
+ stream.abort(err);
129
+ }
130
+ finally {
131
+ clearTimeout(timeout);
132
+ }
133
+ }
134
+ async handleHopProtocol({ stream, request, connection }) {
135
+ this.log('received hop message');
136
+ switch (request.type) {
137
+ case HopMessage.Type.RESERVE:
138
+ await this.handleReserve({ stream, request, connection });
139
+ break;
140
+ case HopMessage.Type.CONNECT:
141
+ await this.handleConnect({ stream, request, connection });
142
+ break;
143
+ default: {
144
+ this.log.error('invalid hop request type %s via peer %p', request.type, connection.remotePeer);
145
+ await stream.pb(HopMessage).write({ type: HopMessage.Type.STATUS, status: Status.UNEXPECTED_MESSAGE });
146
+ }
147
+ }
148
+ }
149
+ async handleReserve({ stream, request, connection }) {
150
+ const hopstr = stream.pb(HopMessage);
151
+ this.log('hop reserve request from %p', connection.remotePeer);
152
+ if (isRelayAddr(connection.remoteAddr)) {
153
+ this.log.error('relay reservation over circuit connection denied for peer: %p', connection.remotePeer);
154
+ await hopstr.write({ type: HopMessage.Type.STATUS, status: Status.PERMISSION_DENIED });
155
+ return;
156
+ }
157
+ if ((await this.connectionGater.denyInboundRelayReservation?.(connection.remotePeer)) === true) {
158
+ this.log.error('reservation for %p denied by connection gater', connection.remotePeer);
159
+ await hopstr.write({ type: HopMessage.Type.STATUS, status: Status.PERMISSION_DENIED });
160
+ return;
161
+ }
162
+ const result = this.reservationStore.reserve(connection.remotePeer, connection.remoteAddr);
163
+ if (result.status !== Status.OK) {
164
+ await hopstr.write({ type: HopMessage.Type.STATUS, status: result.status });
165
+ return;
166
+ }
167
+ try {
168
+ // tag relay target peer
169
+ // result.expire is non-null if `ReservationStore.reserve` returns with status == OK
170
+ if (result.expire != null) {
171
+ const ttl = (result.expire * 1000) - Date.now();
172
+ await this.peerStore.merge(connection.remotePeer, {
173
+ tags: {
174
+ [RELAY_SOURCE_TAG]: { value: 1, ttl }
175
+ }
176
+ });
177
+ }
178
+ await hopstr.write({
179
+ type: HopMessage.Type.STATUS,
180
+ status: Status.OK,
181
+ reservation: await this.makeReservation(connection.remotePeer, BigInt(result.expire ?? 0)),
182
+ limit: this.reservationStore.get(connection.remotePeer)?.limit
183
+ });
184
+ this.log('sent confirmation response to %s', connection.remotePeer);
185
+ }
186
+ catch (err) {
187
+ this.log.error('failed to send confirmation response to %p', connection.remotePeer, err);
188
+ this.reservationStore.removeReservation(connection.remotePeer);
189
+ }
190
+ }
191
+ async makeReservation(remotePeer, expire) {
192
+ const addrs = [];
193
+ for (const relayAddr of this.addressManager.getAddresses()) {
194
+ if (relayAddr.toString().includes('/p2p-circuit')) {
195
+ continue;
196
+ }
197
+ addrs.push(relayAddr.bytes);
198
+ }
199
+ const voucher = await RecordEnvelope.seal(new ReservationVoucherRecord({
200
+ peer: remotePeer,
201
+ relay: this.peerId,
202
+ expiration: Number(expire)
203
+ }), this.peerId);
204
+ return {
205
+ addrs,
206
+ expire,
207
+ voucher: voucher.marshal()
208
+ };
209
+ }
210
+ async handleConnect({ stream, request, connection }) {
211
+ const hopstr = stream.pb(HopMessage);
212
+ if (isRelayAddr(connection.remoteAddr)) {
213
+ this.log.error('relay reservation over circuit connection denied for peer: %p', connection.remotePeer);
214
+ await hopstr.write({ type: HopMessage.Type.STATUS, status: Status.PERMISSION_DENIED });
215
+ return;
216
+ }
217
+ this.log('hop connect request from %p', connection.remotePeer);
218
+ let dstPeer;
219
+ try {
220
+ if (request.peer == null) {
221
+ this.log.error('no peer info in hop connect request');
222
+ throw new Error('no peer info in request');
223
+ }
224
+ request.peer.addrs.forEach(multiaddr);
225
+ dstPeer = peerIdFromBytes(request.peer.id);
226
+ }
227
+ catch (err) {
228
+ this.log.error('invalid hop connect request via peer %p %s', connection.remotePeer, err);
229
+ await hopstr.write({ type: HopMessage.Type.STATUS, status: Status.MALFORMED_MESSAGE });
230
+ return;
231
+ }
232
+ if (!this.reservationStore.hasReservation(dstPeer)) {
233
+ this.log.error('hop connect denied for destination peer %p not having a reservation for %p with status %s', dstPeer, connection.remotePeer, Status.NO_RESERVATION);
234
+ await hopstr.write({ type: HopMessage.Type.STATUS, status: Status.NO_RESERVATION });
235
+ return;
236
+ }
237
+ if ((await this.connectionGater.denyOutboundRelayedConnection?.(connection.remotePeer, dstPeer)) === true) {
238
+ this.log.error('hop connect for %p to %p denied by connection gater', connection.remotePeer, dstPeer);
239
+ await hopstr.write({ type: HopMessage.Type.STATUS, status: Status.PERMISSION_DENIED });
240
+ return;
241
+ }
242
+ const connections = this.connectionManager.getConnections(dstPeer);
243
+ if (connections.length === 0) {
244
+ this.log('hop connect denied for destination peer %p not having a connection for %p as there is no destination connection', dstPeer, connection.remotePeer);
245
+ await hopstr.write({ type: HopMessage.Type.STATUS, status: Status.NO_RESERVATION });
246
+ return;
247
+ }
248
+ const destinationConnection = connections[0];
249
+ const destinationStream = await this.stopHop({
250
+ connection: destinationConnection,
251
+ request: {
252
+ type: StopMessage.Type.CONNECT,
253
+ peer: {
254
+ id: connection.remotePeer.toBytes(),
255
+ addrs: []
256
+ }
257
+ }
258
+ });
259
+ if (destinationStream == null) {
260
+ this.log.error('failed to open stream to destination peer %p', destinationConnection?.remotePeer);
261
+ await hopstr.write({ type: HopMessage.Type.STATUS, status: Status.CONNECTION_FAILED });
262
+ return;
263
+ }
264
+ await hopstr.write({ type: HopMessage.Type.STATUS, status: Status.OK });
265
+ const sourceStream = stream.unwrap();
266
+ this.log('connection from %p to %p established - merging streams', connection.remotePeer, dstPeer);
267
+ const limit = this.reservationStore.get(dstPeer)?.limit;
268
+ // Short circuit the two streams to create the relayed connection
269
+ createLimitedRelay(sourceStream, destinationStream, this.shutdownController.signal, limit, {
270
+ log: this.log
271
+ });
272
+ }
273
+ /**
274
+ * Send a STOP request to the target peer that the dialing peer wants to contact
275
+ */
276
+ async stopHop({ connection, request }) {
277
+ this.log('starting circuit relay v2 stop request to %s', connection.remotePeer);
278
+ const stream = await connection.newStream([RELAY_V2_STOP_CODEC], {
279
+ maxOutboundStreams: this.maxOutboundStopStreams,
280
+ runOnTransientConnection: true
281
+ });
282
+ const pbstr = pbStream(stream);
283
+ const stopstr = pbstr.pb(StopMessage);
284
+ await stopstr.write(request);
285
+ let response;
286
+ try {
287
+ response = await stopstr.read();
288
+ }
289
+ catch (err) {
290
+ this.log.error('error parsing stop message response from %p', connection.remotePeer);
291
+ }
292
+ if (response == null) {
293
+ this.log.error('could not read response from %p', connection.remotePeer);
294
+ await stream.close();
295
+ return;
296
+ }
297
+ if (response.status === Status.OK) {
298
+ this.log('stop request to %p was successful', connection.remotePeer);
299
+ return pbstr.unwrap();
300
+ }
301
+ this.log('stop request failed with code %d', response.status);
302
+ await stream.close();
303
+ }
304
+ get reservations() {
305
+ return this.reservationStore.reservations;
306
+ }
307
+ }
308
+ export function circuitRelayServer(init = {}) {
309
+ return (components) => {
310
+ return new CircuitRelayServer(components, init);
311
+ };
312
+ }
313
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/server/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAA;AAC7E,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AACjD,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAA;AACpD,OAAO,EAAkB,SAAS,EAAE,MAAM,yBAAyB,CAAA;AACnE,OAAO,EAAE,QAAQ,EAAuB,MAAM,oBAAoB,CAAA;AAClE,OAAO,MAAM,MAAM,SAAS,CAAA;AAC5B,OAAO,EACL,kBAAkB,EAClB,mBAAmB,EACnB,eAAe,EACf,gBAAgB,EAChB,kBAAkB,EAClB,mBAAmB,EACpB,MAAM,iBAAiB,CAAA;AACxB,OAAO,EAAE,UAAU,EAAoB,MAAM,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAClF,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAA;AAChD,OAAO,EAAE,aAAa,EAAwD,MAAM,qBAAqB,CAAA;AACzG,OAAO,EAAE,gBAAgB,EAA6B,MAAM,wBAAwB,CAAA;AACpF,OAAO,EAAE,wBAAwB,EAAE,MAAM,0BAA0B,CAAA;AAanE,MAAM,WAAW,GAAG,CAAC,EAAa,EAAW,EAAE,CAAC,EAAE,CAAC,UAAU,EAAE,CAAC,QAAQ,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AAC,iCAAiC,EAAE,UAAU,CAAC,UAAU,CAAC,CAAA;YACxE,MAAM,MAAM,CAAC,KAAK,EAAE,CAAA;YACpB,OAAM;SACP;QAED,IAAI,QAAQ,CAAC,MAAM,KAAK,MAAM,CAAC,EAAE,EAAE;YACjC,IAAI,CAAC,GAAG,CAAC,mCAAmC,EAAE,UAAU,CAAC,UAAU,CAAC,CAAA;YACpE,OAAO,KAAK,CAAC,MAAM,EAAE,CAAA;SACtB;QAED,IAAI,CAAC,GAAG,CAAC,kCAAkC,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAA;QAC7D,MAAM,MAAM,CAAC,KAAK,EAAE,CAAA;IACtB,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"}
@@ -0,0 +1,49 @@
1
+ import { PeerMap } from '@libp2p/peer-collections';
2
+ import { type Limit, Status } from '../pb/index.js';
3
+ import type { RelayReservation } from '../index.js';
4
+ import type { RecursivePartial } from '@libp2p/interface';
5
+ import type { PeerId } from '@libp2p/interface/peer-id';
6
+ import type { Startable } from '@libp2p/interface/startable';
7
+ import type { Multiaddr } from '@multiformats/multiaddr';
8
+ export type ReservationStatus = Status.OK | Status.PERMISSION_DENIED | Status.RESERVATION_REFUSED;
9
+ export interface ReservationStoreInit {
10
+ maxReservations?: number;
11
+ reservationClearInterval?: number;
12
+ applyDefaultLimit?: boolean;
13
+ /**
14
+ * reservation ttl, default: 2 hours
15
+ */
16
+ reservationTtl?: number;
17
+ /**
18
+ * The maximum time a relayed connection can be open for
19
+ */
20
+ defaultDurationLimit?: number;
21
+ /**
22
+ * The maximum amount of data allowed to be transferred over a relayed connection
23
+ */
24
+ defaultDataLimit?: bigint;
25
+ }
26
+ export type ReservationStoreOptions = RecursivePartial<ReservationStoreInit>;
27
+ export declare class ReservationStore implements Startable {
28
+ readonly reservations: PeerMap<RelayReservation>;
29
+ private _started;
30
+ private interval;
31
+ private readonly maxReservations;
32
+ private readonly reservationClearInterval;
33
+ private readonly applyDefaultLimit;
34
+ private readonly reservationTtl;
35
+ private readonly defaultDurationLimit;
36
+ private readonly defaultDataLimit;
37
+ constructor(options?: ReservationStoreOptions);
38
+ isStarted(): boolean;
39
+ start(): void;
40
+ stop(): void;
41
+ reserve(peer: PeerId, addr: Multiaddr, limit?: Limit): {
42
+ status: ReservationStatus;
43
+ expire?: number;
44
+ };
45
+ removeReservation(peer: PeerId): void;
46
+ hasReservation(dst: PeerId): boolean;
47
+ get(peer: PeerId): RelayReservation | undefined;
48
+ }
49
+ //# sourceMappingURL=reservation-store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"reservation-store.d.ts","sourceRoot":"","sources":["../../../src/server/reservation-store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAA;AAElD,OAAO,EAAE,KAAK,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AACnD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAA;AACnD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAA;AACzD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAA;AACvD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAA;AAC5D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AAExD,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,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DATA_LIMIT, DEFAULT_DURATION_LIMIT, DEFAULT_MAX_RESERVATION_CLEAR_INTERVAL, DEFAULT_MAX_RESERVATION_STORE_SIZE, DEFAULT_MAX_RESERVATION_TTL } from '../constants.js';
3
+ import { Status } from '../pb/index.js';
4
+ export class ReservationStore {
5
+ reservations = new PeerMap();
6
+ _started = false;
7
+ interval;
8
+ maxReservations;
9
+ reservationClearInterval;
10
+ applyDefaultLimit;
11
+ reservationTtl;
12
+ defaultDurationLimit;
13
+ defaultDataLimit;
14
+ constructor(options = {}) {
15
+ this.maxReservations = options.maxReservations ?? DEFAULT_MAX_RESERVATION_STORE_SIZE;
16
+ this.reservationClearInterval = options.reservationClearInterval ?? DEFAULT_MAX_RESERVATION_CLEAR_INTERVAL;
17
+ this.applyDefaultLimit = options.applyDefaultLimit !== false;
18
+ this.reservationTtl = options.reservationTtl ?? DEFAULT_MAX_RESERVATION_TTL;
19
+ this.defaultDurationLimit = options.defaultDurationLimit ?? DEFAULT_DURATION_LIMIT;
20
+ this.defaultDataLimit = options.defaultDataLimit ?? DEFAULT_DATA_LIMIT;
21
+ }
22
+ isStarted() {
23
+ return this._started;
24
+ }
25
+ start() {
26
+ if (this._started) {
27
+ return;
28
+ }
29
+ this._started = true;
30
+ this.interval = setInterval(() => {
31
+ const now = (new Date()).getTime();
32
+ this.reservations.forEach((r, k) => {
33
+ if (r.expire.getTime() < now) {
34
+ this.reservations.delete(k);
35
+ }
36
+ });
37
+ }, this.reservationClearInterval);
38
+ }
39
+ stop() {
40
+ clearInterval(this.interval);
41
+ }
42
+ reserve(peer, addr, limit) {
43
+ if (this.reservations.size >= this.maxReservations && !this.reservations.has(peer)) {
44
+ return { status: Status.RESERVATION_REFUSED };
45
+ }
46
+ const expire = new Date(Date.now() + this.reservationTtl);
47
+ let checkedLimit;
48
+ if (this.applyDefaultLimit) {
49
+ checkedLimit = limit ?? { data: this.defaultDataLimit, duration: this.defaultDurationLimit };
50
+ }
51
+ this.reservations.set(peer, { addr, expire, limit: checkedLimit });
52
+ // return expiry time in seconds
53
+ return { status: Status.OK, expire: Math.round(expire.getTime() / 1000) };
54
+ }
55
+ removeReservation(peer) {
56
+ this.reservations.delete(peer);
57
+ }
58
+ hasReservation(dst) {
59
+ return this.reservations.has(dst);
60
+ }
61
+ get(peer) {
62
+ return this.reservations.get(peer);
63
+ }
64
+ }
65
+ //# sourceMappingURL=reservation-store.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"reservation-store.js","sourceRoot":"","sources":["../../../src/server/reservation-store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAA;AAClD,OAAO,EAAE,kBAAkB,EAAE,sBAAsB,EAAE,sCAAsC,EAAE,kCAAkC,EAAE,2BAA2B,EAAE,MAAM,iBAAiB,CAAA;AACrL,OAAO,EAAc,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAsCnD,MAAM,OAAO,gBAAgB;IACX,YAAY,GAAG,IAAI,OAAO,EAAoB,CAAA;IACtD,QAAQ,GAAG,KAAK,CAAA;IAChB,QAAQ,CAAK;IACJ,eAAe,CAAQ;IACvB,wBAAwB,CAAQ;IAChC,iBAAiB,CAAS;IAC1B,cAAc,CAAQ;IACtB,oBAAoB,CAAQ;IAC5B,gBAAgB,CAAQ;IAEzC,YAAa,UAAmC,EAAE;QAChD,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,CAA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AAoB,EAAE,CAAA;SAC7F;QAED,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,IAAI,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,YAAY,EAAE,CAAC,CAAA;QAElE,gCAAgC;QAChC,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,OAAO,EAAE,GAAG,IAAI,CAAC,EAAE,CAAA;IAC3E,CAAC;IAED,iBAAiB,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,18 @@
1
+ import type { PeerId } from '@libp2p/interface/peer-id';
2
+ import type { Record } from '@libp2p/interface/record';
3
+ export interface ReservationVoucherOptions {
4
+ relay: PeerId;
5
+ peer: PeerId;
6
+ expiration: number;
7
+ }
8
+ export declare class ReservationVoucherRecord implements Record {
9
+ readonly domain = "libp2p-relay-rsvp";
10
+ readonly codec: Uint8Array;
11
+ private readonly relay;
12
+ private readonly peer;
13
+ private readonly expiration;
14
+ constructor({ relay, peer, expiration }: ReservationVoucherOptions);
15
+ marshal(): Uint8Array;
16
+ equals(other: Record): boolean;
17
+ }
18
+ //# sourceMappingURL=reservation-voucher.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"reservation-voucher.d.ts","sourceRoot":"","sources":["../../../src/server/reservation-voucher.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAA;AACvD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,0BAA0B,CAAA;AAEtD,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,36 @@
1
+ import { ReservationVoucher } from '../pb/index.js';
2
+ export class ReservationVoucherRecord {
3
+ domain = 'libp2p-relay-rsvp';
4
+ codec = new Uint8Array([0x03, 0x02]);
5
+ relay;
6
+ peer;
7
+ expiration;
8
+ constructor({ relay, peer, expiration }) {
9
+ this.relay = relay;
10
+ this.peer = peer;
11
+ this.expiration = expiration;
12
+ }
13
+ marshal() {
14
+ return ReservationVoucher.encode({
15
+ relay: this.relay.toBytes(),
16
+ peer: this.peer.toBytes(),
17
+ expiration: BigInt(this.expiration)
18
+ });
19
+ }
20
+ equals(other) {
21
+ if (!(other instanceof ReservationVoucherRecord)) {
22
+ return false;
23
+ }
24
+ if (!this.peer.equals(other.peer)) {
25
+ return false;
26
+ }
27
+ if (!this.relay.equals(other.relay)) {
28
+ return false;
29
+ }
30
+ if (this.expiration !== other.expiration) {
31
+ return false;
32
+ }
33
+ return true;
34
+ }
35
+ }
36
+ //# sourceMappingURL=reservation-voucher.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"reservation-voucher.js","sourceRoot":"","sources":["../../../src/server/reservation-voucher.ts"],"names":[],"mappings":"AAAA,OA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+ import type { ContentRouting } from '@libp2p/interface/content-routing';
4
+ import type { PeerId } from '@libp2p/interface/peer-id';
5
+ import type { PeerStore } from '@libp2p/interface/peer-store';
6
+ import type { Startable } from '@libp2p/interface/startable';
7
+ import type { ConnectionManager } from '@libp2p/interface-internal/connection-manager';
8
+ import type { Registrar } from '@libp2p/interface-internal/registrar';
9
+ import type { TransportManager } from '@libp2p/interface-internal/transport-manager';
10
+ export interface RelayDiscoveryEvents {
11
+ 'relay:discover': CustomEvent<PeerId>;
12
+ }
13
+ export interface RelayDiscoveryComponents {
14
+ peerId: PeerId;
15
+ peerStore: PeerStore;
16
+ connectionManager: ConnectionManager;
17
+ transportManager: TransportManager;
18
+ contentRouting: ContentRouting;
19
+ registrar: Registrar;
20
+ logger: ComponentLogger;
21
+ }
22
+ /**
23
+ * ReservationManager automatically makes a circuit v2 reservation on any connected
24
+ * peers that support the circuit v2 HOP protocol.
25
+ */
26
+ export declare class RelayDiscovery extends TypedEventEmitter<RelayDiscoveryEvents> implements Startable {
27
+ private readonly peerId;
28
+ private readonly peerStore;
29
+ private readonly contentRouting;
30
+ private readonly registrar;
31
+ private started;
32
+ private topologyId?;
33
+ private readonly log;
34
+ constructor(components: RelayDiscoveryComponents);
35
+ isStarted(): boolean;
36
+ start(): Promise<void>;
37
+ stop(): void;
38
+ /**
39
+ * Try to listen on available hop relay connections.
40
+ * The following order will happen while we do not have enough relays:
41
+ *
42
+ * 1. Check the metadata store for known relays, try to listen on the ones we are already connected
43
+ * 2. Dial and try to listen on the peers we know that support hop but are not connected
44
+ * 3. Search the network
45
+ */
46
+ discover(): Promise<void>;
47
+ }
48
+ //# sourceMappingURL=discovery.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"discovery.d.ts","sourceRoot":"","sources":["../../../src/transport/discovery.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAA;AAM5D,OAAO,KAAK,EAAE,eAAe,EAAU,MAAM,mBAA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