diff --git a/.travis.yml b/.travis.yml index bb21bd4028..47f1fcb07a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -46,5 +46,13 @@ jobs: - npm install - LIBP2P_JS=${TRAVIS_BUILD_DIR}/src/index.js npx aegir test -t node --bail + - stage: test + if: type = pull_request + name: example - auto-relay + script: + - cd examples + - npm install + - npm run test -- auto-relay + notifications: email: false \ No newline at end of file diff --git a/examples/auto-relay/README.md b/examples/auto-relay/README.md new file mode 100644 index 0000000000..fbb3b7a046 --- /dev/null +++ b/examples/auto-relay/README.md @@ -0,0 +1,192 @@ +# Auto relay + +Auto Relay enables libp2p nodes to dynamically find and bind to relays on the network. Once binding (listening) is done, the node can and should advertise its addresses on the network, allowing any other node to dial it over its bound relay(s). +While direct connections to nodes are preferable, it's not always possible to do so due to NATs or browser limitations. + +## 0. Setup the example + +Before moving into the examples, you should run `npm install` on the top level `js-libp2p` folder, in order to install all the dependencies needed for this example. Once the install finishes, you should move into the example folder with `cd examples/auto-relay`. + +This example comes with 3 main files. A `relay.js` file to be used in the first step, a `listener.js` file to be used in the second step and a `dialer.js` file to be used on the third step. All of these scripts will run their own libp2p node, which will interact with the previous ones. All nodes must be running in order for you to proceed. + +## 1. Set up a relay node + +In the first step of this example, we need to configure and run a relay node in order for our target node to bind to for accepting inbound connections. + +The relay node will need to have its relay subsystem enabled, as well as its HOP capability. It can be configured as follows: + +```js +const Libp2p = require('libp2p') +const Websockets = require('libp2p-websockets') +const { NOISE } = require('libp2p-noise') +const MPLEX = require('libp2p-mplex') + +const node = await Libp2p.create({ + modules: { + transport: [Websockets], + connEncryption: [NOISE], + streamMuxer: [MPLEX] + }, + addresses: { + listen: ['/ip4/0.0.0.0/tcp/0/ws'] + // TODO check "What is next?" section + // announce: ['/dns4/auto-relay.libp2p.io/tcp/443/wss/p2p/QmWDn2LY8nannvSWJzruUYoLZ4vV83vfCBwd8DipvdgQc3'] + }, + config: { + relay: { + enabled: true, + hop: { + enabled: true + }, + advertise: { + enabled: true, + } + } + } +}) + +await node.start() + +console.log(`Node started with id ${node.peerId.toB58String()}`) +console.log('Listening on:') +node.multiaddrs.forEach((ma) => console.log(`${ma.toString()}/p2p/${node.peerId.toB58String()}`)) +``` + +The Relay HOP advertise functionality is **NOT** required to be enabled. However, if you are interested in advertising on the network that this node is available to be used as a HOP Relay you can enable it. A content router module or Rendezvous needs to be configured to leverage this option. + +You should now run the following to start the relay node: + +```sh +node relay.js +``` + +This should print out something similar to the following: + +```sh +Node started with id QmWDn2LY8nannvSWJzruUYoLZ4vV83vfCBwd8DipvdgQc3 +Listening on: +/ip4/127.0.0.1/tcp/61592/ws/p2p/QmWDn2LY8nannvSWJzruUYoLZ4vV83vfCBwd8DipvdgQc3 +/ip4/192.168.1.120/tcp/61592/ws/p2p/QmWDn2LY8nannvSWJzruUYoLZ4vV83vfCBwd8DipvdgQc3 +``` + +## 2. Set up a listener node with Auto Relay Enabled + +One of the typical use cases for Auto Relay is nodes behind a NAT or browser nodes due to their inability to expose a public address. For running a libp2p node that automatically binds itself to connected HOP relays, you can see the following: + +```js +const Libp2p = require('libp2p') +const Websockets = require('libp2p-websockets') +const { NOISE } = require('libp2p-noise') +const MPLEX = require('libp2p-mplex') + +const relayAddr = process.argv[2] +if (!relayAddr) { + throw new Error('the relay address needs to be specified as a parameter') +} + +const node = await Libp2p.create({ + modules: { + transport: [Websockets], + connEncryption: [NOISE], + streamMuxer: [MPLEX] + }, + config: { + relay: { + enabled: true, + autoRelay: { + enabled: true, + maxListeners: 2 + } + } + } +}) + +await node.start() +console.log(`Node started with id ${node.peerId.toB58String()}`) + +const conn = await node.dial(relayAddr) + +// Wait for connection and relay to be bind for the example purpose +await new Promise((resolve) => { + node.peerStore.on('change:multiaddrs', ({ peerId }) => { + // Updated self multiaddrs? + if (peerId.equals(node.peerId)) { + resolve() + } + }) +}) + +console.log(`Connected to the HOP relay ${conn.remotePeer.toString()}`) +console.log(`Advertising with a relay address of ${node.multiaddrs[0].toString()}/p2p/${node.peerId.toB58String()}`) +``` + +As you can see in the code, we need to provide the relay address, `relayAddr`, as a process argument. This node will dial the provided relay address and automatically bind to it. + +You should now run the following to start the node running Auto Relay: + +```sh +node listener.js /ip4/192.168.1.120/tcp/58941/ws/p2p/QmQKCBm87HQMbFqy14oqC85pMmnRrj6iD46ggM6reqNpsd +``` + +This should print out something similar to the following: + +```sh +Node started with id QmerrWofKF358JE6gv3z74cEAyL7z1KqhuUoVfGEynqjRm +Connected to the HOP relay QmWDn2LY8nannvSWJzruUYoLZ4vV83vfCBwd8DipvdgQc3 +Advertising with a relay address of /ip4/192.168.1.120/tcp/61592/ws/p2p/QmWDn2LY8nannvSWJzruUYoLZ4vV83vfCBwd8DipvdgQc3/p2p-circuit/p2p/QmerrWofKF358JE6gv3z74cEAyL7z1KqhuUoVfGEynqjRm +``` + +Per the address, it is possible to verify that the auto relay node is listening on the circuit relay node address. + +Instead of dialing this relay manually, you could set up this node with the Bootstrap module and provide it in the bootstrap list. Moreover, you can use other `peer-discovery` modules to discover peers in the network and the node will automatically bind to the relays that support HOP until reaching the maximum number of listeners. + +## 3. Set up a dialer node for testing connectivity + +Now that you have a relay node and a node bound to that relay, you can test connecting to the auto relay node via the relay. + +```js +const Libp2p = require('libp2p') +const Websockets = require('libp2p-websockets') +const { NOISE } = require('libp2p-noise') +const MPLEX = require('libp2p-mplex') + +const autoRelayNodeAddr = process.argv[2] +if (!autoRelayNodeAddr) { + throw new Error('the auto relay node address needs to be specified') +} + +const node = await Libp2p.create({ + modules: { + transport: [Websockets], + connEncryption: [NOISE], + streamMuxer: [MPLEX] + } +}) + +await node.start() +console.log(`Node started with id ${node.peerId.toB58String()}`) + +const conn = await node.dial(autoRelayNodeAddr) +console.log(`Connected to the auto relay node via ${conn.remoteAddr.toString()}`) +``` + +You should now run the following to start the relay node using the listen address from step 2: + +```sh +node dialer.js /ip4/192.168.1.120/tcp/58941/ws/p2p/QmQKCBm87HQMbFqy14oqC85pMmnRrj6iD46ggM6reqNpsd +``` + +Once you start your test node, it should print out something similar to the following: + +```sh +Node started: Qme7iEzDxFoFhhkrsrkHkMnM11aPYjysaehP4NZeUfVMKG +Connected to the auto relay node via /ip4/192.168.1.120/tcp/61592/ws/p2p/QmWDn2LY8nannvSWJzruUYoLZ4vV83vfCBwd8DipvdgQc3/p2p-circuit/p2p/QmerrWofKF358JE6gv3z74cEAyL7z1KqhuUoVfGEynqjRm +``` + +As you can see from the output, the remote address of the established connection uses the relayed connection. + +## 4. What is next? + +Before moving into production, there are a few things that you should take into account. + +A relay node should not advertise its private address in a real world scenario, as the node would not be reachable by others. You should provide an array of public addresses in the libp2p `addresses.announce` option. If you are using websockets, bear in mind that due to browser’s security policies you cannot establish unencrypted connection from secure context. The simplest solution is to setup SSL with nginx and proxy to the node and setup a domain name for the certificate. diff --git a/examples/auto-relay/dialer.js b/examples/auto-relay/dialer.js new file mode 100644 index 0000000000..ebf5fd1249 --- /dev/null +++ b/examples/auto-relay/dialer.js @@ -0,0 +1,29 @@ +'use strict' + +const Libp2p = require('libp2p') +const Websockets = require('libp2p-websockets') +const { NOISE } = require('libp2p-noise') +const MPLEX = require('libp2p-mplex') + +async function main () { + const autoRelayNodeAddr = process.argv[2] + if (!autoRelayNodeAddr) { + throw new Error('the auto relay node address needs to be specified') + } + + const node = await Libp2p.create({ + modules: { + transport: [Websockets], + connEncryption: [NOISE], + streamMuxer: [MPLEX] + } + }) + + await node.start() + console.log(`Node started with id ${node.peerId.toB58String()}`) + + const conn = await node.dial(autoRelayNodeAddr) + console.log(`Connected to the auto relay node via ${conn.remoteAddr.toString()}`) +} + +main() diff --git a/examples/auto-relay/listener.js b/examples/auto-relay/listener.js new file mode 100644 index 0000000000..0f94374067 --- /dev/null +++ b/examples/auto-relay/listener.js @@ -0,0 +1,47 @@ +'use strict' + +const Libp2p = require('libp2p') +const Websockets = require('libp2p-websockets') +const { NOISE } = require('libp2p-noise') +const MPLEX = require('libp2p-mplex') + +async function main () { + const relayAddr = process.argv[2] + if (!relayAddr) { + throw new Error('the relay address needs to be specified as a parameter') + } + + const node = await Libp2p.create({ + modules: { + transport: [Websockets], + connEncryption: [NOISE], + streamMuxer: [MPLEX] + }, + config: { + relay: { + enabled: true, + autoRelay: { + enabled: true, + maxListeners: 2 + } + } + } + }) + + await node.start() + console.log(`Node started with id ${node.peerId.toB58String()}`) + + const conn = await node.dial(relayAddr) + + console.log(`Connected to the HOP relay ${conn.remotePeer.toString()}`) + + // Wait for connection and relay to be bind for the example purpose + node.peerStore.on('change:multiaddrs', ({ peerId }) => { + // Updated self multiaddrs? + if (peerId.equals(node.peerId)) { + console.log(`Advertising with a relay address of ${node.multiaddrs[0].toString()}/p2p/${node.peerId.toB58String()}`) + } + }) +} + +main() diff --git a/examples/auto-relay/relay.js b/examples/auto-relay/relay.js new file mode 100644 index 0000000000..2a18c3a769 --- /dev/null +++ b/examples/auto-relay/relay.js @@ -0,0 +1,40 @@ +'use strict' + +const Libp2p = require('libp2p') +const Websockets = require('libp2p-websockets') +const { NOISE } = require('libp2p-noise') +const MPLEX = require('libp2p-mplex') + +async function main () { + const node = await Libp2p.create({ + modules: { + transport: [Websockets], + connEncryption: [NOISE], + streamMuxer: [MPLEX] + }, + addresses: { + listen: ['/ip4/0.0.0.0/tcp/0/ws'] + // TODO check "What is next?" section + // announce: ['/dns4/auto-relay.libp2p.io/tcp/443/wss/p2p/QmWDn2LY8nannvSWJzruUYoLZ4vV83vfCBwd8DipvdgQc3'] + }, + config: { + relay: { + enabled: true, + hop: { + enabled: true + }, + advertise: { + enabled: true, + } + } + } + }) + + await node.start() + + console.log(`Node started with id ${node.peerId.toB58String()}`) + console.log('Listening on:') + node.multiaddrs.forEach((ma) => console.log(`${ma.toString()}/p2p/${node.peerId.toB58String()}`)) +} + +main() diff --git a/examples/auto-relay/test.js b/examples/auto-relay/test.js new file mode 100644 index 0000000000..0eaf8cc3ae --- /dev/null +++ b/examples/auto-relay/test.js @@ -0,0 +1,94 @@ +'use strict' + +const path = require('path') +const execa = require('execa') +const pDefer = require('p-defer') +const uint8ArrayToString = require('uint8arrays/to-string') + +function startProcess (name, args = []) { + return execa('node', [path.join(__dirname, name), ...args], { + cwd: path.resolve(__dirname), + all: true + }) +} + +async function test () { + let output1 = '' + let output2 = '' + let output3 = '' + let relayAddr + let autoRelayAddr + + const proc1Ready = pDefer() + const proc2Ready = pDefer() + + // Step 1 process + process.stdout.write('relay.js\n') + + const proc1 = startProcess('relay.js') + proc1.all.on('data', async (data) => { + process.stdout.write(data) + + output1 += uint8ArrayToString(data) + + if (output1.includes('Listening on:') && output1.includes('/p2p/')) { + relayAddr = output1.trim().split('Listening on:\n')[1].split('\n')[0] + proc1Ready.resolve() + } + }) + + await proc1Ready.promise + process.stdout.write('==================================================================\n') + + // Step 2 process + process.stdout.write('listener.js\n') + + const proc2 = startProcess('listener.js', [relayAddr]) + proc2.all.on('data', async (data) => { + process.stdout.write(data) + + output2 += uint8ArrayToString(data) + + if (output2.includes('Advertising with a relay address of') && output2.includes('/p2p/')) { + autoRelayAddr = output2.trim().split('Advertising with a relay address of ')[1] + proc2Ready.resolve() + } + }) + + await proc2Ready.promise + process.stdout.write('==================================================================\n') + + // Step 3 process + process.stdout.write('dialer.js\n') + + const proc3 = startProcess('dialer.js', [autoRelayAddr]) + proc3.all.on('data', async (data) => { + process.stdout.write(data) + + output3 += uint8ArrayToString(data) + + if (output3.includes('Connected to the auto relay node via')) { + const remoteAddr = output3.trim().split('Connected to the auto relay node via ')[1] + + if (remoteAddr === autoRelayAddr) { + proc3.kill() + proc2.kill() + proc1.kill() + } else { + throw new Error('dialer did not dial through the relay') + } + } + }) + + await Promise.all([ + proc1, + proc2, + proc3 + ]).catch((err) => { + if (err.signal !== 'SIGTERM') { + throw err + } + }) +} + +module.exports = test \ No newline at end of file diff --git a/examples/package.json b/examples/package.json new file mode 100644 index 0000000000..feaf656d1b --- /dev/null +++ b/examples/package.json @@ -0,0 +1,16 @@ +{ + "name": "libp2p-examples", + "version": "1.0.0", + "description": "Examples of how to use libp2p", + "scripts": { + "test": "node ./test.js", + "test:all": "node ./test-all.js" + }, + "license": "MIT", + "dependencies": { + "execa": "^2.1.0", + "fs-extra": "^8.1.0", + "p-defer": "^3.0.0", + "which": "^2.0.1" + } +} diff --git a/examples/test-all.js b/examples/test-all.js new file mode 100644 index 0000000000..3ee99e45fa --- /dev/null +++ b/examples/test-all.js @@ -0,0 +1,33 @@ +'use strict' + +process.on('unhandedRejection', (err) => { + console.error(err) + + process.exit(1) +}) + +const path = require('path') +const fs = require('fs') +const { + waitForOutput +} = require('./utils') + +async function testAll () { + for (const dir of fs.readdirSync(__dirname)) { + if (dir === 'node_modules' || dir === 'tests_output') { + continue + } + + const stats = fs.statSync(path.join(__dirname, dir)) + + if (!stats.isDirectory()) { + continue + } + + await waitForOutput('npm info ok', 'npm', ['test', '--', dir], { + cwd: __dirname + }) + } +} + +testAll() diff --git a/examples/test.js b/examples/test.js new file mode 100644 index 0000000000..3da6eccdb9 --- /dev/null +++ b/examples/test.js @@ -0,0 +1,95 @@ +'use strict' + +process.env.NODE_ENV = 'test' +process.env.CI = true // needed for some "clever" build tools + +const fs = require('fs-extra') +const path = require('path') +const execa = require('execa') +const dir = path.join(__dirname, process.argv[2]) + +testExample(dir) + .then(() => {}, (err) => { + if (err.exitCode) { + process.exit(err.exitCode) + } + + console.error(err) + process.exit(1) + }) + +async function testExample (dir) { + await installDeps(dir) + await build(dir) + await runTest(dir) + // TODO: add browser test setup +} + +async function installDeps (dir) { + if (!fs.existsSync(path.join(dir, 'package.json'))) { + console.info('Nothing to install in', dir) + return + } + + if (fs.existsSync(path.join(dir, 'node_modules'))) { + console.info('Dependencies already installed in', dir) + return + } + + const proc = execa.command('npm install', { + cwd: dir + }) + proc.all.on('data', (data) => { + process.stdout.write(data) + }) + + await proc +} + +async function build (dir) { + const pkgJson = path.join(dir, 'package.json') + + if (!fs.existsSync(pkgJson)) { + console.info('Nothing to build in', dir) + return + } + + const pkg = require(pkgJson) + let build + + if (pkg.scripts.bundle) { + build = 'bundle' + } + + if (pkg.scripts.build) { + build = 'build' + } + + if (!build) { + console.info('No "build" or "bundle" script in', pkgJson) + return + } + + const proc = execa('npm', ['run', build], { + cwd: dir + }) + proc.all.on('data', (data) => { + process.stdout.write(data) + }) + + await proc +} + +async function runTest (dir) { + console.info('Running node tests in', dir) + const testFile = path.join(dir, 'test.js') + + if (!fs.existsSync(testFile)) { + console.info('Nothing to test in', dir) + return + } + + const runTest = require(testFile) + + await runTest() +} \ No newline at end of file diff --git a/examples/utils.js b/examples/utils.js new file mode 100644 index 0000000000..aec6df5418 --- /dev/null +++ b/examples/utils.js @@ -0,0 +1,61 @@ +'use strict' + +const execa = require('execa') +const fs = require('fs-extra') +const which = require('which') + +async function isExecutable (command) { + try { + await fs.access(command, fs.constants.X_OK) + + return true + } catch (err) { + if (err.code === 'ENOENT') { + return isExecutable(await which(command)) + } + + if (err.code === 'EACCES') { + return false + } + + throw err + } +} + +async function waitForOutput (expectedOutput, command, args = [], opts = {}) { + if (!await isExecutable(command)) { + args.unshift(command) + command = 'node' + } + + const proc = execa(command, args, opts) + let output = '' + let time = 120000 + + let timeout = setTimeout(() => { + throw new Error(`Did not see "${expectedOutput}" in output from "${[command].concat(args).join(' ')}" after ${time/1000}s`) + }, time) + + proc.all.on('data', (data) => { + process.stdout.write(data) + + output += data.toString('utf8') + + if (output.includes(expectedOutput)) { + clearTimeout(timeout) + proc.kill() + } + }) + + try { + await proc + } catch (err) { + if (!err.killed) { + throw err + } + } +} + +module.exports = { + waitForOutput +} diff --git a/package.json b/package.json index b1ae4c5ebe..042a9a0664 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "test": "npm run test:node && npm run test:browser", "test:node": "aegir test -t node -f \"./test/**/*.{node,spec}.js\"", "test:browser": "aegir test -t browser", + "test:examples": "cd examples && npm run test:all", "release": "aegir release -t node -t browser", "release-minor": "aegir release --type minor -t node -t browser", "release-major": "aegir release --type major -t node -t browser",