Skip to content

Commit 8f7eda4

Browse files
authored
feat: TCP client in Brave (#754)
- TCP transport - Local DNS discovery - enabled ws-star signaling - enabled preload and delegated routing
2 parents 7ba096d + a62ac26 commit 8f7eda4

File tree

19 files changed

+1655
-860
lines changed

19 files changed

+1655
-860
lines changed

add-on/_locales/en/messages.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@
236236
"description": "An option title on the Preferences screen (option_ipfsNodeConfig_title)"
237237
},
238238
"option_ipfsNodeConfig_description": {
239-
"message": "Additional configuration for the embedded IPFS node (arrays will be merged). Must be valid JSON.",
239+
"message": "Additional configuration for the embedded JS IPFS node. Must be valid JSON.",
240240
"description": "An option description on the Preferences screen (option_ipfsNodeConfig_description)"
241241
},
242242
"option_ipfsNodeType_external": {

add-on/src/lib/dir-view.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const mainStyle = require('ipfs-http-response/src/dir-view/style')
66
function buildFilesList (path, links) {
77
const rows = links.map((link) => {
88
let row = [
9-
`<div class="ipfs-icon ipfs-_blank">&nbsp;</div>`,
9+
'<div class="ipfs-icon ipfs-_blank">&nbsp;</div>',
1010
`<a href="${path}${path.endsWith('/') ? '' : '/'}${link.name}">${link.name}</a>`,
1111
filesize(link.size)
1212
]

add-on/src/lib/ipfs-client/embedded-chromesockets.js

-153
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
'use strict'
2+
3+
const browser = require('webextension-polyfill')
4+
5+
const { optionDefaults } = require('../../options')
6+
const chromeSocketsBundle = require('./libp2p-bundle')
7+
const mergeOptions = require('merge-options')
8+
const getPort = require('get-port')
9+
const { getIPv4, getIPv6 } = require('webrtc-ips')
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+
// additional default js-ipfs config specific to runtime with chrome.sockets APIs
16+
const chromeDefaultOpts = {
17+
config: {
18+
Addresses: {
19+
API: '/ip4/127.0.0.1/tcp/5003',
20+
Gateway: '/ip4/127.0.0.1/tcp/9091',
21+
/* Sidenote on API & Gateway:
22+
Gateway can run without API port,
23+
but Web UI needs API (can't use window.ipfs due to sandboxing)
24+
*/
25+
Swarm: [
26+
// optional ws-star signaling provides a backup for non-LAN peer discovery
27+
// (this will be removed when autorelay and DHT are stable in js-ipfs)
28+
'/dns4/ws-star1.par.dwebops.pub.com/tcp/443/wss/p2p-websocket-star',
29+
'/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star'
30+
],
31+
// Delegated Content and Peer Routing: https://github.com/ipfs/js-ipfs/pull/2195
32+
Delegates: // [] // TODO: enable delegates
33+
[
34+
'/dns4/node1.delegate.ipfs.io/tcp/443/https',
35+
'/dns4/node0.delegate.ipfs.io/tcp/443/https'
36+
]
37+
},
38+
Discovery: {
39+
MDNS: {
40+
Enabled: true,
41+
Interval: 10
42+
}
43+
},
44+
Swarm: {
45+
ConnMgr: {
46+
LowWater: 100,
47+
HighWater: 250
48+
}
49+
},
50+
Bootstrap: [
51+
// Prioritize TCP Bootstrappers from https://github.com/ipfs/js-ipfs/blob/v0.37.1/src/core/runtime/config-nodejs.js#L22
52+
'/ip4/104.236.176.52/tcp/4001/ipfs/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z',
53+
'/ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ',
54+
'/ip4/104.236.179.241/tcp/4001/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM',
55+
'/ip4/162.243.248.213/tcp/4001/ipfs/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm',
56+
'/ip4/128.199.219.111/tcp/4001/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu',
57+
'/ip4/104.236.76.40/tcp/4001/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64',
58+
'/ip4/178.62.158.247/tcp/4001/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd',
59+
'/ip4/178.62.61.185/tcp/4001/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3',
60+
'/ip4/104.236.151.122/tcp/4001/ipfs/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx',
61+
'/ip6/2604:a880:1:20::1f9:9001/tcp/4001/ipfs/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z',
62+
'/ip6/2604:a880:1:20::203:d001/tcp/4001/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM',
63+
'/ip6/2604:a880:0:1010::23:d001/tcp/4001/ipfs/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm',
64+
'/ip6/2400:6180:0:d0::151:6001/tcp/4001/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu',
65+
'/ip6/2604:a880:800:10::4a:5001/tcp/4001/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64',
66+
'/ip6/2a03:b0c0:0:1010::23:1001/tcp/4001/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd',
67+
'/ip6/2a03:b0c0:1:d0::e7:1/tcp/4001/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3',
68+
'/ip6/2604:a880:1:20::1d9:6001/tcp/4001/ipfs/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx',
69+
// Twist: connect to preload nodes, but over tcp :)
70+
'/dns4/node0.preload.ipfs.io/tcp/4001/ipfs/QmZMxNdpMkewiVZLMRxaNxUeZpDUb34pWjZ1kZvsd16Zic',
71+
'/dns4/node1.preload.ipfs.io/tcp/4001/ipfs/Qmbut9Ywz9YEDrz8ySBSgWyJk41Uvm2QJPhwDJzJyGFsD6',
72+
// WebSockets versions from https://github.com/ipfs/js-ipfs/blob/v0.37.1/src/core/runtime/config-browser.js#L20
73+
'/dns4/ams-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd',
74+
'/dns4/lon-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3',
75+
'/dns4/sfo-3.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM',
76+
'/dns4/sgp-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu',
77+
'/dns4/nyc-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm',
78+
'/dns4/nyc-2.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64',
79+
'/dns4/node0.preload.ipfs.io/tcp/443/wss/ipfs/QmZMxNdpMkewiVZLMRxaNxUeZpDUb34pWjZ1kZvsd16Zic',
80+
'/dns4/node1.preload.ipfs.io/tcp/443/wss/ipfs/Qmbut9Ywz9YEDrz8ySBSgWyJk41Uvm2QJPhwDJzJyGFsD6'
81+
]
82+
},
83+
// preload: { enabled: false, addresses: [] }
84+
/* TODO: when we have p2p transport, are preloads still needed? should Brave have own nodes? */
85+
preload: {
86+
enabled: true,
87+
addresses: [
88+
'/dns4/node1.preload.ipfs.io/tcp/443/https',
89+
'/dns4/node0.preload.ipfs.io/tcp/443/https'
90+
]
91+
}
92+
}
93+
94+
async function buildConfig (opts, log) {
95+
const defaultOpts = JSON.parse(optionDefaults.ipfsNodeConfig)
96+
const userOpts = JSON.parse(opts.ipfsNodeConfig)
97+
const chromeOpts = JSON.parse(JSON.stringify(chromeDefaultOpts))
98+
99+
// find a free TCP port for incoming connections
100+
const freeTcpPort = await getPort({ port: getPort.makeRange(4042, 4100) })
101+
// find out local network IPs
102+
const ipv4 = await getIPv4()
103+
const ipv6 = await getIPv6()
104+
// add TCP multiaddrs
105+
if (ipv4) {
106+
chromeOpts.config.Addresses.Swarm.unshift(`/ip4/${ipv4}/tcp/${freeTcpPort}`)
107+
}
108+
if (ipv6) {
109+
chromeOpts.config.Addresses.Swarm.unshift(`/ip6/${ipv6}/tcp/${freeTcpPort}`)
110+
}
111+
// append user-provided multiaddrs
112+
chromeOpts.config.Addresses.Swarm = chromeOpts.config.Addresses.Swarm.concat(userOpts.config.Addresses.Swarm)
113+
114+
// merge configs
115+
const finalOpts = {
116+
start: false,
117+
libp2p: chromeSocketsBundle
118+
}
119+
const ipfsNodeConfig = mergeOptions(defaultOpts, userOpts, chromeOpts, finalOpts)
120+
121+
// Detect when API or Gateway port is not available (taken by something else)
122+
// We find the next free port and update configuration to use it instead
123+
const multiaddr2port = (ma) => parseInt(new URL(multiaddr2httpUrl(ma)).port, 10)
124+
const gatewayPort = multiaddr2port(ipfsNodeConfig.config.Addresses.Gateway)
125+
const apiPort = multiaddr2port(ipfsNodeConfig.config.Addresses.API)
126+
log(`checking if ports are available: api: ${apiPort}, gateway: ${gatewayPort}`)
127+
const freeGatewayPort = await getPort({ port: getPort.makeRange(gatewayPort, gatewayPort + 100) })
128+
const freeApiPort = await getPort({ port: getPort.makeRange(apiPort, apiPort + 100) })
129+
if (gatewayPort !== freeGatewayPort || apiPort !== freeApiPort) {
130+
log(`updating config to available ports: api: ${freeApiPort}, gateway: ${freeGatewayPort}`)
131+
const addrs = ipfsNodeConfig.config.Addresses
132+
addrs.Gateway = addrs.Gateway.replace(gatewayPort.toString(), freeGatewayPort.toString())
133+
addrs.API = addrs.API.replace(apiPort.toString(), freeApiPort.toString())
134+
}
135+
136+
return ipfsNodeConfig
137+
}
138+
139+
async function syncConfig (ipfs, opts, log) {
140+
const storedConfig = await browser.storage.local.get('ipfsNodeConfig')
141+
if (storedConfig && storedConfig.ipfsNodeConfig) {
142+
const maGw = await ipfs.config.get('Addresses.Gateway')
143+
const maApi = await ipfs.config.get('Addresses.API')
144+
const httpGw = multiaddr2httpUrl(maGw)
145+
const httpApi = multiaddr2httpUrl(maApi)
146+
// update config in browser.storage to ports from js-ipfs instance
147+
const changes = {
148+
customGatewayUrl: httpGw,
149+
ipfsApiUrl: httpApi
150+
}
151+
// update ipfsNodeConfig if ports changed (eg. due to old port being busy)
152+
const cfg = JSON.parse(storedConfig.ipfsNodeConfig)
153+
if (maGw !== cfg.config.Addresses.Gateway ||
154+
maApi !== cfg.config.Addresses.API) {
155+
cfg.config.Addresses.Gateway = maGw
156+
cfg.config.Addresses.API = maApi
157+
changes.ipfsNodeConfig = JSON.stringify(cfg, null, 2)
158+
}
159+
// update runtime config in place
160+
Object.assign(opts, changes)
161+
// save config to browser.storage (triggers async client restart if ports changed)
162+
log(`synchronizing ipfsNodeConfig with customGatewayUrl (${changes.customGatewayUrl}) and ipfsApiUrl (${changes.ipfsApiUrl})`)
163+
await browser.storage.local.set(changes)
164+
}
165+
}
166+
167+
module.exports = { buildConfig, syncConfig }

0 commit comments

Comments
 (0)