|
| 1 | +'use strict' |
| 2 | +/* eslint-env browser, webextensions */ |
| 3 | + |
| 4 | +const browser = require('webextension-polyfill') |
| 5 | + |
| 6 | +const { optionDefaults } = require('../../options') |
| 7 | +const chromeSocketsBundle = require('./libp2p-bundle') |
| 8 | +const mergeOptions = require('merge-options') |
| 9 | +const getPort = require('get-port') |
| 10 | + |
| 11 | +const multiaddr = require('multiaddr') |
| 12 | +const maToUri = require('multiaddr-to-uri') |
| 13 | +const multiaddr2httpUrl = (ma) => maToUri(ma.includes('/http') ? ma : multiaddr(ma).encapsulate('/http')) |
| 14 | + |
| 15 | +const chromeSocketsOpts = { |
| 16 | + config: { |
| 17 | + Addresses: { |
| 18 | + API: '/ip4/127.0.0.1/tcp/5003', |
| 19 | + Gateway: '/ip4/127.0.0.1/tcp/9091', |
| 20 | + /* Sidenote on API & Gateway: |
| 21 | + Gateway can run without API port, |
| 22 | + but Web UI needs API (can't use window.ipfs due to sandboxing) |
| 23 | + */ |
| 24 | + Swarm: [ |
| 25 | + // TODO: listening on TCP (override IP and port at runtime in buildConfig()?) |
| 26 | + '/ip4/0.0.0.0/tcp/0' |
| 27 | + // TODO: disabled -star signaling: it makes a backup non-LAN peer discovery, but break the boot if server is down |
| 28 | + // '/dns4/ws-star1.par.dwebops.pub.com/tcp/443/wss/p2p-websocket-star' |
| 29 | + ], |
| 30 | + // Delegated Content and Peer Routing: https://github.com/ipfs/js-ipfs/pull/2195 |
| 31 | + Delegates: // [] // TODO: enable delegates |
| 32 | + [ |
| 33 | + '/dns4/node1.delegate.ipfs.io/tcp/443/https', |
| 34 | + '/dns4/node0.delegate.ipfs.io/tcp/443/https' |
| 35 | + ] |
| 36 | + }, |
| 37 | + Discovery: { |
| 38 | + MDNS: { |
| 39 | + Enabled: true, |
| 40 | + Interval: 10 |
| 41 | + } |
| 42 | + }, |
| 43 | + Swarm: { |
| 44 | + ConnMgr: { |
| 45 | + LowWater: 100, |
| 46 | + HighWater: 250 |
| 47 | + } |
| 48 | + }, |
| 49 | + Bootstrap: [ |
| 50 | + // Prioritize TCP Bootstrappers from https://github.com/ipfs/js-ipfs/blob/v0.37.1/src/core/runtime/config-nodejs.js#L22 |
| 51 | + '/ip4/104.236.176.52/tcp/4001/ipfs/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z', |
| 52 | + '/ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ', |
| 53 | + '/ip4/104.236.179.241/tcp/4001/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM', |
| 54 | + '/ip4/162.243.248.213/tcp/4001/ipfs/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm', |
| 55 | + '/ip4/128.199.219.111/tcp/4001/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu', |
| 56 | + '/ip4/104.236.76.40/tcp/4001/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64', |
| 57 | + '/ip4/178.62.158.247/tcp/4001/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd', |
| 58 | + '/ip4/178.62.61.185/tcp/4001/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3', |
| 59 | + '/ip4/104.236.151.122/tcp/4001/ipfs/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx', |
| 60 | + '/ip6/2604:a880:1:20::1f9:9001/tcp/4001/ipfs/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z', |
| 61 | + '/ip6/2604:a880:1:20::203:d001/tcp/4001/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM', |
| 62 | + '/ip6/2604:a880:0:1010::23:d001/tcp/4001/ipfs/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm', |
| 63 | + '/ip6/2400:6180:0:d0::151:6001/tcp/4001/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu', |
| 64 | + '/ip6/2604:a880:800:10::4a:5001/tcp/4001/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64', |
| 65 | + '/ip6/2a03:b0c0:0:1010::23:1001/tcp/4001/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd', |
| 66 | + '/ip6/2a03:b0c0:1:d0::e7:1/tcp/4001/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3', |
| 67 | + '/ip6/2604:a880:1:20::1d9:6001/tcp/4001/ipfs/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx', |
| 68 | + // Twist: connect to preload nodes, but over tcp :) |
| 69 | + '/dns4/node0.preload.ipfs.io/tcp/4001/ipfs/QmZMxNdpMkewiVZLMRxaNxUeZpDUb34pWjZ1kZvsd16Zic', |
| 70 | + '/dns4/node1.preload.ipfs.io/tcp/4001/ipfs/Qmbut9Ywz9YEDrz8ySBSgWyJk41Uvm2QJPhwDJzJyGFsD6', |
| 71 | + // WebSockets versions from https://github.com/ipfs/js-ipfs/blob/v0.37.1/src/core/runtime/config-browser.js#L20 |
| 72 | + '/dns4/ams-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd', |
| 73 | + '/dns4/lon-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3', |
| 74 | + '/dns4/sfo-3.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM', |
| 75 | + '/dns4/sgp-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu', |
| 76 | + '/dns4/nyc-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm', |
| 77 | + '/dns4/nyc-2.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64', |
| 78 | + '/dns4/node0.preload.ipfs.io/tcp/443/wss/ipfs/QmZMxNdpMkewiVZLMRxaNxUeZpDUb34pWjZ1kZvsd16Zic', |
| 79 | + '/dns4/node1.preload.ipfs.io/tcp/443/wss/ipfs/Qmbut9Ywz9YEDrz8ySBSgWyJk41Uvm2QJPhwDJzJyGFsD6' |
| 80 | + ] |
| 81 | + }, |
| 82 | + // preload: { enabled: false, addresses: [] } |
| 83 | + /* TODO: when we have p2p transport, are preloads still needed? should Brave have own nodes? */ |
| 84 | + preload: { |
| 85 | + enabled: true, |
| 86 | + addresses: [ |
| 87 | + '/dns4/node1.preload.ipfs.io/tcp/443/https', |
| 88 | + '/dns4/node0.preload.ipfs.io/tcp/443/https' |
| 89 | + ] |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +async function buildConfig (opts, log) { |
| 94 | + const defaultOpts = JSON.parse(optionDefaults.ipfsNodeConfig) |
| 95 | + const userOpts = JSON.parse(opts.ipfsNodeConfig) |
| 96 | + const ipfsNodeConfig = mergeOptions(defaultOpts, userOpts, chromeSocketsOpts, { start: false, libp2p: chromeSocketsBundle }) |
| 97 | + |
| 98 | + // Detect when API or Gateway port is not available (taken by something else) |
| 99 | + // We find the next free port and update configuration to use it instead |
| 100 | + const multiaddr2port = (ma) => parseInt(new URL(multiaddr2httpUrl(ma)).port, 10) |
| 101 | + const gatewayPort = multiaddr2port(ipfsNodeConfig.config.Addresses.Gateway) |
| 102 | + const apiPort = multiaddr2port(ipfsNodeConfig.config.Addresses.API) |
| 103 | + log(`checking if ports are available: api: ${apiPort}, gateway: ${gatewayPort}`) |
| 104 | + const freeGatewayPort = await getPort({ port: getPort.makeRange(gatewayPort, gatewayPort + 100) }) |
| 105 | + const freeApiPort = await getPort({ port: getPort.makeRange(apiPort, apiPort + 100) }) |
| 106 | + if (gatewayPort !== freeGatewayPort || apiPort !== freeApiPort) { |
| 107 | + log(`updating config to available ports: api: ${freeApiPort}, gateway: ${freeGatewayPort}`) |
| 108 | + const addrs = ipfsNodeConfig.config.Addresses |
| 109 | + addrs.Gateway = addrs.Gateway.replace(gatewayPort.toString(), freeGatewayPort.toString()) |
| 110 | + addrs.API = addrs.API.replace(apiPort.toString(), freeApiPort.toString()) |
| 111 | + } |
| 112 | + |
| 113 | + return ipfsNodeConfig |
| 114 | +} |
| 115 | + |
| 116 | +async function syncConfig (ipfs, opts, log) { |
| 117 | + const localConfig = await browser.storage.local.get('ipfsNodeConfig') |
| 118 | + if (localConfig && localConfig.ipfsNodeConfig) { |
| 119 | + const gwMa = await ipfs.config.get('Addresses.Gateway') |
| 120 | + const apiMa = await ipfs.config.get('Addresses.API') |
| 121 | + const httpGateway = multiaddr2httpUrl(gwMa) |
| 122 | + const httpApi = multiaddr2httpUrl(apiMa) |
| 123 | + // update config in browser.storage to ports from js-ipfs instance |
| 124 | + const ipfsNodeConfig = JSON.parse(localConfig.ipfsNodeConfig) |
| 125 | + ipfsNodeConfig.config.Addresses.Gateway = gwMa |
| 126 | + ipfsNodeConfig.config.Addresses.API = apiMa |
| 127 | + const configChanges = { |
| 128 | + customGatewayUrl: httpGateway, |
| 129 | + ipfsApiUrl: httpApi, |
| 130 | + ipfsNodeConfig: JSON.stringify(ipfsNodeConfig, null, 2) |
| 131 | + } |
| 132 | + // update runtime config in place |
| 133 | + Object.assign(opts, configChanges) |
| 134 | + // update config in browser.storage (triggers async client restart if ports changed) |
| 135 | + log(`synchronizing ipfsNodeConfig with customGatewayUrl (${configChanges.customGatewayUrl}) and ipfsApiUrl (${configChanges.ipfsApiUrl})`) |
| 136 | + await browser.storage.local.set(configChanges) |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +module.exports = { buildConfig, syncConfig } |
0 commit comments