helia-coord 1.2.20 → 1.2.22
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.
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
import ThisNodeEntity from '../entities/this-node-entity.js'
|
|
7
7
|
import Schema from './schema.js'
|
|
8
8
|
import Util from '../util/utils.js'
|
|
9
|
+
import { publicIpv4 } from 'public-ip'
|
|
9
10
|
|
|
10
11
|
class ThisNodeUseCases {
|
|
11
12
|
constructor (localConfig = {}) {
|
|
@@ -19,9 +20,11 @@ class ThisNodeUseCases {
|
|
|
19
20
|
|
|
20
21
|
// Encapsulate dependencies
|
|
21
22
|
this.utils = new Util()
|
|
23
|
+
this.publicIp = publicIpv4
|
|
22
24
|
|
|
23
25
|
// Optional JSON-LD used for announcements. If present, will override
|
|
24
26
|
// default announcement object in Schema library.
|
|
27
|
+
this.tcpPort = localConfig.tcpPort
|
|
25
28
|
this.announceJsonLd = localConfig.announceJsonLd
|
|
26
29
|
|
|
27
30
|
// If consuming app wants to configure itself as a Circuit Relay, it can
|
|
@@ -70,6 +73,15 @@ class ThisNodeUseCases {
|
|
|
70
73
|
selfData.ipfsId = this.adapters.ipfs.ipfsPeerId
|
|
71
74
|
selfData.ipfsMultiaddrs = this.adapters.ipfs.ipfsMultiaddrs
|
|
72
75
|
|
|
76
|
+
// Try to auto-detect the public multiaddr and add it.
|
|
77
|
+
if (this.tcpPort) {
|
|
78
|
+
const ip4 = await this.publicIp()
|
|
79
|
+
console.log(`helia-coord using this IP address: ${ip4}, and this TCP port: ${this.tcpPort}`)
|
|
80
|
+
const detectedMultiaddr = `/ip4/${ip4}/tcp/${this.tcpPort}/p2p/${selfData.ipfsId}`
|
|
81
|
+
// detectedMultiaddr = this.multiaddr(detectedMultiaddr)
|
|
82
|
+
selfData.ipfsMultiaddrs.push(detectedMultiaddr)
|
|
83
|
+
}
|
|
84
|
+
|
|
73
85
|
// Aggregate data from the BCH adapter.
|
|
74
86
|
const bchData = await this.adapters.bch.generateBchId()
|
|
75
87
|
selfData.bchAddr = bchData.cashAddress
|
|
@@ -256,7 +268,7 @@ class ThisNodeUseCases {
|
|
|
256
268
|
// Get connected peers
|
|
257
269
|
const connectedPeers = await this.adapters.ipfs.getPeers()
|
|
258
270
|
this.adapters.log.statusLog(
|
|
259
|
-
|
|
271
|
+
0,
|
|
260
272
|
'refreshPeerConnections() connectedPeers: ',
|
|
261
273
|
connectedPeers
|
|
262
274
|
)
|
|
@@ -279,7 +291,7 @@ class ThisNodeUseCases {
|
|
|
279
291
|
// We do not need to do anything.
|
|
280
292
|
if (connectedPeer.length) {
|
|
281
293
|
this.adapters.log.statusLog(
|
|
282
|
-
|
|
294
|
+
0,
|
|
283
295
|
`Skipping peer in refreshPeerConnections(). Already connected to peer ${thisPeer}`
|
|
284
296
|
)
|
|
285
297
|
continue
|
|
@@ -293,7 +305,7 @@ class ThisNodeUseCases {
|
|
|
293
305
|
// If broadcastedAt value is older than 10 minutes, skip connecting
|
|
294
306
|
// to the peer. It may be stale information.
|
|
295
307
|
if (!this.isFreshPeer(peerData)) {
|
|
296
|
-
this.adapters.log.statusLog(
|
|
308
|
+
this.adapters.log.statusLog(0, `Peer ${peerData.from} is stale. Skipping.`)
|
|
297
309
|
continue
|
|
298
310
|
}
|
|
299
311
|
|
|
@@ -331,10 +343,13 @@ class ThisNodeUseCases {
|
|
|
331
343
|
// Try a direct connection with the peer by going through
|
|
332
344
|
// the multiaddrs in the announcement object.
|
|
333
345
|
const filteredMultiaddrs = this.utils.filterMultiaddrs(peerData.data.ipfsMultiaddrs)
|
|
334
|
-
|
|
346
|
+
console.log('filteredMultiaddrs: ', filteredMultiaddrs)
|
|
335
347
|
|
|
336
348
|
for (let j = 0; j < filteredMultiaddrs.length; j++) {
|
|
337
349
|
const multiaddr = filteredMultiaddrs[j]
|
|
350
|
+
this.adapters.log.statusLog(0,
|
|
351
|
+
`Trying a direct connecto to peer ${thisPeer} with this multiaddr: ${multiaddr}.`
|
|
352
|
+
)
|
|
338
353
|
|
|
339
354
|
// Attempt to connect to the node through a circuit relay.
|
|
340
355
|
connected = await this.adapters.ipfs.connectToPeer({ multiaddr })
|
|
@@ -356,7 +371,7 @@ class ThisNodeUseCases {
|
|
|
356
371
|
// Break out of the loop once we've made a successful connection.
|
|
357
372
|
break
|
|
358
373
|
} else {
|
|
359
|
-
this.adapters.log.statusLog(
|
|
374
|
+
this.adapters.log.statusLog(0,
|
|
360
375
|
`Failed to connect to peer ${thisPeer} through direct connection: ${multiaddr}. Reason: ${connected.details}`
|
|
361
376
|
)
|
|
362
377
|
}
|
package/lib/util/utils.js
CHANGED
|
@@ -11,6 +11,7 @@ class Util {
|
|
|
11
11
|
// This function takes an array of multiaddrs as input, and returns an array
|
|
12
12
|
// filtered to remove any local network IP addresses.
|
|
13
13
|
filterMultiaddrs (multiaddrs) {
|
|
14
|
+
console.log('filterMultiaddrs() input: ', multiaddrs)
|
|
14
15
|
try {
|
|
15
16
|
const filteredMultiaddrs = multiaddrs.filter((x) => {
|
|
16
17
|
if (x.includes('127.0.0.1')) return false
|
|
@@ -18,7 +19,7 @@ class Util {
|
|
|
18
19
|
if (x.includes('quic')) return false
|
|
19
20
|
if (x.includes('p2p-circuit')) return false
|
|
20
21
|
if (x.includes('192.168.')) return false
|
|
21
|
-
if (x.includes('172.
|
|
22
|
+
if (x.includes('172.1')) return false
|
|
22
23
|
if (x.includes('/10.')) return false
|
|
23
24
|
return true
|
|
24
25
|
})
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "helia-coord",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.22",
|
|
4
4
|
"description": "A JS library for helping IPFS peers coordinate, find a common interest, and stay connected around that interest.",
|
|
5
5
|
"main": "./index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -34,6 +34,7 @@
|
|
|
34
34
|
"@chris.troutner/retry-queue": "1.0.8",
|
|
35
35
|
"@multiformats/multiaddr": "12.1.8",
|
|
36
36
|
"bch-encrypt-lib": "2.1.1",
|
|
37
|
+
"public-ip": "6.0.1",
|
|
37
38
|
"uuid": "9.0.0"
|
|
38
39
|
},
|
|
39
40
|
"peerDependencies": {
|