Skip to content

Commit 3221fcc

Browse files
committed
Fix even more bugs
1 parent 1b69642 commit 3221fcc

7 files changed

Lines changed: 85 additions & 29 deletions

File tree

debug/libp2p.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const ZeroNetNode = require("zeronet-swarm")
22
const PeerId = require("peer-id")
3+
const multiaddr=require("multiaddr")
34
//const Client = require("zeronet-client")
45

56
PeerId.create((e, id) => {
@@ -17,7 +18,7 @@ PeerId.create((e, id) => {
1718
}, err => {
1819
if (err) throw err
1920

20-
node.dial(node.peerInfo, (e, c) => {
21+
node.dial(/*node.peerInfo*/multiaddr("/ip4/127.0.0.1/tcp/15543/"), (e, c) => {
2122
c.client.cmd.getFile({
2223
site: "1HeLLo4uzjaLetFx6NH3PMwFP3qbRbTf3D",
2324
inner_path: "content.json",

zeronet-crypto/protocol.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,14 @@ module.exports = function ZeroNetCrypto(protocol) {
3838
if (!me.index[crypto]) return cb(new Error("Unsupported crypto " + crypto))
3939
log("upgrading to crypto", crypto)
4040
conf.crypto = crypto
41-
me.index[crypto].getSocket(client.getRaw(), conf, (err, conn) => {
41+
client.getRaw((err, conn) => {
4242
if (err) return cb(err)
43-
protocol.upgradeConn(conf)(conn, (err, client) => {
43+
me.index[crypto].getSocket(conn, conf, (err, conn) => {
4444
if (err) return cb(err)
45-
return cb(null, client)
45+
protocol.upgradeConn(conf)(conn, (err, client) => {
46+
if (err) return cb(err)
47+
return cb(null, client)
48+
})
4649
})
4750
})
4851
}

zeronet-protocol/client.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,18 @@ const pull = require('pull-stream')
99
const Pushable = require('pull-pushable')
1010
const Readable = require("stream").Readable
1111

12+
const debug = require("debug")
13+
1214
module.exports = function Client(conn, protocol, zeronet, opt) {
1315
const self = this
16+
const log = debug("zeronet:protocol:client")
1417

1518
/* Handling */
1619

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

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

4248
self.write = d => {
49+
log("sent data", addrs, d)
4350
p.json(d)
4451
}
4552

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

6572
m.on("msg", data => {
73+
log("got data", addrs, data)
6674
if (data.cmd == "response") {
6775
handleResponse(data)
6876
} else {
@@ -86,4 +94,16 @@ module.exports = function Client(conn, protocol, zeronet, opt) {
8694
pull.drain(() => {})
8795
)
8896

97+
/* getRaw */
98+
99+
self.getRaw = cb => {
100+
try {
101+
//p.destroy()
102+
r.destroy()
103+
} catch (e) {
104+
cb(e)
105+
}
106+
cb(null, conn)
107+
}
108+
89109
}

zeronet-protocol/defaults.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ module.exports = function Defaults(protocol, zeronet) {
99
body: Buffer.isBuffer,
1010
location: "number",
1111
size: "number"
12+
}, (data, cb) => {
13+
if (!zeronet.zites[data.zite]) return cb(new Error("Unknown site"))
14+
//TODO: finish
1215
})
1316

1417
protocol.handle("ping", {}, {
@@ -58,10 +61,4 @@ module.exports = function Defaults(protocol, zeronet) {
5861
peers: "object",
5962
peers_onion: "object"
6063
})
61-
62-
protocol.handle("ping", {}, {
63-
body: b => b == "pong"
64-
}, (data, cb) => cb({
65-
body: "pong"
66-
}))
6764
}

zeronet-protocol/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const PeerRequestHandler = require(__dirname + "/peer-request-handler")
55

66
const Defaults = require(__dirname + "/defaults")
77
const Crypto = require("zeronet-crypto/protocol")
8+
//const Crypto_secio = require("zeronet-crypto/secio")
89
const debug = require("debug")
910

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

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

5254
Crypto(self)
55+
//Crypto_secio(self, zeronet)
5356

5457
}

zeronet-protocol/peer-request-handler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module.exports = function PeerRequestHandler(name, _req, client, handler) {
1414
to: req_id
1515
}
1616
if (err) {
17-
data.error = err instanceof Error ? err.toString().split("\n")[0] : err.toString()
17+
resp.error = err instanceof Error ? err.toString().split("\n")[0] : err.toString()
1818
} else {
1919
for (var p in _req.defOut)
2020
resp[p] = res[p]

zeronet-swarm/dial/index.js

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,32 @@
33
const Connection = require('interface-connection').Connection
44
const debug = require('debug')
55
const log = debug('zeronet:dial')
6+
const crypto = require("crypto")
7+
8+
const sha5 = text => crypto.createHash('sha512').update(text).digest('hex')
9+
const multiaddr = require("multiaddr")
10+
const Id = require("peer-id")
11+
const Peer = require('peer-info')
12+
13+
const BASE58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
14+
const bs58 = require('base-x')(BASE58)
615

716
module.exports = function dial(swarm, ZProtocol) {
17+
function getId(pi, cb) {
18+
if (multiaddr.isMultiaddr(pi)) {
19+
const ad = pi.toString()
20+
const md = pi
21+
let pid = Id.createFromB58String("Q" + bs58.encode(sha5(ad).substr(0, 34)))
22+
Peer.create(pid, (err, pi) => {
23+
if (err) return cb(err)
24+
pi.multiaddrs.add(md)
25+
return cb(null, pi)
26+
})
27+
} else if (Peer.isPeerInfo(pi)) {
28+
return cb(null, pi)
29+
}
30+
}
31+
832
return (pi, protocol, callback) => {
933
if (typeof protocol === 'function') {
1034
callback = protocol
@@ -15,25 +39,33 @@ module.exports = function dial(swarm, ZProtocol) {
1539

1640
const proxyConn = new Connection()
1741

18-
const b58Id = pi.id.toB58String()
19-
log('dialing %s', b58Id)
20-
21-
if (!swarm.conns[b58Id]) {
22-
attemptDial(pi, (err, conn) => {
23-
if (err) {
24-
return callback(err)
25-
}
26-
conn.setPeerInfo(pi)
27-
protocolLayer(conn, err => {
28-
if (err) return callback(err)
29-
swarm.conns[b58Id] = conn
30-
return callback(null, conn)
42+
getId(pi, (err, _pi) => {
43+
if (err) {
44+
return callback(err)
45+
}
46+
47+
pi = _pi
48+
49+
const b58Id = pi.id.toB58String()
50+
log('dialing %s', b58Id)
51+
52+
if (!swarm.conns[b58Id]) {
53+
attemptDial(pi, (err, conn) => {
54+
if (err) {
55+
return callback(err)
56+
}
57+
conn.setPeerInfo(pi)
58+
protocolLayer(conn, err => {
59+
if (err) return callback(err)
60+
swarm.conns[b58Id] = conn
61+
return callback(null, conn)
62+
})
3163
})
32-
})
33-
} else {
34-
const conn = swarm.conns[b58Id]
35-
return callback(null, conn.client)
36-
}
64+
} else {
65+
const conn = swarm.conns[b58Id]
66+
return callback(null, conn.client)
67+
}
68+
})
3769

3870
return proxyConn
3971

0 commit comments

Comments
 (0)