Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support custom listener options #822

Merged
merged 7 commits into from
Dec 9, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
26 changes: 26 additions & 0 deletions doc/CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
lovemyliwu marked this conversation as resolved.
Show resolved Hide resolved

```js
const transportKey = WebRTCStar.prototype[Symbol.toStringTag]
const node = await Libp2p.create({
modules: {
transport: [WebRTCStar],
streamMuxer: [MPLEX],
connEncryption: [NOISE]
},
lovemyliwu marked this conversation as resolved.
Show resolved Hide resolved
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:
Expand Down
4 changes: 3 additions & 1 deletion src/transport-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class TransportManager {
this.upgrader = upgrader
this._transports = new Map()
this._listeners = new Map()
this._listenerOptions = new Map()
this.faultTolerance = faultTolerance
}

Expand Down Expand Up @@ -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, [])
}
Expand Down Expand Up @@ -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.get(key), this.onConnection)
this._listeners.get(key).push(listener)

// We need to attempt to listen on everything
Expand Down
6 changes: 5 additions & 1 deletion test/transports/transport-manager.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -40,14 +41,17 @@ 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)
// Ephemeral ip addresses may result in multiple listeners
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 () => {
Expand Down
6 changes: 5 additions & 1 deletion test/transports/transport-manager.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down