@libp2p/circuit-relay-v2 3.2.24-6059227cb → 3.2.24-87bc8d4fb

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 (51) hide show
  1. package/dist/index.min.js +1 -1
  2. package/dist/index.min.js.map +4 -4
  3. package/dist/src/constants.d.ts +6 -1
  4. package/dist/src/constants.d.ts.map +1 -1
  5. package/dist/src/constants.js +6 -1
  6. package/dist/src/constants.js.map +1 -1
  7. package/dist/src/index.d.ts +7 -162
  8. package/dist/src/index.d.ts.map +1 -1
  9. package/dist/src/index.js +2 -12
  10. package/dist/src/index.js.map +1 -1
  11. package/dist/src/server/index.d.ts +45 -41
  12. package/dist/src/server/index.d.ts.map +1 -1
  13. package/dist/src/server/index.js +51 -32
  14. package/dist/src/server/index.js.map +1 -1
  15. package/dist/src/server/reservation-store.d.ts +36 -2
  16. package/dist/src/server/reservation-store.d.ts.map +1 -1
  17. package/dist/src/server/reservation-store.js.map +1 -1
  18. package/dist/src/transport/discovery.d.ts +17 -2
  19. package/dist/src/transport/discovery.d.ts.map +1 -1
  20. package/dist/src/transport/discovery.js +2 -2
  21. package/dist/src/transport/discovery.js.map +1 -1
  22. package/dist/src/transport/index.d.ts +42 -34
  23. package/dist/src/transport/index.d.ts.map +1 -1
  24. package/dist/src/transport/index.js +5 -291
  25. package/dist/src/transport/index.js.map +1 -1
  26. package/dist/src/transport/reservation-store.d.ts +37 -3
  27. package/dist/src/transport/reservation-store.d.ts.map +1 -1
  28. package/dist/src/transport/reservation-store.js +6 -4
  29. package/dist/src/transport/reservation-store.js.map +1 -1
  30. package/dist/src/transport/transport.d.ts +54 -0
  31. package/dist/src/transport/transport.d.ts.map +1 -0
  32. package/dist/src/transport/transport.js +314 -0
  33. package/dist/src/transport/transport.js.map +1 -0
  34. package/dist/src/utils.d.ts.map +1 -1
  35. package/dist/src/utils.js +59 -36
  36. package/dist/src/utils.js.map +1 -1
  37. package/package.json +24 -19
  38. package/src/constants.ts +7 -1
  39. package/src/index.ts +8 -198
  40. package/src/server/index.ts +105 -37
  41. package/src/server/reservation-store.ts +42 -2
  42. package/src/transport/discovery.ts +22 -4
  43. package/src/transport/index.ts +46 -338
  44. package/src/transport/reservation-store.ts +46 -8
  45. package/src/transport/transport.ts +380 -0
  46. package/src/utils.ts +66 -37
  47. package/dist/src/transport/stream-to-conn.d.ts +0 -19
  48. package/dist/src/transport/stream-to-conn.d.ts.map +0 -1
  49. package/dist/src/transport/stream-to-conn.js +0 -60
  50. package/dist/src/transport/stream-to-conn.js.map +0 -1
  51. package/src/transport/stream-to-conn.ts +0 -91
@@ -1,3 +1,7 @@
1
+ /**
2
+ * Multicodec code
3
+ */
4
+ export declare const CIRCUIT_PROTO_CODE = 290;
1
5
  /**
2
6
  * The maximum number of relay reservations the relay server will accept
3
7
  */
@@ -13,13 +17,14 @@ export declare const DEFAULT_RESERVATION_CONCURRENCY = 1;
13
17
  /**
14
18
  * How long to wait for a reservation attempt to finish
15
19
  */
16
- export declare const DEFAULT_RESERVATION_COMPLETION_TIMEOUT = 5000;
20
+ export declare const DEFAULT_RESERVATION_COMPLETION_TIMEOUT = 2000;
17
21
  /**
18
22
  * How long to let the reservation attempt queue to grow
19
23
  */
20
24
  export declare const DEFAULT_MAX_RESERVATION_QUEUE_LENGTH = 100;
21
25
  export declare const RELAY_SOURCE_TAG = "circuit-relay-source";
22
26
  export declare const KEEP_ALIVE_TAG = "keep-alive-circuit-relay";
27
+ export declare const KEEP_ALIVE_SOURCE_TAG = "keep-alive-circuit-relay-source";
23
28
  export declare const DEFAULT_DURATION_LIMIT: number;
24
29
  export declare const DEFAULT_DATA_LIMIT: bigint;
25
30
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":"AAKA;;GAEG;AACH,eAAO,MAAM,kCAAkC,KAAK,CAAA;AAEpD;;GAEG;AACH,eAAO,MAAM,2BAA2B,QAAkB,C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A;AAEjD;;GAEG;AACH,eAAO,MAAM,kBAAkB,oCAAoC,CAAA;AAEnE;;GAEG;AACH,eAAO,MAAM,mBAAmB,qCAAqC,CAAA;AAErE;;GAEG;AACH,eAAO,MAAM,mBAAmB,QAAc,CAAA;AAE9C;;GAEG;AACH,eAAO,MAAM,yBAAyB,QAAc,CAAA;AAEpD,eAAO,MAAM,eAAe,MAAM,CAAA;AAElC,eAAO,MAAM,6BAA6B,OAAO,CAAA;AACjD,eAAO,MAAM,mCAAmC,QAAQ,CAAA"}
@@ -1,6 +1,10 @@
1
1
  import { KEEP_ALIVE } from '@libp2p/interface';
2
2
  const second = 1000;
3
3
  const minute = 60 * second;
4
+ /**
5
+ * Multicodec code
6
+ */
7
+ export const CIRCUIT_PROTO_CODE = 290;
4
8
  /**
5
9
  * The maximum number of relay reservations the relay server will accept
6
10
  */
@@ -16,13 +20,14 @@ export const DEFAULT_RESERVATION_CONCURRENCY = 1;
16
20
  /**
17
21
  * How long to wait for a reservation attempt to finish
18
22
  */
19
- export const DEFAULT_RESERVATION_COMPLETION_TIMEOUT = 5_000;
23
+ export const DEFAULT_RESERVATION_COMPLETION_TIMEOUT = 2000;
20
24
  /**
21
25
  * How long to let the reservation attempt queue to grow
22
26
  */
23
27
  export const DEFAULT_MAX_RESERVATION_QUEUE_LENGTH = 100;
24
28
  export const RELAY_SOURCE_TAG = 'circuit-relay-source';
25
29
  export const KEEP_ALIVE_TAG = `${KEEP_ALIVE}-circuit-relay`;
30
+ export const KEEP_ALIVE_SOURCE_TAG = `${KEEP_ALIVE}-circuit-relay-source`;
26
31
  // circuit v2 connection limits
27
32
  // https://github.com/libp2p/go-libp2p/blob/master/p2p/protocol/circuitv2/relay/resources.go#L61-L66
28
33
  // 2 min is the default connection duration
@@ -1 +1 @@
1
- {"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAA;AAE9C,MAAM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MAAM,CAAC,MAAM,eAAe,GAAG,GAAG,CAAA;AAElC,MAAM,CAAC,MAAM,6BAA6B,GAAG,IAAI,CAAA;AACjD,MAAM,CAAC,MAAM,mCAAmC,GAAG,KAAK,CAAA"}
@@ -40,8 +40,6 @@
40
40
  */
41
41
  import { TypedEventEmitter } from 'main-event';
42
42
  import type { Limit } from './pb/index.js';
43
- import type { ComponentLogger, ConnectionGater, Libp2pEvents, Metrics, PeerId, PeerStore, PrivateKey, TopologyFilter, Transport, TypedEventTarget, Upgrader } from '@libp2p/interface';
44
- import type { AddressManager, ConnectionManager, RandomWalk, Registrar, TransportManager } from '@libp2p/interface-internal';
45
43
  import type { PeerMap } from '@libp2p/peer-collections';
46
44
  import type { Multiaddr } from '@multiformats/multiaddr';
47
45
  import type { RetimeableAbortSignal } from 'retimeable-signal';
@@ -73,165 +71,12 @@ export interface CircuitRelayServiceEvents {
73
71
  export interface CircuitRelayService extends TypedEventEmitter<CircuitRelayServiceEvents> {
74
72
  reservations: PeerMap<RelayReservation>;
75
73
  }
74
+ export { circuitRelayServer } from './server/index.js';
75
+ export type { CircuitRelayServerInit, CircuitRelayServerComponents } from './server/index.js';
76
+ export type { ReservationStoreInit as ServerReservationStoreInit } from './server/reservation-store.js';
77
+ export { circuitRelayTransport } from './transport/index.js';
78
+ export type { RelayDiscoveryComponents } from './transport/discovery.js';
79
+ export type { ReservationStoreInit as TransportReservationStoreInit } from './transport/reservation-store.js';
80
+ export type { CircuitRelayTransportInit, CircuitRelayTransportComponents } from './transport/index.js';
76
81
  export { RELAY_V2_HOP_CODEC, RELAY_V2_STOP_CODEC } from './constants.js';
77
- export interface ServerReservationStoreInit {
78
- /**
79
- * maximum number of reservations allowed
80
- *
81
- * @default 15
82
- */
83
- maxReservations?: number;
84
- /**
85
- * interval after which stale reservations are cleared
86
- *
87
- * @default 300000
88
- */
89
- reservationClearInterval?: number;
90
- /**
91
- * apply default relay limits to a new reservation
92
- *
93
- * @default true
94
- */
95
- applyDefaultLimit?: boolean;
96
- /**
97
- * reservation ttl
98
- *
99
- * @default 7200000
100
- */
101
- reservationTtl?: number;
102
- /**
103
- * The maximum time a relayed connection can be open for
104
- */
105
- defaultDurationLimit?: number;
106
- /**
107
- * The maximum amount of data allowed to be transferred over a relayed connection
108
- */
109
- defaultDataLimit?: bigint;
110
- }
111
- export interface CircuitRelayServerComponents {
112
- registrar: Registrar;
113
- peerStore: PeerStore;
114
- addressManager: AddressManager;
115
- peerId: PeerId;
116
- privateKey: PrivateKey;
117
- connectionManager: ConnectionManager;
118
- connectionGater: ConnectionGater;
119
- logger: ComponentLogger;
120
- metrics?: Metrics;
121
- }
122
- export interface CircuitRelayServerInit {
123
- /**
124
- * Incoming hop requests must complete within this time in ms otherwise
125
- * the stream will be reset
126
- *
127
- * @default 30000
128
- */
129
- hopTimeout?: number;
130
- /**
131
- * Configuration of reservations
132
- */
133
- reservations?: ServerReservationStoreInit;
134
- /**
135
- * The maximum number of simultaneous HOP inbound streams that can be open at once
136
- */
137
- maxInboundHopStreams?: number;
138
- /**
139
- * The maximum number of simultaneous HOP outbound streams that can be open at once
140
- */
141
- maxOutboundHopStreams?: number;
142
- /**
143
- * The maximum number of simultaneous STOP outbound streams that can be open at
144
- * once.
145
- *
146
- * @default 300
147
- */
148
- maxOutboundStopStreams?: number;
149
- }
150
- export declare function circuitRelayServer(init?: CircuitRelayServerInit): (components: CircuitRelayServerComponents) => CircuitRelayService;
151
- export interface RelayDiscoveryEvents {
152
- 'relay:discover': CustomEvent<PeerId>;
153
- }
154
- export interface TransportReservationStoreComponents {
155
- peerId: PeerId;
156
- connectionManager: ConnectionManager;
157
- peerStore: PeerStore;
158
- events: TypedEventTarget<Libp2pEvents>;
159
- logger: ComponentLogger;
160
- metrics?: Metrics;
161
- }
162
- export interface TransportReservationStoreInit {
163
- /**
164
- * Multiple relays may be discovered simultaneously - to prevent listening
165
- * on too many relays, this value controls how many to attempt to reserve a
166
- * slot on at once. If set to more than one, we may end up listening on
167
- * more relays than the `maxReservations` value, but on networks with poor
168
- * connectivity the user may wish to attempt to reserve on multiple relays
169
- * simultaneously.
170
- *
171
- * @default 1
172
- */
173
- reservationConcurrency?: number;
174
- /**
175
- * Limit the number of potential relays we will dial
176
- *
177
- * @default 100
178
- */
179
- maxReservationQueueLength?: number;
180
- /**
181
- * When creating a reservation it must complete within this number of ms
182
- *
183
- * @default 5000
184
- */
185
- reservationCompletionTimeout?: number;
186
- }
187
- export interface RelayDiscoveryComponents {
188
- peerStore: PeerStore;
189
- connectionManager: ConnectionManager;
190
- transportManager: TransportManager;
191
- registrar: Registrar;
192
- logger: ComponentLogger;
193
- randomWalk: RandomWalk;
194
- events: TypedEventTarget<Libp2pEvents>;
195
- }
196
- export interface RelayDiscoveryInit {
197
- filter?: TopologyFilter;
198
- }
199
- export interface CircuitRelayTransportComponents extends RelayDiscoveryComponents {
200
- peerId: PeerId;
201
- upgrader: Upgrader;
202
- addressManager: AddressManager;
203
- connectionGater: ConnectionGater;
204
- }
205
- /**
206
- * RelayConfig configures the circuit v2 relay transport.
207
- */
208
- export interface CircuitRelayTransportInit extends TransportReservationStoreInit {
209
- /**
210
- * An optional filter used to prevent duplicate attempts to reserve relay
211
- * slots on the same peer
212
- */
213
- discoveryFilter?: TopologyFilter;
214
- /**
215
- * The maximum number of simultaneous STOP inbound streams that can be open at
216
- * once - each inbound relayed connection uses a STOP stream
217
- *
218
- * @default 300
219
- */
220
- maxInboundStopStreams?: number;
221
- /**
222
- * The maximum number of simultaneous STOP outbound streams that can be open
223
- * at once. If this transport is used along with the relay server these
224
- * settings should be set to the same value
225
- *
226
- * @default 300
227
- */
228
- maxOutboundStopStreams?: number;
229
- /**
230
- * When creating a reservation it must complete within this number of ms
231
- *
232
- * @default 10_000
233
- */
234
- reservationCompletionTimeout?: number;
235
- }
236
- export declare function circuitRelayTransport(init?: CircuitRelayTransportInit): (components: CircuitRelayTransportComponents) => Transport;
237
82
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"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,OAAO,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,CAAA;AAC5D,YAAY,EAAE,wBAAwB,EAAE,MAAM,0BAA0B,CAAA;AACxE,YAAY,EAAE,oBAAoB,IAAI,6BAA6B,EAAE,MAAM,kCAAkC,CAAA;AAC7G,YAAY,EAAE,yBAAyB,EAAE,+BAA+B,EAAE,MAAM,sBAAsB,CAAA;AAEtG,OAAO,EACL,kBAAkB,EAClB,mBAAmB,EACpB,MAAM,gBAAgB,CAAA"}
package/dist/src/index.js CHANGED
@@ -39,17 +39,7 @@
39
39
  * ```
40
40
  */
41
41
  import { TypedEventEmitter } from 'main-event';
42
- import { CircuitRelayServer } from './server/index.js';
43
- import { CircuitRelayTransport } from "./transport/index.js";
42
+ export { circuitRelayServer } from './server/index.js';
43
+ export { circuitRelayTransport } from './transport/index.js';
44
44
  export { RELAY_V2_HOP_CODEC, RELAY_V2_STOP_CODEC } from './constants.js';
45
- export function circuitRelayServer(init = {}) {
46
- return (components) => {
47
- return new CircuitRelayServer(components, init);
48
- };
49
- }
50
- export function circuitRelayTransport(init = {}) {
51
- return (components) => {
52
- return new CircuitRelayTransport(components, init);
53
- };
54
- }
55
45
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AAEH,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAC9C,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAA;AACtD,OAAO,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,CAAA;AA2C5D,OAAO,EACL,kBAAkB,EAClB,mBAAmB,EACpB,MAAM,gBAAgB,CAAA;AAuFvB,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;AAmGD,MAAM,UAAU,qBAAqB,CAAE,OAAkC,EAAE;IACzE,OAAO,CAAC,UAAU,EAAE,EAAE;QACpB,OAAO,IAAI,qBAAqB,CAAC,UAAU,EAAE,IAAI,CAAC,CAAA;IACpD,CAAC,CAAA;AACH,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AAEH,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAyC9C,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAA;AAGtD,OAAO,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,CAAA;AAK5D,OAAO,EACL,kBAAkB,EAClB,mBAAmB,EACpB,MAAM,gBAAgB,CAAA"}
@@ -1,10 +1,37 @@
1
- import { TypedEventEmitter } from 'main-event';
2
1
  import { HopMessage, StopMessage } from '../pb/index.js';
3
- import type { CircuitRelayServerComponents, CircuitRelayServerInit, CircuitRelayService, RelayReservation } from '../index.js';
4
- import type { Reservation } from '../pb/index.js';
5
- import type { Connection, Stream, PeerId, Startable, AbortOptions } from '@libp2p/interface';
6
- import type { PeerMap } from '@libp2p/peer-collections';
7
- import type { ProtobufStream } from '@libp2p/utils';
2
+ import type { ReservationStoreInit } from './reservation-store.js';
3
+ import type { CircuitRelayService, RelayReservation } from '../index.js';
4
+ import type { ComponentLogger, Connection, Stream, ConnectionGater, PeerId, PeerStore, PrivateKey, Metrics } from '@libp2p/interface';
5
+ import type { AddressManager, ConnectionManager, Registrar } from '@libp2p/interface-internal';
6
+ import type { ProtobufStream } from 'it-protobuf-stream';
7
+ export interface CircuitRelayServerInit {
8
+ /**
9
+ * Incoming hop requests must complete within this time in ms otherwise
10
+ * the stream will be reset
11
+ *
12
+ * @default 30000
13
+ */
14
+ hopTimeout?: number;
15
+ /**
16
+ * Configuration of reservations
17
+ */
18
+ reservations?: ReservationStoreInit;
19
+ /**
20
+ * The maximum number of simultaneous HOP inbound streams that can be open at once
21
+ */
22
+ maxInboundHopStreams?: number;
23
+ /**
24
+ * The maximum number of simultaneous HOP outbound streams that can be open at once
25
+ */
26
+ maxOutboundHopStreams?: number;
27
+ /**
28
+ * The maximum number of simultaneous STOP outbound streams that can be open at
29
+ * once.
30
+ *
31
+ * @default 300
32
+ */
33
+ maxOutboundStopStreams?: number;
34
+ }
8
35
  export interface HopProtocolOptions {
9
36
  connection: Connection;
10
37
  request: HopMessage;
@@ -14,44 +41,21 @@ export interface StopOptions {
14
41
  connection: Connection;
15
42
  request: StopMessage;
16
43
  }
44
+ export interface CircuitRelayServerComponents {
45
+ registrar: Registrar;
46
+ peerStore: PeerStore;
47
+ addressManager: AddressManager;
48
+ peerId: PeerId;
49
+ privateKey: PrivateKey;
50
+ connectionManager: ConnectionManager;
51
+ connectionGater: ConnectionGater;
52
+ logger: ComponentLogger;
53
+ metrics?: Metrics;
54
+ }
17
55
  export interface RelayServerEvents {
18
56
  'relay:reservation': CustomEvent<RelayReservation>;
19
57
  'relay:advert:success': CustomEvent<unknown>;
20
58
  'relay:advert:error': CustomEvent<Error>;
21
59
  }
22
- export declare class CircuitRelayServer extends TypedEventEmitter<RelayServerEvents> implements Startable, CircuitRelayService {
23
- private readonly components;
24
- private readonly reservationStore;
25
- private started;
26
- private readonly hopTimeout;
27
- private readonly shutdownController;
28
- private readonly maxInboundHopStreams?;
29
- private readonly maxOutboundHopStreams?;
30
- private readonly maxOutboundStopStreams;
31
- private readonly log;
32
- /**
33
- * Creates an instance of Relay
34
- */
35
- constructor(components: CircuitRelayServerComponents, init?: CircuitRelayServerInit);
36
- readonly [Symbol.toStringTag] = "@libp2p/circuit-relay-v2-server";
37
- isStarted(): boolean;
38
- /**
39
- * Start Relay service
40
- */
41
- start(): Promise<void>;
42
- /**
43
- * Stop Relay service
44
- */
45
- stop(): Promise<void>;
46
- onHop(stream: Stream, connection: Connection): Promise<void>;
47
- handleHopProtocol({ stream, request, connection }: HopProtocolOptions, options: AbortOptions): Promise<void>;
48
- handleReserve({ stream, connection }: HopProtocolOptions, options: AbortOptions): Promise<void>;
49
- makeReservation(remotePeer: PeerId, expire: bigint): Promise<Reservation>;
50
- handleConnect({ stream, request, connection }: HopProtocolOptions, options: AbortOptions): Promise<void>;
51
- /**
52
- * Send a STOP request to the target peer that the dialing peer wants to contact
53
- */
54
- stopHop({ connection, request }: StopOptions, options: AbortOptions): Promise<Stream | undefined>;
55
- get reservations(): PeerMap<RelayReservation>;
56
- }
60
+ export declare function circuitRelayServer(init?: CircuitRelayServerInit): (components: CircuitRelayServerComponents) => CircuitRelayService;
57
61
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/server/index.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,iBAAiB,EAAmB,MAAM,YAAY,CAAA;AAS/D,OAAO,EAAE,UAAU,EAAU,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAIhE,OAAO,KAAK,EAAE,4BAA4B,EAAE,sBAAsB,EAAE,mBAAmB,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAA;AAC9H,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AACjD,OAAO,KAAK,EAAU,UAAU,EAAE,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AACpG,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAA;AACvD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,eAAe,CAAA;AAEnD,MAAM,WAAW,kBAAkB;IACjC,UAAU,EAAE,UAAU,CAAA;IACtB,OAAO,EAAE,UAAU,CAAA;IACnB,MAAM,EAAE,cAAc,CAAC,MAAM,CAAC,CAAA;CAC/B;AAED,MAAM,WAAW,WAAW;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AAA;IAEnC;;OAEG;IACH,oBAAoB,CAAC,EAAE,MAAM,CAAA;IAE7B;;OAEG;IACH,qBAAqB,CAAC,EAAE,MAAM,CAAA;IAE9B;;;;;OAKG;IACH,sBAAsB,CAAC,EAAE,MAAM,CAAA;CAChC;AAED,MAAM,WAAW,kBAAkB;IACjC,UAAU,EAAE,UAAU,CAAA;IACtB,OAAO,EAAE,UAAU,CAAA;IACnB,MAAM,EAAE,cAAc,CAAC,MAAM,CAAC,CAAA;CAC/B;AAED,MAAM,WAAW,WAAW;IAC1B,UAAU,EAAE,UAAU,CAAA;IACtB,OAAO,EAAE,WAAW,CAAA;CACrB;AAED,MAAM,WAAW,4BAA4B;IAC3C,SAAS,EAAE,SAAS,CAAA;IACpB,SAAS,EAAE,SAAS,CAAA;IACpB,cAAc,EAAE,cAAc,CAAA;IAC9B,MAAM,EAAE,MAAM,CAAA;IACd,UAAU,EAAE,UAAU,CAAA;IACtB,iBAAiB,EAAE,iBAAiB,CAAA;IACpC,eAAe,EAAE,eAAe,CAAA;IAChC,MAAM,EAAE,eAAe,CAAA;IACvB,OAAO,CAAC,EAAE,OAAO,CAAA;CAClB;AAED,MAAM,WAAW,iBAAiB;IAChC,mBAAmB,EAAE,WAAW,CAAC,gBAAgB,CAAC,CAAA;IAClD,sBAAsB,EAAE,WAAW,CAAC,OAAO,CAAC,CAAA;IAC5C,oBAAoB,EAAE,WAAW,CAAC,KAAK,CAAC,CAAA;CACzC;AA+VD,wBAAgB,kBAAkB,CAAE,IAAI,GAAE,sBAA2B,GAAG,CAAC,UAAU,EAAE,4BAA4B,KAAK,mBAAmB,CAIxI"}
@@ -1,21 +1,27 @@
1
1
  import { publicKeyToProtobuf } from '@libp2p/crypto/keys';
2
2
  import { peerIdFromMultihash } from '@libp2p/peer-id';
3
3
  import { RecordEnvelope } from '@libp2p/peer-record';
4
- import { pbStream } from '@libp2p/utils';
5
4
  import { multiaddr } from '@multiformats/multiaddr';
6
- import { Circuit } from '@multiformats/multiaddr-matcher';
5
+ import { pbStream } from 'it-protobuf-stream';
7
6
  import { TypedEventEmitter, setMaxListeners } from 'main-event';
8
7
  import * as Digest from 'multiformats/hashes/digest';
9
- import { DEFAULT_HOP_TIMEOUT, MAX_CONNECTIONS, RELAY_SOURCE_TAG, RELAY_V2_HOP_CODEC, RELAY_V2_STOP_CODEC } from '../constants.js';
8
+ import { CIRCUIT_PROTO_CODE, DEFAULT_HOP_TIMEOUT, KEEP_ALIVE_SOURCE_TAG, MAX_CONNECTIONS, RELAY_SOURCE_TAG, RELAY_V2_HOP_CODEC, RELAY_V2_STOP_CODEC } from '../constants.js';
10
9
  import { HopMessage, Status, StopMessage } from '../pb/index.js';
11
10
  import { createLimitedRelay } from '../utils.js';
12
11
  import { ReservationStore } from './reservation-store.js';
13
12
  import { ReservationVoucherRecord } from './reservation-voucher.js';
13
+ const isRelayAddr = (ma) => ma.protoCodes().includes(CIRCUIT_PROTO_CODE);
14
14
  const defaults = {
15
15
  maxOutboundStopStreams: MAX_CONNECTIONS
16
16
  };
17
- export class CircuitRelayServer extends TypedEventEmitter {
18
- components;
17
+ class CircuitRelayServer extends TypedEventEmitter {
18
+ registrar;
19
+ peerStore;
20
+ addressManager;
21
+ peerId;
22
+ privateKey;
23
+ connectionManager;
24
+ connectionGater;
19
25
  reservationStore;
20
26
  started;
21
27
  hopTimeout;
@@ -30,7 +36,13 @@ export class CircuitRelayServer extends TypedEventEmitter {
30
36
  constructor(components, init = {}) {
31
37
  super();
32
38
  this.log = components.logger.forComponent('libp2p:circuit-relay:server');
33
- this.components = components;
39
+ this.registrar = components.registrar;
40
+ this.peerStore = components.peerStore;
41
+ this.addressManager = components.addressManager;
42
+ this.peerId = components.peerId;
43
+ this.privateKey = components.privateKey;
44
+ this.connectionManager = components.connectionManager;
45
+ this.connectionGater = components.connectionGater;
34
46
  this.started = false;
35
47
  this.hopTimeout = init?.hopTimeout ?? DEFAULT_HOP_TIMEOUT;
36
48
  this.maxInboundHopStreams = init.maxInboundHopStreams;
@@ -39,7 +51,6 @@ export class CircuitRelayServer extends TypedEventEmitter {
39
51
  this.reservationStore = new ReservationStore(components, init.reservations);
40
52
  this.shutdownController = new AbortController();
41
53
  setMaxListeners(Infinity, this.shutdownController.signal);
42
- this.onHop = this.onHop.bind(this);
43
54
  }
44
55
  [Symbol.toStringTag] = '@libp2p/circuit-relay-v2-server';
45
56
  isStarted() {
@@ -52,7 +63,11 @@ export class CircuitRelayServer extends TypedEventEmitter {
52
63
  if (this.started) {
53
64
  return;
54
65
  }
55
- await this.components.registrar.handle(RELAY_V2_HOP_CODEC, this.onHop, {
66
+ await this.registrar.handle(RELAY_V2_HOP_CODEC, (data) => {
67
+ void this.onHop(data).catch(err => {
68
+ this.log.error(err);
69
+ });
70
+ }, {
56
71
  maxInboundStreams: this.maxInboundHopStreams,
57
72
  maxOutboundStreams: this.maxOutboundHopStreams,
58
73
  runOnLimitedConnection: true
@@ -65,15 +80,13 @@ export class CircuitRelayServer extends TypedEventEmitter {
65
80
  async stop() {
66
81
  this.reservationStore.clear();
67
82
  this.shutdownController.abort();
68
- await this.components.registrar.unhandle(RELAY_V2_HOP_CODEC);
83
+ await this.registrar.unhandle(RELAY_V2_HOP_CODEC);
69
84
  this.started = false;
70
85
  }
71
- async onHop(stream, connection) {
86
+ async onHop({ connection, stream }) {
72
87
  this.log('received circuit v2 hop protocol stream from %p', connection.remotePeer);
73
- const signal = AbortSignal.timeout(this.hopTimeout);
74
- setMaxListeners(Infinity, signal);
75
88
  const options = {
76
- signal
89
+ signal: AbortSignal.timeout(this.hopTimeout)
77
90
  };
78
91
  const pbstr = pbStream(stream);
79
92
  try {
@@ -115,12 +128,12 @@ export class CircuitRelayServer extends TypedEventEmitter {
115
128
  async handleReserve({ stream, connection }, options) {
116
129
  const hopstr = stream.pb(HopMessage);
117
130
  this.log('hop reserve request from %p', connection.remotePeer);
118
- if (Circuit.exactMatch(connection.remoteAddr)) {
131
+ if (isRelayAddr(connection.remoteAddr)) {
119
132
  this.log.error('relay reservation over circuit connection denied for peer: %p', connection.remotePeer);
120
133
  await hopstr.write({ type: HopMessage.Type.STATUS, status: Status.PERMISSION_DENIED }, options);
121
134
  return;
122
135
  }
123
- if ((await this.components.connectionGater.denyInboundRelayReservation?.(connection.remotePeer)) === true) {
136
+ if ((await this.connectionGater.denyInboundRelayReservation?.(connection.remotePeer)) === true) {
124
137
  this.log.error('reservation for %p denied by connection gater', connection.remotePeer);
125
138
  await hopstr.write({ type: HopMessage.Type.STATUS, status: Status.PERMISSION_DENIED }, options);
126
139
  return;
@@ -135,11 +148,12 @@ export class CircuitRelayServer extends TypedEventEmitter {
135
148
  // result.expire is non-null if `ReservationStore.reserve` returns with status == OK
136
149
  if (result.expire != null) {
137
150
  const ttl = (result.expire * 1000) - Date.now();
138
- await this.components.peerStore.merge(connection.remotePeer, {
151
+ await this.peerStore.merge(connection.remotePeer, {
139
152
  tags: {
140
- [RELAY_SOURCE_TAG]: { value: 1, ttl }
153
+ [RELAY_SOURCE_TAG]: { value: 1, ttl },
154
+ [KEEP_ALIVE_SOURCE_TAG]: { value: 1, ttl }
141
155
  }
142
- }, options);
156
+ });
143
157
  }
144
158
  await hopstr.write({
145
159
  type: HopMessage.Type.STATUS,
@@ -148,18 +162,17 @@ export class CircuitRelayServer extends TypedEventEmitter {
148
162
  limit: this.reservationStore.get(connection.remotePeer)?.limit
149
163
  }, options);
150
164
  this.log('sent confirmation response to %s', connection.remotePeer);
151
- // close writable end of stream
152
- await hopstr.unwrap().unwrap().close(options);
153
165
  }
154
166
  catch (err) {
155
167
  this.log.error('failed to send confirmation response to %p - %e', connection.remotePeer, err);
156
168
  this.reservationStore.removeReservation(connection.remotePeer);
157
169
  try {
158
- await this.components.peerStore.merge(connection.remotePeer, {
170
+ await this.peerStore.merge(connection.remotePeer, {
159
171
  tags: {
160
- [RELAY_SOURCE_TAG]: undefined
172
+ [RELAY_SOURCE_TAG]: undefined,
173
+ [KEEP_ALIVE_SOURCE_TAG]: undefined
161
174
  }
162
- }, options);
175
+ });
163
176
  }
164
177
  catch (err) {
165
178
  this.log.error('failed to untag relay source peer %p - %e', connection.remotePeer, err);
@@ -168,7 +181,7 @@ export class CircuitRelayServer extends TypedEventEmitter {
168
181
  }
169
182
  async makeReservation(remotePeer, expire) {
170
183
  const addrs = [];
171
- for (const relayAddr of this.components.addressManager.getAddresses()) {
184
+ for (const relayAddr of this.addressManager.getAddresses()) {
172
185
  if (relayAddr.toString().includes('/p2p-circuit')) {
173
186
  continue;
174
187
  }
@@ -176,9 +189,9 @@ export class CircuitRelayServer extends TypedEventEmitter {
176
189
  }
177
190
  const envelope = await RecordEnvelope.seal(new ReservationVoucherRecord({
178
191
  peer: remotePeer,
179
- relay: this.components.peerId,
192
+ relay: this.peerId,
180
193
  expiration: expire
181
- }), this.components.privateKey);
194
+ }), this.privateKey);
182
195
  return {
183
196
  addrs,
184
197
  expire,
@@ -187,7 +200,7 @@ export class CircuitRelayServer extends TypedEventEmitter {
187
200
  payloadType: envelope.payloadType,
188
201
  payload: {
189
202
  peer: remotePeer.toMultihash().bytes,
190
- relay: this.components.peerId.toMultihash().bytes,
203
+ relay: this.peerId.toMultihash().bytes,
191
204
  expiration: expire
192
205
  },
193
206
  signature: envelope.signature
@@ -196,7 +209,7 @@ export class CircuitRelayServer extends TypedEventEmitter {
196
209
  }
197
210
  async handleConnect({ stream, request, connection }, options) {
198
211
  const hopstr = stream.pb(HopMessage);
199
- if (Circuit.matches(connection.remoteAddr)) {
212
+ if (isRelayAddr(connection.remoteAddr)) {
200
213
  this.log.error('relay reservation over circuit connection denied for peer: %p', connection.remotePeer);
201
214
  await hopstr.write({ type: HopMessage.Type.STATUS, status: Status.PERMISSION_DENIED }, options);
202
215
  return;
@@ -222,12 +235,12 @@ export class CircuitRelayServer extends TypedEventEmitter {
222
235
  await hopstr.write({ type: HopMessage.Type.STATUS, status: Status.NO_RESERVATION }, options);
223
236
  return;
224
237
  }
225
- if ((await this.components.connectionGater.denyOutboundRelayedConnection?.(connection.remotePeer, dstPeer)) === true) {
238
+ if ((await this.connectionGater.denyOutboundRelayedConnection?.(connection.remotePeer, dstPeer)) === true) {
226
239
  this.log.error('hop connect for %p to %p denied by connection gater', connection.remotePeer, dstPeer);
227
240
  await hopstr.write({ type: HopMessage.Type.STATUS, status: Status.PERMISSION_DENIED }, options);
228
241
  return;
229
242
  }
230
- const connections = this.components.connectionManager.getConnections(dstPeer);
243
+ const connections = this.connectionManager.getConnections(dstPeer);
231
244
  if (connections.length === 0) {
232
245
  this.log('hop connect denied for destination peer %p not having a connection for %p as there is no destination connection', dstPeer, connection.remotePeer);
233
246
  await hopstr.write({ type: HopMessage.Type.STATUS, status: Status.NO_RESERVATION }, options);
@@ -255,9 +268,10 @@ export class CircuitRelayServer extends TypedEventEmitter {
255
268
  status: Status.OK,
256
269
  limit: reservation?.limit
257
270
  }, options);
271
+ const sourceStream = stream.unwrap();
258
272
  this.log('connection from %p to %p established - merging streams', connection.remotePeer, dstPeer);
259
273
  // Short circuit the two streams to create the relayed connection
260
- createLimitedRelay(stream.unwrap(), destinationStream, this.shutdownController.signal, reservation, {
274
+ createLimitedRelay(sourceStream, destinationStream, this.shutdownController.signal, reservation, {
261
275
  log: this.log
262
276
  });
263
277
  }
@@ -266,7 +280,7 @@ export class CircuitRelayServer extends TypedEventEmitter {
266
280
  */
267
281
  async stopHop({ connection, request }, options) {
268
282
  this.log('starting circuit relay v2 stop request to %s', connection.remotePeer);
269
- const stream = await connection.newStream(RELAY_V2_STOP_CODEC, {
283
+ const stream = await connection.newStream([RELAY_V2_STOP_CODEC], {
270
284
  maxOutboundStreams: this.maxOutboundStopStreams,
271
285
  runOnLimitedConnection: true,
272
286
  ...options
@@ -297,4 +311,9 @@ export class CircuitRelayServer extends TypedEventEmitter {
297
311
  return this.reservationStore.reservations;
298
312
  }
299
313
  }
314
+ export function circuitRelayServer(init = {}) {
315
+ return (components) => {
316
+ return new CircuitRelayServer(components, init);
317
+ };
318
+ }
300
319
  //# sourceMappingURL=index.js.map