diff --git a/src/client/index.js b/src/client/index.js index f2de817..2fd5349 100644 --- a/src/client/index.js +++ b/src/client/index.js @@ -76,9 +76,26 @@ class Connection { } async dial (addr) { - /* - - TODO: get peer id, open new conn via muxed, do handshake, do dial, forward - */ + const id = addr.getPeerId() + const _id = ID.createFromB58String(id)._id + + const conn = await prom(cb => this.muxed.newStream(cb)) + + const stream = handshake() + pull( + conn, + stream, + conn + ) + + const shake = stream.handshake + const rpc = LP.wrap(shake, {push: shake.write}) + + rpc.writeProto(DialRequest, {target: _id}) + const {error} = await rpc.readProto(DialResponse) + if (error) { translateAndThrow(error) } + + return shake.rest() } } diff --git a/src/micro-switch/index.js b/src/micro-switch/index.js index 70f9958..65c1549 100644 --- a/src/micro-switch/index.js +++ b/src/micro-switch/index.js @@ -105,7 +105,7 @@ class MicroSwitch { if (err) { reject(err) } else { - resolve(address) + resolve(listener) } }) })) diff --git a/src/server/index.js b/src/server/index.js index 8c6a7ef..5544635 100644 --- a/src/server/index.js +++ b/src/server/index.js @@ -119,6 +119,10 @@ class Server { async start () { await this.switch.startListen() } + + async stop () { + await this.switch.stopListen() + } } module.exports = Server