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

Commit 46de6e7

Browse files
committed
feat: adding passive/active dialing test
1 parent 1abdeef commit 46de6e7

File tree

1 file changed

+37
-53
lines changed

1 file changed

+37
-53
lines changed

src/circuit/relay.js

Lines changed: 37 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ const once = require('once')
1313

1414
const multicodec = require('./../multicodec')
1515

16-
const log = debug('libp2p:circuit:relay')
17-
log.err = debug('libp2p:circuit:error:relay')
16+
const log = debug('swarm:circuit:relay')
17+
log.err = debug('swarm:circuit:error:relay')
1818

1919
class Relay extends EE {
2020

@@ -29,21 +29,22 @@ class Relay extends EE {
2929
*/
3030
constructor (options) {
3131
super()
32-
this.config = options
33-
this.libp2p
32+
this.config = Object.assign(options, {Active: true})
33+
this.swarm = null
34+
this.active = options.Active
3435
}
3536

3637
/**
3738
* Mount the relay
38-
* @param {libp2p} libp2p
39+
* @param {swarm} swarm
3940
*
4041
* @return {void}
4142
*/
42-
mount (libp2p) {
43-
this.libp2p = libp2p
44-
this.libp2p.handle(multicodec.hop, (proto, conn) => {
45-
let stream = handshake({timeout: 1000 * 60})
46-
let shake = stream.handshake
43+
mount (swarm) {
44+
this.swarm = swarm
45+
this.swarm.handle(multicodec.hop, (proto, conn) => {
46+
const stream = handshake({timeout: 1000 * 60})
47+
const shake = stream.handshake
4748

4849
pull(
4950
stream,
@@ -55,66 +56,49 @@ class Relay extends EE {
5556
if (err) {
5657
log.err(err)
5758

59+
// TODO: pull-length-prefixed should probably return an `Error` object with a error code
5860
if (typeof err === 'string' && err.includes('size longer than max permitted length of')) {
59-
this.emit('error', String(constants.RESPONSE.HOP.DST_ADDR_TOO_LONG))
60-
shake.write(String(constants.RESPONSE.HOP.DST_ADDR_TOO_LONG))
61+
this.emit('circuit:error', String(constants.RESPONSE.HOP.DST_ADDR_TOO_LONG))
62+
shake.write(new Buffer(String(constants.RESPONSE.HOP.DST_ADDR_TOO_LONG)))
6163
return
6264
}
6365

64-
shake.write(String(constants.RESPONSE.FAILURE))
66+
shake.write(new Buffer(String(constants.RESPONSE.FAILURE)))
67+
return
68+
}
69+
70+
let addr
71+
try {
72+
addr = multiaddr(msg.toString()) // read the src multiaddr
73+
} catch (err) {
74+
this.emit('circuit:error', constants.RESPONSE.HOP.DST_MULTIADDR_INVALID)
75+
shake.write(new Buffer(String(constants.RESPONSE.HOP.DST_MULTIADDR_INVALID)))
76+
return
77+
}
78+
79+
if (addr.getPeerId() === this.swarm._peerInfo.id.toB58String()) {
80+
this.emit('circuit:error', constants.RESPONSE.HOP.CANT_CONNECT_TO_SELF)
81+
shake.write(new Buffer(String(constants.RESPONSE.HOP.CANT_CONNECT_TO_SELF)))
6582
return
6683
}
6784

68-
let addr = multiaddr(msg.toString()) // read the src multiaddr
69-
if (addr.getPeerId() === this.libp2p.peerInfo.id.toB58String()) {
70-
this.emit('error', String(constants.RESPONSE.HOP.CANT_CONNECT_TO_SELF))
71-
shake.write(String(constants.RESPONSE.HOP.CANT_CONNECT_TO_SELF))
85+
if (!this.active && !this.swarm.conns[addr.getPeerId()]) {
86+
this.emit('circuit:error', constants.RESPONSE.HOP.NO_CONN_TO_DST)
87+
shake.write(new Buffer(String(constants.RESPONSE.HOP.NO_CONN_TO_DST)))
7288
return
7389
}
7490

7591
this.circuit(shake.rest(), addr, (err) => {
7692
if (err) {
7793
log.err(err)
78-
this.emit('error', err)
94+
this.emit('circuit:error', err)
7995
}
8096

81-
this.emit('circuit')
97+
this.emit('circuit:success')
8298
})
8399
})
84100
})
85-
}
86-
87-
/**
88-
* Write error message to conn
89-
*
90-
* @param {Connection} conn
91-
* @param {Number} errCode
92-
* @param {Function} cb
93-
* @return {void}
94-
*/
95-
writeErr (conn, errCode, cb) {
96-
let stream = handshake({timeout: 1000 * 60}, cb)
97-
let shake = stream.handshake
98-
99-
// create handshake stream
100-
pull(
101-
stream,
102-
conn,
103-
stream
104-
)
105-
106-
pull(
107-
pull.values([errCode]),
108-
lp.encode(),
109-
pull.collect((err, encoded) => {
110-
if (err) {
111-
return cb(err)
112-
}
113-
114-
shake.write(encoded[0])
115-
cb()
116-
})
117-
)
101+
this.emit('mounted')
118102
}
119103

120104
/**
@@ -175,7 +159,7 @@ class Relay extends EE {
175159
_dialPeer (ma, callback) {
176160
const peerInfo = new PeerInfo(PeerId.createFromB58String(ma.getPeerId()))
177161
peerInfo.multiaddrs.add(ma)
178-
this.libp2p.dial(peerInfo, multicodec.stop, once((err, conn) => {
162+
this.swarm.dial(peerInfo, multicodec.stop, once((err, conn) => {
179163
if (err) {
180164
log.err(err)
181165
return callback(err)

0 commit comments

Comments
 (0)