Skip to content

Commit 9a0e259

Browse files
authored
Merge branch 'deploys' into master
2 parents 5305f01 + 4c01db9 commit 9a0e259

File tree

7 files changed

+155
-28
lines changed

7 files changed

+155
-28
lines changed

Diff for: examples/http.js

+22-15
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,34 @@
11
;(function(){
22
var cluster = require('cluster');
33
if(cluster.isMaster){
4-
return cluster.fork() && cluster.on('exit', function(){ cluster.fork(); require('../lib/crashed'); });
4+
return cluster.fork() && cluster.on('exit',function(){ cluster.fork(); require('../lib/crashed') });
55
}
66

7-
var fs = require('fs');
8-
var config = {
9-
port: process.env.OPENSHIFT_NODEJS_PORT || process.env.VCAP_APP_PORT || process.env.PORT || process.argv[2] || 8765,
10-
peers: process.env.PEERS && process.env.PEERS.split(',') || []
7+
var fs = require('fs'), env = process.env;
8+
var GUN = require('../'); // require('gun');
9+
var opt = {
10+
port: env.PORT || process.argv[2] || 8765,
11+
peers: env.PEERS && env.PEERS.split(',') || []
1112
};
12-
var Gun = require('../'); // require('gun')
1313

14-
if(process.env.HTTPS_KEY){
15-
config.key = fs.readFileSync(process.env.HTTPS_KEY);
16-
config.cert = fs.readFileSync(process.env.HTTPS_CERT);
17-
config.server = require('https').createServer(config, Gun.serve(__dirname));
14+
if(fs.existsSync((opt.home = require('os').homedir())+'/cert.pem')){
15+
env.HTTPS_KEY = env.HTTPS_KEY || opt.home+'/key.pem';
16+
env.HTTPS_CERT = env.HTTPS_CERT || opt.home+'/cert.pem';
17+
}
18+
if(env.HTTPS_KEY){
19+
opt.port = 443;
20+
opt.key = fs.readFileSync(env.HTTPS_KEY);
21+
opt.cert = fs.readFileSync(env.HTTPS_CERT);
22+
opt.server = require('https').createServer(opt, GUN.serve(__dirname));
23+
require('http').createServer(function(req, res){
24+
res.writeHead(301, {"Location": "https://"+req.headers['host']+req.url });
25+
res.end();
26+
}).listen(80);
1827
} else {
19-
config.server = require('http').createServer(Gun.serve(__dirname));
28+
opt.server = require('http').createServer(GUN.serve(__dirname));
2029
}
2130

22-
var gun = Gun({web: config.server.listen(config.port), peers: config.peers});
23-
24-
console.log('Relay peer started on port ' + config.port + ' with /gun');
25-
31+
var gun = GUN({web: opt.server.listen(opt.port), peers: opt.peers});
32+
console.log('Relay peer started on port ' + opt.port + ' with /gun');
2633
module.exports = gun;
2734
}());

Diff for: examples/https.sh

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

Diff for: examples/install.sh

+30-12
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,51 @@
33
# README
44
# This will install nodejs and npm on your system,
55
# should work on most places other than Windows.
6+
# For it to run on boot as a server, a recent OS is needed.
67
# Set any environment variables before you run this,
78
# like `export RAD=false` to disable storage, or
89
# pass file paths of `HTTPS_CERT` & `HTTPS_KEY`, etc.
910
# Copy paste and run each line into your terminal.
1011
# If you are on Windows, http://nodejs.org/download/ has
1112
# an installer that will automatically do it for you.
1213
# curl -o- https://raw.githubusercontent.com/amark/gun/master/examples/install.sh | bash
14+
# wget -O - https://raw.githubusercontent.com/amark/gun/master/examples/install.sh | bash
1315

1416
#debian/ubuntu
15-
su -
17+
cd ~
1618
apt-get install sudo -y
1719
sudo apt-get update -y
18-
sudo apt-get install curl git git-core screen -y
20+
sudo apt-get install curl git git-core systemd -y
21+
sudo apt-get install systemctl -y
1922
#fedora/openSUSE
2023
sudo yum check-update -y
21-
sudo yum install curl git git-core screen -y
24+
sudo yum install curl git git-core systemd -y
25+
sudo yum install systemctl -y
26+
27+
#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`
2228

2329
# install nodejs
24-
git clone http://github.com/isaacs/nave.git
25-
sudo ./nave/nave.sh usemain stable
26-
# If you just want nodejs and npm but not gun, stop here.
30+
git clone https://github.com/isaacs/nave.git
31+
./nave/nave.sh usemain stable
2732

28-
npm install gun
29-
cd ./node_modules/gun
33+
# If you just want nodejs and npm but not gun, stop here.
34+
#npm install gun@latest
35+
#cd ./node_modules/gun
36+
mkdir node_modules
37+
git clone https://github.com/amark/gun.git
38+
cd gun
39+
git checkout .
40+
git pull
41+
git checkout master
42+
git checkout $VERSION
43+
git pull
3044
npm install .
3145

32-
# to start the gun examples:
33-
screen -S relay
34-
sudo npm start 80 # change `80` to `443` for https or `8765` for development purposes.
35-
# You can now safely CTRL+A+D to escape without stopping the peer. To stop `killall screen` or `killall node`.
46+
cp ./examples/relay.service /lib/systemd/system/relay.service
47+
echo $PWD >> /lib/systemd/system/relay.service
48+
echo "fs.file-max = 999999" >> /etc/sysctl.conf
49+
ulimit -u unlimited
50+
sysctl -p /etc/sysctl.conf
51+
systemctl daemon-reload
52+
systemctl enable relay
53+
systemctl restart relay

Diff for: examples/relay.service

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[Unit]
2+
Description=GUN relay
3+
Documentation=https://gun.eco
4+
After=network.target
5+
6+
[Install]
7+
WantedBy=multi-user.target
8+
9+
[Service]
10+
Environment=PATH=/usr/bin:/usr/local/bin
11+
LimitNOFILE=infinity
12+
LimitNPROC=infinity
13+
LimitCORE=infinity
14+
StartLimitBurst=999999
15+
StartLimitIntervalSec=999999
16+
Restart=always
17+
ExecStart=node examples/http.js 80
18+
# Environment=NODE_ENV=production
19+
WorkingDirectory=

Diff for: examples/stats.html

+26
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,31 @@
112112
// tbd later
113113
return chart;
114114
}
115+
;(function(){
116+
if('https' != (''+location).slice(0,5) && "localhost" != location.hostname){
117+
$('body').append("<button id='https'>click here to try generating https certs</button>");
118+
if(/^(?!0)(?!.*\.$)((1?\d?\d|25[0-5]|2[0-4]\d)(\.|$)){4}$/.test(location.hostname)){
119+
$('#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.");
120+
return;
121+
}
122+
$('body').append("<input id='email' placeholder='email'/>");
123+
$('#https').on('click', function(){
124+
$(this).text("look at console.log for errors, if none, try https");
125+
var gun = GUN(location.origin + '/gun');
126+
if(!$('#email').val()){
127+
$(this).text("email necessary for certs! Type it in & click here again.");
128+
return;
129+
}
130+
setTimeout(function(){
131+
gun._.opt.mesh.say({dam: 'service', try: 'https', email: $('#email').val(), domain: location.hostname});
132+
setTimeout(function(){
133+
if(gun._.opt.mesh.near){ return }
134+
$('#https').text("It might have worked! try HTTPS!");
135+
}, 9000);
136+
}, 999);
137+
});
138+
}
139+
}());
115140
/*
116141
Notes to Self about Debugging:
117142
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 @@
123148
7. Watch out for get/put loops times, maybe indicating (5) issues?
124149
*/
125150
</script>
151+
<script src="../gun.js"></script>
126152
</body>
127153
</html>

Diff for: lib/axe.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ function start(root){
205205
},1000);
206206
}());
207207

208+
setTimeout(function(){ require('./service')(root) },9);
209+
208210
;(function(){ // THIS IS THE MOB MODULE;
209211
//return; // WORK IN PROGRESS, TEST FINALIZED, NEED TO MAKE STABLE.
210212
/*
@@ -219,7 +221,7 @@ function start(root){
219221
The mob threshold might be determined by other factors,
220222
like how much RAM or CPU stress we have.
221223
*/
222-
opt.mob = opt.mob || parseFloat((opt.env||'').MOB) || 9900; // should be based on ulimit, some clouds as low as 10K.
224+
opt.mob = opt.mob || parseFloat((opt.env||'').MOB) || 999999; // should be based on ulimit, some clouds as low as 10K.
223225

224226
// handle rebalancing a mob of peers:
225227
root.on('hi', function(peer){

Diff for: lib/service.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
module.exports = function(root){
2+
var mesh = root.opt.mesh, cmd = {}, run = require('child_process').exec, fs = require('fs'), home = require('os').homedir(), examp = require('path').resolve(__dirname, '../examples');
3+
mesh.hear['service'] = function(msg, peer){
4+
if(!fs.existsSync('/lib/systemd/system/relay.service')){
5+
mesh.say({dam: '!', err: "Not serviced."});
6+
return;
7+
}
8+
try{ (cmd[msg.try]||cmd.any)(msg, peer); }catch(err){ mesh.say({dam: '!', err: "service error: "+err}) }
9+
}
10+
cmd.https = function(msg, peer){ var log;
11+
if(!msg.email || !msg.domain){
12+
mesh.say({dam: '!', err: 'Domain/email missing, use `location.hostname`!'});
13+
return;
14+
}
15+
if(fs.existsSync(home+'/cert.pem')){
16+
mesh.say({dam: '!', err: 'Cert already exists.'});
17+
return;
18+
}
19+
fs.writeFile(examp+'/../email', msg.email, function(){});
20+
run("bash "+examp+"/https.sh", {env: {'EMAIL': msg.email, 'WEB': examp, 'DOMAIN': msg.domain}}, function(e, out, err){
21+
log = "|"+e+"|"+out+"|"+err;
22+
mesh.say({dam: '!', log: ''+log}, peer);
23+
setTimeout(function(){ process.exit() },999);
24+
});
25+
}
26+
cmd.update = function(msg, peer){ var log, pass;
27+
try{ pass = (''+fs.readFileSync(home+'/pass')).trim() }catch(e){}
28+
if(!pass || (msg.pass||'').trim() != pass){ return }
29+
root.stats.stay.updated = +new Date;
30+
run("bash "+examp+"/install.sh", {env: {VERSION: msg.version||''}}, function(e, out, err){
31+
log = e+"|"+out+"|"+err;
32+
mesh.say({dam: '!', log: ''+log}, peer);
33+
setTimeout(function(){ process.exit() },999);
34+
});
35+
}
36+
;(function update(){ var last;
37+
if(!fs.existsSync(home+'/cert.pem')){ return }
38+
setTimeout(update, 1000*60*60*24);
39+
last = root.stats.stay.updated || 0;
40+
if(+new Date - last < 1000*60*60*24*15){ return }
41+
root.stats.stay.updated = +new Date;
42+
run("bash "+examp+"/install.sh", {}, function(){});
43+
}());
44+
45+
cmd.any = function(){};
46+
47+
};

0 commit comments

Comments
 (0)