@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.
- package/LICENSE +4 -0
- package/README.md +69 -0
- package/dist/index.min.js +45 -0
- package/dist/src/constants.d.ts +55 -0
- package/dist/src/constants.d.ts.map +1 -0
- package/dist/src/constants.js +61 -0
- package/dist/src/constants.js.map +1 -0
- package/dist/src/index.d.ts +56 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +39 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/pb/index.d.ts +93 -0
- package/dist/src/pb/index.d.ts.map +1 -0
- package/dist/src/pb/index.js +425 -0
- package/dist/src/pb/index.js.map +1 -0
- package/dist/src/server/advert-service.d.ts +46 -0
- package/dist/src/server/advert-service.d.ts.map +1 -0
- package/dist/src/server/advert-service.js +72 -0
- package/dist/src/server/advert-service.js.map +1 -0
- package/dist/src/server/index.d.ts +67 -0
- package/dist/src/server/index.d.ts.map +1 -0
- package/dist/src/server/index.js +313 -0
- package/dist/src/server/index.js.map +1 -0
- package/dist/src/server/reservation-store.d.ts +49 -0
- package/dist/src/server/reservation-store.d.ts.map +1 -0
- package/dist/src/server/reservation-store.js +65 -0
- package/dist/src/server/reservation-store.js.map +1 -0
- package/dist/src/server/reservation-voucher.d.ts +18 -0
- package/dist/src/server/reservation-voucher.d.ts.map +1 -0
- package/dist/src/server/reservation-voucher.js +36 -0
- package/dist/src/server/reservation-voucher.js.map +1 -0
- package/dist/src/transport/discovery.d.ts +48 -0
- package/dist/src/transport/discovery.d.ts.map +1 -0
- package/dist/src/transport/discovery.js +97 -0
- package/dist/src/transport/discovery.js.map +1 -0
- package/dist/src/transport/index.d.ts +58 -0
- package/dist/src/transport/index.d.ts.map +1 -0
- package/dist/src/transport/index.js +279 -0
- package/dist/src/transport/index.js.map +1 -0
- package/dist/src/transport/listener.d.ts +11 -0
- package/dist/src/transport/listener.d.ts.map +1 -0
- package/dist/src/transport/listener.js +66 -0
- package/dist/src/transport/listener.js.map +1 -0
- package/dist/src/transport/reservation-store.d.ts +74 -0
- package/dist/src/transport/reservation-store.d.ts.map +1 -0
- package/dist/src/transport/reservation-store.js +209 -0
- package/dist/src/transport/reservation-store.js.map +1 -0
- package/dist/src/utils.d.ts +14 -0
- package/dist/src/utils.d.ts.map +1 -0
- package/dist/src/utils.js +106 -0
- package/dist/src/utils.js.map +1 -0
- package/package.json +83 -0
- package/src/constants.ts +79 -0
- package/src/index.ts +64 -0
- package/src/pb/index.proto +67 -0
- package/src/pb/index.ts +539 -0
- package/src/server/advert-service.ts +109 -0
- package/src/server/index.ts +446 -0
- package/src/server/reservation-store.ts +116 -0
- package/src/server/reservation-voucher.ts +51 -0
- package/src/transport/discovery.ts +138 -0
- package/src/transport/index.ts +399 -0
- package/src/transport/listener.ts +98 -0
- package/src/transport/reservation-store.ts +312 -0
- package/src/utils.ts +134 -0
@@ -0,0 +1,97 @@
|
|
1
|
+
import { TypedEventEmitter } from '@libp2p/interface/events';
|
2
|
+
import { RELAY_RENDEZVOUS_NS, RELAY_V2_HOP_CODEC } from '../constants.js';
|
3
|
+
import { namespaceToCid } from '../utils.js';
|
4
|
+
/**
|
5
|
+
* ReservationManager automatically makes a circuit v2 reservation on any connected
|
6
|
+
* peers that support the circuit v2 HOP protocol.
|
7
|
+
*/
|
8
|
+
export class RelayDiscovery extends TypedEventEmitter {
|
9
|
+
peerId;
|
10
|
+
peerStore;
|
11
|
+
contentRouting;
|
12
|
+
registrar;
|
13
|
+
started;
|
14
|
+
topologyId;
|
15
|
+
log;
|
16
|
+
constructor(components) {
|
17
|
+
super();
|
18
|
+
this.log = components.logger.forComponent('libp2p:circuit-relay:discover-relays');
|
19
|
+
this.started = false;
|
20
|
+
this.peerId = components.peerId;
|
21
|
+
this.peerStore = components.peerStore;
|
22
|
+
this.contentRouting = components.contentRouting;
|
23
|
+
this.registrar = components.registrar;
|
24
|
+
}
|
25
|
+
isStarted() {
|
26
|
+
return this.started;
|
27
|
+
}
|
28
|
+
async start() {
|
29
|
+
// register a topology listener for when new peers are encountered
|
30
|
+
// that support the hop protocol
|
31
|
+
this.topologyId = await this.registrar.register(RELAY_V2_HOP_CODEC, {
|
32
|
+
notifyOnTransient: true,
|
33
|
+
onConnect: (peerId) => {
|
34
|
+
this.safeDispatchEvent('relay:discover', { detail: peerId });
|
35
|
+
}
|
36
|
+
});
|
37
|
+
void this.discover()
|
38
|
+
.catch(err => {
|
39
|
+
this.log.error('error listening on relays', err);
|
40
|
+
});
|
41
|
+
this.started = true;
|
42
|
+
}
|
43
|
+
stop() {
|
44
|
+
if (this.topologyId != null) {
|
45
|
+
this.registrar.unregister(this.topologyId);
|
46
|
+
}
|
47
|
+
this.started = false;
|
48
|
+
}
|
49
|
+
/**
|
50
|
+
* Try to listen on available hop relay connections.
|
51
|
+
* The following order will happen while we do not have enough relays:
|
52
|
+
*
|
53
|
+
* 1. Check the metadata store for known relays, try to listen on the ones we are already connected
|
54
|
+
* 2. Dial and try to listen on the peers we know that support hop but are not connected
|
55
|
+
* 3. Search the network
|
56
|
+
*/
|
57
|
+
async discover() {
|
58
|
+
this.log('searching peer store for relays');
|
59
|
+
const peers = (await this.peerStore.all({
|
60
|
+
filters: [
|
61
|
+
// filter by a list of peers supporting RELAY_V2_HOP and ones we are not listening on
|
62
|
+
(peer) => {
|
63
|
+
return peer.protocols.includes(RELAY_V2_HOP_CODEC);
|
64
|
+
}
|
65
|
+
],
|
66
|
+
orders: [
|
67
|
+
() => Math.random() < 0.5 ? 1 : -1
|
68
|
+
]
|
69
|
+
}));
|
70
|
+
for (const peer of peers) {
|
71
|
+
this.log('found relay peer %p in content peer store', peer.id);
|
72
|
+
this.safeDispatchEvent('relay:discover', { detail: peer.id });
|
73
|
+
}
|
74
|
+
this.log('found %d relay peers in peer store', peers.length);
|
75
|
+
try {
|
76
|
+
this.log('searching content routing for relays');
|
77
|
+
const cid = await namespaceToCid(RELAY_RENDEZVOUS_NS);
|
78
|
+
let found = 0;
|
79
|
+
for await (const provider of this.contentRouting.findProviders(cid)) {
|
80
|
+
if (provider.multiaddrs.length > 0 && !provider.id.equals(this.peerId)) {
|
81
|
+
const peerId = provider.id;
|
82
|
+
found++;
|
83
|
+
await this.peerStore.merge(peerId, {
|
84
|
+
multiaddrs: provider.multiaddrs
|
85
|
+
});
|
86
|
+
this.log('found relay peer %p in content routing', peerId);
|
87
|
+
this.safeDispatchEvent('relay:discover', { detail: peerId });
|
88
|
+
}
|
89
|
+
}
|
90
|
+
this.log('found %d relay peers in content routing', found);
|
91
|
+
}
|
92
|
+
catch (err) {
|
93
|
+
this.log.error('failed when finding relays on the network', err);
|
94
|
+
}
|
95
|
+
}
|
96
|
+
}
|
97
|
+
//# sourceMappingURL=discovery.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"discovery.js","sourceRoot":"","sources":["../../../src/transport/discovery.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAA;AAC5D,OAAO,EACL,mBAAmB,EACnB,kBAAkB,EACnB,MAAM,iBAAiB,CAAA;AACxB,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AAwB5C;;;GAGG;AACH,MAAM,OAAO,cAAe,SAAQ,iBAAuC;IACxD,MAAM,CAAQ;IACd,SAAS,CAAW;IACpB,cAAc,CAAgB;IAC9B,SAAS,CAAW;IAC7B,OAAO,CAAS;IAChB,UAAU,CAAS;IACV,GAAG,CAAQ;IAE5B,YAAa,UAAoC;QAC/C,KAAK,EAAE,CAAA;QAEP,IAAI,CAAC,GAAG,GAAG,UAAU,CAAC,MAAM,CAAC,YAAY,CAAC,sCAAsC,CAAC,CAAA;QACjF,IAAI,CAAC,OAAO,GAAG,KAAK,CAAA;QACpB,IAAI,CAAC,MAAM,GAAG,UAAU,CAAC,MAAM,CAAA;QAC/B,IAAI,CAAC,SAAS,GAAG,UAAU,CAAC,SAAS,CAAA;QACr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I,CAAC,GAAG,CAAC,yCAAyC,EAAE,KAAK,CAAC,CAAA;SAC3D;QAAC,OAAO,GAAQ,EAAE;YACjB,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,2CAA2C,EAAE,GAAG,CAAC,CAAA;SACjE;IACH,CAAC;CACF"}
|
@@ -0,0 +1,58 @@
|
|
1
|
+
import { type Transport, type Upgrader } from '@libp2p/interface/transport';
|
2
|
+
import { type RelayDiscoveryComponents } from './discovery.js';
|
3
|
+
import { type RelayStoreInit } from './reservation-store.js';
|
4
|
+
import type { Libp2pEvents, ComponentLogger } from '@libp2p/interface';
|
5
|
+
import type { ConnectionGater } from '@libp2p/interface/connection-gater';
|
6
|
+
import type { ContentRouting } from '@libp2p/interface/content-routing';
|
7
|
+
import type { TypedEventTarget } from '@libp2p/interface/events';
|
8
|
+
import type { PeerId } from '@libp2p/interface/peer-id';
|
9
|
+
import type { PeerStore } from '@libp2p/interface/peer-store';
|
10
|
+
import type { AddressManager } from '@libp2p/interface-internal/address-manager';
|
11
|
+
import type { ConnectionManager } from '@libp2p/interface-internal/connection-manager';
|
12
|
+
import type { Registrar } from '@libp2p/interface-internal/registrar';
|
13
|
+
export interface CircuitRelayTransportComponents extends RelayDiscoveryComponents {
|
14
|
+
peerId: PeerId;
|
15
|
+
peerStore: PeerStore;
|
16
|
+
registrar: Registrar;
|
17
|
+
connectionManager: ConnectionManager;
|
18
|
+
upgrader: Upgrader;
|
19
|
+
addressManager: AddressManager;
|
20
|
+
contentRouting: ContentRouting;
|
21
|
+
connectionGater: ConnectionGater;
|
22
|
+
events: TypedEventTarget<Libp2pEvents>;
|
23
|
+
logger: ComponentLogger;
|
24
|
+
}
|
25
|
+
/**
|
26
|
+
* RelayConfig configures the circuit v2 relay transport.
|
27
|
+
*/
|
28
|
+
export interface CircuitRelayTransportInit extends RelayStoreInit {
|
29
|
+
/**
|
30
|
+
* The number of peers running diable relays to search for and
|
31
|
+
* connect to. (default: 0)
|
32
|
+
*/
|
33
|
+
discoverRelays?: number;
|
34
|
+
/**
|
35
|
+
* The maximum number of simultaneous STOP inbound streams that can be open at
|
36
|
+
* once - each inbound relayed connection uses a STOP stream (default: 300)
|
37
|
+
*/
|
38
|
+
maxInboundStopStreams?: number;
|
39
|
+
/**
|
40
|
+
* The maximum number of simultaneous STOP outbound streams that can be open at
|
41
|
+
* once. If this transport is used along with the relay server these settings
|
42
|
+
* should be set to the same value (default: 300)
|
43
|
+
*/
|
44
|
+
maxOutboundStopStreams?: number;
|
45
|
+
/**
|
46
|
+
* Incoming STOP requests (e.g. when a remote peer wants to dial us via a relay)
|
47
|
+
* must finish the initial protocol negotiation within this timeout in ms
|
48
|
+
* (default: 30000)
|
49
|
+
*/
|
50
|
+
stopTimeout?: number;
|
51
|
+
/**
|
52
|
+
* When creating a reservation it must complete within this number of ms
|
53
|
+
* (default: 10000)
|
54
|
+
*/
|
55
|
+
reservationCompletionTimeout?: number;
|
56
|
+
}
|
57
|
+
export declare function circuitRelayTransport(init?: CircuitRelayTransportInit): (components: CircuitRelayTransportComponents) => Transport;
|
58
|
+
//# sourceMappingURL=index.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/transport/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAU,KAAK,SAAS,EAA6C,KAAK,QAAQ,EAAE,MAAM,6BAA6B,CAAA;AAQ9H,OAAO,EAAkB,KAAK,wBAAwB,EAAE,MAAM,gBAAgB,CAAA;AAE9E,OAAO,EAAE,KAAK,cAAc,EAAoB,MAAM,wBAAwB,CAAA;AAC9E,OAAO,KAAK,EAAE,YAAY,EAAgB,eAAe,EAAU,MAAM,mBAAmB,CAAA;AAE5F,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,oCAAoC,CAAA;AACzE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,mCAAmC,CAAA;AACvE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAA;AAChE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAA;AACvD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAA;AAC7D,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,4CAA4C,CAAA;AAChF,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,+CAA+C,CAAA;AACtF,OAAO,KAAK,EAAsB,SAAS,EAAE,MAAM,sCAAsC,CAAA;AAiBzF,MAAM,WAAW,+BAAgC,SAAQ,wBAAwB;IAC/E,MAAM,EAAE,MAAM,CAAA;IACd,SAAS,EAAE,SAAS,CAAA;IACpB,SAAS,EAAE,SAAS,CAAA;IACpB,iBAAiB,EAAE,iBAAiB,CAAA;IACpC,QAAQ,EAAE,QAAQ,CAAA;IAClB,cAAc,EAAE,cAAc,CAAA;IAC9B,cAAc,EAAE,cAAc,CAAA;IAC9B,eAAe,EAAE,eAAe,CAAA;IAChC,MAAM,EAAE,gBAAgB,CAAC,YAAY,CAAC,CAAA;IACtC,MAAM,EAAE,eAAe,CAAA;CACxB;AAYD;;GAEG;AACH,MAAM,WAAW,yBAA0B,SAAQ,cAAc;IAC/D;;;OAGG;IACH,cAAc,CAAC,EAAE,MAAM,CAAA;IAEvB;;;OAGG;IACH,qBAAqB,CAAC,EAAE,MAAM,CAAA;IAE9B;;;;OAIG;IACH,sBAAsB,CAAC,EAAE,MAAM,CAAA;IAE/B;;;;OAIG;IACH,WAAW,CAAC,EAAE,MAAM,CAAA;IAEpB;;;OAGG;IACH,4BAA4B,CAAC,EAAE,MAAM,CAAA;CACtC;AA0SD,wBAAgB,qBAAqB,CAAE,IAAI,GAAE,yBAA8B,GAAG,CAAC,UAAU,EAAE,+BAA+B,KAAK,SAAS,CAIvI"}
|
@@ -0,0 +1,279 @@
|
|
1
|
+
import { CodeError } from '@libp2p/interface/errors';
|
2
|
+
import { symbol } from '@libp2p/interface/transport';
|
3
|
+
import { peerIdFromBytes, peerIdFromString } from '@libp2p/peer-id';
|
4
|
+
import { streamToMaConnection } from '@libp2p/utils/stream-to-ma-conn';
|
5
|
+
import * as mafmt from '@multiformats/mafmt';
|
6
|
+
import { multiaddr } from '@multiformats/multiaddr';
|
7
|
+
import { pbStream } from 'it-protobuf-stream';
|
8
|
+
import { CIRCUIT_PROTO_CODE, ERR_HOP_REQUEST_FAILED, ERR_RELAYED_DIAL, MAX_CONNECTIONS, RELAY_V2_HOP_CODEC, RELAY_V2_STOP_CODEC } from '../constants.js';
|
9
|
+
import { StopMessage, HopMessage, Status } from '../pb/index.js';
|
10
|
+
import { RelayDiscovery } from './discovery.js';
|
11
|
+
import { createListener } from './listener.js';
|
12
|
+
import { ReservationStore } from './reservation-store.js';
|
13
|
+
const isValidStop = (request) => {
|
14
|
+
if (request.peer == null) {
|
15
|
+
return false;
|
16
|
+
}
|
17
|
+
try {
|
18
|
+
request.peer.addrs.forEach(multiaddr);
|
19
|
+
}
|
20
|
+
catch {
|
21
|
+
return false;
|
22
|
+
}
|
23
|
+
return true;
|
24
|
+
};
|
25
|
+
const defaults = {
|
26
|
+
maxInboundStopStreams: MAX_CONNECTIONS,
|
27
|
+
maxOutboundStopStreams: MAX_CONNECTIONS,
|
28
|
+
stopTimeout: 30000
|
29
|
+
};
|
30
|
+
class CircuitRelayTransport {
|
31
|
+
discovery;
|
32
|
+
registrar;
|
33
|
+
peerStore;
|
34
|
+
connectionManager;
|
35
|
+
peerId;
|
36
|
+
upgrader;
|
37
|
+
addressManager;
|
38
|
+
connectionGater;
|
39
|
+
reservationStore;
|
40
|
+
logger;
|
41
|
+
maxInboundStopStreams;
|
42
|
+
maxOutboundStopStreams;
|
43
|
+
stopTimeout;
|
44
|
+
started;
|
45
|
+
log;
|
46
|
+
constructor(components, init) {
|
47
|
+
this.log = components.logger.forComponent('libp2p:circuit-relay:transport');
|
48
|
+
this.registrar = components.registrar;
|
49
|
+
this.peerStore = components.peerStore;
|
50
|
+
this.connectionManager = components.connectionManager;
|
51
|
+
this.logger = components.logger;
|
52
|
+
this.peerId = components.peerId;
|
53
|
+
this.upgrader = components.upgrader;
|
54
|
+
this.addressManager = components.addressManager;
|
55
|
+
this.connectionGater = components.connectionGater;
|
56
|
+
this.maxInboundStopStreams = init.maxInboundStopStreams ?? defaults.maxInboundStopStreams;
|
57
|
+
this.maxOutboundStopStreams = init.maxOutboundStopStreams ?? defaults.maxOutboundStopStreams;
|
58
|
+
this.stopTimeout = init.stopTimeout ?? defaults.stopTimeout;
|
59
|
+
if (init.discoverRelays != null && init.discoverRelays > 0) {
|
60
|
+
this.discovery = new RelayDiscovery(components);
|
61
|
+
this.discovery.addEventListener('relay:discover', (evt) => {
|
62
|
+
this.reservationStore.addRelay(evt.detail, 'discovered')
|
63
|
+
.catch(err => {
|
64
|
+
this.log.error('could not add discovered relay %p', evt.detail, err);
|
65
|
+
});
|
66
|
+
});
|
67
|
+
}
|
68
|
+
this.reservationStore = new ReservationStore(components, init);
|
69
|
+
this.reservationStore.addEventListener('relay:not-enough-relays', () => {
|
70
|
+
this.discovery?.discover()
|
71
|
+
.catch(err => {
|
72
|
+
this.log.error('could not discover relays', err);
|
73
|
+
});
|
74
|
+
});
|
75
|
+
this.started = false;
|
76
|
+
}
|
77
|
+
isStarted() {
|
78
|
+
return this.started;
|
79
|
+
}
|
80
|
+
async start() {
|
81
|
+
await this.reservationStore.start();
|
82
|
+
await this.discovery?.start();
|
83
|
+
await this.registrar.handle(RELAY_V2_STOP_CODEC, (data) => {
|
84
|
+
void this.onStop(data).catch(err => {
|
85
|
+
this.log.error('error while handling STOP protocol', err);
|
86
|
+
data.stream.abort(err);
|
87
|
+
});
|
88
|
+
}, {
|
89
|
+
maxInboundStreams: this.maxInboundStopStreams,
|
90
|
+
maxOutboundStreams: this.maxOutboundStopStreams,
|
91
|
+
runOnTransientConnection: true
|
92
|
+
});
|
93
|
+
this.started = true;
|
94
|
+
}
|
95
|
+
async stop() {
|
96
|
+
this.discovery?.stop();
|
97
|
+
await this.reservationStore.stop();
|
98
|
+
await this.registrar.unhandle(RELAY_V2_STOP_CODEC);
|
99
|
+
this.started = false;
|
100
|
+
}
|
101
|
+
[symbol] = true;
|
102
|
+
[Symbol.toStringTag] = 'libp2p/circuit-relay-v2';
|
103
|
+
/**
|
104
|
+
* Dial a peer over a relay
|
105
|
+
*/
|
106
|
+
async dial(ma, options = {}) {
|
107
|
+
if (ma.protoCodes().filter(code => code === CIRCUIT_PROTO_CODE).length !== 1) {
|
108
|
+
const errMsg = 'Invalid circuit relay address';
|
109
|
+
this.log.error(errMsg, ma);
|
110
|
+
throw new CodeError(errMsg, ERR_RELAYED_DIAL);
|
111
|
+
}
|
112
|
+
// Check the multiaddr to see if it contains a relay and a destination peer
|
113
|
+
const addrs = ma.toString().split('/p2p-circuit');
|
114
|
+
const relayAddr = multiaddr(addrs[0]);
|
115
|
+
const destinationAddr = multiaddr(addrs[addrs.length - 1]);
|
116
|
+
const relayId = relayAddr.getPeerId();
|
117
|
+
const destinationId = destinationAddr.getPeerId();
|
118
|
+
if (relayId == null || destinationId == null) {
|
119
|
+
const errMsg = `Circuit relay dial to ${ma.toString()} failed as address did not have peer ids`;
|
120
|
+
this.log.error(errMsg);
|
121
|
+
throw new CodeError(errMsg, ERR_RELAYED_DIAL);
|
122
|
+
}
|
123
|
+
const relayPeer = peerIdFromString(relayId);
|
124
|
+
const destinationPeer = peerIdFromString(destinationId);
|
125
|
+
let disconnectOnFailure = false;
|
126
|
+
const relayConnections = this.connectionManager.getConnections(relayPeer);
|
127
|
+
let relayConnection = relayConnections[0];
|
128
|
+
if (relayConnection == null) {
|
129
|
+
await this.peerStore.merge(relayPeer, {
|
130
|
+
multiaddrs: [relayAddr]
|
131
|
+
});
|
132
|
+
relayConnection = await this.connectionManager.openConnection(relayPeer, options);
|
133
|
+
disconnectOnFailure = true;
|
134
|
+
}
|
135
|
+
let stream;
|
136
|
+
try {
|
137
|
+
stream = await relayConnection.newStream([RELAY_V2_HOP_CODEC]);
|
138
|
+
return await this.connectV2({
|
139
|
+
stream,
|
140
|
+
connection: relayConnection,
|
141
|
+
destinationPeer,
|
142
|
+
destinationAddr,
|
143
|
+
relayAddr,
|
144
|
+
ma,
|
145
|
+
disconnectOnFailure
|
146
|
+
});
|
147
|
+
}
|
148
|
+
catch (err) {
|
149
|
+
this.log.error('circuit relay dial to destination %p via relay %p failed', destinationPeer, relayPeer, err);
|
150
|
+
if (stream != null) {
|
151
|
+
stream.abort(err);
|
152
|
+
}
|
153
|
+
disconnectOnFailure && await relayConnection.close();
|
154
|
+
throw err;
|
155
|
+
}
|
156
|
+
}
|
157
|
+
async connectV2({ stream, connection, destinationPeer, destinationAddr, relayAddr, ma, disconnectOnFailure }) {
|
158
|
+
try {
|
159
|
+
const pbstr = pbStream(stream);
|
160
|
+
const hopstr = pbstr.pb(HopMessage);
|
161
|
+
await hopstr.write({
|
162
|
+
type: HopMessage.Type.CONNECT,
|
163
|
+
peer: {
|
164
|
+
id: destinationPeer.toBytes(),
|
165
|
+
addrs: [multiaddr(destinationAddr).bytes]
|
166
|
+
}
|
167
|
+
});
|
168
|
+
const status = await hopstr.read();
|
169
|
+
if (status.status !== Status.OK) {
|
170
|
+
throw new CodeError(`failed to connect via relay with status ${status?.status?.toString() ?? 'undefined'}`, ERR_HOP_REQUEST_FAILED);
|
171
|
+
}
|
172
|
+
const maConn = streamToMaConnection({
|
173
|
+
stream: pbstr.unwrap(),
|
174
|
+
remoteAddr: ma,
|
175
|
+
localAddr: relayAddr.encapsulate(`/p2p-circuit/p2p/${this.peerId.toString()}`),
|
176
|
+
logger: this.logger
|
177
|
+
});
|
178
|
+
this.log('new outbound transient connection %a', maConn.remoteAddr);
|
179
|
+
return await this.upgrader.upgradeOutbound(maConn, {
|
180
|
+
transient: true
|
181
|
+
});
|
182
|
+
}
|
183
|
+
catch (err) {
|
184
|
+
this.log.error(`Circuit relay dial to destination ${destinationPeer.toString()} via relay ${connection.remotePeer.toString()} failed`, err);
|
185
|
+
disconnectOnFailure && await connection.close();
|
186
|
+
throw err;
|
187
|
+
}
|
188
|
+
}
|
189
|
+
/**
|
190
|
+
* Create a listener
|
191
|
+
*/
|
192
|
+
createListener(options) {
|
193
|
+
return createListener({
|
194
|
+
connectionManager: this.connectionManager,
|
195
|
+
relayStore: this.reservationStore,
|
196
|
+
logger: this.logger
|
197
|
+
});
|
198
|
+
}
|
199
|
+
/**
|
200
|
+
* Filter check for all Multiaddrs that this transport can dial on
|
201
|
+
*
|
202
|
+
* @param {Multiaddr[]} multiaddrs
|
203
|
+
* @returns {Multiaddr[]}
|
204
|
+
*/
|
205
|
+
filter(multiaddrs) {
|
206
|
+
multiaddrs = Array.isArray(multiaddrs) ? multiaddrs : [multiaddrs];
|
207
|
+
return multiaddrs.filter((ma) => {
|
208
|
+
return mafmt.Circuit.matches(ma);
|
209
|
+
});
|
210
|
+
}
|
211
|
+
/**
|
212
|
+
* An incoming STOP request means a remote peer wants to dial us via a relay
|
213
|
+
*/
|
214
|
+
async onStop({ connection, stream }) {
|
215
|
+
const signal = AbortSignal.timeout(this.stopTimeout);
|
216
|
+
const pbstr = pbStream(stream).pb(StopMessage);
|
217
|
+
const request = await pbstr.read({
|
218
|
+
signal
|
219
|
+
});
|
220
|
+
this.log('new circuit relay v2 stop stream from %p with type %s', connection.remotePeer, request.type);
|
221
|
+
if (request?.type === undefined) {
|
222
|
+
this.log.error('type was missing from circuit v2 stop protocol request from %s', connection.remotePeer);
|
223
|
+
await pbstr.write({ type: StopMessage.Type.STATUS, status: Status.MALFORMED_MESSAGE }, {
|
224
|
+
signal
|
225
|
+
});
|
226
|
+
await stream.close();
|
227
|
+
return;
|
228
|
+
}
|
229
|
+
// Validate the STOP request has the required input
|
230
|
+
if (request.type !== StopMessage.Type.CONNECT) {
|
231
|
+
this.log.error('invalid stop connect request via peer %p', connection.remotePeer);
|
232
|
+
await pbstr.write({ type: StopMessage.Type.STATUS, status: Status.UNEXPECTED_MESSAGE }, {
|
233
|
+
signal
|
234
|
+
});
|
235
|
+
await stream.close();
|
236
|
+
return;
|
237
|
+
}
|
238
|
+
if (!isValidStop(request)) {
|
239
|
+
this.log.error('invalid stop connect request via peer %p', connection.remotePeer);
|
240
|
+
await pbstr.write({ type: StopMessage.Type.STATUS, status: Status.MALFORMED_MESSAGE }, {
|
241
|
+
signal
|
242
|
+
});
|
243
|
+
await stream.close();
|
244
|
+
return;
|
245
|
+
}
|
246
|
+
const remotePeerId = peerIdFromBytes(request.peer.id);
|
247
|
+
if ((await this.connectionGater.denyInboundRelayedConnection?.(connection.remotePeer, remotePeerId)) === true) {
|
248
|
+
this.log.error('connection gater denied inbound relayed connection from %p', connection.remotePeer);
|
249
|
+
await pbstr.write({ type: StopMessage.Type.STATUS, status: Status.PERMISSION_DENIED }, {
|
250
|
+
signal
|
251
|
+
});
|
252
|
+
await stream.close();
|
253
|
+
return;
|
254
|
+
}
|
255
|
+
this.log.trace('sending success response to %p', connection.remotePeer);
|
256
|
+
await pbstr.write({ type: StopMessage.Type.STATUS, status: Status.OK }, {
|
257
|
+
signal
|
258
|
+
});
|
259
|
+
const remoteAddr = connection.remoteAddr.encapsulate(`/p2p-circuit/p2p/${remotePeerId.toString()}`);
|
260
|
+
const localAddr = this.addressManager.getAddresses()[0];
|
261
|
+
const maConn = streamToMaConnection({
|
262
|
+
stream: pbstr.unwrap().unwrap(),
|
263
|
+
remoteAddr,
|
264
|
+
localAddr,
|
265
|
+
logger: this.logger
|
266
|
+
});
|
267
|
+
this.log('new inbound transient connection %a', maConn.remoteAddr);
|
268
|
+
await this.upgrader.upgradeInbound(maConn, {
|
269
|
+
transient: true
|
270
|
+
});
|
271
|
+
this.log('%s connection %a upgraded', 'inbound', maConn.remoteAddr);
|
272
|
+
}
|
273
|
+
}
|
274
|
+
export function circuitRelayTransport(init = {}) {
|
275
|
+
return (components) => {
|
276
|
+
return new CircuitRelayTransport(components, init);
|
277
|
+
};
|
278
|
+
}
|
279
|
+
//# sourceMappingURL=index.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":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,MAAM,EAAE,CAAC,MAAM,EAAE;YAC/B,UAAU;YACV,SAAS;YACT,MAAM,EAAE,IAAI,CAAC,MAAM;SACpB,CAAC,CAAA;QAEF,IAAI,CAAC,GAAG,CAAC,qCAAqC,EAAE,MAAM,CAAC,UAAU,CAAC,CAAA;QAClE,MAAM,IAAI,CAAC,QAAQ,CAAC,cAAc,CAAC,MAAM,EAAE;YACzC,SAAS,EAAE,IAAI;SAChB,CAAC,CAAA;QACF,IAAI,CAAC,GAAG,CAAC,2BAA2B,EAAE,SAAS,EAAE,MAAM,CAAC,UAAU,CAAC,CAAA;IACrE,CAAC;CACF;AAED,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"}
|
@@ -0,0 +1,11 @@
|
|
1
|
+
import type { ReservationStore } from './reservation-store.js';
|
2
|
+
import type { ComponentLogger } from '@libp2p/interface';
|
3
|
+
import type { Listener } from '@libp2p/interface/transport';
|
4
|
+
import type { ConnectionManager } from '@libp2p/interface-internal/connection-manager';
|
5
|
+
export interface CircuitRelayTransportListenerComponents {
|
6
|
+
connectionManager: ConnectionManager;
|
7
|
+
relayStore: ReservationStore;
|
8
|
+
logger: ComponentLogger;
|
9
|
+
}
|
10
|
+
export declare function createListener(options: CircuitRelayTransportListenerComponents): Listener;
|
11
|
+
//# sourceMappingURL=listener.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"listener.d.ts","sourceRoot":"","sources":["../../../src/transport/listener.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAA;AAC9D,OAAO,KAAK,EAAE,eAAe,EAAU,MAAM,mBAAmB,CAAA;AAEhE,OAAO,KAAK,EAAE,QAAQ,EAAkB,MAAM,6BAA6B,CAAA;AAC3E,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,+CAA+C,CAAA;AAGtF,MAAM,WAAW,uCAAuC;IACtD,iBAAiB,EAAE,iBAAiB,CAAA;IACpC,UAAU,EAAE,gBAAgB,CAAA;IAC5B,MAAM,EAAE,eAAe,CAAA;CACxB;AAgFD,wBAAgB,cAAc,CAAE,OAAO,EAAE,uCAAuC,GAAG,QAAQ,CAE1F"}
|
@@ -0,0 +1,66 @@
|
|
1
|
+
import { CodeError } from '@libp2p/interface/errors';
|
2
|
+
import { TypedEventEmitter } from '@libp2p/interface/events';
|
3
|
+
import { PeerMap } from '@libp2p/peer-collections';
|
4
|
+
import { multiaddr } from '@multiformats/multiaddr';
|
5
|
+
class CircuitRelayTransportListener extends TypedEventEmitter {
|
6
|
+
connectionManager;
|
7
|
+
relayStore;
|
8
|
+
listeningAddrs;
|
9
|
+
log;
|
10
|
+
constructor(components) {
|
11
|
+
super();
|
12
|
+
this.log = components.logger.forComponent('libp2p:circuit-relay:transport:listener');
|
13
|
+
this.connectionManager = components.connectionManager;
|
14
|
+
this.relayStore = components.relayStore;
|
15
|
+
this.listeningAddrs = new PeerMap();
|
16
|
+
// remove listening addrs when a relay is removed
|
17
|
+
this.relayStore.addEventListener('relay:removed', this._onRemoveRelayPeer);
|
18
|
+
}
|
19
|
+
_onRemoveRelayPeer = (evt) => {
|
20
|
+
this.#removeRelayPeer(evt.detail);
|
21
|
+
};
|
22
|
+
async listen(addr) {
|
23
|
+
this.log('listen on %a', addr);
|
24
|
+
// remove the circuit part to get the peer id of the relay
|
25
|
+
const relayAddr = addr.decapsulate('/p2p-circuit');
|
26
|
+
const relayConn = await this.connectionManager.openConnection(relayAddr);
|
27
|
+
if (!this.relayStore.hasReservation(relayConn.remotePeer)) {
|
28
|
+
// addRelay calls transportManager.listen which calls this listen method
|
29
|
+
await this.relayStore.addRelay(relayConn.remotePeer, 'configured');
|
30
|
+
return;
|
31
|
+
}
|
32
|
+
const reservation = this.relayStore.getReservation(relayConn.remotePeer);
|
33
|
+
if (reservation == null) {
|
34
|
+
throw new CodeError('Did not have reservation after making reservation', 'ERR_NO_RESERVATION');
|
35
|
+
}
|
36
|
+
if (this.listeningAddrs.has(relayConn.remotePeer)) {
|
37
|
+
this.log('already listening on relay %p', relayConn.remotePeer);
|
38
|
+
return;
|
39
|
+
}
|
40
|
+
// add all addresses from the relay reservation
|
41
|
+
this.listeningAddrs.set(relayConn.remotePeer, reservation.addrs.map(buf => {
|
42
|
+
return multiaddr(buf).encapsulate('/p2p-circuit');
|
43
|
+
}));
|
44
|
+
this.safeDispatchEvent('listening', {});
|
45
|
+
}
|
46
|
+
getAddrs() {
|
47
|
+
return [...this.listeningAddrs.values()].flat();
|
48
|
+
}
|
49
|
+
async close() {
|
50
|
+
}
|
51
|
+
#removeRelayPeer(peerId) {
|
52
|
+
const had = this.listeningAddrs.has(peerId);
|
53
|
+
this.log('relay peer removed %p - had reservation', peerId, had);
|
54
|
+
this.listeningAddrs.delete(peerId);
|
55
|
+
if (had) {
|
56
|
+
this.log.trace('removing relay event listener for peer %p', peerId);
|
57
|
+
this.relayStore.removeEventListener('relay:removed', this._onRemoveRelayPeer);
|
58
|
+
// Announce listen addresses change
|
59
|
+
this.safeDispatchEvent('close', {});
|
60
|
+
}
|
61
|
+
}
|
62
|
+
}
|
63
|
+
export function createListener(options) {
|
64
|
+
return new CircuitRelayTransportListener(options);
|
65
|
+
}
|
66
|
+
//# sourceMappingURL=listener.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"listener.js","sourceRoot":"","sources":["../../../src/transport/listener.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAA;AACpD,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAA;AAC5D,OAAO,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAA;AAClD,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AAcnD,MAAM,6BAA8B,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CACF;AAED,MAAM,UAAU,cAAc,CAAE,OAAgD;IAC9E,OAAO,IAAI,6BAA6B,CAAC,OAAO,CAAC,CAAA;AACnD,CAAC"}
|
@@ -0,0 +1,74 @@
|
|
1
|
+
import { TypedEventEmitter, type TypedEventTarget } from '@libp2p/interface/events';
|
2
|
+
import type { Reservation } from '../pb/index.js';
|
3
|
+
import type { Libp2pEvents, ComponentLogger } from '@libp2p/interface';
|
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 { TransportManager } from '@libp2p/interface-internal/transport-manager';
|
9
|
+
export interface RelayStoreComponents {
|
10
|
+
peerId: PeerId;
|
11
|
+
connectionManager: ConnectionManager;
|
12
|
+
transportManager: TransportManager;
|
13
|
+
peerStore: PeerStore;
|
14
|
+
events: TypedEventTarget<Libp2pEvents>;
|
15
|
+
logger: ComponentLogger;
|
16
|
+
}
|
17
|
+
export interface RelayStoreInit {
|
18
|
+
/**
|
19
|
+
* Multiple relays may be discovered simultaneously - to prevent listening
|
20
|
+
* on too many relays, this value controls how many to attempt to reserve a
|
21
|
+
* slot on at once. If set to more than one, we may end up listening on
|
22
|
+
* more relays than the `maxReservations` value, but on networks with poor
|
23
|
+
* connectivity the user may wish to attempt to reserve on multiple relays
|
24
|
+
* simultaneously. (default: 1)
|
25
|
+
*/
|
26
|
+
reservationConcurrency?: number;
|
27
|
+
/**
|
28
|
+
* How many discovered relays to allow in the reservation store
|
29
|
+
*/
|
30
|
+
discoverRelays?: number;
|
31
|
+
/**
|
32
|
+
* Limit the number of potential relays we will dial (default: 100)
|
33
|
+
*/
|
34
|
+
maxReservationQueueLength?: number;
|
35
|
+
/**
|
36
|
+
* When creating a reservation it must complete within this number of ms
|
37
|
+
* (default: 5000)
|
38
|
+
*/
|
39
|
+
reservationCompletionTimeout?: number;
|
40
|
+
}
|
41
|
+
export type RelayType = 'discovered' | 'configured';
|
42
|
+
export interface ReservationStoreEvents {
|
43
|
+
'relay:not-enough-relays': CustomEvent;
|
44
|
+
'relay:removed': CustomEvent<PeerId>;
|
45
|
+
}
|
46
|
+
export declare class ReservationStore extends TypedEventEmitter<ReservationStoreEvents> implements Startable {
|
47
|
+
#private;
|
48
|
+
private readonly peerId;
|
49
|
+
private readonly connectionManager;
|
50
|
+
private readonly transportManager;
|
51
|
+
private readonly peerStore;
|
52
|
+
private readonly events;
|
53
|
+
private readonly reserveQueue;
|
54
|
+
private readonly reservations;
|
55
|
+
private readonly maxDiscoveredRelays;
|
56
|
+
private readonly maxReservationQueueLength;
|
57
|
+
private readonly reservationCompletionTimeout;
|
58
|
+
private started;
|
59
|
+
private readonly log;
|
60
|
+
constructor(components: RelayStoreComponents, init?: RelayStoreInit);
|
61
|
+
isStarted(): boolean;
|
62
|
+
start(): Promise<void>;
|
63
|
+
stop(): Promise<void>;
|
64
|
+
/**
|
65
|
+
* If the number of current relays is beneath the configured `maxReservations`
|
66
|
+
* value, and the passed peer id is not our own, and we have a non-relayed connection
|
67
|
+
* to the remote, and the remote peer speaks the hop protocol, try to reserve a slot
|
68
|
+
* on the remote peer
|
69
|
+
*/
|
70
|
+
addRelay(peerId: PeerId, type: RelayType): Promise<void>;
|
71
|
+
hasReservation(peerId: PeerId): boolean;
|
72
|
+
getReservation(peerId: PeerId): Reservation | undefined;
|
73
|
+
}
|
74
|
+
//# sourceMappingURL=reservation-store.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"reservation-store.d.ts","sourceRoot":"","sources":["../../../src/transport/reservation-store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,KAAK,gBAAgB,EAAE,MAAM,0BAA0B,CAAA;AAQnF,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AACjD,OAAO,KAAK,EAAE,YAAY,E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