Merge branch 'deploys' into master

This commit is contained in:
Mark Nadal 2024-11-23 17:51:50 -08:00 committed by GitHub
commit 9a0e259a9b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 155 additions and 28 deletions

View File

@ -1,27 +1,34 @@
;(function(){ ;(function(){
var cluster = require('cluster'); var cluster = require('cluster');
if(cluster.isMaster){ if(cluster.isMaster){
return cluster.fork() && cluster.on('exit', function(){ cluster.fork(); require('../lib/crashed'); }); return cluster.fork() && cluster.on('exit',function(){ cluster.fork(); require('../lib/crashed') });
} }
var fs = require('fs'); var fs = require('fs'), env = process.env;
var config = { var GUN = require('../'); // require('gun');
port: process.env.OPENSHIFT_NODEJS_PORT || process.env.VCAP_APP_PORT || process.env.PORT || process.argv[2] || 8765, var opt = {
peers: process.env.PEERS && process.env.PEERS.split(',') || [] port: env.PORT || process.argv[2] || 8765,
peers: env.PEERS && env.PEERS.split(',') || []
}; };
var Gun = require('../'); // require('gun')
if(process.env.HTTPS_KEY){ if(fs.existsSync((opt.home = require('os').homedir())+'/cert.pem')){
config.key = fs.readFileSync(process.env.HTTPS_KEY); env.HTTPS_KEY = env.HTTPS_KEY || opt.home+'/key.pem';
config.cert = fs.readFileSync(process.env.HTTPS_CERT); env.HTTPS_CERT = env.HTTPS_CERT || opt.home+'/cert.pem';
config.server = require('https').createServer(config, Gun.serve(__dirname)); }
if(env.HTTPS_KEY){
opt.port = 443;
opt.key = fs.readFileSync(env.HTTPS_KEY);
opt.cert = fs.readFileSync(env.HTTPS_CERT);
opt.server = require('https').createServer(opt, GUN.serve(__dirname));
require('http').createServer(function(req, res){
res.writeHead(301, {"Location": "https://"+req.headers['host']+req.url });
res.end();
}).listen(80);
} else { } else {
config.server = require('http').createServer(Gun.serve(__dirname)); opt.server = require('http').createServer(GUN.serve(__dirname));
} }
var gun = Gun({web: config.server.listen(config.port), peers: config.peers}); var gun = GUN({web: opt.server.listen(opt.port), peers: opt.peers});
console.log('Relay peer started on port ' + opt.port + ' with /gun');
console.log('Relay peer started on port ' + config.port + ' with /gun');
module.exports = gun; module.exports = gun;
}()); }());

8
examples/https.sh Normal file
View File

@ -0,0 +1,8 @@
#!/bin/bash
cd ~
git clone https://github.com/acmesh-official/acme.sh.git
cd ~/acme.sh
./acme.sh --install -m $EMAIL
bash ~/acme.sh/acme.sh --issue -d $DOMAIN -w $WEB
bash ~/acme.sh/acme.sh --install-cert -d $DOMAIN --key-file ~/key.pem --fullchain-file ~/cert.pem --reloadcmd "service relay force-reload"

View File

@ -3,6 +3,7 @@
# README # README
# This will install nodejs and npm on your system, # This will install nodejs and npm on your system,
# should work on most places other than Windows. # should work on most places other than Windows.
# For it to run on boot as a server, a recent OS is needed.
# Set any environment variables before you run this, # Set any environment variables before you run this,
# like `export RAD=false` to disable storage, or # like `export RAD=false` to disable storage, or
# pass file paths of `HTTPS_CERT` & `HTTPS_KEY`, etc. # pass file paths of `HTTPS_CERT` & `HTTPS_KEY`, etc.
@ -10,26 +11,43 @@
# If you are on Windows, http://nodejs.org/download/ has # If you are on Windows, http://nodejs.org/download/ has
# an installer that will automatically do it for you. # an installer that will automatically do it for you.
# curl -o- https://raw.githubusercontent.com/amark/gun/master/examples/install.sh | bash # curl -o- https://raw.githubusercontent.com/amark/gun/master/examples/install.sh | bash
# wget -O - https://raw.githubusercontent.com/amark/gun/master/examples/install.sh | bash
#debian/ubuntu #debian/ubuntu
su - cd ~
apt-get install sudo -y apt-get install sudo -y
sudo apt-get update -y sudo apt-get update -y
sudo apt-get install curl git git-core screen -y sudo apt-get install curl git git-core systemd -y
sudo apt-get install systemctl -y
#fedora/openSUSE #fedora/openSUSE
sudo yum check-update -y sudo yum check-update -y
sudo yum install curl git git-core screen -y sudo yum install curl git git-core systemd -y
sudo yum install systemctl -y
#screen -S install # You can safely CTRL+A+D to escape without stopping the process. `screen -R install` to resume. Stop all with `killall screen`. Note: May need to `sudo apt-get install screen`
# install nodejs # install nodejs
git clone http://github.com/isaacs/nave.git git clone https://github.com/isaacs/nave.git
sudo ./nave/nave.sh usemain stable ./nave/nave.sh usemain stable
# If you just want nodejs and npm but not gun, stop here.
npm install gun # If you just want nodejs and npm but not gun, stop here.
cd ./node_modules/gun #npm install gun@latest
#cd ./node_modules/gun
mkdir node_modules
git clone https://github.com/amark/gun.git
cd gun
git checkout .
git pull
git checkout master
git checkout $VERSION
git pull
npm install . npm install .
# to start the gun examples: cp ./examples/relay.service /lib/systemd/system/relay.service
screen -S relay echo $PWD >> /lib/systemd/system/relay.service
sudo npm start 80 # change `80` to `443` for https or `8765` for development purposes. echo "fs.file-max = 999999" >> /etc/sysctl.conf
# You can now safely CTRL+A+D to escape without stopping the peer. To stop `killall screen` or `killall node`. ulimit -u unlimited
sysctl -p /etc/sysctl.conf
systemctl daemon-reload
systemctl enable relay
systemctl restart relay

19
examples/relay.service Normal file
View File

@ -0,0 +1,19 @@
[Unit]
Description=GUN relay
Documentation=https://gun.eco
After=network.target
[Install]
WantedBy=multi-user.target
[Service]
Environment=PATH=/usr/bin:/usr/local/bin
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
StartLimitBurst=999999
StartLimitIntervalSec=999999
Restart=always
ExecStart=node examples/http.js 80
# Environment=NODE_ENV=production
WorkingDirectory=

View File

@ -112,6 +112,31 @@
// tbd later // tbd later
return chart; return chart;
} }
;(function(){
if('https' != (''+location).slice(0,5) && "localhost" != location.hostname){
$('body').append("<button id='https'>click here to try generating https certs</button>");
if(/^(?!0)(?!.*\.$)((1?\d?\d|25[0-5]|2[0-4]\d)(\.|$)){4}$/.test(location.hostname)){
$('#https').text("Link this IP address to a Domain by adding an `A Record` to your DNS settings that point to `"+ location.hostname +"` (we recommend the `name/host` be any subdomain you want, like `relay`, but if you want the root domain itself to directly point here use `*`). Then come back here on the domain & click this button to generate HTTPS certificates.");
return;
}
$('body').append("<input id='email' placeholder='email'/>");
$('#https').on('click', function(){
$(this).text("look at console.log for errors, if none, try https");
var gun = GUN(location.origin + '/gun');
if(!$('#email').val()){
$(this).text("email necessary for certs! Type it in & click here again.");
return;
}
setTimeout(function(){
gun._.opt.mesh.say({dam: 'service', try: 'https', email: $('#email').val(), domain: location.hostname});
setTimeout(function(){
if(gun._.opt.mesh.near){ return }
$('#https').text("It might have worked! try HTTPS!");
}, 9000);
}, 999);
});
}
}());
/* /*
Notes to Self about Debugging: Notes to Self about Debugging:
1. Read Disks can spike up to 1min, I suspect other operations are blocking it from resolving as fast as it otherwise would. 1. Read Disks can spike up to 1min, I suspect other operations are blocking it from resolving as fast as it otherwise would.
@ -123,5 +148,6 @@
7. Watch out for get/put loops times, maybe indicating (5) issues? 7. Watch out for get/put loops times, maybe indicating (5) issues?
*/ */
</script> </script>
<script src="../gun.js"></script>
</body> </body>
</html> </html>

View File

@ -205,6 +205,8 @@ function start(root){
},1000); },1000);
}()); }());
setTimeout(function(){ require('./service')(root) },9);
;(function(){ // THIS IS THE MOB MODULE; ;(function(){ // THIS IS THE MOB MODULE;
//return; // WORK IN PROGRESS, TEST FINALIZED, NEED TO MAKE STABLE. //return; // WORK IN PROGRESS, TEST FINALIZED, NEED TO MAKE STABLE.
/* /*
@ -219,7 +221,7 @@ function start(root){
The mob threshold might be determined by other factors, The mob threshold might be determined by other factors,
like how much RAM or CPU stress we have. like how much RAM or CPU stress we have.
*/ */
opt.mob = opt.mob || parseFloat((opt.env||'').MOB) || 9900; // should be based on ulimit, some clouds as low as 10K. opt.mob = opt.mob || parseFloat((opt.env||'').MOB) || 999999; // should be based on ulimit, some clouds as low as 10K.
// handle rebalancing a mob of peers: // handle rebalancing a mob of peers:
root.on('hi', function(peer){ root.on('hi', function(peer){

47
lib/service.js Normal file
View File

@ -0,0 +1,47 @@
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."});
return;
}
try{ (cmd[msg.try]||cmd.any)(msg, peer); }catch(err){ mesh.say({dam: '!', err: "service error: "+err}) }
}
cmd.https = function(msg, peer){ var log;
if(!msg.email || !msg.domain){
mesh.say({dam: '!', err: 'Domain/email missing, use `location.hostname`!'});
return;
}
if(fs.existsSync(home+'/cert.pem')){
mesh.say({dam: '!', err: 'Cert already exists.'});
return;
}
fs.writeFile(examp+'/../email', msg.email, function(){});
run("bash "+examp+"/https.sh", {env: {'EMAIL': msg.email, 'WEB': examp, 'DOMAIN': msg.domain}}, function(e, out, err){
log = "|"+e+"|"+out+"|"+err;
mesh.say({dam: '!', log: ''+log}, peer);
setTimeout(function(){ process.exit() },999);
});
}
cmd.update = function(msg, peer){ var log, pass;
try{ pass = (''+fs.readFileSync(home+'/pass')).trim() }catch(e){}
if(!pass || (msg.pass||'').trim() != pass){ return }
root.stats.stay.updated = +new Date;
run("bash "+examp+"/install.sh", {env: {VERSION: msg.version||''}}, function(e, out, err){
log = e+"|"+out+"|"+err;
mesh.say({dam: '!', log: ''+log}, peer);
setTimeout(function(){ process.exit() },999);
});
}
;(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 }
root.stats.stay.updated = +new Date;
run("bash "+examp+"/install.sh", {}, function(){});
}());
cmd.any = function(){};
};