Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add full onion support (#2) #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
20 changes: 16 additions & 4 deletions lib/common-zero.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@
const arrayOrNull = v => Array.isArray(v) || v == null
const delta = (a, b) => a - b === 0 ? 0 : a - b < 0 ? b - a : a - b

module.exports.def = {
const assert = require('assert')
// quick tests.
assert(delta(0, 2) === 2)
assert(delta(3, -4) === 7)
assert(delta(0, 0) === 0)

const Zero = module.exports

Zero.def = {
in: {
protobuf: {
1: 'repeated string hashes',
2: 'repeated bytes onion_signs',
3: 'repeated string onions',
4: 'int64 onion_sign_this',
4: 'string onion_sign_this',
5: 'repeated string add',
6: 'int32 need_num',
7: 'repeated string need_types',
Expand All @@ -19,7 +27,7 @@ module.exports.def = {
strict: {
onions: arrayOrNull,
onion_signs: arrayOrNull,
onion_sign_this: [v => v == null, v => delta(Date.getTime(), v * 1000) < 3 * 60 * 1000 && typeof v === 'number'],
onion_sign_this: [v => v == null, v => typeof v === 'string' && !isNaN(parseInt(v, 10)) && delta(Zero.pyTime(), parseInt(v, 10)) < 3 * 60],
add: arrayOrNull,
need_num: 'number',
need_types: arrayOrNull,
Expand All @@ -29,10 +37,14 @@ module.exports.def = {
}
}

module.exports.fakeZeroNet = () => {
Zero.fakeZeroNet = () => {
return {
rev: '0',
version: 'v0',
swarm: {}
}
}

Zero.pyTime = () => Math.floor(new Date().getTime() / 1000)

Zero.delta = delta
30 changes: 27 additions & 3 deletions lib/server/parse-zero.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,29 @@ module.exports = function parseZero (swarm, server) {
})
},
onion: cb => {
/*
if "onion_signs" in params and len(params["onion_signs"]) == len(set(params["onions"])):
# Check if all sign is correct
if time.time() - float(params["onion_sign_this"]) < 3*60: # Peer has 3 minute to sign the message
onions_signed = []
# Check onion signs
for onion_publickey, onion_sign in params["onion_signs"].items():
if CryptRsa.verify(params["onion_sign_this"], onion_publickey, onion_sign):
onions_signed.append(CryptRsa.publickeyToOnion(onion_publickey))
else:
break
# Check if the same onion addresses signed as the announced onces
if sorted(onions_signed) == sorted(set(params["onions"])):
all_onions_signed = True
else:
all_onions_signed = False
else:
# Onion sign this out of 3 minute
all_onions_signed = False
else:
# Incorrect signs number
all_onions_signed = False
*/
cb() // TODO: add
}
}
Expand All @@ -138,7 +161,7 @@ module.exports = function parseZero (swarm, server) {
parallel(hashes.map(h => cb => {
getSwarm(h, (err, swarm) => {
if (err) return cb(err)
announce(swarm, peer, data.need_types || [], needNum, (err, peers, needSign) => {
announce(swarm, peer, data.need_types || [], needNum, (err, peers, needSign) => { // announce all peers for hash and save response
if (err) return cb(err)
onionNeedSign = onionNeedSign || needSign
hash2peer[h] = peers
Expand All @@ -148,7 +171,7 @@ module.exports = function parseZero (swarm, server) {
}), err => {
if (err) log(err)
if (err) return cb(err)
let res = {
let res = { // pack hash2peer
peers: hashes.map(h => {
let r = {
ip4: [],
Expand All @@ -165,7 +188,8 @@ module.exports = function parseZero (swarm, server) {
})
}
// console.log(res)
cb(null, res)
if (onionNeedSign) res.onion_sign_this = common.pyTime().toString() // py4a + floats = nightmare fuel
cb(null, res) // finishes zeronet cmd
})
})
})
Expand Down