libp2p 0.42.2-45fc415b → 0.42.2-593deefe
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.min.js +29 -29
- package/dist/src/circuit/client.d.ts +81 -0
- package/dist/src/circuit/client.d.ts.map +1 -0
- package/dist/src/circuit/client.js +285 -0
- package/dist/src/circuit/client.js.map +1 -0
- package/dist/src/circuit/constants.d.ts +7 -8
- package/dist/src/circuit/constants.d.ts.map +1 -1
- package/dist/src/circuit/constants.js +11 -8
- package/dist/src/circuit/constants.js.map +1 -1
- package/dist/src/circuit/hop.d.ts +23 -0
- package/dist/src/circuit/hop.d.ts.map +1 -0
- package/dist/src/circuit/hop.js +167 -0
- package/dist/src/circuit/hop.js.map +1 -0
- package/dist/src/circuit/index.d.ts +47 -47
- package/dist/src/circuit/index.d.ts.map +1 -1
- package/dist/src/circuit/index.js +1 -68
- package/dist/src/circuit/index.js.map +1 -1
- package/dist/src/circuit/interfaces.d.ts +27 -0
- package/dist/src/circuit/interfaces.d.ts.map +1 -0
- package/dist/src/circuit/interfaces.js +2 -0
- package/dist/src/circuit/interfaces.js.map +1 -0
- package/dist/src/circuit/multicodec.d.ts +2 -1
- package/dist/src/circuit/multicodec.d.ts.map +1 -1
- package/dist/src/circuit/multicodec.js +2 -1
- package/dist/src/circuit/multicodec.js.map +1 -1
- package/dist/src/circuit/pb/index.d.ts +83 -43
- package/dist/src/circuit/pb/index.d.ts.map +1 -1
- package/dist/src/circuit/pb/index.js +363 -127
- package/dist/src/circuit/pb/index.js.map +1 -1
- package/dist/src/circuit/relay.d.ts +34 -0
- package/dist/src/circuit/relay.d.ts.map +1 -0
- package/dist/src/circuit/relay.js +61 -0
- package/dist/src/circuit/relay.js.map +1 -0
- package/dist/src/circuit/reservation-store.d.ts +42 -0
- package/dist/src/circuit/reservation-store.d.ts.map +1 -0
- package/dist/src/circuit/reservation-store.js +59 -0
- package/dist/src/circuit/reservation-store.js.map +1 -0
- package/dist/src/circuit/reservation-voucher.d.ts +18 -0
- package/dist/src/circuit/reservation-voucher.d.ts.map +1 -0
- package/dist/src/circuit/reservation-voucher.js +33 -0
- package/dist/src/circuit/reservation-voucher.js.map +1 -0
- package/dist/src/circuit/stop.d.ts +19 -0
- package/dist/src/circuit/stop.d.ts.map +1 -0
- package/dist/src/circuit/stop.js +67 -0
- package/dist/src/circuit/stop.js.map +1 -0
- package/dist/src/circuit/transport.d.ts +24 -9
- package/dist/src/circuit/transport.d.ts.map +1 -1
- package/dist/src/circuit/transport.js +133 -118
- package/dist/src/circuit/transport.js.map +1 -1
- package/dist/src/circuit/utils.d.ts +5 -0
- package/dist/src/circuit/utils.d.ts.map +1 -1
- package/dist/src/circuit/utils.js +114 -0
- package/dist/src/circuit/utils.js.map +1 -1
- package/dist/src/config.d.ts.map +1 -1
- package/dist/src/config.js +2 -3
- package/dist/src/config.js.map +1 -1
- package/dist/src/connection-manager/dialer/dial-request.d.ts.map +1 -1
- package/dist/src/connection-manager/dialer/dial-request.js +7 -0
- package/dist/src/connection-manager/dialer/dial-request.js.map +1 -1
- package/dist/src/errors.d.ts +2 -1
- package/dist/src/errors.d.ts.map +1 -1
- package/dist/src/errors.js +1 -0
- package/dist/src/errors.js.map +1 -1
- package/dist/src/fetch/pb/proto.d.ts +3 -3
- package/dist/src/fetch/pb/proto.d.ts.map +1 -1
- package/dist/src/fetch/pb/proto.js +4 -3
- package/dist/src/fetch/pb/proto.js.map +1 -1
- package/dist/src/identify/pb/message.d.ts +2 -2
- package/dist/src/identify/pb/message.d.ts.map +1 -1
- package/dist/src/identify/pb/message.js +1 -0
- package/dist/src/identify/pb/message.js.map +1 -1
- package/dist/src/insecure/pb/proto.d.ts +3 -3
- package/dist/src/insecure/pb/proto.d.ts.map +1 -1
- package/dist/src/insecure/pb/proto.js +4 -5
- package/dist/src/insecure/pb/proto.js.map +1 -1
- package/dist/src/libp2p.d.ts +2 -0
- package/dist/src/libp2p.d.ts.map +1 -1
- package/dist/src/libp2p.js +10 -3
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/transport-manager.d.ts.map +1 -1
- package/dist/src/transport-manager.js +2 -3
- package/dist/src/transport-manager.js.map +1 -1
- package/dist/src/upgrader.d.ts.map +1 -1
- package/dist/src/upgrader.js.map +1 -1
- package/package.json +12 -11
- package/src/circuit/client.ts +381 -0
- package/src/circuit/constants.ts +14 -8
- package/src/circuit/hop.ts +210 -0
- package/src/circuit/index.ts +49 -111
- package/src/circuit/interfaces.ts +28 -0
- package/src/circuit/multicodec.ts +2 -1
- package/src/circuit/pb/index.proto +57 -32
- package/src/circuit/pb/index.ts +448 -138
- package/src/circuit/relay.ts +87 -0
- package/src/circuit/reservation-store.ts +106 -0
- package/src/circuit/reservation-voucher.ts +51 -0
- package/src/circuit/stop.ts +92 -0
- package/src/circuit/transport.ts +166 -131
- package/src/circuit/utils.ts +145 -0
- package/src/config.ts +2 -3
- package/src/connection-manager/dialer/dial-request.ts +7 -0
- package/src/errors.ts +2 -1
- package/src/fetch/pb/proto.ts +8 -7
- package/src/identify/pb/message.ts +3 -2
- package/src/insecure/pb/proto.ts +8 -9
- package/src/libp2p.ts +12 -3
- package/src/transport-manager.ts +2 -3
- package/src/upgrader.ts +4 -4
- package/src/version.ts +1 -1
- package/dist/src/circuit/auto-relay.d.ts +0 -46
- package/dist/src/circuit/auto-relay.d.ts.map +0 -1
- package/dist/src/circuit/auto-relay.js +0 -220
- package/dist/src/circuit/auto-relay.js.map +0 -1
- package/dist/src/circuit/circuit/hop.d.ts +0 -42
- package/dist/src/circuit/circuit/hop.d.ts.map +0 -1
- package/dist/src/circuit/circuit/hop.js +0 -142
- package/dist/src/circuit/circuit/hop.js.map +0 -1
- package/dist/src/circuit/circuit/stop.d.ts +0 -24
- package/dist/src/circuit/circuit/stop.d.ts.map +0 -1
- package/dist/src/circuit/circuit/stop.js +0 -51
- package/dist/src/circuit/circuit/stop.js.map +0 -1
- package/dist/src/circuit/circuit/stream-handler.d.ts +0 -40
- package/dist/src/circuit/circuit/stream-handler.d.ts.map +0 -1
- package/dist/src/circuit/circuit/stream-handler.js +0 -59
- package/dist/src/circuit/circuit/stream-handler.js.map +0 -1
- package/dist/src/circuit/circuit/utils.d.ts +0 -7
- package/dist/src/circuit/circuit/utils.d.ts.map +0 -1
- package/dist/src/circuit/circuit/utils.js +0 -43
- package/dist/src/circuit/circuit/utils.js.map +0 -1
- package/src/circuit/IMPLEMENTATION_NOTES.md +0 -128
- package/src/circuit/README.md +0 -111
- package/src/circuit/auto-relay.ts +0 -292
- package/src/circuit/circuit/hop.ts +0 -220
- package/src/circuit/circuit/stop.ts +0 -83
- package/src/circuit/circuit/stream-handler.ts +0 -87
- package/src/circuit/circuit/utils.ts +0 -44
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type { PeerId } from '@libp2p/interface-peer-id';
|
|
2
|
+
import type { AddressSorter } from '@libp2p/interface-peer-store';
|
|
3
|
+
import type { Connection } from '@libp2p/interface-connection';
|
|
4
|
+
import { EventEmitter } from '@libp2p/interfaces/events';
|
|
5
|
+
import type { Startable } from '@libp2p/interfaces/startable';
|
|
6
|
+
import type { Components } from '../components.js';
|
|
7
|
+
import type { RelayReservationManagerConfig } from './index.js';
|
|
8
|
+
import { PeerList } from '@libp2p/peer-collections';
|
|
9
|
+
/**
|
|
10
|
+
* CircuitServiceInit initializes the circuit service using values
|
|
11
|
+
* from the provided config and an @type{AddressSorter}.
|
|
12
|
+
*/
|
|
13
|
+
export interface CircuitServiceInit extends RelayReservationManagerConfig {
|
|
14
|
+
/**
|
|
15
|
+
* Allows prioritizing addresses from the peerstore for dialing. The
|
|
16
|
+
* default behavior is to prioritise public addresses.
|
|
17
|
+
*/
|
|
18
|
+
addressSorter?: AddressSorter;
|
|
19
|
+
/**
|
|
20
|
+
* A callback to invoke when an error occurs in the circuit service.
|
|
21
|
+
*/
|
|
22
|
+
onError?: (error: Error) => void;
|
|
23
|
+
}
|
|
24
|
+
export interface RelayReservationManagerEvents {
|
|
25
|
+
'relay:reservation': CustomEvent;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* ReservationManager automatically makes a circuit v2 reservation on any connected
|
|
29
|
+
* peers that support the circuit v2 HOP protocol.
|
|
30
|
+
*/
|
|
31
|
+
export declare class RelayReservationManager extends EventEmitter<RelayReservationManagerEvents> implements Startable {
|
|
32
|
+
private readonly components;
|
|
33
|
+
private readonly addressSorter;
|
|
34
|
+
private readonly maxReservations;
|
|
35
|
+
private readonly relays;
|
|
36
|
+
private readonly reservationMap;
|
|
37
|
+
private readonly onError;
|
|
38
|
+
private started;
|
|
39
|
+
constructor(components: Components, init: CircuitServiceInit);
|
|
40
|
+
isStarted(): boolean;
|
|
41
|
+
start(): void;
|
|
42
|
+
stop(): Promise<void>;
|
|
43
|
+
/**
|
|
44
|
+
* Check if a peer supports the relay protocol.
|
|
45
|
+
* If the protocol is not supported, check if it was supported before and remove it as a listen relay.
|
|
46
|
+
* If the protocol is supported, check if the peer supports **HOP** and add it as a listener if
|
|
47
|
+
* inside the threshold.
|
|
48
|
+
*/
|
|
49
|
+
_onProtocolChange({ peerId, protocols }: {
|
|
50
|
+
peerId: PeerId;
|
|
51
|
+
protocols: string[];
|
|
52
|
+
}): Promise<void>;
|
|
53
|
+
/**
|
|
54
|
+
* Handle case when peer connects. If we already have the peer in the protobook,
|
|
55
|
+
* we treat this event as an `onProtocolChange`.
|
|
56
|
+
*/
|
|
57
|
+
_onPeerConnect({ detail: connection }: CustomEvent<Connection>): void;
|
|
58
|
+
/**
|
|
59
|
+
* Peer disconnects
|
|
60
|
+
*/
|
|
61
|
+
_onPeerDisconnected(evt: CustomEvent<Connection>): void;
|
|
62
|
+
/**
|
|
63
|
+
* Attempt to listen on the given relay connection
|
|
64
|
+
*/
|
|
65
|
+
_addListenRelay(connection: Connection, peerId: PeerId): Promise<void>;
|
|
66
|
+
/**
|
|
67
|
+
* Remove listen relay
|
|
68
|
+
*/
|
|
69
|
+
_removeListenRelay(PeerId: PeerId): Promise<void>;
|
|
70
|
+
/**
|
|
71
|
+
* Try to listen on available hop relay connections.
|
|
72
|
+
* The following order will happen while we do not have enough relays.
|
|
73
|
+
* 1. Check the metadata store for known relays, try to listen on the ones we are already connected.
|
|
74
|
+
* 2. Dial and try to listen on the peers we know that support hop but are not connected.
|
|
75
|
+
* 3. Search the network.
|
|
76
|
+
*/
|
|
77
|
+
_listenOnAvailableHopRelays(peersToIgnore?: PeerList): Promise<void>;
|
|
78
|
+
_tryToListenOnRelay(peerId: PeerId): Promise<void>;
|
|
79
|
+
private readonly createOrRefreshReservation;
|
|
80
|
+
}
|
|
81
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../../src/circuit/client.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAA;AACvD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,8BAA8B,CAAA;AACjE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,8BAA8B,CAAA;AAM9D,OAAO,EAAE,YAAY,EAAe,MAAM,2BAA2B,CAAA;AACrE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAA;AAC7D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAClD,OAAO,KAAK,EAAE,6BAA6B,EAAE,MAAM,YAAY,CAAA;AAC/D,OAAO,EAAoB,QAAQ,EAAE,MAAM,0BAA0B,CAAA;AAMrE;;;GAGG;AACH,MAAM,WAAW,kBAAmB,SAAQ,6BAA6B;IACvE;;;OAGG;IACH,aAAa,CAAC,EAAE,aAAa,CAAA;IAC7B;;OAEG;IACH,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAA;CACjC;AAED,MAAM,WAAW,6BAA6B;IAC5C,mBAAmB,EAAE,WAAW,CAAA;CACjC;AAED;;;G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+
import { CIRCUIT_PROTO_CODE, DEFAULT_MAX_RESERVATIONS, RELAY_RENDEZVOUS_NS } from './constants.js';
|
|
5
|
+
import sort from 'it-sort';
|
|
6
|
+
import all from 'it-all';
|
|
7
|
+
import { pipe } from 'it-pipe';
|
|
8
|
+
import { publicAddressesFirst } from '@libp2p/utils/address-sort';
|
|
9
|
+
import { reserve } from './hop.js';
|
|
10
|
+
import { EventEmitter, CustomEvent } from '@libp2p/interfaces/events';
|
|
11
|
+
import { PeerSet, PeerMap, PeerList } from '@libp2p/peer-collections';
|
|
12
|
+
const log = logger('libp2p:circuit:client');
|
|
13
|
+
const noop = () => { };
|
|
14
|
+
/**
|
|
15
|
+
* ReservationManager automatically makes a circuit v2 reservation on any connected
|
|
16
|
+
* peers that support the circuit v2 HOP protocol.
|
|
17
|
+
*/
|
|
18
|
+
export class RelayReservationManager extends EventEmitter {
|
|
19
|
+
constructor(components, init) {
|
|
20
|
+
super();
|
|
21
|
+
this.createOrRefreshReservation = async (peerId) => {
|
|
22
|
+
try {
|
|
23
|
+
const connections = this.components.connectionManager.getConnections(peerId);
|
|
24
|
+
if (connections.length === 0) {
|
|
25
|
+
throw new Error('No connections to peer');
|
|
26
|
+
}
|
|
27
|
+
const connection = connections[0];
|
|
28
|
+
const reservation = await reserve(connection);
|
|
29
|
+
const refreshReservation = this.createOrRefreshReservation;
|
|
30
|
+
if (reservation != null) {
|
|
31
|
+
log('new reservation on %p', peerId);
|
|
32
|
+
// clear any previous timeouts
|
|
33
|
+
const previous = this.reservationMap.get(peerId);
|
|
34
|
+
if (previous != null) {
|
|
35
|
+
clearTimeout(previous);
|
|
36
|
+
}
|
|
37
|
+
const timeout = setTimeout((peerId) => {
|
|
38
|
+
void refreshReservation(peerId).catch(err => {
|
|
39
|
+
log.error('error refreshing reservation for %p', peerId, err);
|
|
40
|
+
});
|
|
41
|
+
}, Math.max(getExpiration(reservation.expire) - 100, 0), peerId);
|
|
42
|
+
this.reservationMap.set(peerId, timeout);
|
|
43
|
+
this.dispatchEvent(new CustomEvent('relay:reservation'));
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
catch (err) {
|
|
47
|
+
log.error(err);
|
|
48
|
+
await this._removeListenRelay(peerId);
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
this.started = false;
|
|
52
|
+
this.components = components;
|
|
53
|
+
this.addressSorter = init.addressSorter ?? publicAddressesFirst;
|
|
54
|
+
this.maxReservations = init.maxReservations ?? DEFAULT_MAX_RESERVATIONS;
|
|
55
|
+
this.relays = new PeerSet();
|
|
56
|
+
this.reservationMap = new PeerMap();
|
|
57
|
+
this.onError = init.onError ?? noop;
|
|
58
|
+
this._onProtocolChange = this._onProtocolChange.bind(this);
|
|
59
|
+
this._onPeerDisconnected = this._onPeerDisconnected.bind(this);
|
|
60
|
+
this._onPeerConnect = this._onPeerConnect.bind(this);
|
|
61
|
+
this.components.peerStore.addEventListener('change:protocols', (evt) => {
|
|
62
|
+
void this._onProtocolChange(evt.detail).catch(err => {
|
|
63
|
+
log.error('handling protocol change failed', err);
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
this.components.connectionManager.addEventListener('peer:disconnect', this._onPeerDisconnected);
|
|
67
|
+
this.components.connectionManager.addEventListener('peer:connect', this._onPeerConnect);
|
|
68
|
+
}
|
|
69
|
+
isStarted() {
|
|
70
|
+
return this.started;
|
|
71
|
+
}
|
|
72
|
+
start() {
|
|
73
|
+
void this._listenOnAvailableHopRelays().catch(err => { log.error('error listening on relays', err); });
|
|
74
|
+
this.started = true;
|
|
75
|
+
}
|
|
76
|
+
async stop() {
|
|
77
|
+
this.reservationMap.forEach((timer) => clearTimeout(timer));
|
|
78
|
+
this.reservationMap.clear();
|
|
79
|
+
this.relays.clear();
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Check if a peer supports the relay protocol.
|
|
83
|
+
* If the protocol is not supported, check if it was supported before and remove it as a listen relay.
|
|
84
|
+
* If the protocol is supported, check if the peer supports **HOP** and add it as a listener if
|
|
85
|
+
* inside the threshold.
|
|
86
|
+
*/
|
|
87
|
+
async _onProtocolChange({ peerId, protocols }) {
|
|
88
|
+
if (peerId.equals(this.components.peerId)) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
// Check if it has the protocol
|
|
92
|
+
const hasProtocol = protocols.includes(RELAY_V2_HOP_CODEC);
|
|
93
|
+
log.trace('Peer %p protocol change %p', peerId, this.components.peerId);
|
|
94
|
+
// If no protocol, check if we were keeping the peer before as a listenRelay
|
|
95
|
+
if (!hasProtocol) {
|
|
96
|
+
if (this.relays.has(peerId)) {
|
|
97
|
+
await this._removeListenRelay(peerId);
|
|
98
|
+
}
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
if (this.relays.has(peerId)) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
// If protocol, check if can hop, store info in the metadataBook and listen on it
|
|
105
|
+
try {
|
|
106
|
+
const connections = this.components.connectionManager.getConnections(peerId);
|
|
107
|
+
// if no connections, try to listen on relay
|
|
108
|
+
if (connections.length === 0) {
|
|
109
|
+
void this._tryToListenOnRelay(peerId);
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
const connection = connections[0];
|
|
113
|
+
// Do not hop on a relayed connection
|
|
114
|
+
if (connection.remoteAddr.protoCodes().includes(CIRCUIT_PROTO_CODE)) {
|
|
115
|
+
log('relayed connection to %p will not be used to hop on', peerId);
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
await this._addListenRelay(connection, peerId);
|
|
119
|
+
}
|
|
120
|
+
catch (err) {
|
|
121
|
+
log.error('could not add %p as relay', peerId);
|
|
122
|
+
this.onError(err);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Handle case when peer connects. If we already have the peer in the protobook,
|
|
127
|
+
* we treat this event as an `onProtocolChange`.
|
|
128
|
+
*/
|
|
129
|
+
_onPeerConnect({ detail: connection }) {
|
|
130
|
+
void this.components.peerStore.protoBook.get(connection.remotePeer)
|
|
131
|
+
.then((protocols) => {
|
|
132
|
+
void this._onProtocolChange({ peerId: connection.remotePeer, protocols })
|
|
133
|
+
.catch((err) => log.error('handling reconnect failed', err));
|
|
134
|
+
}, (err) => {
|
|
135
|
+
// this is not necessarily an error as we will only have the protocols stored
|
|
136
|
+
// in case of a reconnect
|
|
137
|
+
log.trace('could not fetch protocols for peer: %p', connection.remotePeer, err);
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Peer disconnects
|
|
142
|
+
*/
|
|
143
|
+
_onPeerDisconnected(evt) {
|
|
144
|
+
const connection = evt.detail;
|
|
145
|
+
const peerId = connection.remotePeer;
|
|
146
|
+
clearTimeout(this.reservationMap.get(peerId));
|
|
147
|
+
this.reservationMap.delete(peerId);
|
|
148
|
+
// Not listening on this relay
|
|
149
|
+
if (!this.relays.has(peerId)) {
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
this._removeListenRelay(peerId).catch(err => {
|
|
153
|
+
log.error(err);
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Attempt to listen on the given relay connection
|
|
158
|
+
*/
|
|
159
|
+
async _addListenRelay(connection, peerId) {
|
|
160
|
+
log.trace('peerId %p is being added as relay', peerId);
|
|
161
|
+
try {
|
|
162
|
+
// Check if already enough relay reservations
|
|
163
|
+
if (this.relays.size >= this.maxReservations) {
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
await this.createOrRefreshReservation(peerId);
|
|
167
|
+
// Get peer known addresses and sort them with public addresses first
|
|
168
|
+
const remoteAddrs = await pipe(await this.components.peerStore.addressBook.get(connection.remotePeer), (source) => sort(source, this.addressSorter), async (source) => await all(source));
|
|
169
|
+
// Attempt to listen on relay
|
|
170
|
+
const result = await Promise.all(remoteAddrs.map(async (addr) => {
|
|
171
|
+
let multiaddr = addr.multiaddr;
|
|
172
|
+
if (multiaddr.getPeerId() == null) {
|
|
173
|
+
multiaddr = multiaddr.encapsulate(`/p2p/${connection.remotePeer.toString()}`);
|
|
174
|
+
}
|
|
175
|
+
multiaddr = multiaddr.encapsulate('/p2p-circuit');
|
|
176
|
+
try {
|
|
177
|
+
// Announce multiaddrs will update on listen success by TransportManager event being triggered
|
|
178
|
+
await this.components.transportManager.listen([multiaddr]);
|
|
179
|
+
return true;
|
|
180
|
+
}
|
|
181
|
+
catch (err) {
|
|
182
|
+
log.error('error listening on circuit address', multiaddr, err);
|
|
183
|
+
this.onError(err);
|
|
184
|
+
}
|
|
185
|
+
return false;
|
|
186
|
+
}));
|
|
187
|
+
if (result.includes(true)) {
|
|
188
|
+
this.relays.add(peerId);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
catch (err) {
|
|
192
|
+
this.relays.delete(peerId);
|
|
193
|
+
log.error('error adding relay for %p %s', peerId, err);
|
|
194
|
+
this.onError(err);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Remove listen relay
|
|
199
|
+
*/
|
|
200
|
+
async _removeListenRelay(PeerId) {
|
|
201
|
+
const recheck = this.relays.has(PeerId);
|
|
202
|
+
this.relays.delete(PeerId);
|
|
203
|
+
if (recheck) {
|
|
204
|
+
// TODO: this should be responsibility of the connMgr
|
|
205
|
+
await this._listenOnAvailableHopRelays(new PeerList([PeerId]));
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Try to listen on available hop relay connections.
|
|
210
|
+
* The following order will happen while we do not have enough relays.
|
|
211
|
+
* 1. Check the metadata store for known relays, try to listen on the ones we are already connected.
|
|
212
|
+
* 2. Dial and try to listen on the peers we know that support hop but are not connected.
|
|
213
|
+
* 3. Search the network.
|
|
214
|
+
*/
|
|
215
|
+
async _listenOnAvailableHopRelays(peersToIgnore = new PeerList([])) {
|
|
216
|
+
// Check if already listening on enough relays
|
|
217
|
+
if (this.relays.size >= this.maxReservations) {
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
const knownHopsToDial = [];
|
|
221
|
+
const peers = (await this.components.peerStore.all())
|
|
222
|
+
// filter by a list of peers supporting RELAY_V2_HOP and ones we are not listening on
|
|
223
|
+
.filter(({ id, protocols }) => protocols.includes(RELAY_V2_HOP_CODEC) && !this.relays.has(id) && !peersToIgnore.includes(id))
|
|
224
|
+
.map(({ id }) => {
|
|
225
|
+
const connections = this.components.connectionManager.getConnections(id);
|
|
226
|
+
if (connections.length === 0) {
|
|
227
|
+
knownHopsToDial.push(id);
|
|
228
|
+
return [id, null];
|
|
229
|
+
}
|
|
230
|
+
return [id, connections[0]];
|
|
231
|
+
})
|
|
232
|
+
.sort(() => Math.random() - 0.5);
|
|
233
|
+
// Check if we have known hop peers to use and attempt to listen on the already connected
|
|
234
|
+
for (const [id, conn] of peers) {
|
|
235
|
+
await this._addListenRelay(conn, id);
|
|
236
|
+
// Check if already listening on enough relays
|
|
237
|
+
if (this.relays.size >= this.maxReservations) {
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
// Try to listen on known peers that are not connected
|
|
242
|
+
for (const peerId of knownHopsToDial) {
|
|
243
|
+
// Check if already listening on enough relays
|
|
244
|
+
if (this.relays.size >= this.maxReservations) {
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
await this._tryToListenOnRelay(peerId);
|
|
248
|
+
}
|
|
249
|
+
// Try to find relays to hop on the network
|
|
250
|
+
try {
|
|
251
|
+
const cid = await namespaceToCid(RELAY_RENDEZVOUS_NS);
|
|
252
|
+
for await (const provider of this.components.contentRouting.findProviders(cid)) {
|
|
253
|
+
if (provider.multiaddrs.length > 0 &&
|
|
254
|
+
!provider.id.equals(this.components.peerId)) {
|
|
255
|
+
const peerId = provider.id;
|
|
256
|
+
await this.components.peerStore.addressBook.add(peerId, provider.multiaddrs);
|
|
257
|
+
await this._tryToListenOnRelay(peerId);
|
|
258
|
+
// Check if already listening on enough relays
|
|
259
|
+
if (this.relays.size >= this.maxReservations) {
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
catch (err) {
|
|
266
|
+
log.error('failed when finding relays on the network', err);
|
|
267
|
+
this.onError(err);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
async _tryToListenOnRelay(peerId) {
|
|
271
|
+
try {
|
|
272
|
+
if (peerId.equals(this.components.peerId)) {
|
|
273
|
+
log.trace('Skipping dialling self %p', peerId.toString());
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
const connection = await this.components.connectionManager.openConnection(peerId);
|
|
277
|
+
await this._addListenRelay(connection, peerId);
|
|
278
|
+
}
|
|
279
|
+
catch (err) {
|
|
280
|
+
log.error('Could not connect and listen on relay %p', peerId, err);
|
|
281
|
+
this.onError(err);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../../src/circuit/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AACvC,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAA;AACpD,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AAC1D,OAAO,EACL,kBAAkB,EAClB,wBAAwB,EACxB,mBAAmB,EACpB,MAAM,gBAAgB,CAAA;AAIvB,OAAO,IAAI,MAAM,SAAS,CAAA;AAC1B,OAAO,GAAG,MAAM,QAAQ,CAAA;AACxB,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAA;AAC9B,OAAO,EAAE,oBAAoB,EAAE,MAAM,4BAA4B,CAAA;AACjE,OAAO,EAAE,OAAO,EAAE,MAAM,UAAU,CAAA;AAClC,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,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|
13
|
-
/**
|
|
14
|
-
* PeerStore metadaBook key for HOP relay service
|
|
15
|
-
*/
|
|
16
|
-
export declare const HOP_METADATA_KEY = "hop_relay";
|
|
17
|
-
/**
|
|
18
|
-
* PeerStore metadaBook value for HOP relay service
|
|
19
|
-
*/
|
|
20
|
-
export declare const HOP_METADATA_VALUE = "true";
|
|
21
13
|
/**
|
|
22
14
|
* Relay HOP relay service namespace for discovery
|
|
23
15
|
*/
|
|
24
16
|
export declare const RELAY_RENDEZVOUS_NS = "/libp2p/relay";
|
|
17
|
+
/**
|
|
18
|
+
* Maximum reservations for auto relay
|
|
19
|
+
*/
|
|
20
|
+
export declare const DEFAULT_MAX_RESERVATIONS = 1;
|
|
21
|
+
export declare const RELAY_DESTINATION_TAG = "relay-destination";
|
|
22
|
+
export declare const DEFAULT_DURATION_LIMIT: number;
|
|
23
|
+
export declare const DEFAULT_DATA_LIMIT: bigint;
|
|
25
24
|
//# sourceMappingURL=constants.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../../src/circuit/constants.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,eAAO,MAAM,oBAAoB,QAAc,CAAA;AAE/C;;GAEG;AACH,eAAO,MAAM,aAAa,QAAc,CAAA;AAExC;;GAEG;AACH,eAAO,MAAM,kBAAkB,MAAM,CAAA;AAErC;;GAEG;AACH,eAAO,MAAM,
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../../src/circuit/constants.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,eAAO,MAAM,oBAAoB,QAAc,CAAA;AAE/C;;GAEG;AACH,eAAO,MAAM,aAAa,QAAc,CAAA;AAExC;;GAEG;AACH,eAAO,MAAM,kBAAkB,MAAM,CAAA;AAErC;;GAEG;AACH,eAAO,MAAM,mBAAmB,kBAAkB,CAAA;AAElD;;GAEG;AACH,eAAO,MAAM,wBAAwB,IAAI,CAAA;AAEzC,eAAO,MAAM,qBAAqB,sBAAsB,CAAA;AAMxD,eAAO,MAAM,sBAAsB,QAAgB,CAAA;AAGnD,eAAO,MAAM,kBAAkB,QAAkB,CAAA"}
|
|
@@ -11,16 +11,19 @@ export const ADVERTISE_TTL = 30 * minute;
|
|
|
11
11
|
* Multicodec code
|
|
12
12
|
*/
|
|
13
13
|
export const CIRCUIT_PROTO_CODE = 290;
|
|
14
|
-
/**
|
|
15
|
-
* PeerStore metadaBook key for HOP relay service
|
|
16
|
-
*/
|
|
17
|
-
export const HOP_METADATA_KEY = 'hop_relay';
|
|
18
|
-
/**
|
|
19
|
-
* PeerStore metadaBook value for HOP relay service
|
|
20
|
-
*/
|
|
21
|
-
export const HOP_METADATA_VALUE = 'true';
|
|
22
14
|
/**
|
|
23
15
|
* Relay HOP relay service namespace for discovery
|
|
24
16
|
*/
|
|
25
17
|
export const RELAY_RENDEZVOUS_NS = '/libp2p/relay';
|
|
18
|
+
/**
|
|
19
|
+
* Maximum reservations for auto relay
|
|
20
|
+
*/
|
|
21
|
+
export const DEFAULT_MAX_RESERVATIONS = 1;
|
|
22
|
+
export const RELAY_DESTINATION_TAG = 'relay-destination';
|
|
23
|
+
// circuit v2 connection limits
|
|
24
|
+
// https://github.com/libp2p/go-libp2p/blob/master/p2p/protocol/circuitv2/relay/resources.go#L61-L66
|
|
25
|
+
// 2 min is the default connection duration
|
|
26
|
+
export const DEFAULT_DURATION_LIMIT = 2 * 60 * 1000;
|
|
27
|
+
// 128k is the default data limit
|
|
28
|
+
export const DEFAULT_DATA_LIMIT = BigInt(1 << 17);
|
|
26
29
|
//# sourceMappingURL=constants.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../../src/circuit/constants.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,GAAG,EAAE,GAAG,IAAI,CAAA;AAExB;;GAEG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAAG,EAAE,GAAG,MAAM,CAAA;AAE/C;;GAEG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,EAAE,GAAG,MAAM,CAAA;AAExC;;GAEG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,GAAG,CAAA;AAErC;;GAEG;AACH,MAAM,CAAC,MAAM,
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../../src/circuit/constants.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,GAAG,EAAE,GAAG,IAAI,CAAA;AAExB;;GAEG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAAG,EAAE,GAAG,MAAM,CAAA;AAE/C;;GAEG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,EAAE,GAAG,MAAM,CAAA;AAExC;;GAEG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,GAAG,CAAA;AAErC;;GAEG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,eAAe,CAAA;AAElD;;GAEG;AACH,MAAM,CAAC,MAAM,wBAAwB,GAAG,CAAC,CAAA;AAEzC,MAAM,CAAC,MAAM,qBAAqB,GAAG,mBAAmB,CAAA;AAExD,+BAA+B;AAC/B,oGAAoG;AAEpG,2CAA2C;AAC3C,MAAM,CAAC,MAAM,sBAAsB,GAAG,CAAC,GAAG,EAAE,GAAG,IAAI,CAAA;AAEnD,iCAAiC;AACjC,MAAM,CAAC,MAAM,kBAAkB,GAAG,MAAM,CAAC,CAAC,IAAI,EAAE,CAAC,CAAA"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { PeerId } from '@libp2p/interface-peer-id';
|
|
2
|
+
import type { Connection, Stream } from '@libp2p/interface-connection';
|
|
3
|
+
import { HopMessage, Limit, Reservation } from './pb/index.js';
|
|
4
|
+
import type { Multiaddr } from '@multiformats/multiaddr';
|
|
5
|
+
import type { Acl, ReservationStore } from './interfaces.js';
|
|
6
|
+
import type { ConnectionManager } from '@libp2p/interface-connection-manager';
|
|
7
|
+
import type { ProtobufStream } from 'it-pb-stream';
|
|
8
|
+
import type { PeerStore } from '@libp2p/interface-peer-store';
|
|
9
|
+
export interface HopProtocolOptions {
|
|
10
|
+
connection: Connection;
|
|
11
|
+
request: HopMessage;
|
|
12
|
+
stream: ProtobufStream<Stream>;
|
|
13
|
+
relayPeer: PeerId;
|
|
14
|
+
relayAddrs: Multiaddr[];
|
|
15
|
+
limit?: Limit;
|
|
16
|
+
acl?: Acl;
|
|
17
|
+
reservationStore: ReservationStore;
|
|
18
|
+
connectionManager: ConnectionManager;
|
|
19
|
+
peerStore: PeerStore;
|
|
20
|
+
}
|
|
21
|
+
export declare function handleHopProtocol(options: HopProtocolOptions): Promise<void>;
|
|
22
|
+
export declare function reserve(connection: Connection): Promise<Reservation>;
|
|
23
|
+
//# sourceMappingURL=hop.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hop.d.ts","sourceRoot":"","sources":["../../../src/circuit/hop.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAA;AAGvD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,8BAA8B,CAAA;AACtE,OAAO,EAAE,UAAU,EAAE,KAAK,EAAE,WAAW,EAAuB,MAAM,eAAe,CAAA;AACnF,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AAExD,OAAO,KAAK,EAAE,GAAG,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAA;AAK5D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,sCAAsC,CAAA;AAC7E,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,cAAc,CAAA;AAGlD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAA;AAK7D,MAAM,WAAW,kBAAkB;IACjC,UAAU,EAAE,UAAU,CAAA;IACtB,OAAO,EAAE,UAAU,CAAA;IACnB,MAAM,EAAE,cAAc,CAAC,MAAM,CAAC,CAAA;IAC9B,SAAS,EAAE,MAAM,CAAA;IACjB,UAAU,EAAE,SAAS,EAAE,CAAA;IACvB,KAAK,CAAC,EAAE,KAAK,CAAA;IACb,GAAG,CAAC,EAAE,GAAG,CAAA;IACT,gBAAgB,EAAE,gBAAgB,CAAA;IAClC,iBAAiB,EAAE,iBAAiB,CAAA;IACpC,SAAS,EAAE,SAAS,CAAA;CACrB;AAED,wBAAsB,iBAAiB,CAAE,OAAO,EAAE,kBAAkB,GAAG,OAAO,CAAC,IAAI,CAAC,CAWnF;AAED,wBAAsB,OAAO,CAAE,UAAU,EAAE,UAAU,GAAG,OAAO,CAAC,WAAW,CAAC,CAsB3E"}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { RecordEnvelope } from '@libp2p/peer-record';
|
|
2
|
+
import { logger } from '@libp2p/logger';
|
|
3
|
+
import { HopMessage, Status, StopMessage } from './pb/index.js';
|
|
4
|
+
import { multiaddr } from '@multiformats/multiaddr';
|
|
5
|
+
import { RELAY_V2_HOP_CODEC } from './multicodec.js';
|
|
6
|
+
import { stop } from './stop.js';
|
|
7
|
+
import { ReservationVoucherRecord } from './reservation-voucher.js';
|
|
8
|
+
import { peerIdFromBytes } from '@libp2p/peer-id';
|
|
9
|
+
import { pbStream } from 'it-pb-stream';
|
|
10
|
+
import { CIRCUIT_PROTO_CODE, RELAY_DESTINATION_TAG } from './constants.js';
|
|
11
|
+
import { createLimitedRelay } from './utils.js';
|
|
12
|
+
const log = logger('libp2p:circuit:v2:hop');
|
|
13
|
+
export async function handleHopProtocol(options) {
|
|
14
|
+
const { stream, request } = options;
|
|
15
|
+
log('received hop message');
|
|
16
|
+
switch (request.type) {
|
|
17
|
+
case HopMessage.Type.RESERVE:
|
|
18
|
+
await handleReserve(options);
|
|
19
|
+
break;
|
|
20
|
+
case HopMessage.Type.CONNECT:
|
|
21
|
+
await handleConnect(options);
|
|
22
|
+
break;
|
|
23
|
+
default: {
|
|
24
|
+
log.error('invalid hop request type %s via peer %s', options.request.type, options.connection.remotePeer);
|
|
25
|
+
stream.pb(HopMessage).write({ type: HopMessage.Type.STATUS, status: Status.UNEXPECTED_MESSAGE });
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
export async function reserve(connection) {
|
|
30
|
+
log('requesting reservation from %s', connection.remotePeer);
|
|
31
|
+
const stream = await connection.newStream([RELAY_V2_HOP_CODEC]);
|
|
32
|
+
const pbstr = pbStream(stream);
|
|
33
|
+
const hopstr = pbstr.pb(HopMessage);
|
|
34
|
+
hopstr.write({ type: HopMessage.Type.RESERVE });
|
|
35
|
+
let response;
|
|
36
|
+
try {
|
|
37
|
+
response = await hopstr.read();
|
|
38
|
+
}
|
|
39
|
+
catch (err) {
|
|
40
|
+
log.error('error passing reserve message response from %s because', connection.remotePeer, err.message);
|
|
41
|
+
stream.close();
|
|
42
|
+
throw err;
|
|
43
|
+
}
|
|
44
|
+
if (response.status === Status.OK && (response.reservation != null)) {
|
|
45
|
+
return response.reservation;
|
|
46
|
+
}
|
|
47
|
+
const errMsg = `reservation failed with status ${response.status ?? 'undefined'}`;
|
|
48
|
+
log.error(errMsg);
|
|
49
|
+
throw new Error(errMsg);
|
|
50
|
+
}
|
|
51
|
+
const isRelayAddr = (ma) => ma.protoCodes().includes(CIRCUIT_PROTO_CODE);
|
|
52
|
+
async function handleReserve({ connection, stream: pbstr, relayPeer, relayAddrs, acl, reservationStore, peerStore }) {
|
|
53
|
+
const hopstr = pbstr.pb(HopMessage);
|
|
54
|
+
log('hop reserve request from %s', connection.remotePeer);
|
|
55
|
+
if (isRelayAddr(connection.remoteAddr)) {
|
|
56
|
+
log.error('relay reservation over circuit connection denied for peer: %p', connection.remotePeer);
|
|
57
|
+
hopstr.write({ type: HopMessage.Type.STATUS, status: Status.PERMISSION_DENIED });
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
if ((await acl?.allowReserve?.(connection.remotePeer, connection.remoteAddr)) === false) {
|
|
61
|
+
log.error('acl denied reservation to %s', connection.remotePeer);
|
|
62
|
+
hopstr.write({ type: HopMessage.Type.STATUS, status: Status.PERMISSION_DENIED });
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const result = await reservationStore.reserve(connection.remotePeer, connection.remoteAddr);
|
|
66
|
+
if (result.status !== Status.OK) {
|
|
67
|
+
hopstr.write({ type: HopMessage.Type.STATUS, status: result.status });
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
try {
|
|
71
|
+
// tag relayed peer
|
|
72
|
+
// result.expire is non-null if `ReservationStore.reserve` returns with status == OK
|
|
73
|
+
if (result.expire != null) {
|
|
74
|
+
const ttl = new Date().getTime() - result.expire;
|
|
75
|
+
await peerStore.tagPeer(relayPeer, RELAY_DESTINATION_TAG, { value: 1, ttl });
|
|
76
|
+
}
|
|
77
|
+
hopstr.write({
|
|
78
|
+
type: HopMessage.Type.STATUS,
|
|
79
|
+
status: Status.OK,
|
|
80
|
+
reservation: await makeReservation(relayAddrs, relayPeer, connection.remotePeer, BigInt(result.expire ?? 0)),
|
|
81
|
+
limit: (await reservationStore.get(relayPeer))?.limit
|
|
82
|
+
});
|
|
83
|
+
log('sent confirmation response to %s', connection.remotePeer);
|
|
84
|
+
}
|
|
85
|
+
catch (err) {
|
|
86
|
+
log.error('failed to send confirmation response to %s', connection.remotePeer);
|
|
87
|
+
await reservationStore.removeReservation(connection.remotePeer);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
async function handleConnect(options) {
|
|
91
|
+
const { connection, stream, request, reservationStore, connectionManager, acl } = options;
|
|
92
|
+
const hopstr = stream.pb(HopMessage);
|
|
93
|
+
log('hop connect request from %s', connection.remotePeer);
|
|
94
|
+
let dstPeer;
|
|
95
|
+
try {
|
|
96
|
+
if (request.peer == null) {
|
|
97
|
+
log.error('no peer info in hop connect request');
|
|
98
|
+
throw new Error('no peer info in request');
|
|
99
|
+
}
|
|
100
|
+
request.peer.addrs.forEach(multiaddr);
|
|
101
|
+
dstPeer = peerIdFromBytes(request.peer.id);
|
|
102
|
+
}
|
|
103
|
+
catch (err) {
|
|
104
|
+
log.error('invalid hop connect request via peer %p %s', connection.remotePeer, err);
|
|
105
|
+
hopstr.write({ type: HopMessage.Type.STATUS, status: Status.MALFORMED_MESSAGE });
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
if (acl?.allowConnect !== undefined) {
|
|
109
|
+
const status = await acl.allowConnect(connection.remotePeer, connection.remoteAddr, dstPeer);
|
|
110
|
+
if (status !== Status.OK) {
|
|
111
|
+
log.error('hop connect denied for %s with status %s', connection.remotePeer, status);
|
|
112
|
+
hopstr.write({ type: HopMessage.Type.STATUS, status: status });
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
if (!await reservationStore.hasReservation(dstPeer)) {
|
|
117
|
+
log.error('hop connect denied for %s with status %s', connection.remotePeer, Status.NO_RESERVATION);
|
|
118
|
+
hopstr.write({ type: HopMessage.Type.STATUS, status: Status.NO_RESERVATION });
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
const connections = connectionManager.getConnections(dstPeer);
|
|
122
|
+
if (connections.length === 0) {
|
|
123
|
+
log('hop connect denied for %s as there is no destination connection', connection.remotePeer);
|
|
124
|
+
hopstr.write({ type: HopMessage.Type.STATUS, status: Status.NO_RESERVATION });
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
const destinationConnection = connections[0];
|
|
128
|
+
log('hop connect request from %s to %s is valid', connection.remotePeer, dstPeer);
|
|
129
|
+
const destinationStream = await stop({
|
|
130
|
+
connection: destinationConnection,
|
|
131
|
+
request: {
|
|
132
|
+
type: StopMessage.Type.CONNECT,
|
|
133
|
+
peer: {
|
|
134
|
+
id: connection.remotePeer.toBytes(),
|
|
135
|
+
addrs: [multiaddr('/p2p/' + connection.remotePeer.toString()).bytes]
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
if (destinationStream == null) {
|
|
140
|
+
log.error('failed to open stream to destination peer %s', destinationConnection?.remotePeer);
|
|
141
|
+
hopstr.write({ type: HopMessage.Type.STATUS, status: Status.CONNECTION_FAILED });
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
hopstr.write({ type: HopMessage.Type.STATUS, status: Status.OK });
|
|
145
|
+
const sourceStream = stream.unwrap();
|
|
146
|
+
log('connection to destination established, short circuiting streams...');
|
|
147
|
+
const limit = (await reservationStore.get(dstPeer))?.limit;
|
|
148
|
+
// Short circuit the two streams to create the relayed connection
|
|
149
|
+
return createLimitedRelay(sourceStream, destinationStream, limit);
|
|
150
|
+
}
|
|
151
|
+
async function makeReservation(relayAddrs, relayPeerId, remotePeer, expire) {
|
|
152
|
+
const addrs = [];
|
|
153
|
+
for (const relayAddr of relayAddrs) {
|
|
154
|
+
addrs.push(relayAddr.bytes);
|
|
155
|
+
}
|
|
156
|
+
const voucher = await RecordEnvelope.seal(new ReservationVoucherRecord({
|
|
157
|
+
peer: remotePeer,
|
|
158
|
+
relay: relayPeerId,
|
|
159
|
+
expiration: Number(expire)
|
|
160
|
+
}), relayPeerId);
|
|
161
|
+
return {
|
|
162
|
+
addrs,
|
|
163
|
+
expire,
|
|
164
|
+
voucher: voucher.marshal()
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
//# sourceMappingURL=hop.js.map
|