user creation stuff
Showing
1 changed file
with
41 additions
and
42 deletions
... | @@ -10,7 +10,7 @@ const Gun = require("gun/gun"); // do not load storage adaptors by default | ... | @@ -10,7 +10,7 @@ const Gun = require("gun/gun"); // do not load storage adaptors by default |
10 | require("./gun-ws.js"); // required to allow external websockets into gun constructor | 10 | require("./gun-ws.js"); // required to allow external websockets into gun constructor |
11 | require("./mem.js"); // disable to allow file writing for debug | 11 | require("./mem.js"); // disable to allow file writing for debug |
12 | require("gun/sea"); | 12 | require("gun/sea"); |
13 | 13 | require("gun/lib/then"); | |
14 | const SEA = Gun.SEA; | 14 | const SEA = Gun.SEA; |
15 | const http = require("http"); | 15 | const http = require("http"); |
16 | const https = require("https"); | 16 | const https = require("https"); |
... | @@ -33,30 +33,22 @@ if (!process.env.hasOwnProperty('SSL')||process.env.SSL == false) { | ... | @@ -33,30 +33,22 @@ if (!process.env.hasOwnProperty('SSL')||process.env.SSL == false) { |
33 | } | 33 | } |
34 | let sigs ={}; | 34 | let sigs ={}; |
35 | async function hasValidToken(msg,pathname) { | 35 | async function hasValidToken(msg,pathname) { |
36 | return new Promise((res,rej)=>{ | 36 | return true; |
37 | var sg = null; | ||
38 | var token = null; | ||
39 | token = (msg && msg.headers && msg.headers.token) ? msg.headers.token : '"fail"'; | ||
40 | sg = sigs && pathname && sigs.hasOwnProperty(pathname) ? sigs[pathname] : false; | ||
41 | //console.log("validating", msg ,"pathname",pathname, "sigs[pathname]",sigs[pathname],"token",token,sigs); | ||
42 | var result = false; | ||
43 | console.log(token,sg,sigs); | ||
44 | try { result = JSON.parse(token) === sg } catch(err){ console.log("error?",err); } | ||
45 | console.log("result",result, JSON.parse(token), sg); | ||
46 | return res(result); | ||
47 | }); | ||
48 | } | 37 | } |
49 | // LRU with last used sockets | 38 | // LRU with last used sockets |
50 | const QuickLRU = require("quick-lru"); | 39 | const QuickLRU = require("quick-lru"); |
51 | const lru = new QuickLRU({ maxSize: 10, onEviction: false }); | 40 | const lru = new QuickLRU({ maxSize: 10, onEviction: false }); |
52 | 41 | ||
53 | server.on("upgrade", async function(request, socket, head) { | 42 | server.on("upgrade", async function(request, socket, head) { |
54 | var parsed = url.parse(request.url,true); | 43 | let parsed = url.parse(request.url,true); |
55 | console.log("parsed",parsed); | 44 | if(debug) console.log("parsed",parsed); |
56 | var sig = parsed.query && parsed.query.sig ? parsed.query.sig : false; | 45 | let sig = parsed.query && parsed.query.sig ? parsed.query.sig : false; |
57 | console.log(parsed.query,parsed.query.sig); | 46 | let creator = parsed.query && parsed.query.creator ? parsed.query.creator : "server"; |
58 | var pathname = parsed.pathname || "/gun"; | 47 | let pathname = parsed.pathname || "/gun"; |
59 | if (debug) console.log("Got WS request", pathname); | 48 | if (debug) console.log("Got WS request", pathname); |
49 | |||
50 | let roomname = pathname.split().slice(1).join(""); | ||
51 | console.log("roomname",roomname); | ||
60 | var gun = { gun: false, server: false }; | 52 | var gun = { gun: false, server: false }; |
61 | if (pathname) { | 53 | if (pathname) { |
62 | if (lru.has(pathname)) { | 54 | if (lru.has(pathname)) { |
... | @@ -70,29 +62,10 @@ server.on("upgrade", async function(request, socket, head) { | ... | @@ -70,29 +62,10 @@ server.on("upgrade", async function(request, socket, head) { |
70 | gun.server = new WebSocket.Server({ noServer: true, path: pathname }); | 62 | gun.server = new WebSocket.Server({ noServer: true, path: pathname }); |
71 | if (debug) console.log("set peer", request.headers.host + pathname); | 63 | if (debug) console.log("set peer", request.headers.host + pathname); |
72 | if(sig) { | 64 | if(sig) { |
73 | if(sigs.hasOwnProperty(pathname)){ | 65 | sigs[pathname]=sig; |
74 | if(sig != sigs[pathname]) { console.log("someone is trying to overwrite our room",sig,pathname); return; } | 66 | if(debug) console.log("stored sig ",sig,"to pathname",pathname); |
75 | } | ||
76 | sigs[pathname]=sig; | ||
77 | console.log("stored sig ",sig,"to pathname",pathname); | ||
78 | Gun.on('opt', function (ctx) { | ||
79 | if (ctx.once) return | ||
80 | ctx.on('in', function (msg) { | ||
81 | var to = this.to; | ||
82 | if (msg.put) { | ||
83 | if (hasValidToken(msg,pathname)) { | ||
84 | console.log('writing',msg,sig); | ||
85 | to.next(msg) | ||
86 | } else { | ||
87 | console.log('not writing',msg,sig); | ||
88 | } | ||
89 | } else { | ||
90 | to.next(msg) | ||
91 | } | ||
92 | }) | ||
93 | }) | ||
94 | } | 67 | } |
95 | console.log("gunsea",Gun.SEA); | 68 | //console.log("gunsea",Gun.SEA); |
96 | SEA.throw = 1; | 69 | SEA.throw = 1; |
97 | /*Gun.on('opt',function(ctx){ | 70 | /*Gun.on('opt',function(ctx){ |
98 | if(ctx.once) return; | 71 | if(ctx.once) return; |
... | @@ -101,7 +74,7 @@ server.on("upgrade", async function(request, socket, head) { | ... | @@ -101,7 +74,7 @@ server.on("upgrade", async function(request, socket, head) { |
101 | this.to.next(msg); | 74 | this.to.next(msg); |
102 | }) | 75 | }) |
103 | })*/ | 76 | })*/ |
104 | var g = gun.gun = Gun({ | 77 | const g = gun.gun = Gun({ |
105 | peers: [], // should we use self as peer? | 78 | peers: [], // should we use self as peer? |
106 | localStorage: false, | 79 | localStorage: false, |
107 | file: false, // "tmp/" + pathname, | 80 | file: false, // "tmp/" + pathname, |
... | @@ -110,8 +83,34 @@ server.on("upgrade", async function(request, socket, head) { | ... | @@ -110,8 +83,34 @@ server.on("upgrade", async function(request, socket, head) { |
110 | ws: { noServer: true, path: pathname, web: gun.server }, | 83 | ws: { noServer: true, path: pathname, web: gun.server }, |
111 | web: gun.server | 84 | web: gun.server |
112 | }); | 85 | }); |
113 | |||
114 | lru.set(pathname, gun); | 86 | lru.set(pathname, gun); |
87 | let obj = {roomname:roomname,creator:creator,socket:{}}; | ||
88 | if(sig) { | ||
89 | let user = g.user(); | ||
90 | user.create(username,sig,async function(ack){ | ||
91 | console.log("We've got ack",ack); | ||
92 | if(ack.err){ console.log("error in user.create",ack.err); } | ||
93 | let auth = await new Promise ((res,rej)=>{ | ||
94 | return user.auth(username,sig,res); | ||
95 | }); | ||
96 | if(auth.err){ console.log('error in auth',auth.err); } | ||
97 | console.log("auth",auth); | ||
98 | Object.assign(obj,{ | ||
99 | pub:ack.pub, | ||
100 | passwordProtected:true | ||
101 | }) | ||
102 | let roomnode = user.get(roomname).put(obj); | ||
103 | let putnode = g.get('rtcmeeting').get(roomname).put(roomnode); | ||
104 | let rack= await putnode.then(); | ||
105 | console.log("room created",rack); | ||
106 | }); | ||
107 | } else { | ||
108 | ;(async ()=>{ | ||
109 | let roomnode = g.get("rtcmeeting").get(roomname).put(obj); | ||
110 | let rack = await roomnode.then(); | ||
111 | console.log("room created",ack); | ||
112 | })() | ||
113 | } | ||
115 | } | 114 | } |
116 | } | 115 | } |
117 | if (gun.server) { | 116 | if (gun.server) { | ... | ... |
-
Please register or sign in to post a comment