From 111953a1bfdffaea80c207a874ff71ced6dbea87 Mon Sep 17 00:00:00 2001 From: Mark Nadal Date: Tue, 15 Jan 2019 10:51:48 -0800 Subject: [PATCH] patch + update `.mesh` to _ --- gun.js | 21 ++++++------- lib/store.js | 9 ++---- lib/webrtc.js | 28 ++++++++--------- nts.js | 4 +-- package-lock.json | 2 +- package.json | 2 +- sea.js | 77 ++++++++++++++++++++--------------------------- 7 files changed, 61 insertions(+), 82 deletions(-) diff --git a/gun.js b/gun.js index cb8c132d..51d96263 100644 --- a/gun.js +++ b/gun.js @@ -959,7 +959,6 @@ function output(msg){ var put, get, at = this.as, back = at.back, root = at.root, tmp; - if(!msg.I){ msg.I = at.$ } if(!msg.$){ msg.$ = at.$ } this.to.next(msg); if(get = msg.get){ @@ -1215,7 +1214,6 @@ at.on('in', {get: at.get, put: Gun.val.link.ify(get['#']), $: at.$, '@': msg['@']}); return; } - msg.$ = at.root.$; Gun.on.put(msg, at.root.$); } var empty = {}, u; @@ -1312,7 +1310,7 @@ //else if(!cat.async && msg.put !== at.put && root.stop && root.stop[at.id]){ return } root.stop && (root.stop[at.id] = true); - //root.stop && (root.stop.ID = root.stop.ID || Gun.text.random(2)); + //root.stop && (root.stop.id = root.stop.id || Gun.text.random(2)); //if((tmp = root.stop) && (tmp = tmp[at.id] || (tmp[at.id] = {})) && tmp[cat.id]){ return } tmp && (tmp[cat.id] = true); if(eve.seen && at.id && eve.seen[at.id]){ return eve.to.next(msg) } //if((tmp = root.stop)){ if(tmp[at.id]){ return } tmp[at.id] = msg.root; } // temporary fix till a better solution? @@ -1812,7 +1810,7 @@ root.on('out', function(msg){ if(msg.lS){ return } - if(msg.I && msg.put && !msg['@'] && !empty(opt.peers)){ + if(Gun.is(msg.$) && msg.put && !msg['@'] && !empty(opt.peers)){ id = msg['#']; Gun.graph.is(msg.put, null, map); if(!to){ to = setTimeout(flush, opt.wait || 1) } @@ -1888,7 +1886,7 @@ return; // Hmm, what if we have peers but we are disconnected? } //console.log("lS get", lex, data); - root.on('in', {'@': msg['#'], put: Gun.graph.node(data), how: 'lS', lS: msg.I}); + root.on('in', {'@': msg['#'], put: Gun.graph.node(data), how: 'lS', lS: msg.$ || root.$}); }; Gun.debug? setTimeout(to,1) : to(); }); @@ -1938,8 +1936,8 @@ if((tmp = msg['@']) && (tmp = ctx.dup.s[tmp]) && (tmp = tmp.it) - && tmp.mesh){ - mesh.say(msg, tmp.mesh.via, 1); + && tmp._){ + mesh.say(msg, (tmp._).via, 1); tmp['##'] = msg['##']; return; } @@ -1971,9 +1969,9 @@ (tmp = dup.s)[hash] = tmp[id]; } } - (msg.mesh = function(){}).via = peer; + (msg._ = function(){}).via = peer; if((tmp = msg['><'])){ - msg.mesh.to = Type.obj.map(tmp.split(','), function(k,i,m){m(k,true)}); + (msg._).to = Type.obj.map(tmp.split(','), function(k,i,m){m(k,true)}); } if(msg.dam){ if(tmp = mesh.hear[msg.dam]){ @@ -1981,7 +1979,6 @@ } return; } - ctx.on('in', msg); return; @@ -2012,7 +2009,7 @@ } var tmp, wire = peer.wire || ((opt.wire) && opt.wire(peer)), msh, raw;// || open(peer, ctx); // TODO: Reopen! if(!wire){ return } - msh = msg.mesh || empty; + msh = (msg._) || empty; if(peer === msh.via){ return } if(!(raw = msh.raw)){ raw = mesh.raw(msg) } if((tmp = msg['@']) @@ -2063,7 +2060,7 @@ mesh.raw = function(msg){ if(!msg){ return '' } - var dup = ctx.dup, msh = msg.mesh || {}, put, hash, tmp; + var dup = ctx.dup, msh = (msg._) || {}, put, hash, tmp; if(tmp = msh.raw){ return tmp } if(typeof msg === 'string'){ return msg } if(msg['@'] && (tmp = msg.put)){ diff --git a/lib/store.js b/lib/store.js index c694a6ab..10230707 100644 --- a/lib/store.js +++ b/lib/store.js @@ -3,16 +3,11 @@ var Gun = (typeof window !== "undefined")? window.Gun : require('../gun'); Gun.on('create', function(root){ this.to.next(root); var opt = root.opt, u; - if(typeof window !== "undefined"){ - opt.window = window; - } - //if(true !== opt.radisk && (!opt.window && !process.env.RAD_ENV && !process.env.AWS_S3_BUCKET) && false !== opt.localStorage){ return } - //if(true !== opt.radisk){ return } if(false === opt.radisk){ return } - var Radisk = (opt.window && opt.window.Radisk) || require('./radisk'); + var Radisk = (Gun.window && Gun.window.Radisk) || require('./radisk'); var Radix = Radisk.Radix; - opt.store = opt.store || (!opt.window && require('./rfs')(opt)); + opt.store = opt.store || (!Gun.window && require('./rfs')(opt)); var rad = Radisk(opt), esc = String.fromCharCode(27); root.on('put', function(msg){ diff --git a/lib/webrtc.js b/lib/webrtc.js index c8111ccd..9d37d3de 100644 --- a/lib/webrtc.js +++ b/lib/webrtc.js @@ -20,7 +20,7 @@ opt.RTCPeerConnection = rtcpc; opt.RTCSessionDescription = rtcsd; opt.RTCIceCandidate = rtcic; - opt.webrtc = opt.webrtc || {'iceServers': [ + opt.rtc = opt.rtc || {'iceServers': [ {url: 'stun:stun.l.google.com:19302'}, {url: "stun:stun.sipgate.net:3478"}, {url: "stun:stun.stunprotocol.org"}, @@ -28,33 +28,33 @@ {url: "stun:217.10.68.152:10000"}, {url: 'stun:stun.services.mozilla.com'} ]}; - opt.webrtc.dataChannel = opt.webrtc.dataChannel || {ordered: false, maxRetransmits: 2}; - opt.webrtc.sdp = opt.webrtc.sdp || {mandatory: {OfferToReceiveAudio: false, OfferToReceiveVideo: false}}; + opt.rtc.dataChannel = opt.rtc.dataChannel || {ordered: false, maxRetransmits: 2}; + opt.rtc.sdp = opt.rtc.sdp || {mandatory: {OfferToReceiveAudio: false, OfferToReceiveVideo: false}}; var mesh = opt.mesh = opt.mesh || Gun.Mesh(root); root.on('create', function(at){ this.to.next(at); - setTimeout(function(){ root.on('out', {webrtc: {id: opt.pid}}) },1); // announce ourself + setTimeout(function(){ root.on('out', {rtc: {id: opt.pid}}) },1); // announce ourself }); root.on('in', function(msg){ - if(msg.webrtc){ open(msg) } + if(msg.rtc){ open(msg) } this.to.next(msg); }); function open(msg){ - var rtc = msg.webrtc, peer, tmp; + var rtc = msg.rtc, peer, tmp; if(!rtc || !rtc.id){ return } if(tmp = rtc.answer){ if(!(peer = opt.peers[rtc.id]) || peer.remoteSet){ return } return peer.setRemoteDescription(peer.remoteSet = new opt.RTCSessionDescription(tmp)); } if(tmp = rtc.candidate){ - peer = opt.peers[rtc.id] || open({webrtc: {id: rtc.id}}); + peer = opt.peers[rtc.id] || open({rtc: {id: rtc.id}}); return peer.addIceCandidate(new opt.RTCIceCandidate(tmp)); } if(opt.peers[rtc.id]){ return } - (peer = new opt.RTCPeerConnection(opt.webrtc)).id = rtc.id; - var wire = peer.wire = peer.createDataChannel('dc', opt.webrtc.dataChannel); + (peer = new opt.RTCPeerConnection(opt.rtc)).id = rtc.id; + var wire = peer.wire = peer.createDataChannel('dc', opt.rtc.dataChannel); mesh.hi(peer); wire.onclose = function(){ mesh.bye(peer); @@ -77,7 +77,7 @@ }; peer.onicecandidate = function(e){ // source: EasyRTC! if(!e.candidate){ return } - root.on('out', {'@': msg['#'], webrtc: {candidate: e.candidate, id: opt.pid}}); + root.on('out', {'@': msg['#'], rtc: {candidate: e.candidate, id: opt.pid}}); } peer.ondatachannel = function(e){ var rc = e.channel; @@ -89,14 +89,14 @@ peer.setRemoteDescription(new opt.RTCSessionDescription(tmp)); peer.createAnswer(function(answer){ peer.setLocalDescription(answer); - root.on('out', {'@': msg['#'], webrtc: {answer: answer, id: opt.pid}}); - }, function(){}, opt.webrtc.sdp); + root.on('out', {'@': msg['#'], rtc: {answer: answer, id: opt.pid}}); + }, function(){}, opt.rtc.sdp); return; } peer.createOffer(function(offer){ peer.setLocalDescription(offer); - root.on('out', {'@': msg['#'], webrtc: {offer: offer, id: opt.pid}}); - }, function(){}, opt.webrtc.sdp); + root.on('out', {'@': msg['#'], rtc: {offer: offer, id: opt.pid}}); + }, function(){}, opt.rtc.sdp); return peer; } }); diff --git a/nts.js b/nts.js index e8a491c7..c0ee6b5b 100644 --- a/nts.js +++ b/nts.js @@ -31,7 +31,7 @@ Gun.state.drift = Gun.state.drift || 0; setTimeout(function ping(){ - var NTS = {}, ack = Gun.text.random(), msg = {'#': ack, nts: true, gun: ctx.gun}; + var NTS = {}, ack = Gun.text.random(), msg = {'#': ack, nts: true}; NTS.start = Gun.state(); ask[ack] = function(at){ NTS.end = Gun.state(); @@ -46,4 +46,4 @@ }, 1); }); // test by opening up examples/game/nts.html on devices that aren't NTP synced. -}()); +}()); \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index f8dd58d5..f92e0417 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "gun", - "version": "0.9.999997", + "version": "0.9.999998", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index d851b46e..56c7506e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gun", - "version": "0.9.999997", + "version": "0.9.999998", "description": "A realtime, decentralized, offline-first, graph data synchronization engine.", "main": "index.js", "browser": "gun.min.js", diff --git a/sea.js b/sea.js index fc2a3f9f..28264b4f 100644 --- a/sea.js +++ b/sea.js @@ -1108,11 +1108,11 @@ // signature handles data output, it is a proxy to the security function. function signature(msg){ - if(msg.user){ + if((msg._||noop).user){ return this.to.next(msg); } var ctx = this.as; - msg.user = ctx.user; + (msg._||(msg._=function(){})).user = ctx.user; security.call(this, msg); } @@ -1153,9 +1153,9 @@ each.pubs(val, key, node, soul); return; } if('~' === soul.slice(0,1) && 2 === (tmp = soul.slice(1)).split('.').length){ // special case, account data for a public key. - each.pub(val, key, node, soul, tmp, msg.user); return; + each.pub(val, key, node, soul, tmp, (msg._||noop).user); return; } - each.any(val, key, node, soul, msg.user); return; + each.any(val, key, node, soul, (msg._||noop).user); return; return each.end({err: "No other data allowed!"}); }; each.alias = function(val, key, node, soul){ // Example: {_:#~@, ~@alice: {#~@alice}} @@ -1174,7 +1174,7 @@ return each.end({err: "Account must match!"}); } check['user'+soul+key] = 1; - if(msg.I && user && user.is && pub === user.is.pub){ + if(Gun.is(msg.$) && user && user.is && pub === user.is.pub){ SEA.sign(SEA.opt.prep(tmp = SEA.opt.parse(val), key, node, soul), (user._).sea, function(data){ var rel; if(u === data){ return each.end({err: SEA.err || 'Pub signature fail.'}) } if(rel = Gun.val.link.is(val)){ @@ -1199,20 +1199,12 @@ }); }; each.any = function(val, key, node, soul, user){ var tmp, pub; - if(!user || !user.is){ - if(tmp = SEA.opt.pub(soul)){ - check['any'+soul+key] = 1; - SEA.verify(SEA.opt.pack(val,key,node,soul), pub = tmp, function(data){ var rel; - data = SEA.opt.unpack(data, key, node); - if(u === data){ return each.end({err: "Mismatched owner on '" + key + "'."}) } // thanks @rogowski ! - if((rel = Gun.val.link.is(data)) && pub === SEA.opt.pub(rel)){ - (at.sea.own[rel] = at.sea.own[rel] || {})[pub] = true; - } - check['any'+soul+key] = 0; - each.end({ok: 1}); - }); + if(!(pub = SEA.opt.pub(soul))){ + if(at.opt.secure){ + each.end({err: "Soul is missing public key at '" + key + "'."}); return; } + // TODO: Ask community if should auto-sign non user-graph data. check['any'+soul+key] = 1; at.on('secure', function(msg){ this.off(); check['any'+soul+key] = 0; @@ -1222,38 +1214,33 @@ //each.end({err: "Data cannot be modified."}); return; } - if(!(tmp = SEA.opt.pub(soul))){ - if(at.opt.secure){ - each.end({err: "Soul is missing public key at '" + key + "'."}); + if(Gun.is(msg.$) && user && user.is && pub === user.is.pub){ + /*var other = Gun.obj.map(at.sea.own[soul], function(v, p){ + if((user.is||{}).pub !== p){ return p } + }); + if(other){ + each.any(val, key, node, soul); return; - } - // TODO: Ask community if should auto-sign non user-graph data. - check['any'+soul+key] = 0; - each.end({ok: 1}); + }*/ + check['any'+soul+key] = 1; + SEA.sign(SEA.opt.prep(tmp = SEA.opt.parse(val), key, node, soul), (user._).sea, function(data){ + if(u === data){ return each.end({err: 'My signature fail.'}) } + node[key] = JSON.stringify({':': SEA.opt.unpack(data.m), '~': data.s}); + check['any'+soul+key] = 0; + each.end({ok: 1}); + }, {check: SEA.opt.pack(tmp, key, node, soul), raw: 1}); return; } - if(!msg.I){ // only sign data put out from this instance. - each.any(val, key, node, soul); - return; - } - if((pub = tmp) !== (user.is||noop).pub){ - each.any(val, key, node, soul); - return; - } - /*var other = Gun.obj.map(at.sea.own[soul], function(v, p){ - if((user.is||{}).pub !== p){ return p } - }); - if(other){ - each.any(val, key, node, soul); - return; - }*/ check['any'+soul+key] = 1; - SEA.sign(SEA.opt.prep(tmp = SEA.opt.parse(val), key, node, soul), (user._).sea, function(data){ - if(u === data){ return each.end({err: 'My signature fail.'}) } - node[key] = JSON.stringify({':': SEA.opt.unpack(data.m), '~': data.s}); + SEA.verify(SEA.opt.pack(val,key,node,soul), pub, function(data){ var rel; + data = SEA.opt.unpack(data, key, node); + if(u === data){ return each.end({err: "Mismatched owner on '" + key + "'."}) } // thanks @rogowski ! + if((rel = Gun.val.link.is(data)) && pub === SEA.opt.pub(rel)){ + (at.sea.own[rel] = at.sea.own[rel] || {})[pub] = true; + } check['any'+soul+key] = 0; each.end({ok: 1}); - }, {check: SEA.opt.pack(tmp, key, node, soul), raw: 1}); + }); } each.end = function(ctx){ // TODO: Can't you just switch this to each.end = cb? if(each.err){ return } @@ -1265,7 +1252,7 @@ if(Gun.obj.map(check, function(no){ if(no){ return true } })){ return } - msg.user = at.user; // already been through firewall, does not need to again on out. + (msg._||{}).user = at.user || security; // already been through firewall, does not need to again on out. to.next(msg); }; Gun.obj.map(msg.put, each.node); @@ -1306,7 +1293,7 @@ } } SEA.opt.shuffle_attack = 1546329600000; // Jan 1, 2019 - var noop = {}, u; + var noop = function(){}, u; var fl = Math.floor; // TODO: Still need to fix inconsistent state issue. var rel_is = Gun.val.rel.is; // TODO: Potential bug? If pub/priv key starts with `-`? IDK how possible.