Skip to content

Commit

Permalink
Fix even more bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
mkg20001 committed Jul 2, 2017
1 parent 1b69642 commit 3221fcc
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 29 deletions.
3 changes: 2 additions & 1 deletion debug/libp2p.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const ZeroNetNode = require("zeronet-swarm")
const PeerId = require("peer-id")
const multiaddr=require("multiaddr")
//const Client = require("zeronet-client")

PeerId.create((e, id) => {
Expand All @@ -17,7 +18,7 @@ PeerId.create((e, id) => {
}, err => {
if (err) throw err

node.dial(node.peerInfo, (e, c) => {
node.dial(/*node.peerInfo*/multiaddr("/ip4/127.0.0.1/tcp/15543/"), (e, c) => {
c.client.cmd.getFile({
site: "1HeLLo4uzjaLetFx6NH3PMwFP3qbRbTf3D",
inner_path: "content.json",
Expand Down
9 changes: 6 additions & 3 deletions zeronet-crypto/protocol.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,14 @@ module.exports = function ZeroNetCrypto(protocol) {
if (!me.index[crypto]) return cb(new Error("Unsupported crypto " + crypto))
log("upgrading to crypto", crypto)
conf.crypto = crypto
me.index[crypto].getSocket(client.getRaw(), conf, (err, conn) => {
client.getRaw((err, conn) => {
if (err) return cb(err)
protocol.upgradeConn(conf)(conn, (err, client) => {
me.index[crypto].getSocket(conn, conf, (err, conn) => {
if (err) return cb(err)
return cb(null, client)
protocol.upgradeConn(conf)(conn, (err, client) => {
if (err) return cb(err)
return cb(null, client)
})
})
})
}
Expand Down
20 changes: 20 additions & 0 deletions zeronet-protocol/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@ const pull = require('pull-stream')
const Pushable = require('pull-pushable')
const Readable = require("stream").Readable

const debug = require("debug")

module.exports = function Client(conn, protocol, zeronet, opt) {
const self = this
const log = debug("zeronet:protocol:client")

/* Handling */

const handlers = self.handlers = protocol.getHandlers(self)
let addrs
conn.getObservedAddrs((e, a) => addrs = (opt.isServer ? "=> " : "<= ") + a.map(a => a.toString()).join(", "))
log("initializing", addrs)

function handleIn(data) {
if (handlers[data.cmd]) handlers[data.cmd].recv(data)
Expand All @@ -40,6 +46,7 @@ module.exports = function Client(conn, protocol, zeronet, opt) {
self.addCallback = addCallback

self.write = d => {
log("sent data", addrs, d)
p.json(d)
}

Expand All @@ -63,6 +70,7 @@ module.exports = function Client(conn, protocol, zeronet, opt) {
const m = msgstream(r)

m.on("msg", data => {
log("got data", addrs, data)
if (data.cmd == "response") {
handleResponse(data)
} else {
Expand All @@ -86,4 +94,16 @@ module.exports = function Client(conn, protocol, zeronet, opt) {
pull.drain(() => {})
)

/* getRaw */

self.getRaw = cb => {
try {
//p.destroy()
r.destroy()
} catch (e) {
cb(e)
}
cb(null, conn)
}

}
9 changes: 3 additions & 6 deletions zeronet-protocol/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ module.exports = function Defaults(protocol, zeronet) {
body: Buffer.isBuffer,
location: "number",
size: "number"
}, (data, cb) => {
if (!zeronet.zites[data.zite]) return cb(new Error("Unknown site"))
//TODO: finish
})

protocol.handle("ping", {}, {
Expand Down Expand Up @@ -58,10 +61,4 @@ module.exports = function Defaults(protocol, zeronet) {
peers: "object",
peers_onion: "object"
})

protocol.handle("ping", {}, {
body: b => b == "pong"
}, (data, cb) => cb({
body: "pong"
}))
}
3 changes: 3 additions & 0 deletions zeronet-protocol/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const PeerRequestHandler = require(__dirname + "/peer-request-handler")

const Defaults = require(__dirname + "/defaults")
const Crypto = require("zeronet-crypto/protocol")
//const Crypto_secio = require("zeronet-crypto/secio")
const debug = require("debug")

module.exports = function Protocol(swarm, node, zeronet) {
Expand All @@ -26,6 +27,7 @@ module.exports = function Protocol(swarm, node, zeronet) {
}

self.handle = (name, def, defret, cb) => {
if (commands[name]) throw new Error(name + " is already handled")
log("Handling", name)
commands[name] = new PeerRequest(name, def, defret)
handlers[name] = cb
Expand All @@ -50,5 +52,6 @@ module.exports = function Protocol(swarm, node, zeronet) {
}

Crypto(self)
//Crypto_secio(self, zeronet)

}
2 changes: 1 addition & 1 deletion zeronet-protocol/peer-request-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = function PeerRequestHandler(name, _req, client, handler) {
to: req_id
}
if (err) {
data.error = err instanceof Error ? err.toString().split("\n")[0] : err.toString()
resp.error = err instanceof Error ? err.toString().split("\n")[0] : err.toString()
} else {
for (var p in _req.defOut)
resp[p] = res[p]
Expand Down
68 changes: 50 additions & 18 deletions zeronet-swarm/dial/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,32 @@
const Connection = require('interface-connection').Connection
const debug = require('debug')
const log = debug('zeronet:dial')
const crypto = require("crypto")

const sha5 = text => crypto.createHash('sha512').update(text).digest('hex')
const multiaddr = require("multiaddr")
const Id = require("peer-id")
const Peer = require('peer-info')

const BASE58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
const bs58 = require('base-x')(BASE58)

module.exports = function dial(swarm, ZProtocol) {
function getId(pi, cb) {
if (multiaddr.isMultiaddr(pi)) {
const ad = pi.toString()
const md = pi
let pid = Id.createFromB58String("Q" + bs58.encode(sha5(ad).substr(0, 34)))
Peer.create(pid, (err, pi) => {
if (err) return cb(err)
pi.multiaddrs.add(md)
return cb(null, pi)
})
} else if (Peer.isPeerInfo(pi)) {
return cb(null, pi)
}
}

return (pi, protocol, callback) => {
if (typeof protocol === 'function') {
callback = protocol
Expand All @@ -15,25 +39,33 @@ module.exports = function dial(swarm, ZProtocol) {

const proxyConn = new Connection()

const b58Id = pi.id.toB58String()
log('dialing %s', b58Id)

if (!swarm.conns[b58Id]) {
attemptDial(pi, (err, conn) => {
if (err) {
return callback(err)
}
conn.setPeerInfo(pi)
protocolLayer(conn, err => {
if (err) return callback(err)
swarm.conns[b58Id] = conn
return callback(null, conn)
getId(pi, (err, _pi) => {
if (err) {
return callback(err)
}

pi = _pi

const b58Id = pi.id.toB58String()
log('dialing %s', b58Id)

if (!swarm.conns[b58Id]) {
attemptDial(pi, (err, conn) => {
if (err) {
return callback(err)
}
conn.setPeerInfo(pi)
protocolLayer(conn, err => {
if (err) return callback(err)
swarm.conns[b58Id] = conn
return callback(null, conn)
})
})
})
} else {
const conn = swarm.conns[b58Id]
return callback(null, conn.client)
}
} else {
const conn = swarm.conns[b58Id]
return callback(null, conn.client)
}
})

return proxyConn

Expand Down

0 comments on commit 3221fcc

Please sign in to comment.