libp2p 0.35.8-a4bba35 → 0.35.8-ff32eba
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 +22 -22
- package/dist/src/circuit/auto-relay.d.ts.map +1 -1
- package/dist/src/config.d.ts +2 -0
- package/dist/src/config.d.ts.map +1 -1
- package/dist/src/connection-manager/auto-dialler.d.ts.map +1 -1
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/dialer/index.d.ts +7 -2
- package/dist/src/dialer/index.d.ts.map +1 -1
- package/dist/src/errors.d.ts +2 -0
- package/dist/src/fetch/constants.d.ts +2 -0
- package/dist/src/fetch/constants.d.ts.map +1 -0
- package/dist/src/fetch/index.d.ts +76 -0
- package/dist/src/fetch/index.d.ts.map +1 -0
- package/dist/src/fetch/proto.d.ts +134 -0
- package/dist/src/identify/index.d.ts.map +1 -1
- package/dist/src/index.d.ts +110 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/peer-store/address-book.d.ts +3 -1
- package/dist/src/peer-store/address-book.d.ts.map +1 -1
- package/dist/src/peer-store/index.d.ts +5 -2
- package/dist/src/peer-store/index.d.ts.map +1 -1
- package/dist/src/peer-store/store.d.ts.map +1 -1
- package/dist/src/types.d.ts +90 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/upgrader.d.ts +8 -2
- package/dist/src/upgrader.d.ts.map +1 -1
- package/package.json +8 -4
- package/src/circuit/auto-relay.js +36 -22
- package/src/config.js +2 -0
- package/src/connection-manager/auto-dialler.js +15 -6
- package/src/connection-manager/index.js +3 -0
- package/src/dialer/index.js +20 -1
- package/src/errors.js +2 -0
- package/src/fetch/README.md +36 -0
- package/src/fetch/constants.js +6 -0
- package/src/fetch/index.js +159 -0
- package/src/fetch/proto.d.ts +134 -0
- package/src/fetch/proto.js +333 -0
- package/src/fetch/proto.proto +15 -0
- package/src/identify/index.js +21 -17
- package/src/index.js +40 -1
- package/src/peer-store/address-book.js +41 -28
- package/src/peer-store/index.js +4 -2
- package/src/peer-store/store.js +19 -6
- package/src/types.ts +98 -0
- package/src/upgrader.js +29 -1
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import type PeerId from 'peer-id';
|
|
2
|
+
import type { Multiaddr } from 'multiaddr';
|
|
3
|
+
import type { MultiaddrConnection } from 'libp2p-interfaces/src/transport/types';
|
|
4
|
+
export interface ConnectionGater {
|
|
5
|
+
/**
|
|
6
|
+
* denyDialMultiaddr tests whether we're permitted to Dial the
|
|
7
|
+
* specified peer.
|
|
8
|
+
*
|
|
9
|
+
* This is called by the dialer.connectToPeer implementation before
|
|
10
|
+
* dialling a peer.
|
|
11
|
+
*
|
|
12
|
+
* Return true to prevent dialing the passed peer.
|
|
13
|
+
*/
|
|
14
|
+
denyDialPeer: (peerId: PeerId) => Promise<boolean>;
|
|
15
|
+
/**
|
|
16
|
+
* denyDialMultiaddr tests whether we're permitted to dial the specified
|
|
17
|
+
* multiaddr for the given peer.
|
|
18
|
+
*
|
|
19
|
+
* This is called by the dialer.connectToPeer implementation after it has
|
|
20
|
+
* resolved the peer's addrs, and prior to dialling each.
|
|
21
|
+
*
|
|
22
|
+
* Return true to prevent dialing the passed peer on the passed multiaddr.
|
|
23
|
+
*/
|
|
24
|
+
denyDialMultiaddr: (peerId: PeerId, multiaddr: Multiaddr) => Promise<boolean>;
|
|
25
|
+
/**
|
|
26
|
+
* denyInboundConnection tests whether an incipient inbound connection is allowed.
|
|
27
|
+
*
|
|
28
|
+
* This is called by the upgrader, or by the transport directly (e.g. QUIC,
|
|
29
|
+
* Bluetooth), straight after it has accepted a connection from its socket.
|
|
30
|
+
*
|
|
31
|
+
* Return true to deny the incoming passed connection.
|
|
32
|
+
*/
|
|
33
|
+
denyInboundConnection: (maConn: MultiaddrConnection) => Promise<boolean>;
|
|
34
|
+
/**
|
|
35
|
+
* denyOutboundConnection tests whether an incipient outbound connection is allowed.
|
|
36
|
+
*
|
|
37
|
+
* This is called by the upgrader, or by the transport directly (e.g. QUIC,
|
|
38
|
+
* Bluetooth), straight after it has created a connection with its socket.
|
|
39
|
+
*
|
|
40
|
+
* Return true to deny the incoming passed connection.
|
|
41
|
+
*/
|
|
42
|
+
denyOutboundConnection: (peerId: PeerId, maConn: MultiaddrConnection) => Promise<boolean>;
|
|
43
|
+
/**
|
|
44
|
+
* denyInboundEncryptedConnection tests whether a given connection, now encrypted,
|
|
45
|
+
* is allowed.
|
|
46
|
+
*
|
|
47
|
+
* This is called by the upgrader, after it has performed the security
|
|
48
|
+
* handshake, and before it negotiates the muxer, or by the directly by the
|
|
49
|
+
* transport, at the exact same checkpoint.
|
|
50
|
+
*
|
|
51
|
+
* Return true to deny the passed secured connection.
|
|
52
|
+
*/
|
|
53
|
+
denyInboundEncryptedConnection: (peerId: PeerId, maConn: MultiaddrConnection) => Promise<boolean>;
|
|
54
|
+
/**
|
|
55
|
+
* denyOutboundEncryptedConnection tests whether a given connection, now encrypted,
|
|
56
|
+
* is allowed.
|
|
57
|
+
*
|
|
58
|
+
* This is called by the upgrader, after it has performed the security
|
|
59
|
+
* handshake, and before it negotiates the muxer, or by the directly by the
|
|
60
|
+
* transport, at the exact same checkpoint.
|
|
61
|
+
*
|
|
62
|
+
* Return true to deny the passed secured connection.
|
|
63
|
+
*/
|
|
64
|
+
denyOutboundEncryptedConnection: (peerId: PeerId, maConn: MultiaddrConnection) => Promise<boolean>;
|
|
65
|
+
/**
|
|
66
|
+
* denyInboundUpgradedConnection tests whether a fully capable connection is allowed.
|
|
67
|
+
*
|
|
68
|
+
* This is called after encryption has been negotiated and the connection has been
|
|
69
|
+
* multiplexed, if a multiplexer is configured.
|
|
70
|
+
*
|
|
71
|
+
* Return true to deny the passed upgraded connection.
|
|
72
|
+
*/
|
|
73
|
+
denyInboundUpgradedConnection: (peerId: PeerId, maConn: MultiaddrConnection) => Promise<boolean>;
|
|
74
|
+
/**
|
|
75
|
+
* denyOutboundUpgradedConnection tests whether a fully capable connection is allowed.
|
|
76
|
+
*
|
|
77
|
+
* This is called after encryption has been negotiated and the connection has been
|
|
78
|
+
* multiplexed, if a multiplexer is configured.
|
|
79
|
+
*
|
|
80
|
+
* Return true to deny the passed upgraded connection.
|
|
81
|
+
*/
|
|
82
|
+
denyOutboundUpgradedConnection: (peerId: PeerId, maConn: MultiaddrConnection) => Promise<boolean>;
|
|
83
|
+
/**
|
|
84
|
+
* Used by the address book to filter passed addresses.
|
|
85
|
+
*
|
|
86
|
+
* Return true to allow storing the passed multiaddr for the passed peer.
|
|
87
|
+
*/
|
|
88
|
+
filterMultiaddrForPeer: (peer: PeerId, multiaddr: Multiaddr) => Promise<boolean>;
|
|
89
|
+
}
|
|
90
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,SAAS,CAAA;AACjC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,WAAW,CAAA;AAC1C,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,uCAAuC,CAAA;AAEhF,MAAM,WAAW,eAAe;IAC9B;;;;;;;;OAQG;IACH,YAAY,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA;IAElD;;;;;;;;OAQG;IACH,iBAAiB,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,SAAS,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA;IAE7E;;;;;;;OAOG;IACH,qBAAqB,EAAE,CAAC,MAAM,EAAE,mBAAmB,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA;IAExE;;;;;;;OAOG;IACH,sBAAsB,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,mBAAmB,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA;IAEzF;;;;;;;;;OASG;IACH,8BAA8B,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,mBAAmB,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA;IAEjG;;;;;;;;;OASG;IACH,+BAA+B,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,mBAAmB,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA;IAElG;;;;;;;OAOG;IACH,6BAA6B,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,mBAAmB,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA;IAEhG;;;;;;;OAOG;IACH,8BAA8B,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,mBAAmB,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA;IAEjG;;;;OAIG;IACH,sBAAsB,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,SAAS,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA;CACjF"}
|
package/dist/src/upgrader.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ export = Upgrader;
|
|
|
7
7
|
* @typedef {import('libp2p-interfaces/src/crypto/types').Crypto} Crypto
|
|
8
8
|
* @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
|
|
9
9
|
* @typedef {import('multiaddr').Multiaddr} Multiaddr
|
|
10
|
+
* @typedef {import('./types').ConnectionGater} ConnectionGater
|
|
10
11
|
*/
|
|
11
12
|
/**
|
|
12
13
|
* @typedef CryptoResult
|
|
@@ -18,20 +19,24 @@ declare class Upgrader {
|
|
|
18
19
|
/**
|
|
19
20
|
* @param {object} options
|
|
20
21
|
* @param {PeerId} options.localPeer
|
|
22
|
+
* @param {ConnectionGater} options.connectionGater
|
|
23
|
+
*
|
|
21
24
|
* @param {import('./metrics')} [options.metrics]
|
|
22
25
|
* @param {Map<string, Crypto>} [options.cryptos]
|
|
23
26
|
* @param {Map<string, MuxerFactory>} [options.muxers]
|
|
24
27
|
* @param {(connection: Connection) => void} options.onConnection - Called when a connection is upgraded
|
|
25
28
|
* @param {(connection: Connection) => void} options.onConnectionEnd
|
|
26
29
|
*/
|
|
27
|
-
constructor({ localPeer, metrics, cryptos, muxers, onConnectionEnd, onConnection }: {
|
|
30
|
+
constructor({ localPeer, metrics, connectionGater, cryptos, muxers, onConnectionEnd, onConnection }: {
|
|
28
31
|
localPeer: PeerId;
|
|
32
|
+
connectionGater: ConnectionGater;
|
|
29
33
|
metrics?: import("./metrics") | undefined;
|
|
30
34
|
cryptos?: Map<string, import("libp2p-interfaces/src/crypto/types").Crypto> | undefined;
|
|
31
35
|
muxers?: Map<string, import("libp2p-interfaces/src/stream-muxer/types").MuxerFactory> | undefined;
|
|
32
36
|
onConnection: (connection: Connection) => void;
|
|
33
37
|
onConnectionEnd: (connection: Connection) => void;
|
|
34
38
|
});
|
|
39
|
+
connectionGater: import("./types").ConnectionGater;
|
|
35
40
|
localPeer: PeerId;
|
|
36
41
|
metrics: import("./metrics") | undefined;
|
|
37
42
|
cryptos: Map<string, import("libp2p-interfaces/src/crypto/types").Crypto>;
|
|
@@ -129,11 +134,12 @@ declare class Upgrader {
|
|
|
129
134
|
private _multiplexInbound;
|
|
130
135
|
}
|
|
131
136
|
declare namespace Upgrader {
|
|
132
|
-
export { MultiaddrConnection, MuxerFactory, Muxer, MuxedStream, Crypto, Connection, Multiaddr, CryptoResult };
|
|
137
|
+
export { MultiaddrConnection, MuxerFactory, Muxer, MuxedStream, Crypto, Connection, Multiaddr, ConnectionGater, CryptoResult };
|
|
133
138
|
}
|
|
134
139
|
import PeerId = require("peer-id");
|
|
135
140
|
type MultiaddrConnection = import('libp2p-interfaces/src/transport/types').MultiaddrConnection;
|
|
136
141
|
import { Connection } from "libp2p-interfaces/src/connection";
|
|
142
|
+
type ConnectionGater = import('./types').ConnectionGater;
|
|
137
143
|
type MuxerFactory = import('libp2p-interfaces/src/stream-muxer/types').MuxerFactory;
|
|
138
144
|
type Muxer = import('libp2p-interfaces/src/stream-muxer/types').Muxer;
|
|
139
145
|
type MuxedStream = import('libp2p-interfaces/src/stream-muxer/types').MuxedStream;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"upgrader.d.ts","sourceRoot":"","sources":["../../src/upgrader.js"],"names":[],"mappings":";
|
|
1
|
+
{"version":3,"file":"upgrader.d.ts","sourceRoot":"","sources":["../../src/upgrader.js"],"names":[],"mappings":";AAgBA;;;;;;;;;GASG;AAEH;;;;;GAKG;AAEH;IACE;;;;;;;;;;OAUG;IACH;mBATW,MAAM;yBACN,eAAe;;;;mCAKF,UAAU,KAAK,IAAI;sCACnB,UAAU,KAAK,IAAI;OAqB1C;IAVC,mDAAsC;IACtC,kBAA0B;IAC1B,yCAAsB;IACtB,0EAAsB;IACtB,qFAAoB;IACpB,sCAAsC;IACtC,WADW,OAAO,QAAQ,CAAC,GAAG,IAAI,CACb;IACrB,yBAA0B;IAC1B,2BApBsB,UAAU,KAAK,IAAI,CAoBT;IAChC,8BApBsB,UAAU,KAAK,IAAI,CAoBH;IAGxC;;;;;;OAMG;IACH,uBAHW,mBAAmB,GACjB,QAAQ,UAAU,CAAC,CAyE/B;IAED;;;;;;OAMG;IACH,wBAHW,mBAAmB,GACjB,QAAQ,UAAU,CAAC,CAgF/B;IAED;;;;;;;;;;;;OAYG;IACH,0BAgHC;IAED;;;;;;;;OAQG;IACH,kBAGC;IAED;;;;;;;;;OASG;IACH,wBAqBC;IAED;;;;;;;;;;;OAWG;IACH,yBAqBC;IAED;;;;;;;;;OASG;IACH,2BAYC;IAED;;;;;;;;;OASG;IACH,0BAWC;CACF;;;;;2BAxdY,OAAO,uCAAuC,EAAE,mBAAmB;;uBAOnE,OAAO,SAAS,EAAE,eAAe;oBANjC,OAAO,0CAA0C,EAAE,YAAY;aAC/D,OAAO,0CAA0C,EAAE,KAAK;mBACxD,OAAO,0CAA0C,EAAE,WAAW;cAC9D,OAAO,oCAAoC,EAAE,MAAM;;iBAEnD,OAAO,WAAW,EAAE,SAAS;;;;;UAM5B,mBAAmB;gBACnB,MAAM;cACN,MAAM"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "libp2p",
|
|
3
|
-
"version": "0.35.8-
|
|
3
|
+
"version": "0.35.8-ff32eba",
|
|
4
4
|
"description": "JavaScript implementation of libp2p, a modular peer to peer network stack",
|
|
5
5
|
"leadMaintainer": "Jacob Heun <jacobheun@gmail.com>",
|
|
6
6
|
"main": "src/index.js",
|
|
@@ -20,15 +20,17 @@
|
|
|
20
20
|
"scripts": {
|
|
21
21
|
"lint": "aegir lint",
|
|
22
22
|
"build": "aegir build",
|
|
23
|
-
"build:proto": "npm run build:proto:circuit && npm run build:proto:identify && npm run build:proto:plaintext && npm run build:proto:address-book && npm run build:proto:proto-book && npm run build:proto:peer && npm run build:proto:peer-record && npm run build:proto:envelope",
|
|
23
|
+
"build:proto": "npm run build:proto:circuit && npm run build:proto:fetch && npm run build:proto:identify && npm run build:proto:plaintext && npm run build:proto:address-book && npm run build:proto:proto-book && npm run build:proto:peer && npm run build:proto:peer-record && npm run build:proto:envelope",
|
|
24
24
|
"build:proto:circuit": "pbjs -t static-module -w commonjs -r libp2p-circuit --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o src/circuit/protocol/index.js ./src/circuit/protocol/index.proto",
|
|
25
|
+
"build:proto:fetch": "pbjs -t static-module -w commonjs -r libp2p-fetch --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o src/fetch/proto.js ./src/fetch/proto.proto",
|
|
25
26
|
"build:proto:identify": "pbjs -t static-module -w commonjs -r libp2p-identify --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o src/identify/message.js ./src/identify/message.proto",
|
|
26
27
|
"build:proto:plaintext": "pbjs -t static-module -w commonjs -r libp2p-plaintext --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o src/insecure/proto.js ./src/insecure/proto.proto",
|
|
27
28
|
"build:proto:peer": "pbjs -t static-module -w commonjs -r libp2p-peer --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o src/peer-store/pb/peer.js ./src/peer-store/pb/peer.proto",
|
|
28
29
|
"build:proto:peer-record": "pbjs -t static-module -w commonjs -r libp2p-peer-record --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o src/record/peer-record/peer-record.js ./src/record/peer-record/peer-record.proto",
|
|
29
30
|
"build:proto:envelope": "pbjs -t static-module -w commonjs -r libp2p-envelope --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o src/record/envelope/envelope.js ./src/record/envelope/envelope.proto",
|
|
30
|
-
"build:proto-types": "npm run build:proto-types:circuit && npm run build:proto-types:identify && npm run build:proto-types:plaintext && npm run build:proto-types:address-book && npm run build:proto-types:proto-book && npm run build:proto-types:peer && npm run build:proto-types:peer-record && npm run build:proto-types:envelope",
|
|
31
|
+
"build:proto-types": "npm run build:proto-types:circuit && npm run build:proto-types:fetch && npm run build:proto-types:identify && npm run build:proto-types:plaintext && npm run build:proto-types:address-book && npm run build:proto-types:proto-book && npm run build:proto-types:peer && npm run build:proto-types:peer-record && npm run build:proto-types:envelope",
|
|
31
32
|
"build:proto-types:circuit": "pbts -o src/circuit/protocol/index.d.ts src/circuit/protocol/index.js",
|
|
33
|
+
"build:proto-types:fetch": "pbts -o src/fetch/proto.d.ts src/fetch/proto.js",
|
|
32
34
|
"build:proto-types:identify": "pbts -o src/identify/message.d.ts src/identify/message.js",
|
|
33
35
|
"build:proto-types:plaintext": "pbts -o src/insecure/proto.d.ts src/insecure/proto.js",
|
|
34
36
|
"build:proto-types:peer": "pbts -o src/peer-store/pb/peer.d.ts src/peer-store/pb/peer.js",
|
|
@@ -93,11 +95,13 @@
|
|
|
93
95
|
"it-drain": "^1.0.3",
|
|
94
96
|
"it-filter": "^1.0.1",
|
|
95
97
|
"it-first": "^1.0.4",
|
|
98
|
+
"it-foreach": "^0.1.1",
|
|
96
99
|
"it-handshake": "^2.0.0",
|
|
97
100
|
"it-length-prefixed": "^5.0.2",
|
|
98
101
|
"it-map": "^1.0.4",
|
|
99
102
|
"it-merge": "^1.0.0",
|
|
100
103
|
"it-pipe": "^1.1.0",
|
|
104
|
+
"it-sort": "^1.0.1",
|
|
101
105
|
"it-take": "^1.0.0",
|
|
102
106
|
"libp2p-crypto": "^0.21.2",
|
|
103
107
|
"libp2p-interfaces": "^4.0.0",
|
|
@@ -107,7 +111,7 @@
|
|
|
107
111
|
"mortice": "^2.0.1",
|
|
108
112
|
"multiaddr": "^10.0.0",
|
|
109
113
|
"multiformats": "^9.0.0",
|
|
110
|
-
"multistream-select": "^
|
|
114
|
+
"multistream-select": "^3.0.0",
|
|
111
115
|
"mutable-proxy": "^1.0.0",
|
|
112
116
|
"nat-api": "^0.3.1",
|
|
113
117
|
"node-forge": "^1.2.1",
|
|
@@ -8,6 +8,7 @@ const log = Object.assign(debug('libp2p:auto-relay'), {
|
|
|
8
8
|
const { fromString: uint8ArrayFromString } = require('uint8arrays/from-string')
|
|
9
9
|
const { toString: uint8ArrayToString } = require('uint8arrays/to-string')
|
|
10
10
|
const { Multiaddr } = require('multiaddr')
|
|
11
|
+
const all = require('it-all')
|
|
11
12
|
|
|
12
13
|
const { relay: multicodec } = require('./multicodec')
|
|
13
14
|
const { canHop } = require('./circuit/hop')
|
|
@@ -149,27 +150,35 @@ class AutoRelay {
|
|
|
149
150
|
* @returns {Promise<void>}
|
|
150
151
|
*/
|
|
151
152
|
async _addListenRelay (connection, id) {
|
|
152
|
-
// Check if already listening on enough relays
|
|
153
|
-
if (this._listenRelays.size >= this.maxListeners) {
|
|
154
|
-
return
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
// Get peer known addresses and sort them per public addresses first
|
|
158
|
-
const remoteAddrs = await this._peerStore.addressBook.getMultiaddrsForPeer(
|
|
159
|
-
connection.remotePeer, this._addressSorter
|
|
160
|
-
)
|
|
161
|
-
|
|
162
|
-
if (!remoteAddrs || !remoteAddrs.length) {
|
|
163
|
-
return
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
const listenAddr = `${remoteAddrs[0].toString()}/p2p-circuit`
|
|
167
|
-
this._listenRelays.add(id)
|
|
168
|
-
|
|
169
|
-
// Attempt to listen on relay
|
|
170
153
|
try {
|
|
171
|
-
|
|
172
|
-
|
|
154
|
+
// Check if already listening on enough relays
|
|
155
|
+
if (this._listenRelays.size >= this.maxListeners) {
|
|
156
|
+
return
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Get peer known addresses and sort them per public addresses first
|
|
160
|
+
const remoteAddrs = await this._peerStore.addressBook.getMultiaddrsForPeer(
|
|
161
|
+
connection.remotePeer, this._addressSorter
|
|
162
|
+
)
|
|
163
|
+
|
|
164
|
+
// Attempt to listen on relay
|
|
165
|
+
const result = await Promise.all(
|
|
166
|
+
remoteAddrs.map(async addr => {
|
|
167
|
+
try {
|
|
168
|
+
// Announce multiaddrs will update on listen success by TransportManager event being triggered
|
|
169
|
+
await this._transportManager.listen([new Multiaddr(`${addr.toString()}/p2p-circuit`)])
|
|
170
|
+
return true
|
|
171
|
+
} catch (/** @type {any} */ err) {
|
|
172
|
+
this._onError(err)
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return false
|
|
176
|
+
})
|
|
177
|
+
)
|
|
178
|
+
|
|
179
|
+
if (result.includes(true)) {
|
|
180
|
+
this._listenRelays.add(id)
|
|
181
|
+
}
|
|
173
182
|
} catch (/** @type {any} */ err) {
|
|
174
183
|
this._onError(err)
|
|
175
184
|
this._listenRelays.delete(id)
|
|
@@ -206,13 +215,18 @@ class AutoRelay {
|
|
|
206
215
|
}
|
|
207
216
|
|
|
208
217
|
const knownHopsToDial = []
|
|
218
|
+
const peers = await all(this._peerStore.getPeers())
|
|
209
219
|
|
|
210
220
|
// Check if we have known hop peers to use and attempt to listen on the already connected
|
|
211
|
-
for await (const { id, metadata } of
|
|
221
|
+
for await (const { id, metadata } of peers) {
|
|
212
222
|
const idStr = id.toB58String()
|
|
213
223
|
|
|
214
224
|
// Continue to next if listening on this or peer to ignore
|
|
215
|
-
if (this._listenRelays.has(idStr)
|
|
225
|
+
if (this._listenRelays.has(idStr)) {
|
|
226
|
+
continue
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
if (peersToIgnore.includes(idStr)) {
|
|
216
230
|
continue
|
|
217
231
|
}
|
|
218
232
|
|
package/src/config.js
CHANGED
|
@@ -13,6 +13,7 @@ const { FaultTolerance } = require('./transport-manager')
|
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
15
|
* @typedef {import('multiaddr').Multiaddr} Multiaddr
|
|
16
|
+
* @typedef {import('./types').ConnectionGater} ConnectionGater
|
|
16
17
|
* @typedef {import('.').Libp2pOptions} Libp2pOptions
|
|
17
18
|
* @typedef {import('.').constructorOptions} constructorOptions
|
|
18
19
|
*/
|
|
@@ -27,6 +28,7 @@ const DefaultConfig = {
|
|
|
27
28
|
connectionManager: {
|
|
28
29
|
minConnections: 25
|
|
29
30
|
},
|
|
31
|
+
connectionGater: /** @type {ConnectionGater} */ {},
|
|
30
32
|
transportManager: {
|
|
31
33
|
faultTolerance: FaultTolerance.FATAL_ALL
|
|
32
34
|
},
|
|
@@ -5,6 +5,9 @@ const mergeOptions = require('merge-options')
|
|
|
5
5
|
// @ts-ignore retimer does not have types
|
|
6
6
|
const retimer = require('retimer')
|
|
7
7
|
const all = require('it-all')
|
|
8
|
+
const { pipe } = require('it-pipe')
|
|
9
|
+
const filter = require('it-filter')
|
|
10
|
+
const sort = require('it-sort')
|
|
8
11
|
|
|
9
12
|
const log = Object.assign(debug('libp2p:connection-manager:auto-dialler'), {
|
|
10
13
|
error: debug('libp2p:connection-manager:auto-dialler:err')
|
|
@@ -90,21 +93,27 @@ class AutoDialler {
|
|
|
90
93
|
// Sort peers on whether we know protocols of public keys for them
|
|
91
94
|
// TODO: assuming the `peerStore.getPeers()` order is stable this will mean
|
|
92
95
|
// we keep trying to connect to the same peers?
|
|
93
|
-
const peers =
|
|
94
|
-
.
|
|
96
|
+
const peers = await pipe(
|
|
97
|
+
this._libp2p.peerStore.getPeers(),
|
|
98
|
+
(source) => filter(source, (peer) => !peer.id.equals(this._libp2p.peerId)),
|
|
99
|
+
(source) => sort(source, (a, b) => {
|
|
95
100
|
if (b.protocols && b.protocols.length && (!a.protocols || !a.protocols.length)) {
|
|
96
101
|
return 1
|
|
97
102
|
} else if (b.id.pubKey && !a.id.pubKey) {
|
|
98
103
|
return 1
|
|
99
104
|
}
|
|
100
105
|
return -1
|
|
101
|
-
})
|
|
106
|
+
}),
|
|
107
|
+
(source) => all(source)
|
|
108
|
+
)
|
|
102
109
|
|
|
103
110
|
for (let i = 0; this._running && i < peers.length && this._libp2p.connections.size < minConnections; i++) {
|
|
104
|
-
|
|
105
|
-
|
|
111
|
+
const peer = peers[i]
|
|
112
|
+
|
|
113
|
+
if (!this._libp2p.connectionManager.get(peer.id)) {
|
|
114
|
+
log('connecting to a peerStore stored peer %s', peer.id.toB58String())
|
|
106
115
|
try {
|
|
107
|
-
await this._libp2p.dialer.connectToPeer(
|
|
116
|
+
await this._libp2p.dialer.connectToPeer(peer.id)
|
|
108
117
|
} catch (/** @type {any} */ err) {
|
|
109
118
|
log.error('could not connect to peerStore stored peer', err)
|
|
110
119
|
}
|
|
@@ -112,6 +112,9 @@ class ConnectionManager extends EventEmitter {
|
|
|
112
112
|
latencyCheckIntervalMs: this._options.pollInterval,
|
|
113
113
|
dataEmitIntervalMs: this._options.pollInterval
|
|
114
114
|
})
|
|
115
|
+
|
|
116
|
+
// This emitter gets listened to a lot
|
|
117
|
+
this.setMaxListeners(Infinity)
|
|
115
118
|
}
|
|
116
119
|
|
|
117
120
|
/**
|
package/src/dialer/index.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
const debug = require('debug')
|
|
4
|
+
const all = require('it-all')
|
|
5
|
+
const filter = require('it-filter')
|
|
6
|
+
const { pipe } = require('it-pipe')
|
|
4
7
|
const log = Object.assign(debug('libp2p:dialer'), {
|
|
5
8
|
error: debug('libp2p:dialer:err')
|
|
6
9
|
})
|
|
@@ -33,12 +36,14 @@ const METRICS_PENDING_DIAL_TARGETS = 'pending-dial-targets'
|
|
|
33
36
|
* @typedef {import('../peer-store/types').PeerStore} PeerStore
|
|
34
37
|
* @typedef {import('../peer-store/types').Address} Address
|
|
35
38
|
* @typedef {import('../transport-manager')} TransportManager
|
|
39
|
+
* @typedef {import('../types').ConnectionGater} ConnectionGater
|
|
36
40
|
*/
|
|
37
41
|
|
|
38
42
|
/**
|
|
39
43
|
* @typedef {Object} DialerProperties
|
|
40
44
|
* @property {PeerStore} peerStore
|
|
41
45
|
* @property {TransportManager} transportManager
|
|
46
|
+
* @property {ConnectionGater} connectionGater
|
|
42
47
|
*
|
|
43
48
|
* @typedef {(addr:Multiaddr) => Promise<string[]>} Resolver
|
|
44
49
|
*
|
|
@@ -70,6 +75,7 @@ class Dialer {
|
|
|
70
75
|
constructor ({
|
|
71
76
|
transportManager,
|
|
72
77
|
peerStore,
|
|
78
|
+
connectionGater,
|
|
73
79
|
addressSorter = publicAddressesFirst,
|
|
74
80
|
maxParallelDials = MAX_PARALLEL_DIALS,
|
|
75
81
|
maxAddrsToDial = MAX_ADDRS_TO_DIAL,
|
|
@@ -78,6 +84,7 @@ class Dialer {
|
|
|
78
84
|
resolvers = {},
|
|
79
85
|
metrics
|
|
80
86
|
}) {
|
|
87
|
+
this.connectionGater = connectionGater
|
|
81
88
|
this.transportManager = transportManager
|
|
82
89
|
this.peerStore = peerStore
|
|
83
90
|
this.addressSorter = addressSorter
|
|
@@ -136,6 +143,12 @@ class Dialer {
|
|
|
136
143
|
* @returns {Promise<Connection>}
|
|
137
144
|
*/
|
|
138
145
|
async connectToPeer (peer, options = {}) {
|
|
146
|
+
const { id } = getPeer(peer)
|
|
147
|
+
|
|
148
|
+
if (await this.connectionGater.denyDialPeer(id)) {
|
|
149
|
+
throw errCode(new Error('The dial request is blocked by gater.allowDialPeer'), codes.ERR_PEER_DIAL_INTERCEPTED)
|
|
150
|
+
}
|
|
151
|
+
|
|
139
152
|
const dialTarget = await this._createCancellableDialTarget(peer)
|
|
140
153
|
|
|
141
154
|
if (!dialTarget.addrs.length) {
|
|
@@ -203,7 +216,13 @@ class Dialer {
|
|
|
203
216
|
await this.peerStore.addressBook.add(id, multiaddrs)
|
|
204
217
|
}
|
|
205
218
|
|
|
206
|
-
let knownAddrs = await
|
|
219
|
+
let knownAddrs = await pipe(
|
|
220
|
+
await this.peerStore.addressBook.getMultiaddrsForPeer(id, this.addressSorter),
|
|
221
|
+
(source) => filter(source, async (multiaddr) => {
|
|
222
|
+
return !(await this.connectionGater.denyDialMultiaddr(id, multiaddr))
|
|
223
|
+
}),
|
|
224
|
+
(source) => all(source)
|
|
225
|
+
)
|
|
207
226
|
|
|
208
227
|
// If received a multiaddr to dial, it should be the first to use
|
|
209
228
|
// But, if we know other multiaddrs for the peer, we should try them too.
|
package/src/errors.js
CHANGED
|
@@ -12,6 +12,8 @@ exports.codes = {
|
|
|
12
12
|
PUBSUB_NOT_STARTED: 'ERR_PUBSUB_NOT_STARTED',
|
|
13
13
|
DHT_NOT_STARTED: 'ERR_DHT_NOT_STARTED',
|
|
14
14
|
CONN_ENCRYPTION_REQUIRED: 'ERR_CONN_ENCRYPTION_REQUIRED',
|
|
15
|
+
ERR_PEER_DIAL_INTERCEPTED: 'ERR_PEER_DIAL_INTERCEPTED',
|
|
16
|
+
ERR_CONNECTION_INTERCEPTED: 'ERR_CONNECTION_INTERCEPTED',
|
|
15
17
|
ERR_INVALID_PROTOCOLS_FOR_STREAM: 'ERR_INVALID_PROTOCOLS_FOR_STREAM',
|
|
16
18
|
ERR_CONNECTION_ENDED: 'ERR_CONNECTION_ENDED',
|
|
17
19
|
ERR_CONNECTION_FAILED: 'ERR_CONNECTION_FAILED',
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
libp2p-fetch JavaScript Implementation
|
|
2
|
+
=====================================
|
|
3
|
+
|
|
4
|
+
> Libp2p fetch protocol JavaScript implementation
|
|
5
|
+
|
|
6
|
+
## Overview
|
|
7
|
+
|
|
8
|
+
An implementation of the Fetch protocol as described here: https://github.com/libp2p/specs/tree/master/fetch
|
|
9
|
+
|
|
10
|
+
The fetch protocol is a simple protocol for requesting a value corresponding to a key from a peer.
|
|
11
|
+
|
|
12
|
+
## Usage
|
|
13
|
+
|
|
14
|
+
```javascript
|
|
15
|
+
const Libp2p = require('libp2p')
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Given a key (as a string) returns a value (as a Uint8Array), or null if the key isn't found.
|
|
19
|
+
* All keys must be prefixed my the same prefix, which will be used to find the appropriate key
|
|
20
|
+
* lookup function.
|
|
21
|
+
* @param key - a string
|
|
22
|
+
* @returns value - a Uint8Array value that corresponds to the given key, or null if the key doesn't
|
|
23
|
+
* have a corresponding value.
|
|
24
|
+
*/
|
|
25
|
+
async function my_subsystem_key_lookup(key) {
|
|
26
|
+
// app specific callback to lookup key-value pairs.
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Enable this peer to respond to fetch requests for keys that begin with '/my_subsystem_key_prefix/'
|
|
30
|
+
const libp2p = Libp2p.create(...)
|
|
31
|
+
libp2p.fetchService.registerLookupFunction('/my_subsystem_key_prefix/', my_subsystem_key_lookup)
|
|
32
|
+
|
|
33
|
+
const key = '/my_subsystem_key_prefix/{...}'
|
|
34
|
+
const peerDst = PeerId.parse('Qmfoo...') // or Multiaddr instance
|
|
35
|
+
const value = await libp2p.fetch(peerDst, key)
|
|
36
|
+
```
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const debug = require('debug')
|
|
4
|
+
const log = Object.assign(debug('libp2p:fetch'), {
|
|
5
|
+
error: debug('libp2p:fetch:err')
|
|
6
|
+
})
|
|
7
|
+
const errCode = require('err-code')
|
|
8
|
+
const { codes } = require('../errors')
|
|
9
|
+
const lp = require('it-length-prefixed')
|
|
10
|
+
const { FetchRequest, FetchResponse } = require('./proto')
|
|
11
|
+
// @ts-ignore it-handshake does not export types
|
|
12
|
+
const handshake = require('it-handshake')
|
|
13
|
+
const { PROTOCOL } = require('./constants')
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @typedef {import('../')} Libp2p
|
|
17
|
+
* @typedef {import('multiaddr').Multiaddr} Multiaddr
|
|
18
|
+
* @typedef {import('peer-id')} PeerId
|
|
19
|
+
* @typedef {import('libp2p-interfaces/src/stream-muxer/types').MuxedStream} MuxedStream
|
|
20
|
+
* @typedef {(key: string) => Promise<Uint8Array | null>} LookupFunction
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* A simple libp2p protocol for requesting a value corresponding to a key from a peer.
|
|
25
|
+
* Developers can register one or more lookup function for retrieving the value corresponding to
|
|
26
|
+
* a given key. Each lookup function must act on a distinct part of the overall key space, defined
|
|
27
|
+
* by a fixed prefix that all keys that should be routed to that lookup function will start with.
|
|
28
|
+
*/
|
|
29
|
+
class FetchProtocol {
|
|
30
|
+
/**
|
|
31
|
+
* @param {Libp2p} libp2p
|
|
32
|
+
*/
|
|
33
|
+
constructor (libp2p) {
|
|
34
|
+
this._lookupFunctions = new Map() // Maps key prefix to value lookup function
|
|
35
|
+
this._libp2p = libp2p
|
|
36
|
+
this.handleMessage = this.handleMessage.bind(this)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Sends a request to fetch the value associated with the given key from the given peer.
|
|
41
|
+
*
|
|
42
|
+
* @param {PeerId|Multiaddr} peer
|
|
43
|
+
* @param {string} key
|
|
44
|
+
* @returns {Promise<Uint8Array | null>}
|
|
45
|
+
*/
|
|
46
|
+
async fetch (peer, key) {
|
|
47
|
+
// @ts-ignore multiaddr might not have toB58String
|
|
48
|
+
log('dialing %s to %s', this._protocol, peer.toB58String ? peer.toB58String() : peer)
|
|
49
|
+
|
|
50
|
+
const connection = await this._libp2p.dial(peer)
|
|
51
|
+
const { stream } = await connection.newStream(FetchProtocol.PROTOCOL)
|
|
52
|
+
const shake = handshake(stream)
|
|
53
|
+
|
|
54
|
+
// send message
|
|
55
|
+
const request = new FetchRequest({ identifier: key })
|
|
56
|
+
shake.write(lp.encode.single(FetchRequest.encode(request).finish()))
|
|
57
|
+
|
|
58
|
+
// read response
|
|
59
|
+
const response = FetchResponse.decode((await lp.decode.fromReader(shake.reader).next()).value.slice())
|
|
60
|
+
switch (response.status) {
|
|
61
|
+
case (FetchResponse.StatusCode.OK): {
|
|
62
|
+
return response.data
|
|
63
|
+
}
|
|
64
|
+
case (FetchResponse.StatusCode.NOT_FOUND): {
|
|
65
|
+
return null
|
|
66
|
+
}
|
|
67
|
+
case (FetchResponse.StatusCode.ERROR): {
|
|
68
|
+
const errmsg = (new TextDecoder()).decode(response.data)
|
|
69
|
+
throw errCode(new Error('Error in fetch protocol response: ' + errmsg), codes.ERR_INVALID_PARAMETERS)
|
|
70
|
+
}
|
|
71
|
+
default: {
|
|
72
|
+
throw errCode(new Error('Unknown response status'), codes.ERR_INVALID_MESSAGE)
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Invoked when a fetch request is received. Reads the request message off the given stream and
|
|
79
|
+
* responds based on looking up the key in the request via the lookup callback that corresponds
|
|
80
|
+
* to the key's prefix.
|
|
81
|
+
*
|
|
82
|
+
* @param {object} options
|
|
83
|
+
* @param {MuxedStream} options.stream
|
|
84
|
+
* @param {string} options.protocol
|
|
85
|
+
*/
|
|
86
|
+
async handleMessage (options) {
|
|
87
|
+
const { stream } = options
|
|
88
|
+
const shake = handshake(stream)
|
|
89
|
+
const request = FetchRequest.decode((await lp.decode.fromReader(shake.reader).next()).value.slice())
|
|
90
|
+
|
|
91
|
+
let response
|
|
92
|
+
const lookup = this._getLookupFunction(request.identifier)
|
|
93
|
+
if (lookup) {
|
|
94
|
+
const data = await lookup(request.identifier)
|
|
95
|
+
if (data) {
|
|
96
|
+
response = new FetchResponse({ status: FetchResponse.StatusCode.OK, data })
|
|
97
|
+
} else {
|
|
98
|
+
response = new FetchResponse({ status: FetchResponse.StatusCode.NOT_FOUND })
|
|
99
|
+
}
|
|
100
|
+
} else {
|
|
101
|
+
const errmsg = (new TextEncoder()).encode('No lookup function registered for key: ' + request.identifier)
|
|
102
|
+
response = new FetchResponse({ status: FetchResponse.StatusCode.ERROR, data: errmsg })
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
shake.write(lp.encode.single(FetchResponse.encode(response).finish()))
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Given a key, finds the appropriate function for looking up its corresponding value, based on
|
|
110
|
+
* the key's prefix.
|
|
111
|
+
*
|
|
112
|
+
* @param {string} key
|
|
113
|
+
*/
|
|
114
|
+
_getLookupFunction (key) {
|
|
115
|
+
for (const prefix of this._lookupFunctions.keys()) {
|
|
116
|
+
if (key.startsWith(prefix)) {
|
|
117
|
+
return this._lookupFunctions.get(prefix)
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return null
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Registers a new lookup callback that can map keys to values, for a given set of keys that
|
|
125
|
+
* share the same prefix.
|
|
126
|
+
*
|
|
127
|
+
* @param {string} prefix
|
|
128
|
+
* @param {LookupFunction} lookup
|
|
129
|
+
*/
|
|
130
|
+
registerLookupFunction (prefix, lookup) {
|
|
131
|
+
if (this._lookupFunctions.has(prefix)) {
|
|
132
|
+
throw errCode(new Error("Fetch protocol handler for key prefix '" + prefix + "' already registered"), codes.ERR_KEY_ALREADY_EXISTS)
|
|
133
|
+
}
|
|
134
|
+
this._lookupFunctions.set(prefix, lookup)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Registers a new lookup callback that can map keys to values, for a given set of keys that
|
|
139
|
+
* share the same prefix.
|
|
140
|
+
*
|
|
141
|
+
* @param {string} prefix
|
|
142
|
+
* @param {LookupFunction} [lookup]
|
|
143
|
+
*/
|
|
144
|
+
unregisterLookupFunction (prefix, lookup) {
|
|
145
|
+
if (lookup != null) {
|
|
146
|
+
const existingLookup = this._lookupFunctions.get(prefix)
|
|
147
|
+
|
|
148
|
+
if (existingLookup !== lookup) {
|
|
149
|
+
return
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
this._lookupFunctions.delete(prefix)
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
FetchProtocol.PROTOCOL = PROTOCOL
|
|
158
|
+
|
|
159
|
+
exports = module.exports = FetchProtocol
|