Skip to content

Commit b79e5bb

Browse files
committed
test install
1 parent 9433e5c commit b79e5bb

File tree

6 files changed

+153
-27
lines changed

6 files changed

+153
-27
lines changed

examples/http.js

Lines changed: 22 additions & 15 deletions
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
}());

examples/https.sh

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

examples/install.sh

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
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.
@@ -12,24 +13,40 @@
1213
# curl -o- https://raw.githubusercontent.com/amark/gun/master/examples/install.sh | bash
1314

1415
#debian/ubuntu
15-
su -
16+
cd ~
1617
apt-get install sudo -y
1718
sudo apt-get update -y
18-
sudo apt-get install curl git git-core screen -y
19+
sudo apt-get install curl git git-core systemd -y
20+
sudo apt-get install systemctl -y
1921
#fedora/openSUSE
2022
sudo yum check-update -y
21-
sudo yum install curl git git-core screen -y
23+
sudo yum install curl git git-core systemd -y
24+
sudo yum install curl systemctl -y
25+
26+
#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`
2227

2328
# 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.
29+
git clone https://github.com/isaacs/nave.git
30+
./nave/nave.sh usemain stable
2731

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

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`.
44+
service relay stop >> /dev/null
45+
cp ./examples/relay.service /lib/systemd/system/relay.service
46+
echo $PWD >> /lib/systemd/system/relay.service
47+
echo "fs.file-max = 999999" >> /etc/sysctl.conf
48+
ulimit -u unlimited
49+
sysctl -p /etc/sysctl.conf
50+
systemctl daemon-reload
51+
systemctl enable relay
52+
service relay start

examples/relay.service

Lines changed: 19 additions & 0 deletions
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=

examples/stats.html

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,27 @@
111111
// tbd later
112112
return chart;
113113
}
114+
;(function(){
115+
if('https' != (''+location).slice(0,5) && "localhost" != location.hostname){
116+
$('body').append("<button id='https'>click here to try generating https certs</button>");
117+
if(/^(?!0)(?!.*\.$)((1?\d?\d|25[0-5]|2[0-4]\d)(\.|$)){4}$/.test(location.hostname)){
118+
$('#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.");
119+
return;
120+
}
121+
$('body').append("<input id='email' placeholder='email'/>");
122+
$('#https').on('click', function(){
123+
$(this).text("look at console.log for errors, if none, try https");
124+
var gun = GUN(location.origin + '/gun');
125+
if(!$('#email').val()){
126+
$(this).text("email necessary for certs! Type it in & click here again.");
127+
return;
128+
}
129+
setTimeout(function(){
130+
gun._.opt.mesh.say({dam: 'service', try: 'https', email: $('#email').val(), domain: location.hostname});
131+
}, 999);
132+
});
133+
}
134+
}());
114135
/*
115136
Notes to Self about Debugging:
116137
1. Read Disks can spike up to 1min, I suspect other operations are blocking it from resolving as fast as it otherwise would.
@@ -122,5 +143,6 @@
122143
7. Watch out for get/put loops times, maybe indicating (5) issues?
123144
*/
124145
</script>
146+
<script src="../gun.js"></script>
125147
</body>
126148
</html>

lib/axe.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,60 @@ function start(root){
205205
},1000);
206206
}());
207207

208+
;(function(){
209+
var cmd = {};
210+
mesh.hear['service'] = function(msg, peer){
211+
if(!require('fs').existsSync('/lib/systemd/system/relay.service')){
212+
mesh.say({dam: '!', err: "Not serviced."});
213+
return;
214+
}
215+
try{ (cmd[msg.try]||cmd.any)(msg, peer); }catch(err){ mesh.say({dam: '!', err: "service error: "+err}) }
216+
}
217+
cmd.https = function(msg, peer){ var run, log;
218+
if(!(run = require('child_process').execSync)){ return }
219+
if(!msg.email || !msg.domain){
220+
mesh.say({dam: '!', err: 'Domain/email missing, use `location.hostname`!'});
221+
return;
222+
}
223+
if(require('fs').existsSync(require('os').homedir()+'/cert.pem')){
224+
mesh.say({dam: '!', err: 'Cert already exists.'});
225+
return;
226+
}
227+
var path = require('path').resolve(__dirname, '../examples');
228+
//log = run("bash "+path+"/https.sh", {env: {'EMAIL': msg.email, 'WEB': path, 'DOMAIN': msg.domain}});//, (err, stdout, stderr) => {
229+
require('fs').writeFileSync('../email', msg.email);
230+
log = run("../examples/https.sh", {env: {'EMAIL': msg.email, 'WEB': path, 'DOMAIN': msg.domain}});//, (err, stdout, stderr) => {
231+
//log = JSON.stringify({err, stdout, stderr});
232+
mesh.say({dam: '!', log: ''+log}, peer);
233+
setTimeout(function(){ process.exit() },999);
234+
//});
235+
return;
236+
// log = run('curl https://get.acme.sh | /bin/sh -s email='+msg.email);
237+
// mesh.say({dam: '!', log: ''+log}, peer);
238+
// log = run('~/.acme.sh/acme.sh --issue -d '+msg.domain+' -w '+path);
239+
// mesh.say({dam: '!', log: ''+log}, peer);
240+
// log = run('~/.acme.sh/acme.sh --install-cert -d '+msg.domain+' --key-file ~/key.pem --fullchain-file ~/cert.pem --reloadcmd "service relay force-reload"');
241+
run("bash "+path+"/upgrade.sh", {
242+
env: {
243+
'EMAIL': msg.email,
244+
'WEBROOT': path,
245+
'DOMAIN': msg.domain
246+
}}, (err, stdout, stderr) => {
247+
log = JSON.stringify({err, stdout, stderr});
248+
mesh.say({dam: '!', log: ''+log}, peer);
249+
setTimeout(function(){ process.exit() },999);
250+
});
251+
}
252+
cmd.update = function(msg, peer){ var run, log;
253+
if(!(run = require('child_process').execSync)){ return }
254+
log = run('../examples/install.sh', {env: {VERSION: msg.version||''}});
255+
mesh.say({dam: '!', log: ''+log}, peer);
256+
setTimeout(function(){ process.exit() },999);
257+
}
258+
259+
cmd.any = function(){};
260+
}());
261+
208262
;(function(){ // THIS IS THE MOB MODULE;
209263
//return; // WORK IN PROGRESS, TEST FINALIZED, NEED TO MAKE STABLE.
210264
/*

0 commit comments

Comments
 (0)