From f2a954c4d3aa9790a2b46d715c09695487a7463a Mon Sep 17 00:00:00 2001 From: Smite Chow Date: Fri, 4 Dec 2020 16:42:06 +0800 Subject: [PATCH 1/7] support custom listener options --- src/transport-manager.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/transport-manager.js b/src/transport-manager.js index e18841bf02..a217cc5eea 100644 --- a/src/transport-manager.js +++ b/src/transport-manager.js @@ -20,6 +20,7 @@ class TransportManager { this.upgrader = upgrader this._transports = new Map() this._listeners = new Map() + this._listenerOptions = new Map() this.faultTolerance = faultTolerance } @@ -47,6 +48,7 @@ class TransportManager { }) this._transports.set(key, transport) + this._listenerOptions.set(key, transportOptions.listenerOptions || {}) if (!this._listeners.has(key)) { this._listeners.set(key, []) } @@ -154,7 +156,7 @@ class TransportManager { // For each supported multiaddr, create a listener for (const addr of supportedAddrs) { log('creating listener for %s on %s', key, addr) - const listener = transport.createListener({}, this.onConnection) + const listener = transport.createListener(this._listenerOptions[key], this.onConnection) this._listeners.get(key).push(listener) // We need to attempt to listen on everything From f0ad61fa33766f138aefc5c2a259d437940bc497 Mon Sep 17 00:00:00 2001 From: Smite Chow Date: Fri, 4 Dec 2020 18:20:58 +0800 Subject: [PATCH 2/7] fix get listener options --- src/transport-manager.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transport-manager.js b/src/transport-manager.js index a217cc5eea..f9cf8629c5 100644 --- a/src/transport-manager.js +++ b/src/transport-manager.js @@ -156,7 +156,7 @@ class TransportManager { // For each supported multiaddr, create a listener for (const addr of supportedAddrs) { log('creating listener for %s on %s', key, addr) - const listener = transport.createListener(this._listenerOptions[key], this.onConnection) + const listener = transport.createListener(this._listenerOptions.get(key), this.onConnection) this._listeners.get(key).push(listener) // We need to attempt to listen on everything From da2bfab894c7d9c85fdc2e3519be712cb1c373b2 Mon Sep 17 00:00:00 2001 From: Smite Chow Date: Mon, 7 Dec 2020 05:27:32 +0000 Subject: [PATCH 3/7] add doc to explain custom listener options --- doc/CONFIGURATION.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/doc/CONFIGURATION.md b/doc/CONFIGURATION.md index 527ee35733..e4fd400320 100644 --- a/doc/CONFIGURATION.md +++ b/doc/CONFIGURATION.md @@ -651,6 +651,32 @@ const node = await Libp2p.create({ }) ``` +Libp2p will auto call `createListener` after initialized transport, if you want custom listener options, you can provide `listenerOptions` in your transport option. For example, `libp2p-webrtc-star` powered by `libp2p-webrtc-peer`, you can custom the ice server(STUN/TURN) config. + +```js +const transportKey = WebRTCStar.prototype[Symbol.toStringTag] +const node = await Libp2p.create({ + modules: { + transport: [WebRTCStar], + streamMuxer: [MPLEX], + connEncryption: [NOISE] + }, + config: { + transport: { + [transportKey]: { + listenerOptions: { + config: { + iceServers: [ + {"urls": ["turn:YOUR.TURN.SERVER:3478"], "username": "YOUR.USER", "credential": "YOUR.PASSWORD"}, + {"urls": ["stun:YOUR.STUN.SERVER:3478"], "username": "", "credential": ""}] + } + } + } + } + } +}) +``` + ## Configuration examples As libp2p is designed to be a modular networking library, its usage will vary based on individual project needs. We've included links to some existing project configurations for your reference, in case you wish to replicate their configuration: From 323c5ce4d093a63f637a09c9f46e1e49770aeb74 Mon Sep 17 00:00:00 2001 From: Smite Chow Date: Mon, 7 Dec 2020 06:49:13 +0000 Subject: [PATCH 4/7] add ut --- test/transports/transport-manager.node.js | 6 +++++- test/transports/transport-manager.spec.js | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/test/transports/transport-manager.node.js b/test/transports/transport-manager.node.js index 1036230acb..7b53641843 100644 --- a/test/transports/transport-manager.node.js +++ b/test/transports/transport-manager.node.js @@ -10,6 +10,7 @@ const TransportManager = require('../../src/transport-manager') const Transport = require('libp2p-tcp') const multiaddr = require('multiaddr') const mockUpgrader = require('../utils/mockUpgrader') +const sinon = require('sinon') const addrs = [ multiaddr('/ip4/127.0.0.1/tcp/0'), multiaddr('/ip4/127.0.0.1/tcp/0') @@ -40,7 +41,9 @@ describe('Transport Manager (TCP)', () => { }) it('should be able to listen', async () => { - tm.add(Transport.prototype[Symbol.toStringTag], Transport) + tm.add(Transport.prototype[Symbol.toStringTag], Transport, {listenerOptions: {listen: 'carefully'}}) + const transport = tm._transports.get(Transport.prototype[Symbol.toStringTag]) + const spyListener = sinon.spy(transport, 'createListener') await tm.listen() expect(tm._listeners).to.have.key(Transport.prototype[Symbol.toStringTag]) expect(tm._listeners.get(Transport.prototype[Symbol.toStringTag])).to.have.length(addrs.length) @@ -48,6 +51,7 @@ describe('Transport Manager (TCP)', () => { expect(tm.getAddrs().length).to.equal(addrs.length) await tm.close() expect(tm._listeners.get(Transport.prototype[Symbol.toStringTag])).to.have.length(0) + expect(spyListener.firstCall.firstArg).to.deep.equal({listen: 'carefully'}) }) it('should be able to dial', async () => { diff --git a/test/transports/transport-manager.spec.js b/test/transports/transport-manager.spec.js index b32b280725..f91de4afde 100644 --- a/test/transports/transport-manager.spec.js +++ b/test/transports/transport-manager.spec.js @@ -125,7 +125,10 @@ describe('libp2p.transportManager', () => { const spy = sinon.spy() const key = spy.prototype[Symbol.toStringTag] = 'TransportSpy' const customOptions = { - another: 'value' + another: 'value', + listenerOptions: { + listen: 'carefully' + } } libp2p = new Libp2p({ peerId, @@ -143,6 +146,7 @@ describe('libp2p.transportManager', () => { expect(libp2p.transportManager).to.exist() // Our transport and circuit relay expect(libp2p.transportManager._transports.size).to.equal(2) + expect(libp2p.transportManager._listenerOptions.size).to.equal(2) expect(spy).to.have.property('callCount', 1) expect(spy.getCall(0)).to.have.deep.property('args', [{ ...customOptions, From 5b00e254b9d174a17604fb803285eaa102b809c3 Mon Sep 17 00:00:00 2001 From: Smite Chow Date: Mon, 7 Dec 2020 07:04:28 +0000 Subject: [PATCH 5/7] fix code style --- test/transports/transport-manager.node.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/transports/transport-manager.node.js b/test/transports/transport-manager.node.js index 7b53641843..cd1e6fb8b5 100644 --- a/test/transports/transport-manager.node.js +++ b/test/transports/transport-manager.node.js @@ -41,7 +41,7 @@ describe('Transport Manager (TCP)', () => { }) it('should be able to listen', async () => { - tm.add(Transport.prototype[Symbol.toStringTag], Transport, {listenerOptions: {listen: 'carefully'}}) + tm.add(Transport.prototype[Symbol.toStringTag], Transport, { listenerOptions: { listen: 'carefully' } }) const transport = tm._transports.get(Transport.prototype[Symbol.toStringTag]) const spyListener = sinon.spy(transport, 'createListener') await tm.listen() @@ -51,7 +51,7 @@ describe('Transport Manager (TCP)', () => { expect(tm.getAddrs().length).to.equal(addrs.length) await tm.close() expect(tm._listeners.get(Transport.prototype[Symbol.toStringTag])).to.have.length(0) - expect(spyListener.firstCall.firstArg).to.deep.equal({listen: 'carefully'}) + expect(spyListener.firstCall.firstArg).to.deep.equal({ listen: 'carefully' }) }) it('should be able to dial', async () => { From 9107146cdc7cd6a8f8fd40016f238dc452ae54cf Mon Sep 17 00:00:00 2001 From: Smite Chow Date: Tue, 8 Dec 2020 09:50:16 +0800 Subject: [PATCH 6/7] Apply suggestions from code review Co-authored-by: Vasco Santos --- doc/CONFIGURATION.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/doc/CONFIGURATION.md b/doc/CONFIGURATION.md index e4fd400320..e752b4b753 100644 --- a/doc/CONFIGURATION.md +++ b/doc/CONFIGURATION.md @@ -651,7 +651,7 @@ const node = await Libp2p.create({ }) ``` -Libp2p will auto call `createListener` after initialized transport, if you want custom listener options, you can provide `listenerOptions` in your transport option. For example, `libp2p-webrtc-star` powered by `libp2p-webrtc-peer`, you can custom the ice server(STUN/TURN) config. +During Libp2p startup, transport listeners will be created for the configured listen multiaddrs. Some transports support custom listener options and you can set them using the `listenerOptions` in the transport configuration. For example, [libp2p-webrtc-star](https://github.com/libp2p/js-libp2p-webrtc-star) transport listener supports the configuration of its underlying [simple-peer](https://github.com/feross/simple-peer) ice server(STUN/TURN) config as follows: ```js const transportKey = WebRTCStar.prototype[Symbol.toStringTag] @@ -661,6 +661,9 @@ const node = await Libp2p.create({ streamMuxer: [MPLEX], connEncryption: [NOISE] }, + addresses: { + listen: ['/dns4/your-wrtc-star.pub/tcp/443/wss/p2p-webrtc-star'] // your webrtc dns multiaddr + } config: { transport: { [transportKey]: { From ef97e361f63b1344927d092aa5a424f4cfa5cb74 Mon Sep 17 00:00:00 2001 From: Smite Chow Date: Tue, 8 Dec 2020 01:51:59 +0000 Subject: [PATCH 7/7] add missing comma --- doc/CONFIGURATION.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/CONFIGURATION.md b/doc/CONFIGURATION.md index e752b4b753..4105551796 100644 --- a/doc/CONFIGURATION.md +++ b/doc/CONFIGURATION.md @@ -663,7 +663,7 @@ const node = await Libp2p.create({ }, addresses: { listen: ['/dns4/your-wrtc-star.pub/tcp/443/wss/p2p-webrtc-star'] // your webrtc dns multiaddr - } + }, config: { transport: { [transportKey]: {