Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

fix: libp2p now requires encryption module #3085

Merged
merged 4 commits into from
Jun 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions examples/circuit-relaying/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,21 @@ A circuit relay address is a [multiaddress](https://multiformats.io/multiaddr/)

Circuit relay addresses are very flexible and can describe many different aspects of how to esablish the relayed connection. In its simplest form, it looks something like this:

- `/p2p-circuit/ipfs/QmPeer`
- `/p2p-circuit/p2p/QmPeer`

If we want to be specific as to which transport we want to use to establish the relay, we can encode that in the address as well:

- `/ip4/127.0.0.1/tcp/65000/ipfs/QmRelay/p2p-circuit/ipfs/QmPeer`
- `/ip4/127.0.0.1/tcp/65000/p2p/QmRelay/p2p-circuit/ipfs/QmPeer`

This tells us that we want to use `QmRelay` located at address 127.0.0.1 and port 65000.

- `/ip4/127.0.0.1/tcp/65000/ipfs/QmRelay/p2p-circuit/ip4/127.0.0.1/tcp/8080/ws/ipfs/QmPeer`
- `/ip4/127.0.0.1/tcp/65000/p2p/QmRelay/p2p-circuit/ip4/127.0.0.1/tcp/8080/ws/ipfs/QmPeer`

We can take it a step further and encode the same information for the destination peer. In this case, we have it located at 127.0.0.1 on port 8080 and using a Web sockets transport!

- `/ip4/127.0.0.1/tcp/65000/ipfs/QmRelay/p2p-circuit`
- `/ip4/127.0.0.1/tcp/65000/p2p/QmRelay/p2p-circuit`

If a node is configured with this address, it will use the specified host (`/ip4/127.0.0.1/tcp/65000/ipfs/QmRelay`) as a relay and it will be reachable over this relay.
If a node is configured with this address, it will use the specified host (`/ip4/127.0.0.1/tcp/65000/p2p/QmRelay`) as a relay and it will be reachable over this relay.
- There could multiple addresses of this sort specified in the config, in which case the node will be reachable over all of them.
- This is useful if, for example, the node is behind a firewall but wants to be reachable from the outside over a specific relay.

Expand Down Expand Up @@ -227,36 +227,36 @@ $ ipfs id
"ID": "QmY73BLYav2gYc9PCEnjQqbfSGiqFv3aMsRXNyKFGtUoGF",
"PublicKey": "CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC84qPFzqajCfnvaJunqt48S1LIBRthXV60q5QClL+dUfOOU/m7v1ZcpNhvFFUN6tVCDaoT5AxEv0czxZiVx/njl6FVIc6tE1J+HWpc8cbAXNY6QbbyzKl/rjp7V8/QClE0JqgjIk84wnWGTwFhOEt0hnpu2XFt9iHaenSfg3EAa8K9MlbxmbawuxNLJJf7VZXkJrUNl6WOglAVU8Sqc4QaahCLVK5Dzo98zDBq1KDBxMbUgH0LTqzr6i+saxkEHZmBKO+mMVT3LzOUx1DQR4pLAw1qgoJstsIZEaJ2XLh975IiI7OKqWYH7+3NyNK2sldJK/4Zko4rH3irmnkAxLcFAgMBAAE=",
"Addresses": [
"/ip4/127.0.0.1/tcp/4001/ipfs/QmY73BLYav2gYc9PCEnjQqbfSGiqFv3aMsRXNyKFGtUoGF",
"/ip4/192.168.1.132/tcp/4001/ipfs/QmY73BLYav2gYc9PCEnjQqbfSGiqFv3aMsRXNyKFGtUoGF",
"/ip6/::1/tcp/4001/ipfs/QmY73BLYav2gYc9PCEnjQqbfSGiqFv3aMsRXNyKFGtUoGF",
"/ip4/186.4.18.182/tcp/13285/ipfs/QmY73BLYav2gYc9PCEnjQqbfSGiqFv3aMsRXNyKFGtUoGF",
"/ip4/186.4.18.182/tcp/13285/ipfs/QmY73BLYav2gYc9PCEnjQqbfSGiqFv3aMsRXNyKFGtUoGF"
"/ip4/127.0.0.1/tcp/4001/p2p/QmY73BLYav2gYc9PCEnjQqbfSGiqFv3aMsRXNyKFGtUoGF",
"/ip4/192.168.1.132/tcp/4001/p2p/QmY73BLYav2gYc9PCEnjQqbfSGiqFv3aMsRXNyKFGtUoGF",
"/ip6/::1/tcp/4001/p2p/QmY73BLYav2gYc9PCEnjQqbfSGiqFv3aMsRXNyKFGtUoGF",
"/ip4/186.4.18.182/tcp/13285/p2p/QmY73BLYav2gYc9PCEnjQqbfSGiqFv3aMsRXNyKFGtUoGF",
"/ip4/186.4.18.182/tcp/13285/p2p/QmY73BLYav2gYc9PCEnjQqbfSGiqFv3aMsRXNyKFGtUoGF"
],
"AgentVersion": "go-ipfs/0.4.14-dev/cb5bb7dd8",
"ProtocolVersion": "ipfs/0.1.0"
}
```

We can then grab the resolved multiaddr from the `Addresses` array — `/ip4/127.0.0.1/tcp/4004/ws/ipfs/Qm...`. Let's note it down somewhere and move to the next step.
We can then grab the resolved multiaddr from the `Addresses` array — `/ip4/127.0.0.1/tcp/4004/ws/p2p/Qm...`. Let's note it down somewhere and move to the next step.

**js ipfs**

```console
$ jsipfs daemon
Initializing daemon...
Swarm listening on /p2p-circuit/ipfs/QmfQj8YwDdy1uP2DpZBa7k38rSGPvhHiC52cdAGWBqoVpq
Swarm listening on /p2p-circuit/ip4/0.0.0.0/tcp/4002/ipfs/QmfQj8YwDdy1uP2DpZBa7k38rSGPvhHiC52cdAGWBqoVpq
Swarm listening on /p2p-circuit/ip4/127.0.0.1/tcp/4003/ws/ipfs/QmfQj8YwDdy1uP2DpZBa7k38rSGPvhHiC52cdAGWBqoVpq
Swarm listening on /ip4/127.0.0.1/tcp/4003/ws/ipfs/QmfQj8YwDdy1uP2DpZBa7k38rSGPvhHiC52cdAGWBqoVpq
Swarm listening on /ip4/127.0.0.1/tcp/4002/ipfs/QmfQj8YwDdy1uP2DpZBa7k38rSGPvhHiC52cdAGWBqoVpq
Swarm listening on /ip4/192.168.1.132/tcp/4002/ipfs/QmfQj8YwDdy1uP2DpZBa7k38rSGPvhHiC52cdAGWBqoVpq
Swarm listening on /p2p-circuit/p2p/QmfQj8YwDdy1uP2DpZBa7k38rSGPvhHiC52cdAGWBqoVpq
Swarm listening on /p2p-circuit/ip4/0.0.0.0/tcp/4002/p2p/QmfQj8YwDdy1uP2DpZBa7k38rSGPvhHiC52cdAGWBqoVpq
Swarm listening on /p2p-circuit/ip4/127.0.0.1/tcp/4003/ws/p2p/QmfQj8YwDdy1uP2DpZBa7k38rSGPvhHiC52cdAGWBqoVpq
Swarm listening on /ip4/127.0.0.1/tcp/4003/ws/p2p/QmfQj8YwDdy1uP2DpZBa7k38rSGPvhHiC52cdAGWBqoVpq
Swarm listening on /ip4/127.0.0.1/tcp/4002/p2p/QmfQj8YwDdy1uP2DpZBa7k38rSGPvhHiC52cdAGWBqoVpq
Swarm listening on /ip4/192.168.1.132/tcp/4002/p2p/QmfQj8YwDdy1uP2DpZBa7k38rSGPvhHiC52cdAGWBqoVpq
API is listening on: /ip4/127.0.0.1/tcp/5002
Gateway (readonly) is listening on: /ip4/127.0.0.1/tcp/9090
Daemon is ready
```

Look out for an address similar to `/ip4/127.0.0.1/tcp/4003/ws/ipfs/Qm...`. Note it down somewhere, and let's move on to the next step.
Look out for an address similar to `/ip4/127.0.0.1/tcp/4003/ws/p2p/Qm...`. Note it down somewhere, and let's move on to the next step.

### 2. Configure and run the bundled example

Expand Down Expand Up @@ -333,7 +333,7 @@ const ipfs = await IPFS.create({

- We connected the browser nodes to an external node over its websocket transport using the `/ip4/127.0.0.1/tcp/4003/ws/ipfs/...` multiaddr. That external node happens to be a `HOP` node, meaning that it can relay connections for our browsers (and other nodes) allowing them to connect

- And finally we connected the two browser nodes using the `/p2p-circuit/ipfs/...` multiaddr. Take a look at the code below in [src/app.js](src/app.js#L103...L108) - lines 103-108
- And finally we connected the two browser nodes using the `/p2p-circuit/p2p/...` multiaddr. Take a look at the code below in [src/app.js](src/app.js#L103...L108) - lines 103-108

```js
try {
Expand Down
2 changes: 1 addition & 1 deletion examples/circuit-relaying/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ <h1>IPFS Simple Messaging</h1>
<ul id="peers"></ul>
</div>
<div class="box peers-box">
<label>Peers Connected:</label>
<label>Swarm Peers:</label>
<ul id="peers-addrs"></ul>
</div>
<div>
Expand Down
1 change: 1 addition & 0 deletions examples/circuit-relaying/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"author": "Dmitriy Ryajov <[email protected]>",
"license": "MIT",
"dependencies": {
"delay": "^4.3.0",
"ipfs": "^0.46.0",
"ipfs-pubsub-room": "^2.0.1"
},
Expand Down
7 changes: 4 additions & 3 deletions examples/circuit-relaying/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ document.addEventListener('DOMContentLoaded', async () => {
const sendMsg = helpers.sendMsg
const updatePeers = helpers.updatePeers
const updateAddrs = helpers.updateAddrs
const updateSwarmPeers = helpers.updateSwarmPeers

const info = await ipfs.id()
console.log('IPFS node ready with id ' + info.id)

let room = createRoom(roomName)

$peerId.innerHTML = `<li>${info.id}</li>`
updateAddrs(info.addresses)

$send.addEventListener('click', () => {
sendMsg(room)
Expand Down Expand Up @@ -95,13 +95,14 @@ document.addEventListener('DOMContentLoaded', async () => {
})

$connect.addEventListener('click', async () => {
const peer = $peer.value
const peer = $peer.value.trim()
$peer.value = ''
try {
await ipfs.swarm.connect(peer)
await updateAddrs(ipfs)
await updateSwarmPeers(ipfs)
} catch (err) {
return console.error(err)
}
$pAddrs.innerHTML += `<li>${peer.trim()}</li>`
})
})
60 changes: 52 additions & 8 deletions examples/circuit-relaying/src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const $message = document.querySelector('#message')
const $msgs = document.querySelector('#msgs')
const $addrs = document.querySelector('#addrs')
const $peers = document.querySelector('#peers')
const $pAddrs = document.querySelector('#peers-addrs')
const delay = require('delay')

const NAMESPACE = 'ipfs-quick-msg'

Expand Down Expand Up @@ -53,22 +55,64 @@ module.exports = (ipfs, peersSet) => {
const tags = Array.from(peersSet).map((p) => {
return `<li>${p}</li>`
})
$peers.innerHTML = tags
$peers.innerHTML = tags.join('')
}

const updateAddrs = (addrs) => {
$addrs.innerHTML = `
<div>
<ul>
${addrs.map((addr) => `<li>${addr.toString()}</li>`)}
<ul>
</div>`
const updateSwarmPeers = async (ipfs) => {
const peers = await ipfs.swarm.peers()

$pAddrs.innerHTML = peers.map(peer => `<li>${peer.peer}</li>`).join('')
}

const updateAddrs = async (ipfs) => {
const info = await ipfs.id()

// see which peers support the circuit relay protocol
const relayAddrs = []
const connections = ipfs.libp2p.connections
const peers = await ipfs.swarm.peers()

for (let i = 0; i < peers.length; i++) {
const {
peer: peerId
} = peers[i]

const cons = connections.get(peerId)

for (let j = 0; j < cons.length; j++) {
const con = cons[j]
const remoteAddr = con.remoteAddr.toString()

for (let k = 0; k < 5; k++) {
// protocols is undefined until the connection is negotiated so try a few times
const protocols = ipfs.libp2p.peerStore.protoBook.get(con.remotePeer)

if (!protocols) {
await delay(500)
continue
}

const isRelay = protocols.find(proto => proto.toString().includes('/circuit/relay'))

// only add addresses for peers that are relays and that we aren't already
// connected to them via another relay
if (isRelay && !remoteAddr.includes('p2p-circuit') && !remoteAddr.includes(info.id)) {
relayAddrs.push(`${remoteAddr}/p2p-circuit/p2p/${info.id}`)
}

break
}
}
}

$addrs.innerHTML = relayAddrs.map((addr) => `<li>${addr.toString()}</li>`).join('')
}

return {
createRoom,
sendMsg,
updatePeers,
updateSwarmPeers,
updateAddrs
}
}
50 changes: 31 additions & 19 deletions examples/circuit-relaying/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ const {
} = require('test-ipfs-example/utils')
const pkg = require('./package.json')

async function testUI (url, relay, localPeerIdFile, remotePeerIdFile) {
async function testUI (url, relayAddr, relayId, localPeerIdFile, remotePeerIdFile) {
const proc = execa(require.resolve('test-ipfs-example/node_modules/.bin/nightwatch'), ['--config', require.resolve('test-ipfs-example/nightwatch.conf.js'), path.join(__dirname, 'test.js')], {
cwd: path.resolve(__dirname, '../'),
env: {
...process.env,
CI: true,
IPFS_EXAMPLE_TEST_URL: url,
IPFS_RELAY_ADDRESS: relay,
IPFS_RELAY_ADDRESS: relayAddr,
IPFS_RELAY_ID: relayId,
IPFS_LOCAL_PEER_ID_FILE: localPeerIdFile,
IPFS_REMOTE_PEER_ID_FILE: remotePeerIdFile
},
Expand Down Expand Up @@ -72,8 +73,8 @@ async function runTest () {
const peerB = path.join(os.tmpdir(), `test-${Date.now()}-b.txt`)

await Promise.all([
testUI(server1.url, id.addresses[0], peerA, peerB),
testUI(server2.url, id.addresses[0], peerB, peerA)
testUI(server1.url, id.addresses[0], id.id, peerA, peerB),
testUI(server2.url, id.addresses[0], id.id, peerB, peerA)
])
} finally {
await ipfsd.stop()
Expand All @@ -85,8 +86,8 @@ async function runTest () {
module.exports = runTest

module.exports[pkg.name] = function (browser) {
let localPeerId = null
let remotePeerId = null
let local = null
let remote = null

browser
.url(process.env.IPFS_EXAMPLE_TEST_URL)
Expand All @@ -96,25 +97,36 @@ module.exports[pkg.name] = function (browser) {
.pause(1000)
.click('#connect')

browser.expect.element('#peers-addrs').text.to.contain(process.env.IPFS_RELAY_ADDRESS)
browser.expect.element('#peers-addrs').text.to.contain(process.env.IPFS_RELAY_ID)
browser.expect.element('#peer-id').text.to.not.equal('')

// exchange peer info
browser.getText('#addrs', (result) => {
localPeerId = result.value.trim()
console.info('got local peer id', localPeerId) // eslint-disable-line no-console
local = {
addr: result.value.trim()
}
console.info(`got local circuit relay address ${local.addr}`) // eslint-disable-line no-console
})
.getText('#peer-id', (result) => {
local.id = result.value.trim()
console.info(`got local peer id ${local.id}`) // eslint-disable-line no-console
})
.perform(async (browser, done) => {
console.info('writing local peer id') // eslint-disable-line no-console
await fs.writeFile(process.env.IPFS_LOCAL_PEER_ID_FILE, localPeerId)
console.info(`writing local data ${local.addr}`) // eslint-disable-line no-console
await fs.writeJson(process.env.IPFS_LOCAL_PEER_ID_FILE, local)

console.info('reading remote peer id') // eslint-disable-line no-console
console.info('reading remote circuit relay address') // eslint-disable-line no-console
for (let i = 0; i < 100; i++) {
try {
remotePeerId = await fs.readFile(process.env.IPFS_REMOTE_PEER_ID_FILE, {
remote = await fs.readJson(process.env.IPFS_REMOTE_PEER_ID_FILE, {
encoding: 'utf8'
})

console.info('got remote peer id', remotePeerId) // eslint-disable-line no-console
if (!remote || !remote.addr || !remote.id) {
throw new Error('Remote circuit relay address was empty')
}

console.info(`got remote circuit relay address ${remote.addr}`) // eslint-disable-line no-console
done()

break
Expand All @@ -125,24 +137,24 @@ module.exports[pkg.name] = function (browser) {
await delay(1000)
}

console.info('connecting to remote peer', remotePeerId) // eslint-disable-line no-console
console.info(`connecting to remote peer ${remote.addr}`) // eslint-disable-line no-console

browser
.clearValue('#peer')
.setValue('#peer', remotePeerId)
.setValue('#peer', remote.addr)
.pause(1000)
.click('#connect')

browser.expect.element('#peers-addrs').text.to.contain(remotePeerId)
browser.expect.element('#peers-addrs').text.to.contain(remote.id)

browser
.clearValue('#message')
.setValue('#message', 'hello')
.pause(1000)
.click('#send')

browser.expect.element('#msgs').text.to.contain(`${remotePeerId.substr(-4)}: hello`)
browser.expect.element('#msgs').text.to.contain(`${localPeerId.substr(-4)}: hello`)
browser.expect.element('#msgs').text.to.contain(`${remote.id.substr(-4)}: hello`)
browser.expect.element('#msgs').text.to.contain(`${local.id.substr(-4)}: hello`)
})

browser.end()
Expand Down
5 changes: 3 additions & 2 deletions packages/ipfs/test/core/libp2p.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const PeerId = require('peer-id')
const Libp2p = require('libp2p')
const EE = require('events')
const libp2pComponent = require('../../src/core/components/libp2p')
const Crypto = require('libp2p-secio')

class DummyTransport {
get [Symbol.toStringTag] () {
Expand Down Expand Up @@ -77,7 +78,7 @@ describe('libp2p customization', function () {
libp2p: (opts) => {
return new Libp2p({
peerId: opts.peerId,
modules: { transport: [DummyTransport] },
modules: { transport: [DummyTransport], connEncryption: [Crypto] },
config: { relay: { enabled: false } }
})
}
Expand All @@ -101,7 +102,7 @@ describe('libp2p customization', function () {
libp2p: (opts) => {
return new Libp2p({
peerId: opts.peerId,
modules: { transport: [DummyTransport] },
modules: { transport: [DummyTransport], connEncryption: [Crypto] },
config: { relay: { enabled: false } }
})
}
Expand Down