helia-coord 1.5.0 → 1.5.2

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.
@@ -375,84 +375,30 @@ class PeerUseCases {
375
375
  continue
376
376
  }
377
377
 
378
- // Try a direct connection with the peer by going through
379
- // the multiaddrs in the announcement object.
380
- const filteredMultiaddrs = this.utils.filterMultiaddrs(peerData.data.ipfsMultiaddrs)
381
- this.adapters.log.statusLog(1, 'filteredMultiaddrs: ', filteredMultiaddrs)
382
-
383
- for (let j = 0; j < filteredMultiaddrs.length; j++) {
384
- const multiaddr = filteredMultiaddrs[j]
385
- this.adapters.log.statusLog(1,
386
- `Trying a direct connecto to peer ${thisPeer} with this multiaddr: ${multiaddr}.`
387
- )
388
-
389
- // Attempt to connect to the node through a circuit relay.
390
- connected = await this.adapters.ipfs.connectToPeer({ multiaddr })
391
- // console.log('direct connection connected: ', connected)
392
-
393
- // If the connection was successful, break out of the relay loop.
394
- // Otherwise try to connect through the next relay.
395
- if (connected.success) {
378
+ // Skip direct connection if the node has a preference for a circuit
379
+ // relay connection.
380
+ const connectPref = peerData.data.ipfsConnectPref
381
+ if (connectPref !== 'cr') {
382
+ // Try a direct connection with the peer by going through
383
+ // the multiaddrs in the announcement object.
384
+ const filteredMultiaddrs = this.utils.filterMultiaddrs(peerData.data.ipfsMultiaddrs)
385
+ this.adapters.log.statusLog(1, 'filteredMultiaddrs: ', filteredMultiaddrs)
386
+
387
+ for (let j = 0; j < filteredMultiaddrs.length; j++) {
388
+ const multiaddr = filteredMultiaddrs[j]
396
389
  this.adapters.log.statusLog(1,
397
- `Successfully connected to peer ${thisPeer} through direct connection: ${multiaddr}.`
390
+ `Trying a direct connecto to peer ${thisPeer} with this multiaddr: ${multiaddr}.`
398
391
  )
399
392
 
400
- // Add the connection multiaddr for this peer to the thisNode object.
401
- this.updatePeerConnectionInfo({ thisPeer })
402
-
403
- // Add the connection multiaddr to the peer, so that we can see
404
- // exactly how we're connected to the peer.
405
- // const thisPeerData = this.thisNode.peerData.filter(x => x.from === thisPeer)
406
- // thisPeerData[0].data.connectionAddr = multiaddr
407
- // peerData.data.connectionAddr = multiaddr
408
-
409
- // Break out of the loop once we've made a successful connection.
410
- break
411
- } else {
412
- this.adapters.log.statusLog(1,
413
- `Failed to connect to peer ${thisPeer} through direct connection: ${multiaddr}. Reason: ${connected.details}`
414
- )
415
- }
416
- }
417
-
418
- if (connected.success) {
419
- continue
420
- }
421
-
422
- // Sort the Circuit Relays by the average of the aboutLatency
423
- // array. Connect to peers through the Relays with the lowest latencies
424
- // first.
425
- const sortedRelays = this.relayUseCases.sortRelays(relays)
426
- // console.log(`sortedRelays: ${JSON.stringify(sortedRelays, null, 2)}`)
427
-
428
- // Loop through each known circuit relay and attempt to connect to the
429
- // peer through a relay.
430
- for (let j = 0; j < sortedRelays.length; j++) {
431
- const thisRelay = sortedRelays[j]
432
- // console.log(`thisRelay: ${JSON.stringify(thisRelay, null, 2)}`)
433
-
434
- // Generate a multiaddr for connecting to the peer through a circuit relay.
435
- // This is for a tcp connection.
436
- // const multiaddr = `${thisRelay.multiaddr}/p2p-circuit/p2p/${thisPeer}`
437
- // console.log(`multiaddr: ${multiaddr}`)
438
-
439
- // Use a WebRTC circuit relay connection, since this is the focus for
440
- // the js-libp2p project and allows establishing of p2p connections.
441
- const multiaddr = `${thisRelay.multiaddr}/p2p-circuit/webrtc/p2p/${thisPeer}`
442
-
443
- // Skip the relay if this node is not connected to it.
444
- if (thisRelay.connected) {
445
- this.adapters.log.statusLog(1, `refreshPeerConnections() connecting to peer with this multiaddr: ${multiaddr}`)
446
-
447
393
  // Attempt to connect to the node through a circuit relay.
448
394
  connected = await this.adapters.ipfs.connectToPeer({ multiaddr })
449
- // console.log('v2 relay connected: ', connected)
395
+ // console.log('direct connection connected: ', connected)
450
396
 
451
397
  // If the connection was successful, break out of the relay loop.
452
398
  // Otherwise try to connect through the next relay.
453
399
  if (connected.success) {
454
400
  this.adapters.log.statusLog(1,
455
- `Successfully connected to peer ${thisPeer} through v2 circuit relay ${thisRelay.multiaddr}.`
401
+ `Successfully connected to peer ${thisPeer} through direct connection: ${multiaddr}.`
456
402
  )
457
403
 
458
404
  // Add the connection multiaddr for this peer to the thisNode object.
@@ -461,13 +407,14 @@ class PeerUseCases {
461
407
  // Add the connection multiaddr to the peer, so that we can see
462
408
  // exactly how we're connected to the peer.
463
409
  // const thisPeerData = this.thisNode.peerData.filter(x => x.from === thisPeer)
410
+ // thisPeerData[0].data.connectionAddr = multiaddr
464
411
  // peerData.data.connectionAddr = multiaddr
465
412
 
466
413
  // Break out of the loop once we've made a successful connection.
467
414
  break
468
415
  } else {
469
416
  this.adapters.log.statusLog(1,
470
- `Failed to connect to peer ${thisPeer} through v2 circuit relay: ${multiaddr}. Reason: ${connected.details}`
417
+ `Failed to connect to peer ${thisPeer} through direct connection: ${multiaddr}. Reason: ${connected.details}`
471
418
  )
472
419
  }
473
420
  }
@@ -476,6 +423,67 @@ class PeerUseCases {
476
423
  if (connected.success) {
477
424
  continue
478
425
  }
426
+
427
+ // Skip this section if the peer has a preference for direct connection.
428
+ if (connectPref !== 'direct') {
429
+ // Sort the Circuit Relays by the average of the aboutLatency
430
+ // array. Connect to peers through the Relays with the lowest latencies
431
+ // first.
432
+ const sortedRelays = this.relayUseCases.sortRelays(relays)
433
+ // console.log(`sortedRelays: ${JSON.stringify(sortedRelays, null, 2)}`)
434
+
435
+ // Loop through each known circuit relay and attempt to connect to the
436
+ // peer through a relay.
437
+ for (let j = 0; j < sortedRelays.length; j++) {
438
+ const thisRelay = sortedRelays[j]
439
+ // console.log(`thisRelay: ${JSON.stringify(thisRelay, null, 2)}`)
440
+
441
+ // Generate a multiaddr for connecting to the peer through a circuit relay.
442
+ // This is for a tcp connection.
443
+ // const multiaddr = `${thisRelay.multiaddr}/p2p-circuit/p2p/${thisPeer}`
444
+ // console.log(`multiaddr: ${multiaddr}`)
445
+
446
+ // Use a WebRTC circuit relay connection, since this is the focus for
447
+ // the js-libp2p project and allows establishing of p2p connections.
448
+ const multiaddr = `${thisRelay.multiaddr}/p2p-circuit/webrtc/p2p/${thisPeer}`
449
+
450
+ // Skip the relay if this node is not connected to it.
451
+ if (thisRelay.connected) {
452
+ this.adapters.log.statusLog(1, `refreshPeerConnections() connecting to peer with this multiaddr: ${multiaddr}`)
453
+
454
+ // Attempt to connect to the node through a circuit relay.
455
+ connected = await this.adapters.ipfs.connectToPeer({ multiaddr })
456
+ // console.log('v2 relay connected: ', connected)
457
+
458
+ // If the connection was successful, break out of the relay loop.
459
+ // Otherwise try to connect through the next relay.
460
+ if (connected.success) {
461
+ this.adapters.log.statusLog(1,
462
+ `Successfully connected to peer ${thisPeer} through v2 circuit relay ${thisRelay.multiaddr}.`
463
+ )
464
+
465
+ // Add the connection multiaddr for this peer to the thisNode object.
466
+ this.updatePeerConnectionInfo({ thisPeer })
467
+
468
+ // Add the connection multiaddr to the peer, so that we can see
469
+ // exactly how we're connected to the peer.
470
+ // const thisPeerData = this.thisNode.peerData.filter(x => x.from === thisPeer)
471
+ // peerData.data.connectionAddr = multiaddr
472
+
473
+ // Break out of the loop once we've made a successful connection.
474
+ break
475
+ } else {
476
+ this.adapters.log.statusLog(1,
477
+ `Failed to connect to peer ${thisPeer} through v2 circuit relay: ${multiaddr}. Reason: ${connected.details}`
478
+ )
479
+ }
480
+ }
481
+ }
482
+ }
483
+
484
+ if (connected.success) {
485
+ continue
486
+ }
479
487
  }
480
488
 
481
489
  const now = new Date()
@@ -14,6 +14,11 @@ class Schema {
14
14
  ipfsMultiaddrs: schemaConfig.ipfsMultiaddrs
15
15
  ? schemaConfig.ipfsMultiaddrs
16
16
  : [],
17
+
18
+ // Connection preference. Default is Circuit Relay. Alternative option
19
+ // is 'direct', which prefers a direct ip4 or ip6 connection.
20
+ ipfsConnectPref: process.env.CONNECT_PREF ? process.env.CONNECT_PREF : 'cr',
21
+
17
22
  isCircuitRelay: schemaConfig.isCircuitRelay
18
23
  ? schemaConfig.isCircuitRelay
19
24
  : false,
@@ -74,6 +79,7 @@ class Schema {
74
79
  type: this.state.type,
75
80
  ipfsMultiaddrs: this.state.ipfsMultiaddrs,
76
81
  orbitdb: this.state.orbitdbId,
82
+ ipfsConnectPref: this.state.ipfsConnectPref,
77
83
 
78
84
  // The circuit relays preferred by this node.
79
85
  circuitRelays: [],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "helia-coord",
3
- "version": "1.5.0",
3
+ "version": "1.5.2",
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",