7d6e5f52 by Jabis Sevón

stuff

1 parent 8e77cd37
Showing 1 changed file with 51 additions and 8 deletions
...@@ -7,6 +7,8 @@ ...@@ -7,6 +7,8 @@
7 const fs = require("fs"); 7 const fs = require("fs");
8 const url = require("url"); 8 const url = require("url");
9 const Gun = require("gun/gun"); // do not load storage adaptors by default 9 const Gun = require("gun/gun"); // do not load storage adaptors by default
10 require("gun/sea");
11 require("gun/lib/then");
10 require("./gun-ws.js"); // required to allow external websockets into gun constructor 12 require("./gun-ws.js"); // required to allow external websockets into gun constructor
11 require("./mem.js"); // disable to allow file writing for debug 13 require("./mem.js"); // disable to allow file writing for debug
12 const http = require("http"); 14 const http = require("http");
...@@ -22,19 +24,37 @@ if (!process.env.hasOwnProperty('SSL')||process.env.SSL == false) { ...@@ -22,19 +24,37 @@ if (!process.env.hasOwnProperty('SSL')||process.env.SSL == false) {
22 var server = http.createServer(); 24 var server = http.createServer();
23 server.listen(process.env.PORT || 8767); 25 server.listen(process.env.PORT || 8767);
24 } else { 26 } else {
25 config.options.key= process.env.SSLKEY ? fs.readFileSync(process.env.SSLKEY) : fs.readFileSync('cert/server.key'), 27 config.options.key= process.env.SSLKEY ? fs.readFileSync(process.env.SSLKEY) : fs.readFileSync('/home/coder/ssl/rig/privkey.pem'),
26 config.options.cert= process.env.SSLCERT ? fs.readFileSync(process.env.SSLCERT) : fs.readFileSync('cert/server.cert') 28 config.options.cert= process.env.SSLCERT ? fs.readFileSync(process.env.SSLCERT) : fs.readFileSync('/home/coder/ssl/rig/fullchain.pem')
27 29
28 var server = https.createServer(config.options); 30 var server = https.createServer(config.options);
29 server.listen(process.env.PORT || 443); 31 server.listen(process.env.PORT || 8767);
32 }
33 let sigs ={};
34 async function hasValidToken(msg,pathname) {
35 return new Promise((res,rej)=>{
36 var sg = null;
37 var token = null;
38 token = (msg && msg.headers && msg.headers.token) ? msg.headers.token : '"fail"';
39 sg = sigs && pathname && sigs.hasOwnProperty(pathname) ? sigs[pathname] : false;
40 //console.log("validating", msg ,"pathname",pathname, "sigs[pathname]",sigs[pathname],"token",token,sigs);
41 var result = false;
42 console.log(token,sg,sigs);
43 try { result = JSON.parse(token) === sg } catch(err){ console.log("error?",err); }
44 console.log("result",result, JSON.parse(token), sg);
45 return res(result);
46 });
30 } 47 }
31
32 // LRU with last used sockets 48 // LRU with last used sockets
33 const QuickLRU = require("quick-lru"); 49 const QuickLRU = require("quick-lru");
34 const lru = new QuickLRU({ maxSize: 10, onEviction: false }); 50 const lru = new QuickLRU({ maxSize: 10, onEviction: false });
35 51
36 server.on("upgrade", async function(request, socket, head) { 52 server.on("upgrade", async function(request, socket, head) {
37 var pathname = url.parse(request.url).pathname || "/gun"; 53 var parsed = url.parse(request.url,true);
54 console.log("parsed",parsed);
55 var sig = parsed.query && parsed.query.sig ? parsed.query.sig : false;
56 console.log(parsed.query,parsed.query.sig);
57 var pathname = parsed.pathname || "/gun";
38 if (debug) console.log("Got WS request", pathname); 58 if (debug) console.log("Got WS request", pathname);
39 var gun = { gun: false, server: false }; 59 var gun = { gun: false, server: false };
40 if (pathname) { 60 if (pathname) {
...@@ -48,15 +68,39 @@ server.on("upgrade", async function(request, socket, head) { ...@@ -48,15 +68,39 @@ server.on("upgrade", async function(request, socket, head) {
48 // NOTE: Only works with lib/ws.js shim allowing a predefined WS as ws.web parameter in Gun constructor 68 // NOTE: Only works with lib/ws.js shim allowing a predefined WS as ws.web parameter in Gun constructor
49 gun.server = new WebSocket.Server({ noServer: true, path: pathname }); 69 gun.server = new WebSocket.Server({ noServer: true, path: pathname });
50 if (debug) console.log("set peer", request.headers.host + pathname); 70 if (debug) console.log("set peer", request.headers.host + pathname);
51 gun.gun = new Gun({ 71 if(sig) {
72 if(sigs.hasOwnProperty(pathname)){
73 if(sig != sigs[pathname]) { console.log("someone is trying to overwrite our room",sig,pathname); return; }
74 }
75 sigs[pathname]=sig;
76 console.log("stored sig ",sig,"to pathname",pathname);
77 Gun.on('opt', function (ctx) {
78 if (ctx.once) return
79 ctx.on('in', function (msg) {
80 var to = this.to;
81 if (msg.put) {
82 if (hasValidToken(msg,pathname)) {
83 console.log('writing',msg,sig);
84 to.next(msg)
85 } else {
86 console.log('not writing',msg,sig);
87 }
88 } else {
89 to.next(msg)
90 }
91 })
92 })
93 }
94 var g = gun.gun = Gun({
52 peers: [], // should we use self as peer? 95 peers: [], // should we use self as peer?
53 localStorage: false, 96 localStorage: false,
54 file: false, 97 file: false, // "tmp/" + pathname,
55 radisk: false, 98 radisk: false,
56 multicast: false, 99 multicast: false,
57 ws: { noServer: true, path: pathname, web: gun.server }, 100 ws: { noServer: true, path: pathname, web: gun.server },
58 web: gun.server 101 web: gun.server
59 }); 102 });
103
60 lru.set(pathname, gun); 104 lru.set(pathname, gun);
61 } 105 }
62 } 106 }
...@@ -67,7 +111,6 @@ server.on("upgrade", async function(request, socket, head) { ...@@ -67,7 +111,6 @@ server.on("upgrade", async function(request, socket, head) {
67 gun.server.emit("connection", ws, request); 111 gun.server.emit("connection", ws, request);
68 }); 112 });
69 } else { 113 } else {
70 if (debug) console.log("destroying socket", pathname);
71 socket.destroy(); 114 socket.destroy();
72 } 115 }
73 }); 116 });
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!