@@ -7,12 +7,14 @@ const { optionDefaults } = require('../../options')
7
7
const chromeSocketsBundle = require ( './libp2p-bundle' )
8
8
const mergeOptions = require ( 'merge-options' )
9
9
const getPort = require ( 'get-port' )
10
+ const { getIPv4, getIPv6 } = require ( 'webrtc-ips' )
10
11
11
12
const multiaddr = require ( 'multiaddr' )
12
13
const maToUri = require ( 'multiaddr-to-uri' )
13
14
const multiaddr2httpUrl = ( ma ) => maToUri ( ma . includes ( '/http' ) ? ma : multiaddr ( ma ) . encapsulate ( '/http' ) )
14
15
15
- const chromeSocketsOpts = {
16
+ // additional default js-ipfs config specific to runtime with chrome.sockets APIs
17
+ const chromeDefaultOpts = {
16
18
config : {
17
19
Addresses : {
18
20
API : '/ip4/127.0.0.1/tcp/5003' ,
@@ -22,10 +24,10 @@ const chromeSocketsOpts = {
22
24
but Web UI needs API (can't use window.ipfs due to sandboxing)
23
25
*/
24
26
Swarm : [
25
- // TODO: listening on TCP (override IP and port at runtime in buildConfig()?)
26
- '/ip4/0.0.0.0/tcp/0' ,
27
- // optional ws-star signaling provides a backup non-LAN peer discovery
28
- '/dns4/ws-star1.par.dwebops.pub.com /tcp/443/wss/p2p-websocket-star'
27
+ // optional ws-star signaling provides a backup for non-LAN peer discovery
28
+ // (this will be removed when autorelay and DHT are stable in js-ipfs)
29
+ '/dns4/ ws-star1.par.dwebops.pub.com/tcp/443/wss/p2p-websocket-star' ,
30
+ '/dns4/ws-star.discovery.libp2p.io /tcp/443/wss/p2p-websocket-star'
29
31
] ,
30
32
// Delegated Content and Peer Routing: https://github.com/ipfs/js-ipfs/pull/2195
31
33
Delegates : // [] // TODO: enable delegates
@@ -93,7 +95,25 @@ const chromeSocketsOpts = {
93
95
async function buildConfig ( opts , log ) {
94
96
const defaultOpts = JSON . parse ( optionDefaults . ipfsNodeConfig )
95
97
const userOpts = JSON . parse ( opts . ipfsNodeConfig )
96
- const ipfsNodeConfig = mergeOptions ( defaultOpts , userOpts , chromeSocketsOpts , { start : false , libp2p : chromeSocketsBundle } )
98
+ const chromeOpts = JSON . parse ( JSON . stringify ( chromeDefaultOpts ) )
99
+
100
+ // find a free TCP port for incoming connections
101
+ const freeTcpPort = await getPort ( { port : getPort . makeRange ( 4042 , 4100 ) } )
102
+ // find out local network IPs
103
+ const ipv4 = await getIPv4 ( )
104
+ const ipv6 = await getIPv6 ( )
105
+ // add TCP multiaddrs
106
+ if ( ipv4 ) {
107
+ chromeOpts . config . Addresses . Swarm . unshift ( `/ip4/${ ipv4 } /tcp/${ freeTcpPort } ` )
108
+ }
109
+ if ( ipv6 ) {
110
+ chromeOpts . config . Addresses . Swarm . unshift ( `/ip6/${ ipv6 } /tcp/${ freeTcpPort } ` )
111
+ }
112
+ // append user-provided multiaddrs
113
+ chromeOpts . config . Addresses . Swarm = chromeOpts . config . Addresses . Swarm . concat ( userOpts . config . Addresses . Swarm )
114
+
115
+ // merge configs
116
+ const ipfsNodeConfig = mergeOptions ( defaultOpts , userOpts , chromeOpts , { start : false , libp2p : chromeSocketsBundle } )
97
117
98
118
// Detect when API or Gateway port is not available (taken by something else)
99
119
// We find the next free port and update configuration to use it instead
0 commit comments