libp2p 2.3.1 → 2.4.0-127abe24b
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/README.md +9 -25
- package/dist/index.min.js +18 -15
- package/dist/src/address-manager/dns-mappings.d.ts +20 -0
- package/dist/src/address-manager/dns-mappings.d.ts.map +1 -0
- package/dist/src/address-manager/dns-mappings.js +139 -0
- package/dist/src/address-manager/dns-mappings.js.map +1 -0
- package/dist/src/{address-manager.d.ts → address-manager/index.d.ts} +54 -10
- package/dist/src/address-manager/index.d.ts.map +1 -0
- package/dist/src/address-manager/index.js +264 -0
- package/dist/src/address-manager/index.js.map +1 -0
- package/dist/src/address-manager/ip-mappings.d.ts +18 -0
- package/dist/src/address-manager/ip-mappings.d.ts.map +1 -0
- package/dist/src/address-manager/ip-mappings.js +143 -0
- package/dist/src/address-manager/ip-mappings.js.map +1 -0
- package/dist/src/address-manager/observed-addresses.d.ts +19 -0
- package/dist/src/address-manager/observed-addresses.d.ts.map +1 -0
- package/dist/src/address-manager/observed-addresses.js +70 -0
- package/dist/src/address-manager/observed-addresses.js.map +1 -0
- package/dist/src/address-manager/transport-addresses.d.ts +19 -0
- package/dist/src/address-manager/transport-addresses.d.ts.map +1 -0
- package/dist/src/address-manager/transport-addresses.js +83 -0
- package/dist/src/address-manager/transport-addresses.js.map +1 -0
- package/dist/src/config/connection-gater.browser.d.ts +7 -3
- package/dist/src/config/connection-gater.browser.d.ts.map +1 -1
- package/dist/src/config/connection-gater.browser.js +16 -4
- package/dist/src/config/connection-gater.browser.js.map +1 -1
- package/dist/src/connection-manager/connection-pruner.d.ts.map +1 -1
- package/dist/src/connection-manager/connection-pruner.js +4 -3
- package/dist/src/connection-manager/connection-pruner.js.map +1 -1
- package/dist/src/connection-manager/dial-queue.d.ts +3 -3
- package/dist/src/connection-manager/dial-queue.d.ts.map +1 -1
- package/dist/src/connection-manager/dial-queue.js +6 -13
- package/dist/src/connection-manager/dial-queue.js.map +1 -1
- package/dist/src/connection-manager/index.d.ts +1 -0
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +10 -6
- package/dist/src/connection-manager/index.js.map +1 -1
- package/dist/src/connection-manager/utils.d.ts +13 -1
- package/dist/src/connection-manager/utils.d.ts.map +1 -1
- package/dist/src/connection-manager/utils.js +33 -1
- package/dist/src/connection-manager/utils.js.map +1 -1
- package/dist/src/index.d.ts +1 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/libp2p.js +1 -1
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/version.d.ts +2 -2
- package/dist/src/version.d.ts.map +1 -1
- package/dist/src/version.js +2 -2
- package/dist/src/version.js.map +1 -1
- package/package.json +27 -25
- package/src/address-manager/dns-mappings.ts +182 -0
- package/src/address-manager/index.ts +413 -0
- package/src/address-manager/ip-mappings.ts +191 -0
- package/src/address-manager/observed-addresses.ts +94 -0
- package/src/address-manager/transport-addresses.ts +116 -0
- package/src/config/connection-gater.browser.ts +18 -4
- package/src/connection-manager/connection-pruner.ts +6 -4
- package/src/connection-manager/dial-queue.ts +12 -19
- package/src/connection-manager/index.ts +14 -8
- package/src/connection-manager/utils.ts +35 -2
- package/src/index.ts +1 -1
- package/src/libp2p.ts +1 -1
- package/src/version.ts +2 -2
- package/LICENSE +0 -4
- package/dist/src/address-manager.d.ts.map +0 -1
- package/dist/src/address-manager.js +0 -214
- package/dist/src/address-manager.js.map +0 -1
- package/dist/typedoc-urls.json +0 -19
- package/src/address-manager.ts +0 -319
package/LICENSE
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"address-manager.d.ts","sourceRoot":"","sources":["../../src/address-manager.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,eAAe,EAAE,YAAY,EAAU,gBAAgB,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAA;AACnH,OAAO,KAAK,EAAE,cAAc,IAAI,uBAAuB,EAAE,gBA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AK,SAAS,EAAE;IAI9B;;OAEG;IACH,gBAAgB,IAAK,SAAS,EAAE;IAIhC;;OAEG;IACH,gBAAgB,IAAK,SAAS,EAAE;IAIhC;;OAEG;IACH,eAAe,CAAE,IAAI,EAAE,SAAS,GAAG,IAAI;IAcvC,mBAAmB,CAAE,IAAI,EAAE,SAAS,GAAG,IAAI;IAoB3C,kBAAkB,CAAE,IAAI,EAAE,SAAS,GAAG,IAAI;IAO1C,YAAY,IAAK,SAAS,EAAE;IA6F5B,aAAa,CAAE,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,GAAG,IAAI;IAMzD,gBAAgB,CAAE,MAAM,EAAE,MAAM,GAAG,IAAI;CAOxC"}
|
|
@@ -1,214 +0,0 @@
|
|
|
1
|
-
import { peerIdFromString } from '@libp2p/peer-id';
|
|
2
|
-
import { debounce } from '@libp2p/utils/debounce';
|
|
3
|
-
import { multiaddr, protocols } from '@multiformats/multiaddr';
|
|
4
|
-
const defaultAddressFilter = (addrs) => addrs;
|
|
5
|
-
/**
|
|
6
|
-
* If the passed multiaddr contains the passed peer id, remove it
|
|
7
|
-
*/
|
|
8
|
-
function stripPeerId(ma, peerId) {
|
|
9
|
-
const observedPeerIdStr = ma.getPeerId();
|
|
10
|
-
// strip our peer id if it has been passed
|
|
11
|
-
if (observedPeerIdStr != null) {
|
|
12
|
-
const observedPeerId = peerIdFromString(observedPeerIdStr);
|
|
13
|
-
// use same encoding for comparison
|
|
14
|
-
if (observedPeerId.equals(peerId)) {
|
|
15
|
-
ma = ma.decapsulate(multiaddr(`/p2p/${peerId.toString()}`));
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
return ma;
|
|
19
|
-
}
|
|
20
|
-
const CODEC_IP4 = 0x04;
|
|
21
|
-
const CODEC_IP6 = 0x29;
|
|
22
|
-
const CODEC_DNS4 = 0x36;
|
|
23
|
-
const CODEC_DNS6 = 0x37;
|
|
24
|
-
export class AddressManager {
|
|
25
|
-
log;
|
|
26
|
-
components;
|
|
27
|
-
// this is an array to allow for duplicates, e.g. multiples of `/ip4/0.0.0.0/tcp/0`
|
|
28
|
-
listen;
|
|
29
|
-
announce;
|
|
30
|
-
observed;
|
|
31
|
-
announceFilter;
|
|
32
|
-
ipDomainMappings;
|
|
33
|
-
where;
|
|
34
|
-
/**
|
|
35
|
-
* Responsible for managing the peer addresses.
|
|
36
|
-
* Peers can specify their listen and announce addresses.
|
|
37
|
-
* The listen addresses will be used by the libp2p transports to listen for new connections,
|
|
38
|
-
* while the announce addresses will be used for the peer addresses' to other peers in the network.
|
|
39
|
-
*/
|
|
40
|
-
constructor(components, init = {}) {
|
|
41
|
-
const { listen = [], announce = [] } = init;
|
|
42
|
-
this.components = components;
|
|
43
|
-
this.log = components.logger.forComponent('libp2p:address-manager');
|
|
44
|
-
this.listen = listen.map(ma => ma.toString());
|
|
45
|
-
this.announce = new Set(announce.map(ma => ma.toString()));
|
|
46
|
-
this.observed = new Map();
|
|
47
|
-
this.ipDomainMappings = new Map();
|
|
48
|
-
this.announceFilter = init.announceFilter ?? defaultAddressFilter;
|
|
49
|
-
// this method gets called repeatedly on startup when transports start listening so
|
|
50
|
-
// debounce it so we don't cause multiple self:peer:update events to be emitted
|
|
51
|
-
this._updatePeerStoreAddresses = debounce(this._updatePeerStoreAddresses.bind(this), 1000);
|
|
52
|
-
// update our stored addresses when new transports listen
|
|
53
|
-
components.events.addEventListener('transport:listening', () => {
|
|
54
|
-
this._updatePeerStoreAddresses();
|
|
55
|
-
});
|
|
56
|
-
// update our stored addresses when existing transports stop listening
|
|
57
|
-
components.events.addEventListener('transport:close', () => {
|
|
58
|
-
this._updatePeerStoreAddresses();
|
|
59
|
-
});
|
|
60
|
-
this.where = new Error('where');
|
|
61
|
-
}
|
|
62
|
-
[Symbol.toStringTag] = '@libp2p/address-manager';
|
|
63
|
-
_updatePeerStoreAddresses() {
|
|
64
|
-
// if announce addresses have been configured, ensure they make it into our peer
|
|
65
|
-
// record for things like identify
|
|
66
|
-
const addrs = this.getAnnounceAddrs()
|
|
67
|
-
.concat(this.components.transportManager.getAddrs())
|
|
68
|
-
.concat([...this.observed.entries()]
|
|
69
|
-
.filter(([_, metadata]) => metadata.confident)
|
|
70
|
-
.map(([str]) => multiaddr(str))).map(ma => {
|
|
71
|
-
// strip our peer id if it is present
|
|
72
|
-
if (ma.getPeerId() === this.components.peerId.toString()) {
|
|
73
|
-
return ma.decapsulate(`/p2p/${this.components.peerId.toString()}`);
|
|
74
|
-
}
|
|
75
|
-
return ma;
|
|
76
|
-
});
|
|
77
|
-
this.components.peerStore.patch(this.components.peerId, {
|
|
78
|
-
multiaddrs: addrs
|
|
79
|
-
})
|
|
80
|
-
.catch(err => { this.log.error('error updating addresses', err); });
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* Get peer listen multiaddrs
|
|
84
|
-
*/
|
|
85
|
-
getListenAddrs() {
|
|
86
|
-
return Array.from(this.listen).map((a) => multiaddr(a));
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* Get peer announcing multiaddrs
|
|
90
|
-
*/
|
|
91
|
-
getAnnounceAddrs() {
|
|
92
|
-
return Array.from(this.announce).map((a) => multiaddr(a));
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* Get observed multiaddrs
|
|
96
|
-
*/
|
|
97
|
-
getObservedAddrs() {
|
|
98
|
-
return Array.from(this.observed).map(([a]) => multiaddr(a));
|
|
99
|
-
}
|
|
100
|
-
/**
|
|
101
|
-
* Add peer observed addresses
|
|
102
|
-
*/
|
|
103
|
-
addObservedAddr(addr) {
|
|
104
|
-
addr = stripPeerId(addr, this.components.peerId);
|
|
105
|
-
const addrString = addr.toString();
|
|
106
|
-
// do not trigger the change:addresses event if we already know about this address
|
|
107
|
-
if (this.observed.has(addrString)) {
|
|
108
|
-
return;
|
|
109
|
-
}
|
|
110
|
-
this.observed.set(addrString, {
|
|
111
|
-
confident: false
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
confirmObservedAddr(addr) {
|
|
115
|
-
addr = stripPeerId(addr, this.components.peerId);
|
|
116
|
-
const addrString = addr.toString();
|
|
117
|
-
const metadata = this.observed.get(addrString) ?? {
|
|
118
|
-
confident: false
|
|
119
|
-
};
|
|
120
|
-
const startingConfidence = metadata.confident;
|
|
121
|
-
this.observed.set(addrString, {
|
|
122
|
-
confident: true
|
|
123
|
-
});
|
|
124
|
-
// only trigger the 'self:peer:update' event if our confidence in an address has changed
|
|
125
|
-
if (!startingConfidence) {
|
|
126
|
-
this._updatePeerStoreAddresses();
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
removeObservedAddr(addr) {
|
|
130
|
-
addr = stripPeerId(addr, this.components.peerId);
|
|
131
|
-
const addrString = addr.toString();
|
|
132
|
-
this.observed.delete(addrString);
|
|
133
|
-
}
|
|
134
|
-
getAddresses() {
|
|
135
|
-
let multiaddrs = this.getAnnounceAddrs();
|
|
136
|
-
if (multiaddrs.length === 0) {
|
|
137
|
-
// no configured announce addrs, add configured listen addresses
|
|
138
|
-
multiaddrs = this.components.transportManager.getAddrs();
|
|
139
|
-
}
|
|
140
|
-
// add observed addresses we are confident in
|
|
141
|
-
multiaddrs = multiaddrs
|
|
142
|
-
.concat(Array.from(this.observed)
|
|
143
|
-
.filter(([ma, metadata]) => metadata.confident)
|
|
144
|
-
.map(([ma]) => multiaddr(ma)));
|
|
145
|
-
const mappedMultiaddrs = [];
|
|
146
|
-
// add ip->domain mappings
|
|
147
|
-
for (const ma of multiaddrs) {
|
|
148
|
-
const tuples = [...ma.stringTuples()];
|
|
149
|
-
let mappedIp = false;
|
|
150
|
-
for (const [ip, domain] of this.ipDomainMappings.entries()) {
|
|
151
|
-
for (let i = 0; i < tuples.length; i++) {
|
|
152
|
-
if (tuples[i][1] !== ip) {
|
|
153
|
-
continue;
|
|
154
|
-
}
|
|
155
|
-
if (tuples[i][0] === CODEC_IP4) {
|
|
156
|
-
tuples[i][0] = CODEC_DNS4;
|
|
157
|
-
tuples[i][1] = domain;
|
|
158
|
-
mappedIp = true;
|
|
159
|
-
}
|
|
160
|
-
if (tuples[i][0] === CODEC_IP6) {
|
|
161
|
-
tuples[i][0] = CODEC_DNS6;
|
|
162
|
-
tuples[i][1] = domain;
|
|
163
|
-
mappedIp = true;
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
if (mappedIp) {
|
|
168
|
-
mappedMultiaddrs.push(multiaddr(`/${tuples.map(tuple => {
|
|
169
|
-
return [
|
|
170
|
-
protocols(tuple[0]).name,
|
|
171
|
-
tuple[1]
|
|
172
|
-
].join('/');
|
|
173
|
-
}).join('/')}`));
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
multiaddrs = multiaddrs.concat(mappedMultiaddrs);
|
|
177
|
-
// dedupe multiaddrs
|
|
178
|
-
const addrSet = new Set();
|
|
179
|
-
multiaddrs = multiaddrs.filter(ma => {
|
|
180
|
-
const maStr = ma.toString();
|
|
181
|
-
if (addrSet.has(maStr)) {
|
|
182
|
-
return false;
|
|
183
|
-
}
|
|
184
|
-
addrSet.add(maStr);
|
|
185
|
-
return true;
|
|
186
|
-
});
|
|
187
|
-
// Create advertising list
|
|
188
|
-
return this.announceFilter(Array.from(addrSet)
|
|
189
|
-
.map(str => {
|
|
190
|
-
const ma = multiaddr(str);
|
|
191
|
-
// do not append our peer id to a path multiaddr as it will become invalid
|
|
192
|
-
if (ma.protos().pop()?.path === true) {
|
|
193
|
-
return ma;
|
|
194
|
-
}
|
|
195
|
-
if (ma.getPeerId() === this.components.peerId.toString()) {
|
|
196
|
-
return ma;
|
|
197
|
-
}
|
|
198
|
-
return ma.encapsulate(`/p2p/${this.components.peerId.toString()}`);
|
|
199
|
-
}));
|
|
200
|
-
}
|
|
201
|
-
addDNSMapping(domain, addresses) {
|
|
202
|
-
addresses.forEach(ip => {
|
|
203
|
-
this.ipDomainMappings.set(ip, domain);
|
|
204
|
-
});
|
|
205
|
-
}
|
|
206
|
-
removeDNSMapping(domain) {
|
|
207
|
-
for (const [key, value] of this.ipDomainMappings.entries()) {
|
|
208
|
-
if (value === domain) {
|
|
209
|
-
this.ipDomainMappings.delete(key);
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
//# sourceMappingURL=address-manager.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"address-manager.js","sourceRoot":"","sources":["../../src/address-manager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAA;AAClD,OAAO,EAAE,QAAQ,EAAE,MAAM,wBAAwB,CAAA;AACjD,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AA4C9D,MAAM,oBAAoB,GAAG,CAAC,KAAkB,EAAe,EAAE,CAAC,KAAK,CAAA;AAMvE;;GAEG;AACH,SAAS,WAAW,CAAE,EAAa,EAAE,MAAc;IACjD,MAAM,iBAAiB,GAAG,EAAE,CAAC,SAAS,EAAE,CAAA;IAExC,0CAA0C;IAC1C,IAAI,iBAAiB,IAAI,IAAI,EAAE,CAAC;QAC9B,MAAM,cAAc,GAAG,gBAAgB,CAAC,iBAAiB,CAAC,CAAA;QAE1D,mCAAmC;QACnC,IAAI,cAAc,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC;YAClC,EAAE,GAAG,EAAE,CAAC,WAAW,CAAC,SAAS,CAAC,QAAQ,MAAM,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC,CAAA;QAC7D,CAAC;IACH,CAAC;IAED,OAAO,EAAE,CAAA;AACX,CAAC;AAED,MAAM,SAAS,GAAG,IAAI,CAAA;AACtB,MAAM,SAAS,GAAG,IAAI,CAAA;AACtB,MAAM,UAAU,GAAG,IAAI,CAAA;AACvB,MAAM,UAAU,GAAG,IAAI,CAAA;AAEvB,MAAM,OAAO,cAAc;IACR,GAAG,CAAQ;IACX,UAAU,CAA0B;IAC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,CAAC;IAED,gBAAgB,CAAE,MAAc;QAC9B,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,IAAI,CAAC,gBAAgB,CAAC,OAAO,EAAE,EAAE,CAAC;YAC3D,IAAI,KAAK,KAAK,MAAM,EAAE,CAAC;gBACrB,IAAI,CAAC,gBAAgB,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;YACnC,CAAC;QACH,CAAC;IACH,CAAC;CACF"}
|
package/dist/typedoc-urls.json
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"AddressFilter": "https://libp2p.github.io/js-libp2p/interfaces/libp2p.index.AddressFilter.html",
|
|
3
|
-
"AddressManagerInit": "https://libp2p.github.io/js-libp2p/interfaces/libp2p.index.AddressManagerInit.html",
|
|
4
|
-
"ConnectionManagerInit": "https://libp2p.github.io/js-libp2p/interfaces/libp2p.index.ConnectionManagerInit.html",
|
|
5
|
-
"ConnectionMonitorInit": "https://libp2p.github.io/js-libp2p/interfaces/libp2p.index.ConnectionMonitorInit.html",
|
|
6
|
-
"Libp2pInit": "https://libp2p.github.io/js-libp2p/interfaces/libp2p.index.Libp2pInit.html",
|
|
7
|
-
".:Libp2pInit": "https://libp2p.github.io/js-libp2p/interfaces/libp2p.index.Libp2pInit.html",
|
|
8
|
-
"TransportManagerInit": "https://libp2p.github.io/js-libp2p/interfaces/libp2p.index.TransportManagerInit.html",
|
|
9
|
-
"Libp2pOptions": "https://libp2p.github.io/js-libp2p/types/libp2p.index.Libp2pOptions.html",
|
|
10
|
-
".:Libp2pOptions": "https://libp2p.github.io/js-libp2p/types/libp2p.index.Libp2pOptions.html",
|
|
11
|
-
"ServiceFactoryMap": "https://libp2p.github.io/js-libp2p/types/libp2p.index.ServiceFactoryMap.html",
|
|
12
|
-
".:ServiceFactoryMap": "https://libp2p.github.io/js-libp2p/types/libp2p.index.ServiceFactoryMap.html",
|
|
13
|
-
"createLibp2p": "https://libp2p.github.io/js-libp2p/functions/libp2p.index.createLibp2p.html",
|
|
14
|
-
".:createLibp2p": "https://libp2p.github.io/js-libp2p/functions/libp2p.index.createLibp2p.html",
|
|
15
|
-
"name": "https://libp2p.github.io/js-libp2p/variables/libp2p.version.name.html",
|
|
16
|
-
"./version:name": "https://libp2p.github.io/js-libp2p/variables/libp2p.version.name.html",
|
|
17
|
-
"version": "https://libp2p.github.io/js-libp2p/variables/libp2p.version.version.html",
|
|
18
|
-
"./version:version": "https://libp2p.github.io/js-libp2p/variables/libp2p.version.version.html"
|
|
19
|
-
}
|
package/src/address-manager.ts
DELETED
|
@@ -1,319 +0,0 @@
|
|
|
1
|
-
import { peerIdFromString } from '@libp2p/peer-id'
|
|
2
|
-
import { debounce } from '@libp2p/utils/debounce'
|
|
3
|
-
import { multiaddr, protocols } from '@multiformats/multiaddr'
|
|
4
|
-
import type { ComponentLogger, Libp2pEvents, Logger, TypedEventTarget, PeerId, PeerStore } from '@libp2p/interface'
|
|
5
|
-
import type { AddressManager as AddressManagerInterface, TransportManager } from '@libp2p/interface-internal'
|
|
6
|
-
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
7
|
-
|
|
8
|
-
export interface AddressManagerInit {
|
|
9
|
-
/**
|
|
10
|
-
* Pass an function in this field to override the list of addresses
|
|
11
|
-
* that are announced to the network
|
|
12
|
-
*/
|
|
13
|
-
announceFilter?: AddressFilter
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* list of multiaddrs string representation to listen
|
|
17
|
-
*/
|
|
18
|
-
listen?: string[]
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* list of multiaddrs string representation to announce
|
|
22
|
-
*/
|
|
23
|
-
announce?: string[]
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* list of multiaddrs string representation to never announce
|
|
27
|
-
*/
|
|
28
|
-
noAnnounce?: string[]
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export interface AddressManagerComponents {
|
|
32
|
-
peerId: PeerId
|
|
33
|
-
transportManager: TransportManager
|
|
34
|
-
peerStore: PeerStore
|
|
35
|
-
events: TypedEventTarget<Libp2pEvents>
|
|
36
|
-
logger: ComponentLogger
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* A function that takes a list of multiaddrs and returns a list
|
|
41
|
-
* to announce
|
|
42
|
-
*/
|
|
43
|
-
export interface AddressFilter {
|
|
44
|
-
(addrs: Multiaddr[]): Multiaddr[]
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const defaultAddressFilter = (addrs: Multiaddr[]): Multiaddr[] => addrs
|
|
48
|
-
|
|
49
|
-
interface ObservedAddressMetadata {
|
|
50
|
-
confident: boolean
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* If the passed multiaddr contains the passed peer id, remove it
|
|
55
|
-
*/
|
|
56
|
-
function stripPeerId (ma: Multiaddr, peerId: PeerId): Multiaddr {
|
|
57
|
-
const observedPeerIdStr = ma.getPeerId()
|
|
58
|
-
|
|
59
|
-
// strip our peer id if it has been passed
|
|
60
|
-
if (observedPeerIdStr != null) {
|
|
61
|
-
const observedPeerId = peerIdFromString(observedPeerIdStr)
|
|
62
|
-
|
|
63
|
-
// use same encoding for comparison
|
|
64
|
-
if (observedPeerId.equals(peerId)) {
|
|
65
|
-
ma = ma.decapsulate(multiaddr(`/p2p/${peerId.toString()}`))
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
return ma
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
const CODEC_IP4 = 0x04
|
|
73
|
-
const CODEC_IP6 = 0x29
|
|
74
|
-
const CODEC_DNS4 = 0x36
|
|
75
|
-
const CODEC_DNS6 = 0x37
|
|
76
|
-
|
|
77
|
-
export class AddressManager implements AddressManagerInterface {
|
|
78
|
-
private readonly log: Logger
|
|
79
|
-
private readonly components: AddressManagerComponents
|
|
80
|
-
// this is an array to allow for duplicates, e.g. multiples of `/ip4/0.0.0.0/tcp/0`
|
|
81
|
-
private readonly listen: string[]
|
|
82
|
-
private readonly announce: Set<string>
|
|
83
|
-
private readonly observed: Map<string, ObservedAddressMetadata>
|
|
84
|
-
private readonly announceFilter: AddressFilter
|
|
85
|
-
private readonly ipDomainMappings: Map<string, string>
|
|
86
|
-
|
|
87
|
-
private readonly where: Error
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Responsible for managing the peer addresses.
|
|
91
|
-
* Peers can specify their listen and announce addresses.
|
|
92
|
-
* The listen addresses will be used by the libp2p transports to listen for new connections,
|
|
93
|
-
* while the announce addresses will be used for the peer addresses' to other peers in the network.
|
|
94
|
-
*/
|
|
95
|
-
constructor (components: AddressManagerComponents, init: AddressManagerInit = {}) {
|
|
96
|
-
const { listen = [], announce = [] } = init
|
|
97
|
-
|
|
98
|
-
this.components = components
|
|
99
|
-
this.log = components.logger.forComponent('libp2p:address-manager')
|
|
100
|
-
this.listen = listen.map(ma => ma.toString())
|
|
101
|
-
this.announce = new Set(announce.map(ma => ma.toString()))
|
|
102
|
-
this.observed = new Map()
|
|
103
|
-
this.ipDomainMappings = new Map()
|
|
104
|
-
this.announceFilter = init.announceFilter ?? defaultAddressFilter
|
|
105
|
-
|
|
106
|
-
// this method gets called repeatedly on startup when transports start listening so
|
|
107
|
-
// debounce it so we don't cause multiple self:peer:update events to be emitted
|
|
108
|
-
this._updatePeerStoreAddresses = debounce(this._updatePeerStoreAddresses.bind(this), 1000)
|
|
109
|
-
|
|
110
|
-
// update our stored addresses when new transports listen
|
|
111
|
-
components.events.addEventListener('transport:listening', () => {
|
|
112
|
-
this._updatePeerStoreAddresses()
|
|
113
|
-
})
|
|
114
|
-
// update our stored addresses when existing transports stop listening
|
|
115
|
-
components.events.addEventListener('transport:close', () => {
|
|
116
|
-
this._updatePeerStoreAddresses()
|
|
117
|
-
})
|
|
118
|
-
|
|
119
|
-
this.where = new Error('where')
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
readonly [Symbol.toStringTag] = '@libp2p/address-manager'
|
|
123
|
-
|
|
124
|
-
_updatePeerStoreAddresses (): void {
|
|
125
|
-
// if announce addresses have been configured, ensure they make it into our peer
|
|
126
|
-
// record for things like identify
|
|
127
|
-
const addrs = this.getAnnounceAddrs()
|
|
128
|
-
.concat(this.components.transportManager.getAddrs())
|
|
129
|
-
.concat(
|
|
130
|
-
[...this.observed.entries()]
|
|
131
|
-
.filter(([_, metadata]) => metadata.confident)
|
|
132
|
-
.map(([str]) => multiaddr(str))
|
|
133
|
-
).map(ma => {
|
|
134
|
-
// strip our peer id if it is present
|
|
135
|
-
if (ma.getPeerId() === this.components.peerId.toString()) {
|
|
136
|
-
return ma.decapsulate(`/p2p/${this.components.peerId.toString()}`)
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
return ma
|
|
140
|
-
})
|
|
141
|
-
|
|
142
|
-
this.components.peerStore.patch(this.components.peerId, {
|
|
143
|
-
multiaddrs: addrs
|
|
144
|
-
})
|
|
145
|
-
.catch(err => { this.log.error('error updating addresses', err) })
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* Get peer listen multiaddrs
|
|
150
|
-
*/
|
|
151
|
-
getListenAddrs (): Multiaddr[] {
|
|
152
|
-
return Array.from(this.listen).map((a) => multiaddr(a))
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Get peer announcing multiaddrs
|
|
157
|
-
*/
|
|
158
|
-
getAnnounceAddrs (): Multiaddr[] {
|
|
159
|
-
return Array.from(this.announce).map((a) => multiaddr(a))
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* Get observed multiaddrs
|
|
164
|
-
*/
|
|
165
|
-
getObservedAddrs (): Multiaddr[] {
|
|
166
|
-
return Array.from(this.observed).map(([a]) => multiaddr(a))
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* Add peer observed addresses
|
|
171
|
-
*/
|
|
172
|
-
addObservedAddr (addr: Multiaddr): void {
|
|
173
|
-
addr = stripPeerId(addr, this.components.peerId)
|
|
174
|
-
const addrString = addr.toString()
|
|
175
|
-
|
|
176
|
-
// do not trigger the change:addresses event if we already know about this address
|
|
177
|
-
if (this.observed.has(addrString)) {
|
|
178
|
-
return
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
this.observed.set(addrString, {
|
|
182
|
-
confident: false
|
|
183
|
-
})
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
confirmObservedAddr (addr: Multiaddr): void {
|
|
187
|
-
addr = stripPeerId(addr, this.components.peerId)
|
|
188
|
-
const addrString = addr.toString()
|
|
189
|
-
|
|
190
|
-
const metadata = this.observed.get(addrString) ?? {
|
|
191
|
-
confident: false
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
const startingConfidence = metadata.confident
|
|
195
|
-
|
|
196
|
-
this.observed.set(addrString, {
|
|
197
|
-
confident: true
|
|
198
|
-
})
|
|
199
|
-
|
|
200
|
-
// only trigger the 'self:peer:update' event if our confidence in an address has changed
|
|
201
|
-
if (!startingConfidence) {
|
|
202
|
-
this._updatePeerStoreAddresses()
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
removeObservedAddr (addr: Multiaddr): void {
|
|
207
|
-
addr = stripPeerId(addr, this.components.peerId)
|
|
208
|
-
const addrString = addr.toString()
|
|
209
|
-
|
|
210
|
-
this.observed.delete(addrString)
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
getAddresses (): Multiaddr[] {
|
|
214
|
-
let multiaddrs = this.getAnnounceAddrs()
|
|
215
|
-
|
|
216
|
-
if (multiaddrs.length === 0) {
|
|
217
|
-
// no configured announce addrs, add configured listen addresses
|
|
218
|
-
multiaddrs = this.components.transportManager.getAddrs()
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
// add observed addresses we are confident in
|
|
222
|
-
multiaddrs = multiaddrs
|
|
223
|
-
.concat(
|
|
224
|
-
Array.from(this.observed)
|
|
225
|
-
.filter(([ma, metadata]) => metadata.confident)
|
|
226
|
-
.map(([ma]) => multiaddr(ma))
|
|
227
|
-
)
|
|
228
|
-
|
|
229
|
-
const mappedMultiaddrs: Multiaddr[] = []
|
|
230
|
-
|
|
231
|
-
// add ip->domain mappings
|
|
232
|
-
for (const ma of multiaddrs) {
|
|
233
|
-
const tuples = [...ma.stringTuples()]
|
|
234
|
-
let mappedIp = false
|
|
235
|
-
|
|
236
|
-
for (const [ip, domain] of this.ipDomainMappings.entries()) {
|
|
237
|
-
for (let i = 0; i < tuples.length; i++) {
|
|
238
|
-
if (tuples[i][1] !== ip) {
|
|
239
|
-
continue
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
if (tuples[i][0] === CODEC_IP4) {
|
|
243
|
-
tuples[i][0] = CODEC_DNS4
|
|
244
|
-
tuples[i][1] = domain
|
|
245
|
-
mappedIp = true
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
if (tuples[i][0] === CODEC_IP6) {
|
|
249
|
-
tuples[i][0] = CODEC_DNS6
|
|
250
|
-
tuples[i][1] = domain
|
|
251
|
-
mappedIp = true
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
if (mappedIp) {
|
|
257
|
-
mappedMultiaddrs.push(
|
|
258
|
-
multiaddr(`/${
|
|
259
|
-
tuples.map(tuple => {
|
|
260
|
-
return [
|
|
261
|
-
protocols(tuple[0]).name,
|
|
262
|
-
tuple[1]
|
|
263
|
-
].join('/')
|
|
264
|
-
}).join('/')
|
|
265
|
-
}`)
|
|
266
|
-
)
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
multiaddrs = multiaddrs.concat(mappedMultiaddrs)
|
|
271
|
-
|
|
272
|
-
// dedupe multiaddrs
|
|
273
|
-
const addrSet = new Set<string>()
|
|
274
|
-
multiaddrs = multiaddrs.filter(ma => {
|
|
275
|
-
const maStr = ma.toString()
|
|
276
|
-
|
|
277
|
-
if (addrSet.has(maStr)) {
|
|
278
|
-
return false
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
addrSet.add(maStr)
|
|
282
|
-
|
|
283
|
-
return true
|
|
284
|
-
})
|
|
285
|
-
|
|
286
|
-
// Create advertising list
|
|
287
|
-
return this.announceFilter(
|
|
288
|
-
Array.from(addrSet)
|
|
289
|
-
.map(str => {
|
|
290
|
-
const ma = multiaddr(str)
|
|
291
|
-
|
|
292
|
-
// do not append our peer id to a path multiaddr as it will become invalid
|
|
293
|
-
if (ma.protos().pop()?.path === true) {
|
|
294
|
-
return ma
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
if (ma.getPeerId() === this.components.peerId.toString()) {
|
|
298
|
-
return ma
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
return ma.encapsulate(`/p2p/${this.components.peerId.toString()}`)
|
|
302
|
-
})
|
|
303
|
-
)
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
addDNSMapping (domain: string, addresses: string[]): void {
|
|
307
|
-
addresses.forEach(ip => {
|
|
308
|
-
this.ipDomainMappings.set(ip, domain)
|
|
309
|
-
})
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
removeDNSMapping (domain: string): void {
|
|
313
|
-
for (const [key, value] of this.ipDomainMappings.entries()) {
|
|
314
|
-
if (value === domain) {
|
|
315
|
-
this.ipDomainMappings.delete(key)
|
|
316
|
-
}
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
}
|