Skip to content

Commit

Permalink
support env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
amark committed Nov 28, 2024
1 parent b1b4089 commit eb6d6a9
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 39 deletions.
1 change: 1 addition & 0 deletions examples/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion examples/relay.service
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 0 additions & 29 deletions lib/axe.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(){
Expand Down
9 changes: 8 additions & 1 deletion lib/crashed.js
Original file line number Diff line number Diff line change
@@ -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) }
Expand Down
16 changes: 9 additions & 7 deletions lib/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(){});
Expand All @@ -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);
};
29 changes: 29 additions & 0 deletions lib/tag.js
Original file line number Diff line number Diff line change
@@ -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
};
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit eb6d6a9

Please sign in to comment.