support env vars

This commit is contained in:
Mark Nadal 2024-11-27 18:13:56 -08:00
parent b1b408971d
commit eb6d6a910c
7 changed files with 50 additions and 39 deletions

View File

@ -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

View File

@ -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

View File

@ -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(){

View File

@ -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) }

View File

@ -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);
};

29
lib/tag.js Normal file
View File

@ -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
};
};

View File

@ -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",