Skip to content
This repository has been archived by the owner on Feb 26, 2021. It is now read-only.

Commit

Permalink
fix: add tcp port support (#38)
Browse files Browse the repository at this point in the history
* Support multiaddrs with tcp/port/\{ws, wss\}. Fixes #37

* making linter happy

* docs: use multiaddrs with tcp port

* enforcing travis detection on travis env

* reverting previous commit

* tests: deactivating this test that fails because travis detection is failing because env vars are not being propagated into the browser

* linter happy
  • Loading branch information
pgte authored and daviddias committed Nov 30, 2017
1 parent 0052d19 commit 92af753
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 27 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Id.create((err, id) => {
if (err) throw err

const peerInfo = new Info(id)
peerInfo.multiaddrs.add(multiaddr("/dns4/ws-star-signal-1.servep2p.com/wss/p2p-websocket-star/"))
peerInfo.multiaddrs.add(multiaddr("/dns4/ws-star-signal-1.servep2p.com/tcp/443/wss/p2p-websocket-star/"))

// TODO -> review why the ID can not be passed by the .listen call
const ws = new WSStar({ id: id }) // the id is required for the crypto challenge
Expand Down Expand Up @@ -123,7 +123,7 @@ Additionally there is a rendezvous server at `ws-star-signal-3.servep2p.com` run

A libp2p-websocket-star address, using the signalling server we provide, looks like:

`/dns4/ws-star-signal-1.servep2p.com/wss/p2p-websocket-star/ipfs/<your-peer-id>`
`/dns4/ws-star-signal-1.servep2p.com/tcp/443/wss/p2p-websocket-star/ipfs/<your-peer-id>`

Note: The address above indicates WebSockets Secure, which can be accessed from both http and https.

Expand Down
42 changes: 19 additions & 23 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,28 @@ const crypto = require('libp2p-crypto')
const mafmt = require('mafmt')

function cleanUrlSIO (ma) {
const maStrSplit = ma.toString().split('/')
let maTCP = ma
if (maTCP.toString().indexOf('/ws') !== -1) maTCP = maTCP.decapsulate('ws')
if (maTCP.toString().indexOf('/wss') !== -1) maTCP = maTCP.decapsulate('wss')
if (maTCP.toString().indexOf('/p2p-websocket-star') !== -1) maTCP = maTCP.decapsulate('p2p-websocket-star')
const protos = ma.protos()
const ipProto = protos[0].name
const tcpProto = protos[1].name
const wsProto = protos[2].name
const stringTuples = ma.stringTuples()
const tcpPort = stringTuples[1][1]

if (mafmt.TCP.matches(maTCP)) {
if (maStrSplit[1] === 'ip4') {
return 'http://' + maStrSplit[2] + ':' + maStrSplit[4]
} else if (maStrSplit[1] === 'ip6') {
return 'http://[' + maStrSplit[2] + ']:' + maStrSplit[4]
} else {
throw new Error('invalid multiaddr: ' + ma.toString())
}
} else if (multiaddr.isName(ma)) {
const wsProto = ma.protos()[1].name
if (wsProto === 'ws') {
return 'http://' + maStrSplit[2]
} else if (wsProto === 'wss') {
return 'https://' + maStrSplit[2]
} else {
throw new Error('invalid multiaddr: ' + ma.toString())
}
} else {
if (tcpProto !== 'tcp' || (wsProto !== 'ws' && wsProto !== 'wss')) {
throw new Error('invalid multiaddr: ' + ma.toString())
}

let host = stringTuples[0][1]
if (ipProto === 'ip6') {
host = '[' + host + ']'
}

let proto = wsProto === 'wss' ? 'https' : 'http'
let port =
(wsProto === 'ws' && tcpPort === 80) || (wsProto === 'wss' && tcpPort === 443)
? '' : tcpPort

return proto + '://' + host + (port ? ':' + port : '')
}

const types = {
Expand Down
9 changes: 7 additions & 2 deletions test/listen.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const multiaddr = require('multiaddr')

const WebSocketStar = require('../src')

const skiptravis = process.env.TRAVIS ? it.skip : it
// const skiptravis = process.env.TRAVIS ? it.skip : it

describe('listen', () => {
let ws
Expand Down Expand Up @@ -54,7 +54,12 @@ describe('listen', () => {
})

// travis ci has some ipv6 issues. circle ci is fine.
skiptravis('listen on IPv6 addr', (done) => {
// Also, aegir is failing to propagate the environment variables
// into the browser: https://github.com/ipfs/aegir/issues/177
// ..., which was causing this test to fail.
// Activate this test after the issue is solved.
// skiptravis('listen on IPv6 addr', (done) => {
it.skip('listen on IPv6 addr', (done) => {
const listener = ws.createListener((conn) => {})

listener.listen(mav6, (err) => {
Expand Down
49 changes: 49 additions & 0 deletions test/utils.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* eslint-env mocha */

'use strict'

const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const multiaddr = require('multiaddr')
const cleanUrlSIO = require('../src/utils').cleanUrlSIO

describe('utils', () => {
const modernMultiaddrStringDNS = '/dns4/star-signal.cloud.ipfs.team/tcp/443/wss/p2p-websocket-star/ipfs/QmWxLfixekyv6GAzvDEtXfXjj7gb1z3G8i5aQNHLhw1zA1'

const modernMultiaddrStringDNS2 = '/dns4/star-signal.cloud.ipfs.team/tcp/9999/wss/p2p-websocket-star/ipfs/QmWxLfixekyv6GAzvDEtXfXjj7gb1z3G8i5aQNHLhw1zA1'
const modernMultiaddrStringDNS3 = '/dns4/star-signal.cloud.ipfs.team/tcp/80/ws/p2p-websocket-star/ipfs/QmWxLfixekyv6GAzvDEtXfXjj7gb1z3G8i5aQNHLhw1zA1'
const modernMultiaddrStringDNS4 = '/dns4/star-signal.cloud.ipfs.team/tcp/8080/ws/p2p-websocket-star/ipfs/QmWxLfixekyv6GAzvDEtXfXjj7gb1z3G8i5aQNHLhw1zA1'

const invalidMultiaddrStringDNS = '/dns4/star-signal.cloud.ipfs.team/udp/8080/wss/p2p-websocket-star/ipfs/QmWxLfixekyv6GAzvDEtXfXjj7gb1z3G8i5aQNHLhw1zA1'
const invalidMultiaddrStringDNS2 = '/dns4/star-signal.cloud.ipfs.team/tcp/8080/p2p-websocket-star/ipfs/QmWxLfixekyv6GAzvDEtXfXjj7gb1z3G8i5aQNHLhw1zA1'
const invalidMultiaddrStringDNS3 = '/dns4/star-signal.cloud.ipfs.team/ws/p2p-websocket-star/ipfs/QmWxLfixekyv6GAzvDEtXfXjj7gb1z3G8i5aQNHLhw1zA1'

// Create actual multiaddrs
const modernMultiaddrDNS = multiaddr(modernMultiaddrStringDNS)
const modernMultiaddrDNS2 = multiaddr(modernMultiaddrStringDNS2)
const modernMultiaddrDNS3 = multiaddr(modernMultiaddrStringDNS3)
const modernMultiaddrDNS4 = multiaddr(modernMultiaddrStringDNS4)

const invalidMultiaddrDNS = multiaddr(invalidMultiaddrStringDNS)
const invalidMultiaddrDNS2 = multiaddr(invalidMultiaddrStringDNS2)
const invalidMultiaddrDNS3 = multiaddr(invalidMultiaddrStringDNS3)

it('cleanUrlSIO websocket-star modern', () => {
const newUrlSIOStringDNS = cleanUrlSIO(modernMultiaddrDNS)
const newUrlSIOStringDNS2 = cleanUrlSIO(modernMultiaddrDNS2)
const newUrlSIOStringDNS3 = cleanUrlSIO(modernMultiaddrDNS3)
const newUrlSIOStringDNS4 = cleanUrlSIO(modernMultiaddrDNS4)

expect(() => cleanUrlSIO(modernMultiaddrDNS)).to.not.throw()
expect(() => cleanUrlSIO(invalidMultiaddrDNS)).to.throw(Error, 'invalid multiaddr')
expect(() => cleanUrlSIO(invalidMultiaddrDNS2)).to.throw(Error, 'invalid multiaddr')
expect(() => cleanUrlSIO(invalidMultiaddrDNS3)).to.throw(Error, 'invalid multiaddr')

expect(newUrlSIOStringDNS).to.equal('https://star-signal.cloud.ipfs.team')
expect(newUrlSIOStringDNS2).to.equal('https://star-signal.cloud.ipfs.team:9999')
expect(newUrlSIOStringDNS3).to.equal('http://star-signal.cloud.ipfs.team')
expect(newUrlSIOStringDNS4).to.equal('http://star-signal.cloud.ipfs.team:8080')
})
})

0 comments on commit 92af753

Please sign in to comment.