Skip to content
This repository was archived by the owner on Aug 23, 2019. It is now read-only.

[WIP] feat: adding relay #181

Merged
merged 2 commits into from
Mar 27, 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
8 changes: 8 additions & 0 deletions src/connection.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const identify = require('libp2p-identify')
const circuit = require('libp2p-circuit').Listener
const multistream = require('multistream-select')
const waterfall = require('async/waterfall')
const debug = require('debug')
Expand Down Expand Up @@ -75,6 +76,13 @@ module.exports = function connection (swarm) {
})
},

relay () {
swarm.relay = true
circuit.listener(swarm).on('connection', (conn) => {
protocolMuxer(swarm.protocols, conn)
})
},

crypto (tag, encrypt) {
if (!tag && !encrypt) {
tag = plaintext.tag
Expand Down
11 changes: 9 additions & 2 deletions src/dial.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,17 @@ module.exports = function dial (swarm) {
}

function attemptDial (pi, cb) {
const tKeys = swarm.availableTransports(pi)
let tKeys = swarm.availableTransports(pi)

if (tKeys.length === 0) {
return cb(new Error('No available transport to dial to'))
if (this.relay) {
// force relay if it's enabled
tKeys = ['Circuit']
// create a generic relay addres
pi.multiaddrs.addSafe(`/p2p-circuit/ipfs/${pi.peerInfo.id}`)
} else {
return cb(new Error('No available transport to dial to'))
}
}

nextTransport(tKeys.shift())
Expand Down
19 changes: 15 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ function Swarm (peerInfo) {
// is the Identify protocol enabled?
this.identify = false

// is relay enabled?
this.relay = false

// Crypto details
this.crypto = plaintext

Expand All @@ -72,7 +75,10 @@ function Swarm (peerInfo) {

if (includes(addr.protoNames(), 'ipfs')) {
return addr.decapsulate('ipfs')
} else if (includes(addr.protoNames(), 'p2p-circuit')) {
return addr.decapsulate('p2p-circuit')
}

return addr
})

Expand Down Expand Up @@ -135,11 +141,16 @@ function Swarm (peerInfo) {
cb()
})
}, cb),
// (cb) => {
// each(this.transports, (transport, cb) => {
// each(transport.listeners, (listener, cb) => {
// listener.close(cb)
// }, cb)
// }, cb)
// },
(cb) => {
each(this.transports, (transport, cb) => {
each(transport.listeners, (listener, cb) => {
listener.close(cb)
}, cb)
each(Object.keys(this.transports), (key, cb) => {
this.transport.close(key, cb)
}, cb)
}
], callback)
Expand Down