Skip to content
This repository was archived by the owner on Jul 21, 2023. It is now read-only.

feat: resolve 0 addresses #64

Merged
merged 5 commits into from
Oct 20, 2017
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
package-lock.json
yarn.lock

# Logs
logs
*.log
Expand Down
2 changes: 1 addition & 1 deletion circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ dependencies:
- sudo apt-get install -f
- sudo apt-get install --only-upgrade lsb-base
- sudo dpkg -i google-chrome.deb
- google-chrome --version
- google-chrome --version
38 changes: 37 additions & 1 deletion src/listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

const Connection = require('interface-connection').Connection
const includes = require('lodash.includes')
const multiaddr = require('multiaddr')
const os = require('os')

function noop () {}

const createServer = require('pull-ws/server') || noop

module.exports = (options, handler) => {
Expand Down Expand Up @@ -30,7 +34,39 @@ module.exports = (options, handler) => {
}

listener.getAddrs = (callback) => {
callback(null, [listeningMultiaddr])
const multiaddrs = []
const address = listener.address()

if (!address) {
return callback(new Error('Listener is not ready yet'))
}

let ipfsId = listeningMultiaddr.getPeerId()

// Because TCP will only return the IPv6 version
// we need to capture from the passed multiaddr
if (listeningMultiaddr.toString().indexOf('ip4') !== -1) {
let m = listeningMultiaddr.decapsulate('tcp')
m = m.encapsulate('/tcp/' + address.port + '/ws')
if (listeningMultiaddr.getPeerId()) {
m = m.encapsulate('/ipfs/' + ipfsId)
}

if (m.toString().indexOf('0.0.0.0') !== -1) {
const netInterfaces = os.networkInterfaces()
Object.keys(netInterfaces).forEach((niKey) => {
netInterfaces[niKey].forEach((ni) => {
if (ni.family === 'IPv4') {
multiaddrs.push(multiaddr(m.toString().replace('0.0.0.0', ni.address)))
}
})
})
} else {
multiaddrs.push(m)
}
}

callback(null, multiaddrs)
}

return listener
Expand Down
44 changes: 38 additions & 6 deletions test/node.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-env mocha */
/* eslint max-nested-callbacks: ["error", 5] */
'use strict'

const chai = require('chai')
Expand Down Expand Up @@ -97,16 +98,45 @@ describe('listen', () => {
})
})

it.skip('getAddrs on port 0 listen', (done) => {
// TODO port 0 not supported yet
it('getAddrs on port 0 listen', (done) => {
const addr = multiaddr(`/ip4/127.0.0.1/tcp/0/ws`)
const listener = ws.createListener((conn) => {
})
listener.listen(addr, () => {
listener.getAddrs((err, addrs) => {
expect(err).to.not.exist()
expect(addrs.length).to.equal(1)
expect(addrs.map((a) => a.toOptions().port)).to.not.include('0')
listener.close(done)
})
})
})

it.skip('getAddrs from listening on 0.0.0.0', (done) => {
// TODO 0.0.0.0 not supported yet
it('getAddrs from listening on 0.0.0.0', (done) => {
const addr = multiaddr(`/ip4/0.0.0.0/tcp/9003/ws`)
const listener = ws.createListener((conn) => {
})
listener.listen(addr, () => {
listener.getAddrs((err, addrs) => {
expect(err).to.not.exist()
expect(addrs.map((a) => a.toOptions().host)).to.not.include('0.0.0.0')
listener.close(done)
})
})
})

it.skip('getAddrs from listening on 0.0.0.0 and port 0', (done) => {
// TODO 0.0.0.0 or port 0 not supported yet
it('getAddrs from listening on 0.0.0.0 and port 0', (done) => {
const addr = multiaddr(`/ip4/0.0.0.0/tcp/0/ws`)
const listener = ws.createListener((conn) => {
})
listener.listen(addr, () => {
listener.getAddrs((err, addrs) => {
expect(err).to.not.exist()
expect(addrs.map((a) => a.toOptions().host)).to.not.include('0.0.0.0')
expect(addrs.map((a) => a.toOptions().port)).to.not.include('0')
listener.close(done)
})
})
})

it('getAddrs preserves IPFS Id', (done) => {
Expand Down Expand Up @@ -350,6 +380,7 @@ describe('valid Connection', () => {
listenerObsAddrs = addrs

listener.close(onClose)

function onClose () {
expect(listenerObsAddrs[0]).to.deep.equal(ma)
expect(dialerObsAddrs.length).to.equal(0)
Expand Down Expand Up @@ -407,6 +438,7 @@ describe('valid Connection', () => {
})

listener.listen(ma, onListen)

function onListen () {
const conn = ws.dial(ma)
conn.setPeerInfo('b')
Expand Down