diff --git a/circle.yml b/circle.yml index 4e1698a..2c57d11 100644 --- a/circle.yml +++ b/circle.yml @@ -1,3 +1,4 @@ +# Warning: This file is automatically synced from https://github.com/ipfs/ci-sync so if you want to change it, please change it there and ask someone to sync all repositories. machine: node: version: stable @@ -6,12 +7,8 @@ dependencies: pre: - google-chrome --version - curl -L -o google-chrome.deb https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb - - for v in $(curl http://archive.ubuntu.com/ubuntu/pool/main/n/nss/ | grep "href=" | grep "libnss3.*deb\"" -o | grep -o "libnss3.*deb" | grep "3.28" | grep "14.04"); do curl -L -o $v http://archive.ubuntu.com/ubuntu/pool/main/n/nss/$v; done && rm libnss3-tools*_i386.deb libnss3-dev*_i386.deb - sudo dpkg -i google-chrome.deb || true - - sudo dpkg -i libnss3*.deb || true - sudo apt-get update - - sudo apt-get install -f || true - - sudo dpkg -i libnss3*.deb - sudo apt-get install -f - sudo apt-get install --only-upgrade lsb-base - sudo dpkg -i google-chrome.deb diff --git a/package.json b/package.json index 890c24e..bc269ad 100644 --- a/package.json +++ b/package.json @@ -58,15 +58,15 @@ "async": "^2.6.0", "debug": "^3.1.0", "interface-connection": "~0.3.2", - "ip-address": "^5.8.8", + "ip-address": "^5.8.9", "libp2p-circuit": "~0.1.4", "libp2p-identify": "~0.6.1", "lodash.includes": "^4.3.0", "multiaddr": "^3.0.1", "multistream-select": "~0.14.1", "once": "^1.4.0", - "peer-id": "~0.10.2", - "peer-info": "~0.11.1", + "peer-id": "~0.10.3", + "peer-info": "~0.11.3", "pull-stream": "^3.6.1" }, "contributors": [ diff --git a/src/dial.js b/src/dial.js index c90a136..e6065e8 100644 --- a/src/dial.js +++ b/src/dial.js @@ -81,28 +81,36 @@ function dial (swarm) { } function attemptDial (pi, cb) { - const tKeys = swarm.availableTransports(pi) - - if (tKeys.length === 0) { + if (!swarm.hasTransports()) { return cb(new Error('No transports registered, dial not possible')) } + const tKeys = swarm.availableTransports(pi) + + let circuitTried = false nextTransport(tKeys.shift()) function nextTransport (key) { - if (!key) { - return dialCircuit((err, circuit) => { - if (err) { - return cb(new Error('Could not dial in any of the transports or relays')) - } + let transport = key + if (!transport) { + if (circuitTried) { + return cb(new Error(`Circuit already tried!`)) + } - cb(null, circuit) - }) + if (!swarm.transports[Circuit.tag]) { + return cb(new Error(`Circuit not enabled!`)) + } + + log(`Falling back to dialing over circuit`) + pi.multiaddrs.add(`/p2p-circuit/ipfs/${pi.id.toB58String()}`) + circuitTried = true + transport = Circuit.tag } - log(`dialing transport ${key}`) - swarm.transport.dial(key, pi, (err, conn) => { + log(`dialing transport ${transport}`) + swarm.transport.dial(transport, pi, (err, conn) => { if (err) { + log(err) return nextTransport(tKeys.shift()) } @@ -131,23 +139,6 @@ function dial (swarm) { } } - function dialCircuit (cb) { - log(`Falling back to dialing over circuit`) - pi.multiaddrs.add(`/p2p-circuit/ipfs/${pi.id.toB58String()}`) - if (!swarm.transports[Circuit.tag]) { - return cb(new Error(`Circuit not enabled!`)) - } - - swarm.transport.dial(Circuit.tag, pi, (err, conn) => { - if (err) { - log(err) - return cb(err) - } - - cb(null, conn) - }) - } - function attemptMuxerUpgrade (conn, cb) { const muxers = Object.keys(swarm.muxers) if (muxers.length === 0) { diff --git a/src/index.js b/src/index.js index 0d17a8e..6d413ca 100644 --- a/src/index.js +++ b/src/index.js @@ -59,6 +59,11 @@ function Swarm (peerInfo, peerBook) { this.transport = transport(this) this.connection = connection(this) + this.hasTransports = () => { + const transports = Object.keys(this.transports).filter((t) => t !== 'Circuit') + return transports && transports.length > 0 + } + this.availableTransports = (pi) => { const myAddrs = pi.multiaddrs.toArray() const myTransports = Object.keys(this.transports) @@ -67,7 +72,7 @@ function Swarm (peerInfo, peerBook) { return myTransports.filter((ts) => this.transports[ts].filter(myAddrs).length > 0) // push Circuit to be the last proto to be dialed .sort((a) => { - return a === 'Circuit' ? -1 : 0 + return a === 'Circuit' ? 1 : 0 }) } diff --git a/test/circuit.js b/test/circuit.js index b65a337..2fb7498 100644 --- a/test/circuit.js +++ b/test/circuit.js @@ -89,6 +89,18 @@ describe(`circuit`, function () { }) }) + it(`should dial circuit ony once`, function (done) { + peerA.multiaddrs.clear() + peerA.multiaddrs.add(`/dns4/wrtc-star.discovery.libp2p.io/tcp/443/wss/p2p-webrtc-star`) + swarmA.dial(peerC, (err, conn) => { + expect(err).to.exist() + expect(err).to.match(/Circuit already tried!/) + expect(conn).to.not.exist() + expect(dialSpyA.callCount).to.be.eql(1) + done() + }) + }) + it(`should dial circuit last`, function (done) { peerC.multiaddrs.clear() peerC.multiaddrs.add(`/p2p-circuit/ipfs/ABCD`)