4bd54afd by Jabis Sevón

mrgconflict

2 parents e4f8e525 24a81b19
Showing 1 changed file with 26 additions and 18 deletions
...@@ -3,8 +3,7 @@ ...@@ -3,8 +3,7 @@
3 * Each Gun is scoped to its ws.path and intended for ephemeral usage 3 * Each Gun is scoped to its ws.path and intended for ephemeral usage
4 * MIT Licensed (C) QXIP 2020 4 * MIT Licensed (C) QXIP 2020
5 */ 5 */
6 6 const no = require('gun/lib/nomem')(); // no-memory storage adapter for RAD
7 var no = require('gun/lib/nomem')(); // no-memory storage adapter for RAD
8 const fs = require("fs"); 7 const fs = require("fs");
9 const url = require("url"); 8 const url = require("url");
10 const Gun = require("gun"); // load defaults 9 const Gun = require("gun"); // load defaults
...@@ -16,8 +15,9 @@ const SEA = Gun.SEA; ...@@ -16,8 +15,9 @@ const SEA = Gun.SEA;
16 const http = require("http"); 15 const http = require("http");
17 const https = require("https"); 16 const https = require("https");
18 const WebSocket = require("ws"); 17 const WebSocket = require("ws");
19 var debug = process.env.DEBUG || true; 18 let debug = process.env.DEBUG || true;
20 var config = {}; 19 let relaypeers = process.env.RELAY || 'https://mg.rig.airfaas.com/';
20 let config = {};
21 if(debug) console.log(SEA, Gun.SEA); 21 if(debug) console.log(SEA, Gun.SEA);
22 config.options = { 22 config.options = {
23 } 23 }
...@@ -50,7 +50,7 @@ server.on("upgrade", async function(request, socket, head) { ...@@ -50,7 +50,7 @@ server.on("upgrade", async function(request, socket, head) {
50 var gun = { gun: false, server: false }; 50 var gun = { gun: false, server: false };
51 if (pathname) { 51 if (pathname) {
52 let roomname = pathname.split("").slice(1).join(""); 52 let roomname = pathname.split("").slice(1).join("");
53 console.log("roomname",roomname); 53 if(debug) console.log("roomname",roomname);
54 if (lru.has(pathname)) { 54 if (lru.has(pathname)) {
55 // Existing Node 55 // Existing Node
56 if (debug) console.log("Recycle id", pathname); 56 if (debug) console.log("Recycle id", pathname);
...@@ -70,12 +70,16 @@ server.on("upgrade", async function(request, socket, head) { ...@@ -70,12 +70,16 @@ server.on("upgrade", async function(request, socket, head) {
70 /*Gun.on('opt',function(ctx){ 70 /*Gun.on('opt',function(ctx){
71 if(ctx.once) return; 71 if(ctx.once) return;
72 ctx.on('in',function(msg){ 72 ctx.on('in',function(msg){
73 console.log(msg); 73 if(debug) console.log(msg);
74 this.to.next(msg); 74 this.to.next(msg);
75 }) 75 })
76 })*/ 76 })*/
77 let qs = ["sig="+encodeURIComponent((sig ? sig :'')),"creator="+encodeURIComponent((creator ? creator : ''))].join("&");
78 let relaypath = pathname+'?'+qs;
79 let peers = relaypeers.split(',').map(function(p){ return p+relaypath; });
80 if(debug) console.log("peers",peers);
77 const g = gun.gun = Gun({ 81 const g = gun.gun = Gun({
78 peers: [], // should we use self as peer? 82 peers: peers, // should we use self as peer?
79 localStorage: false, 83 localStorage: false,
80 store: no, 84 store: no,
81 file: "tmp" + pathname, // make sure not to reuse same storage context 85 file: "tmp" + pathname, // make sure not to reuse same storage context
...@@ -85,26 +89,30 @@ server.on("upgrade", async function(request, socket, head) { ...@@ -85,26 +89,30 @@ server.on("upgrade", async function(request, socket, head) {
85 }); 89 });
86 gun.server = gun.gun.back('opt.ws.web'); // this is the websocket server 90 gun.server = gun.gun.back('opt.ws.web'); // this is the websocket server
87 lru.set(pathname, gun); 91 lru.set(pathname, gun);
88 let obj = {roomname:roomname,creator:creator,socket:{}}; 92 let obj = {
93 roomname:roomname,
94 creator:creator
95 };
96 if(debug) console.log('object is',obj);
89 if(sig) { 97 if(sig) {
90 let user = g.user(); 98 let user = g.user();
91 user.create(roomname,sig,async function(dack){ 99 user.create(roomname,sig, function(dack){
92 console.log("We've got user create ack",dack,roomname,sig); 100 if(debug) console.log("We've got user create ack",dack,roomname,sig);
93 if(dack.err){ console.log("error in user.create",dack.err); } 101 if(dack.err){ console.log("error in user.create",dack.err); }
94 user.auth(roomname,sig,async function(auth){ 102 user.auth(roomname,sig, function(auth){
95 console.log("We've got user auth ack",auth); 103 if(debug) console.log("We've got user auth ack",auth);
96 if(auth.err){ console.log('error in auth',auth.err); } 104 if(auth.err){ console.log('error in auth',auth.err); }
97 //console.log("auth",auth,roomname,sig); 105 //console.log("auth",auth,roomname,sig);
98 Object.assign(obj,{ 106 Object.assign(obj,{
99 pub:dack.pub, 107 pub:dack.pub,
100 passwordProtected:true 108 passwordProtected:'true'
101 }); 109 });
102 console.log("putting object to user",obj,user); 110 if(debug) console.log("putting",roomname,"with object",obj, `to user ${dack.pub}`);
103 user.get(roomname).put(obj,function(roomack){ //TODO: @marknadal fix me 111 user.get(roomname).put(obj,function(roomack){ //TODO: @marknadal fix me
104 console.log("roomnode?",roomack); 112 if(debug) console.log("roomnode?",roomack);
105 var roomnode = user.get(roomname); 113 var roomnode = user.get(roomname);
106 g.get('rtcmeeting').get(roomname).put(roomnode,function(puback){ 114 g.get('rtcmeeting').get(roomname).put(roomnode,function(puback){
107 console.log("put object",puback); 115 if(debug) console.log("put object",puback);
108 }); 116 });
109 }); 117 });
110 }); 118 });
...@@ -112,7 +120,7 @@ server.on("upgrade", async function(request, socket, head) { ...@@ -112,7 +120,7 @@ server.on("upgrade", async function(request, socket, head) {
112 } else { 120 } else {
113 Object.assign(obj,{passwordProtected:false}); 121 Object.assign(obj,{passwordProtected:false});
114 g.get("rtcmeeting").get(roomname).put(obj,function(grack){ 122 g.get("rtcmeeting").get(roomname).put(obj,function(grack){
115 console.log("room created",grack); 123 if(debug) console.log("room created",grack);
116 }); 124 });
117 } 125 }
118 } 126 }
...@@ -127,4 +135,4 @@ server.on("upgrade", async function(request, socket, head) { ...@@ -127,4 +135,4 @@ server.on("upgrade", async function(request, socket, head) {
127 if (debug) console.log("destroying socket", pathname); 135 if (debug) console.log("destroying socket", pathname);
128 socket.destroy(); 136 socket.destroy();
129 } 137 }
130 }); 138 });
...\ No newline at end of file ...\ No newline at end of file
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!