gun multicast transport

This commit is contained in:
Martti Malmi 2019-04-15 13:12:28 -07:00
parent fde74e1254
commit d11abeb781
7 changed files with 153 additions and 153 deletions

View File

@ -16,7 +16,6 @@
config.server = require('http').createServer(Gun.serve(__dirname));
}
require('../lib/multicast');
var gun = Gun({web: config.server.listen(config.port), multicast: { port: 8765 } });
var gun = Gun({web: config.server.listen(config.port)});
console.log('Relay peer started on port ' + config.port + ' with /gun');
}());

11
gun.js
View File

@ -1976,9 +1976,8 @@
if(!raw){ return }
var dup = ctx.dup, id, hash, msg, tmp = raw[0];
if(opt.pack <= raw.length){ return mesh.say({dam: '!', err: "Message too big!"}, peer) }
try{msg = JSON.parse(raw);
}catch(e){opt.log('DAM JSON parse error', e)}
if('{' === tmp){
try{msg = JSON.parse(raw);}catch(e){opt.log('DAM JSON parse error', e)}
if(!msg){ return }
if(dup.check(id = msg['#'])){ return }
dup.track(id, true).it = msg; // GUN core also dedups, so `true` is needed.
@ -2004,6 +2003,7 @@
return;
} else
if('[' === tmp){
try{msg = JSON.parse(raw);}catch(e){opt.log('DAM JSON parse error', e)}
if(!msg){ return }
var i = 0, m;
while(m = msg[i++]){
@ -2063,11 +2063,12 @@
function send(raw, peer){
var wire = peer.wire;
try{
if(wire.send){
wire.send(raw);
} else
if(peer.say){
peer.say(raw);
} else
if(wire.send){
if(wire.readyState && 1 != wire.readyState){ return }
wire.send(raw);
}
}catch(e){
(peer.queue = peer.queue || []).push(raw);

View File

@ -3,62 +3,61 @@ var Gun = (typeof window !== "undefined")? window.Gun : require('../gun');
Gun.on('create', function(root){
this.to.next(root);
var opt = root.opt;
try {
var dgram = require("dgram");
var process = require("process");
} catch (e) {
console.log('multicast is not available');
return;
}
if(false === opt.multicast){ return }
opt.multicast = opt.multicast || {};
var MULTICAST_ADDR = "233.255.255.255";
var MULTICAST_INTERVAL = 1000;
var PORT = 20000;
var DEFAULT_GUN_PORT = 8765;
var ENC = 'utf8';
var udp = opt.multicast = opt.multicast || {};
udp.address = udp.address || '233.255.255.255';
udp.pack = udp.pack || 50000; // UDP messages limited to 65KB.
udp.port = udp.port || 23456;
socket = dgram.createSocket({ type: "udp4", reuseAddr: true });
var noop = function(){}, port;
socket.bind(PORT);
var dgram = require("dgram");
var socket = dgram.createSocket({type: "udp4", reuseAddr: true});
socket.bind(udp.port);
var address;
socket.on("listening", function() {
socket.addMembership(MULTICAST_ADDR);
if (opt.multicast && opt.multicast.port) { // if port is specified, advertise our node
console.log(`Advertising this node (port ${opt.multicast.port}) on multicast (${MULTICAST_ADDR})`);
setInterval(sendMessage, MULTICAST_INTERVAL);
}
address = socket.address();
});
socket.addMembership(udp.address);
udp.peer = {url: udp.address + ':' + udp.port, wire: socket};
function sendMessage() {
var msgObj = {
gun: {
version: Gun.version,
port: opt.multicast.port || DEFAULT_GUN_PORT
udp.peer.say = function(raw){
var buf = Buffer.from(raw, 'utf8');
if(udp.pack <= buf.length){ // message too big!!!
return;
}
};
var message = Buffer.from(JSON.stringify(msgObj), ENC);
socket.send(message, 0, message.length, PORT, MULTICAST_ADDR, function() {
// console.info(`Sending message "${message}"`);
});
}
socket.on("message", function(message, rinfo) {
try {
var msgObj = JSON.parse(message.toString(ENC));
if (!(msgObj.gun && msgObj.gun.port)) { return }
var peer = `http://${rinfo.address}:${msgObj.gun.port}/gun`;
if (!root.opt.peers.hasOwnProperty(peer)) {
console.log(`peer ${peer} found via multicast`);
root.$.opt({peers: [peer]});
}
} catch (e) {
// console.error(`Received multicast from ${rinfo.address}:${rinfo.port} but failed to connect:`, e);
socket.send(buf, 0, buf.length, udp.port, udp.address, noop);
}
opt.mesh.hi(udp.peer);
console.log('multicasting on', udp.peer.url);
return; // below code only needed for when WebSocket connections desired!
setInterval(function broadcast(){
port = port || (opt.web && opt.web.address()||{}).port;
if(!port){ return }
udp.peer.say(JSON.stringify({id: opt.pid || (opt.pid = Math.random().toString(36).slice(2)), port: port}));
}, 1000);
});
socket.on("message", function(raw, info) { try {
if(!raw){ return }
raw = raw.toString('utf8');
opt.mesh.hear(raw, udp.peer);
return; // below code only needed for when WebSocket connections desired!
var message;
message = JSON.parse(raw.toString('utf8'));
if(opt.pid === message.id){ return } // ignore self
var url = 'http://' + info.address + ':' + (port || (opt.web && opt.web.address()||{}).port) + '/gun';
if(root.opt.peers[url]){ return }
console.log('discovered', url, message, info);
root.$.opt(url);
} catch(e){
console.log('multicast error', e, raw);
return;
} });
});

View File

@ -16,6 +16,7 @@
//try{require('../axe');}catch(e){}
require('./file');
require('./evict');
require('./multicast');
if('debug' === process.env.GUN_ENV){ require('./debug') }
module.exports = Gun;
}());

View File

@ -74,7 +74,7 @@ Gun.on('opt', function(root){
wire.on('message', function(msg){
opt.mesh.hear(msg.data || msg, peer);
});
wire.on('close', function(){
wire.on('close', function(a,b,c){
opt.mesh.bye(peer);
});
wire.on('error', function(e){});

192
package-lock.json generated
View File

@ -1,6 +1,6 @@
{
"name": "gun",
"version": "0.9.9999991",
"version": "0.2019.413",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
@ -10,7 +10,7 @@
"integrity": "sha1-63d99gEXI6OxTopywIBcjoZ0a9I=",
"dev": true,
"requires": {
"mime-types": "2.1.18",
"mime-types": "~2.1.18",
"negotiator": "0.6.1"
}
},
@ -113,15 +113,15 @@
"dev": true,
"requires": {
"bytes": "3.0.0",
"content-type": "1.0.4",
"content-type": "~1.0.4",
"debug": "2.6.9",
"depd": "1.1.2",
"http-errors": "1.6.3",
"depd": "~1.1.1",
"http-errors": "~1.6.2",
"iconv-lite": "0.4.19",
"on-finished": "2.3.0",
"on-finished": "~2.3.0",
"qs": "6.5.1",
"raw-body": "2.3.2",
"type-is": "1.6.16"
"type-is": "~1.6.15"
}
},
"brace-expansion": {
@ -130,7 +130,7 @@
"integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==",
"dev": true,
"requires": {
"balanced-match": "1.0.0",
"balanced-match": "^1.0.0",
"concat-map": "0.0.1"
}
},
@ -146,9 +146,9 @@
"integrity": "sha1-bRu2AbB6TvztlwlBMgkwJ8lbwpg=",
"dev": true,
"requires": {
"base64-js": "1.3.0",
"ieee754": "1.1.8",
"isarray": "1.0.0"
"base64-js": "^1.0.2",
"ieee754": "^1.1.4",
"isarray": "^1.0.0"
}
},
"bytes": {
@ -267,7 +267,7 @@
"cookie": "0.3.1",
"debug": "2.3.3",
"engine.io-parser": "1.3.2",
"ws": "1.1.5"
"ws": "~1.1.5"
},
"dependencies": {
"accepts": {
@ -276,7 +276,7 @@
"integrity": "sha1-w8p0NJOGSMPg2cHjKN1otiLChMo=",
"dev": true,
"requires": {
"mime-types": "2.1.18",
"mime-types": "~2.1.11",
"negotiator": "0.6.1"
}
},
@ -307,8 +307,8 @@
"integrity": "sha512-o3KqipXNUdS7wpQzBHSe180lBGO60SoK0yVo3CYJgb2MkobuWuBX6dhkYP5ORCLd55y+SaflMOV5fqAB53ux4w==",
"dev": true,
"requires": {
"options": "0.0.6",
"ultron": "1.0.2"
"options": ">=0.0.5",
"ultron": "1.0.x"
}
}
}
@ -328,7 +328,7 @@
"parsejson": "0.0.3",
"parseqs": "0.0.5",
"parseuri": "0.0.5",
"ws": "1.1.5",
"ws": "~1.1.5",
"xmlhttprequest-ssl": "1.5.3",
"yeast": "0.1.2"
},
@ -360,8 +360,8 @@
"integrity": "sha512-o3KqipXNUdS7wpQzBHSe180lBGO60SoK0yVo3CYJgb2MkobuWuBX6dhkYP5ORCLd55y+SaflMOV5fqAB53ux4w==",
"dev": true,
"requires": {
"options": "0.0.6",
"ultron": "1.0.2"
"options": ">=0.0.5",
"ultron": "1.0.x"
}
}
}
@ -410,36 +410,36 @@
"integrity": "sha1-avilAjUNsyRuzEvs9rWjTSL37VM=",
"dev": true,
"requires": {
"accepts": "1.3.5",
"accepts": "~1.3.5",
"array-flatten": "1.1.1",
"body-parser": "1.18.2",
"content-disposition": "0.5.2",
"content-type": "1.0.4",
"content-type": "~1.0.4",
"cookie": "0.3.1",
"cookie-signature": "1.0.6",
"debug": "2.6.9",
"depd": "1.1.2",
"encodeurl": "1.0.2",
"escape-html": "1.0.3",
"etag": "1.8.1",
"depd": "~1.1.2",
"encodeurl": "~1.0.2",
"escape-html": "~1.0.3",
"etag": "~1.8.1",
"finalhandler": "1.1.1",
"fresh": "0.5.2",
"merge-descriptors": "1.0.1",
"methods": "1.1.2",
"on-finished": "2.3.0",
"parseurl": "1.3.2",
"methods": "~1.1.2",
"on-finished": "~2.3.0",
"parseurl": "~1.3.2",
"path-to-regexp": "0.1.7",
"proxy-addr": "2.0.3",
"proxy-addr": "~2.0.3",
"qs": "6.5.1",
"range-parser": "1.2.0",
"range-parser": "~1.2.0",
"safe-buffer": "5.1.1",
"send": "0.16.2",
"serve-static": "1.13.2",
"setprototypeof": "1.1.0",
"statuses": "1.4.0",
"type-is": "1.6.16",
"statuses": "~1.4.0",
"type-is": "~1.6.16",
"utils-merge": "1.0.1",
"vary": "1.1.2"
"vary": "~1.1.2"
},
"dependencies": {
"safe-buffer": {
@ -457,12 +457,12 @@
"dev": true,
"requires": {
"debug": "2.6.9",
"encodeurl": "1.0.2",
"escape-html": "1.0.3",
"on-finished": "2.3.0",
"parseurl": "1.3.2",
"statuses": "1.4.0",
"unpipe": "1.0.0"
"encodeurl": "~1.0.2",
"escape-html": "~1.0.3",
"on-finished": "~2.3.0",
"parseurl": "~1.3.2",
"statuses": "~1.4.0",
"unpipe": "~1.0.0"
}
},
"forwarded": {
@ -489,12 +489,12 @@
"integrity": "sha512-MJTUg1kjuLeQCJ+ccE4Vpa6kKVXkPYJ2mOCQyUuKLcLQsdrMCpBPUi8qVE6+YuaJkozeA9NusTAw3hLr8Xe5EQ==",
"dev": true,
"requires": {
"fs.realpath": "1.0.0",
"inflight": "1.0.6",
"inherits": "2.0.3",
"minimatch": "3.0.4",
"once": "1.4.0",
"path-is-absolute": "1.0.1"
"fs.realpath": "^1.0.0",
"inflight": "^1.0.4",
"inherits": "2",
"minimatch": "^3.0.4",
"once": "^1.3.0",
"path-is-absolute": "^1.0.0"
}
},
"growl": {
@ -544,10 +544,10 @@
"integrity": "sha1-i1VoC7S+KDoLW/TqLjhYC+HZMg0=",
"dev": true,
"requires": {
"depd": "1.1.2",
"depd": "~1.1.2",
"inherits": "2.0.3",
"setprototypeof": "1.1.0",
"statuses": "1.4.0"
"statuses": ">= 1.4.0 < 2"
}
},
"iconv-lite": {
@ -574,8 +574,8 @@
"integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=",
"dev": true,
"requires": {
"once": "1.4.0",
"wrappy": "1.0.2"
"once": "^1.3.0",
"wrappy": "1"
}
},
"inherits": {
@ -662,7 +662,7 @@
"integrity": "sha512-lc/aahn+t4/SWV/qcmumYjymLsWfN3ELhpmVuUFjgsORruuZPVSwAQryq+HHGvO/SI2KVX26bx+En+zhM8g8hQ==",
"dev": true,
"requires": {
"mime-db": "1.33.0"
"mime-db": "~1.33.0"
}
},
"minimatch": {
@ -671,7 +671,7 @@
"integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==",
"dev": true,
"requires": {
"brace-expansion": "1.1.11"
"brace-expansion": "^1.1.7"
}
},
"minimist": {
@ -741,10 +741,10 @@
"integrity": "sha512-cEq67y6GJ5jcKdANi5XqejqMvM/eIGxuOOE8F+c0XS950jSpvOcjUNHLmIe3/dN/UKyUkb+dri0BU4OgmCJd2g==",
"optional": true,
"requires": {
"mkdirp": "0.5.1",
"nan": "2.12.1",
"tslib": "1.9.3",
"webcrypto-core": "0.1.26"
"mkdirp": "^0.5.1",
"nan": "^2.11.1",
"tslib": "^1.9.3",
"webcrypto-core": "^0.1.25"
}
},
"object-assign": {
@ -774,7 +774,7 @@
"integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=",
"dev": true,
"requires": {
"wrappy": "1.0.2"
"wrappy": "1"
}
},
"options": {
@ -789,10 +789,10 @@
"integrity": "sha1-4+yr3DQn6vELowviJZAaehGasXM=",
"dev": true,
"requires": {
"bluebird": "3.5.1",
"is-promise": "2.1.0",
"bluebird": "^3.4.6",
"is-promise": "^2.1.0",
"platform": "1.3.1",
"socket.io-client": "1.7.4"
"socket.io-client": "^1.4.5"
}
},
"panic-manager": {
@ -801,10 +801,10 @@
"integrity": "sha1-0tvHdgIAMsWwEw0QW/vqewZnMh4=",
"dev": true,
"requires": {
"isarray": "2.0.4",
"panic-client": "1.0.2",
"socket.io": "1.7.4",
"socket.io-client": "1.7.4"
"isarray": "^2.0.0",
"panic-client": "^1.0.0",
"socket.io": "^1.4.8",
"socket.io-client": "^1.4.8"
},
"dependencies": {
"isarray": {
@ -821,9 +821,9 @@
"integrity": "sha512-TcR6M4LaqKjHvAKoAi46w2Y11KPJiMchAEqu00+tlOBxHR8AYvUCBvDLw4+j3MymApVHHWtluOzDaWxEYeGuVw==",
"dev": true,
"requires": {
"bluebird": "3.5.1",
"panic-client": "1.0.2",
"socket.io": "1.7.4"
"bluebird": "^3.3.5",
"panic-client": "^1.0.0",
"socket.io": "^1.4.5"
}
},
"parsejson": {
@ -832,7 +832,7 @@
"integrity": "sha1-q343WfIJ7OmUN5c/fQ8fZK4OZKs=",
"dev": true,
"requires": {
"better-assert": "1.0.2"
"better-assert": "~1.0.0"
}
},
"parseqs": {
@ -841,7 +841,7 @@
"integrity": "sha1-1SCKNzjkZ2bikbouoXNoSSGouJ0=",
"dev": true,
"requires": {
"better-assert": "1.0.2"
"better-assert": "~1.0.0"
}
},
"parseuri": {
@ -850,7 +850,7 @@
"integrity": "sha1-gCBKUNTbt3m/3G6+J3jZDkvOMgo=",
"dev": true,
"requires": {
"better-assert": "1.0.2"
"better-assert": "~1.0.0"
}
},
"parseurl": {
@ -883,7 +883,7 @@
"integrity": "sha512-jQTChiCJteusULxjBp8+jftSQE5Obdl3k4cnmLA6WXtK6XFuWRnvVL7aCiBqaLPM8c4ph0S4tKna8XvmIwEnXQ==",
"dev": true,
"requires": {
"forwarded": "0.1.2",
"forwarded": "~0.1.2",
"ipaddr.js": "1.6.0"
}
},
@ -938,7 +938,7 @@
"depd": "1.1.1",
"inherits": "2.0.3",
"setprototypeof": "1.0.3",
"statuses": "1.4.0"
"statuses": ">= 1.3.1 < 2"
}
},
"setprototypeof": {
@ -962,18 +962,18 @@
"dev": true,
"requires": {
"debug": "2.6.9",
"depd": "1.1.2",
"destroy": "1.0.4",
"encodeurl": "1.0.2",
"escape-html": "1.0.3",
"etag": "1.8.1",
"depd": "~1.1.2",
"destroy": "~1.0.4",
"encodeurl": "~1.0.2",
"escape-html": "~1.0.3",
"etag": "~1.8.1",
"fresh": "0.5.2",
"http-errors": "1.6.3",
"http-errors": "~1.6.2",
"mime": "1.4.1",
"ms": "2.0.0",
"on-finished": "2.3.0",
"range-parser": "1.2.0",
"statuses": "1.4.0"
"on-finished": "~2.3.0",
"range-parser": "~1.2.0",
"statuses": "~1.4.0"
}
},
"serve-static": {
@ -982,9 +982,9 @@
"integrity": "sha512-p/tdJrO4U387R9oMjb1oj7qSMaMfmOyd4j9hOFoxZe2baQszgHcSWjuya/CiT5kgZZKRudHNOA0pYXOl8rQ5nw==",
"dev": true,
"requires": {
"encodeurl": "1.0.2",
"escape-html": "1.0.3",
"parseurl": "1.3.2",
"encodeurl": "~1.0.2",
"escape-html": "~1.0.3",
"parseurl": "~1.3.2",
"send": "0.16.2"
}
},
@ -1001,7 +1001,7 @@
"dev": true,
"requires": {
"debug": "2.3.3",
"engine.io": "1.8.5",
"engine.io": "~1.8.4",
"has-binary": "0.1.7",
"object-assign": "4.1.0",
"socket.io-adapter": "0.5.0",
@ -1063,7 +1063,7 @@
"component-bind": "1.0.0",
"component-emitter": "1.2.1",
"debug": "2.3.3",
"engine.io-client": "1.8.5",
"engine.io-client": "~1.8.4",
"has-binary": "0.1.7",
"indexof": "0.0.1",
"object-component": "0.0.3",
@ -1148,7 +1148,7 @@
"integrity": "sha512-zjaXglF5nnWpsq470jSv6P9DwPvgLkuapYmfDm3JWOm0vkNTVF2tI4UrN2r6jH1qM/uc/WtxYY1hYoA2dOKj5w==",
"dev": true,
"requires": {
"has-flag": "3.0.0"
"has-flag": "^3.0.0"
}
},
"text-encoding": {
@ -1175,7 +1175,7 @@
"dev": true,
"requires": {
"media-typer": "0.3.0",
"mime-types": "2.1.18"
"mime-types": "~2.1.18"
}
},
"uglify-js": {
@ -1184,8 +1184,8 @@
"integrity": "sha512-hS7+TDiqIqvWScCcKRybCQzmMnEzJ4ryl9ErRmW4GFyG48p0/dKZiy/5mVLbsFzU8CCnCgQdxMiJzZythvLzCg==",
"dev": true,
"requires": {
"commander": "2.15.1",
"source-map": "0.6.1"
"commander": "~2.15.0",
"source-map": "~0.6.1"
},
"dependencies": {
"commander": {
@ -1236,7 +1236,7 @@
"integrity": "sha512-BZVgJZkkHyuz8loKvsaOKiBDXDpmMZf5xG4QAOlSeYdXlFUl9c1FRrVnAXcOdb4fTHMG+TRu81odJwwSfKnWTA==",
"optional": true,
"requires": {
"tslib": "1.9.3"
"tslib": "^1.7.1"
}
},
"wrappy": {
@ -1246,11 +1246,11 @@
"dev": true
},
"ws": {
"version": "5.2.0",
"resolved": "https://registry.npmjs.org/ws/-/ws-5.2.0.tgz",
"integrity": "sha512-c18dMeW+PEQdDFzkhDsnBAlS4Z8KGStBQQUcQ5mf7Nf689jyGk0594L+i9RaQuf4gog6SvWLJorz2NfSaqxZ7w==",
"version": "6.2.1",
"resolved": "https://registry.npmjs.org/ws/-/ws-6.2.1.tgz",
"integrity": "sha512-GIyAXC2cB7LjvpgMt9EKS2ldqr0MTrORaleiOno6TweZ6r3TKtoFQWay/2PceJ3RuBasOHzXNn5Lrw1X0bEjqA==",
"requires": {
"async-limiter": "1.0.0"
"async-limiter": "~1.0.0"
}
},
"wtf-8": {
@ -1265,8 +1265,8 @@
"integrity": "sha1-F76T6q4/O3eTWceVtBlwWogX6Gg=",
"dev": true,
"requires": {
"sax": "1.2.1",
"xmlbuilder": "4.2.1"
"sax": ">=0.6.0",
"xmlbuilder": "^4.1.0"
}
},
"xmlbuilder": {
@ -1275,7 +1275,7 @@
"integrity": "sha1-qlijBBoGb5DqoWwvU4n/GfP0YaU=",
"dev": true,
"requires": {
"lodash": "4.17.10"
"lodash": "^4.0.0"
}
},
"xmlhttprequest-ssl": {

View File

@ -50,7 +50,7 @@
"node": ">=0.8.4"
},
"dependencies": {
"ws": "~>5.2.0"
"ws": "~>6.2.1"
},
"optionalDependencies": {
"text-encoding": "^0.7.0",