diff --git a/examples/install.sh b/examples/install.sh index 4ec20dd7c..b930aeebc 100644 --- a/examples/install.sh +++ b/examples/install.sh @@ -33,6 +33,7 @@ git clone https://github.com/isaacs/nave.git # If you just want nodejs and npm but not gun, stop here. #npm install gun@latest #cd ./node_modules/gun +> relay.env mkdir node_modules git clone https://github.com/amark/gun.git cd gun diff --git a/examples/relay.service b/examples/relay.service index 43d2f5698..63fe2202b 100644 --- a/examples/relay.service +++ b/examples/relay.service @@ -8,9 +8,10 @@ WantedBy=multi-user.target [Service] Environment=PATH=/usr/bin:/usr/local/bin +EnvironmentFile=%h/relay.env LimitNOFILE=infinity LimitNPROC=infinity -LimitCORE=infinity +LimitCORE=0 StartLimitBurst=999999 StartLimitIntervalSec=999999 Restart=always diff --git a/lib/axe.js b/lib/axe.js index b61a1c78d..92a2cf1dd 100644 --- a/lib/axe.js +++ b/lib/axe.js @@ -240,35 +240,6 @@ function start(root){ }); }()); - - ;(function(){ // THIS IS THE UNIVERSAL NOTIFICATION MODULE - var to = {}, key = {}, email = require('./email'); - if(email.err){ return } - mesh.hear['tag'] = function(msg, peer, who){ - if(who = key[msg.key]){ who.rate = Math.max(msg.rate||1000*60*15, 1000*60); return } - if(!msg.src || !msg.email){ return } - if(+new Date < peer.emailed + 1000*60*2){ mesh.say({dam:'tag',err:'too fast'},peer); return } // peer can only send notifications > 2min - var src; try{ src = new URL(msg.src = msg.src.split(/\s/)[0]); } catch(e){ return } // throws if invalid URL. - (who = (to[msg.email] = to[msg.email] || {go:{}})).go[''+src] = 1; // we're keeping in-memory for now, maybe will "stay" to disk in future. - peer.emailed = +new Date; - if(who.batch){ return } - key[who.key = Math.random().toString(36).slice(2)] = who; - who.batch = setTimeout(function(){ - email.send({ - from: process.env.EMAIL, - to: msg.email, - subject: "Notification:", - text: 'Someone or a bot tagged you at: (⚠️ only click link if you recognize & trust it ⚠️)\n'+ - '[use #'+who.key+' to unsubscribe please mute this thread by tapping the top most "⋮" button and clicking mute]\n\n' + - Object.keys(who.go).join('\n'), // TODO: NEEDS TO BE CPU SCHEDULED - headers: {'message-id': '<123456789.8765@example.com>'} // hardcode id so all batches also group into the same email thread to reduce clutter. - }, function(err, r){ - who.batch = null; who.go = {}; - err && console.log("email TAG:", err); - }); - }, who.rate || (1000*60*60*24)); // default to 1 day - }; - }()); }; ;(function(){ diff --git a/lib/crashed.js b/lib/crashed.js index ab37ad58b..2721cede1 100644 --- a/lib/crashed.js +++ b/lib/crashed.js @@ -1,5 +1,12 @@ ;(function(){ try { - var fs = require('fs'), logs = [], up = __dirname+'/../'; + var fs = require('fs'), logs = [], up = require('os').homedir()+'/'; + ;(function(){ try{ if(!process.env.EMAIL){ return } // ONLY EMAIL IF DEVELOPER OPTS IN!!! + require('./email').send({ text: "", + from: process.env.EMAIL, + to: "crash@gun.eco", + subject: "RELAY CRASH" + }, noop);}catch(e){}; }()); + return; fs.readdir(up, function(err, list){ try{ var i = 0, f; while(f = list[i++]){ if(0 === f.indexOf('isolate-') && '.log' === f.slice(-4)){ logs.push(f) } diff --git a/lib/service.js b/lib/service.js index 7f479c34a..1bed5a29e 100644 --- a/lib/service.js +++ b/lib/service.js @@ -2,18 +2,18 @@ module.exports = function(root){ var mesh = root.opt.mesh, cmd = {}, run = require('child_process').exec, fs = require('fs'), home = require('os').homedir(), examp = require('path').resolve(__dirname, '../examples'); mesh.hear['service'] = function(msg, peer){ if(!fs.existsSync('/lib/systemd/system/relay.service')){ - mesh.say({dam: '!', err: "Not serviced."}); + mesh.say({dam: '!', err: "Not serviced."}, peer); return; } - try{ (cmd[msg.try]||cmd.any)(msg, peer); }catch(err){ mesh.say({dam: '!', err: "service error: "+err}) } + try{ (cmd[msg.try]||cmd.any)(msg, peer); }catch(err){ mesh.say({dam: '!', err: "service error: "+err}, peer) } } cmd.https = function(msg, peer){ var log; if(!msg.email || !msg.domain){ - mesh.say({dam: '!', err: 'Domain/email missing, use `location.hostname`!'}); + mesh.say({dam: '!', err: 'Domain/email missing, use `location.hostname`!'}, peer); return; } if(fs.existsSync(home+'/cert.pem')){ - mesh.say({dam: '!', err: 'Cert already exists.'}); + mesh.say({dam: '!', err: 'Cert already exists.'}, peer); return; } fs.writeFile(examp+'/../email', msg.email, function(){}); @@ -33,15 +33,17 @@ module.exports = function(root){ setTimeout(function(){ process.exit() },999); }); } - ;(function update(){ var last; + setTimeout(function update(){ var last; if(!fs.existsSync(home+'/cert.pem')){ return } setTimeout(update, 1000*60*60*24); last = root.stats.stay.updated || 0; - if(+new Date - last < 1000*60*60*24*15){ return } + if(+new Date - last < 1000*60*60*24*15){ return } // try to update every half month root.stats.stay.updated = +new Date; run("bash "+examp+"/install.sh", {}, function(){}); - }()); + },999); cmd.any = function(){}; + // list other modules here: + require('./tag')(root); }; \ No newline at end of file diff --git a/lib/tag.js b/lib/tag.js new file mode 100644 index 000000000..dda7fafb4 --- /dev/null +++ b/lib/tag.js @@ -0,0 +1,29 @@ +module.exports = function(root){ // THIS IS THE UNIVERSAL NOTIFICATION MODULE + var to = {}, key = {}, mesh = root.opt.mesh, email = require('./email'); + if(email.err){ return } + mesh.hear['tag'] = function(msg, peer, who){ + if(who = key[msg.key]){ who.rate = Math.max(msg.rate||1000*60*15, 1000*60); return } + if(!msg.src || !msg.email){ return } + if(+new Date < peer.emailed + 1000*60*2){ mesh.say({dam:'tag',err:'too fast'},peer); return } // peer can only send notifications > 2min + var src; try{ src = new URL(msg.src = msg.src.split(/\s/)[0]); } catch(e){ return } // throws if invalid URL. + (who = (to[msg.email] = to[msg.email] || {go:{}})).go[''+src] = 1; // we're keeping in-memory for now, maybe will "stay" to disk in future. + peer.emailed = +new Date; + mesh.say({dam:'tag',ok:'queued in-memory'},peer); + if(who.batch){ return } + key[who.key = Math.random().toString(36).slice(2)] = who; + who.batch = setTimeout(function(){ + email.send({ + from: process.env.EMAIL, + to: msg.email, + subject: "Notification:", + text: 'Someone or a bot tagged you at: (⚠️ only click link if you recognize & trust it ⚠️)\n'+ + '[use #'+who.key+' to unsubscribe please mute this thread by tapping the top most "⋮" button and clicking mute]\n\n' + + Object.keys(who.go).join('\n'), // TODO: NEEDS TO BE CPU SCHEDULED + headers: {'message-id': '<123456789.8765@example.com>'} // hardcode id so all batches also group into the same email thread to reduce clutter. + }, function(err, r){ + who.batch = null; who.go = {}; + err && console.log("email TAG:", err); + }); + }, who.rate || (1000*60*60*24)); // default to 1 day + }; +}; \ No newline at end of file diff --git a/package.json b/package.json index 4443403d7..774f60e99 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "ios": "browser.ios.js", "android": "browser.android.js", "scripts": { - "start": "node --prof examples/http.js", + "start": "node examples/http.js", "debug": "node --prof-process --preprocess -j isolate*.log > v8data.json && rm isolate*.log && echo 'drag & drop ./v8data.json into https://mapbox.github.io/flamebearer/'", "https": "HTTPS_KEY=test/https/server.key HTTPS_CERT=test/https/server.crt npm start", "prepublishOnly": "npm run unbuild",