diff --git a/lib/common-zero.js b/lib/common-zero.js index 8c89eec..bbcafdd 100644 --- a/lib/common-zero.js +++ b/lib/common-zero.js @@ -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', @@ -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, @@ -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 diff --git a/lib/server/parse-zero.js b/lib/server/parse-zero.js index 0b556e5..970f7cc 100644 --- a/lib/server/parse-zero.js +++ b/lib/server/parse-zero.js @@ -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 } } @@ -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 @@ -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: [], @@ -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 }) }) })