freedomboneeee/src/freedombone-app-datserver

378 lines
11 KiB
Bash
Executable File

#!/bin/bash
#
# _____ _ _
# | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# | __| _| -_| -_| . | . | | . | . | | -_|
# |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
#
# Freedom in the Cloud
#
# The main issue here is bootstrapping. What is running
# on the bootstrap server publicbits.org port 6881 ?
#
# Also it appears that users trying to clone have to
# register an account on datbase.org or another datbase
# server
#
# License
# =======
#
# Copyright (C) 2018 Bob Mottram <bob@freedombone.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
VARIANTS='full full-vim'
IN_DEFAULT_INSTALL=0
SHOW_ON_ABOUT=0
DATSERVER_DOMAIN_NAME=
DATSERVER_CODE=
DATSERVER_HYPERCORED_VERSION='1.4.1'
DATSERVER_DIRECTORY=/etc/datserver
DATSERVER_PORT=3282
# bootstrap servers are specified here
DATSERVER_BOOTSTRAP_FILE=$DATSERVER_DIRECTORY/node_modules/datland-swarm-defaults/index.js
DATSERVER_DISCOVERY1='discovery1.publicbits.org'
DATSERVER_DISCOVERY2='discovery2.publicbits.org'
DATSERVER_BOOTSTRAP1='bootstrap1.publicbits.org:6881'
DATSERVER_BOOTSTRAP2='bootstrap2.publicbits.org:6881'
DATSERVER_BOOTSTRAP3='bootstrap3.publicbits.org:6881'
DATSERVER_BOOTSTRAP4='bootstrap4.publicbits.org:6881'
datserver_variables=(MY_USERNAME
DATSERVER_DISCOVERY1
DATSERVER_DISCOVERY2
DATSERVER_BOOTSTRAP1
DATSERVER_BOOTSTRAP2
DATSERVER_BOOTSTRAP3
DATSERVER_BOOTSTRAP4)
function datserver_generate_bootstraps {
{ echo "var extend = require('xtend')";
echo '';
echo "var DAT_DOMAIN = 'dat.local'";
echo 'var DEFAULT_DISCOVERY = [';
echo " '$DATSERVER_DISCOVERY1',";
echo " '$DATSERVER_DISCOVERY2'";
echo ']';
echo 'var DEFAULT_BOOTSTRAP = [';
echo " '$DATSERVER_BOOTSTRAP1',";
echo " '$DATSERVER_BOOTSTRAP2',";
echo " '$DATSERVER_BOOTSTRAP3',";
echo " '$DATSERVER_BOOTSTRAP4'";
echo ']';
echo '';
echo 'var DEFAULT_OPTS = {';
echo ' dns: {server: DEFAULT_DISCOVERY, domain: DAT_DOMAIN},';
echo ' dht: {bootstrap: DEFAULT_BOOTSTRAP}';
echo '}';
echo '';
echo 'module.exports = function (opts) {';
echo ' return extend(DEFAULT_OPTS, opts) // opts takes priority';
echo '}'; } > $DATSERVER_BOOTSTRAP_FILE
chown datserver:datserver $DATSERVER_BOOTSTRAP_FILE
}
function datserver_configure_bootstraps {
read_config_param DATSERVER_DISCOVERY1
read_config_param DATSERVER_DISCOVERY2
read_config_param DATSERVER_BOOTSTRAP1
read_config_param DATSERVER_BOOTSTRAP2
read_config_param DATSERVER_BOOTSTRAP3
read_config_param DATSERVER_BOOTSTRAP4
data=$(mktemp 2>/dev/null)
dialog --backtitle $"Freedombone Control Panel" \
--title $"dat bootstrap servers" \
--form $"Specify discovery and bootstrap servers:\\n" 14 68 6 \
$"Discovery 1:" 1 1 "$DATSERVER_DISCOVERY1" 1 15 50 99 \
$"Discovery 2:" 2 1 "$DATSERVER_DISCOVERY2" 2 15 50 99 \
$"Bootstrap 1:" 3 1 "$DATSERVER_BOOTSTRAP1" 3 15 50 99 \
$"Bootstrap 2:" 4 1 $"$DATSERVER_BOOTSTRAP2" 4 15 50 99 \
$"Bootstrap 3:" 5 1 $"$DATSERVER_BOOTSTRAP3" 5 15 50 99 \
$"Bootstrap 4:" 6 1 $"$DATSERVER_BOOTSTRAP4" 6 15 50 99 \
2> "$data"
sel=$?
case $sel in
1) rm -f "$data"
return;;
255) rm -f "$data"
return;;
esac
DATSERVER_DISCOVERY1=$(sed -n 1p < "$data")
DATSERVER_DISCOVERY2=$(sed -n 2p < "$data")
DATSERVER_BOOTSTRAP1=$(sed -n 3p < "$data")
DATSERVER_BOOTSTRAP2=$(sed -n 4p < "$data")
DATSERVER_BOOTSTRAP3=$(sed -n 4p < "$data")
DATSERVER_BOOTSTRAP4=$(sed -n 4p < "$data")
rm "$data"
write_config_param DATSERVER_DISCOVERY1
write_config_param DATSERVER_DISCOVERY2
write_config_param DATSERVER_BOOTSTRAP1
write_config_param DATSERVER_BOOTSTRAP2
write_config_param DATSERVER_BOOTSTRAP3
write_config_param DATSERVER_BOOTSTRAP4
datserver_generate_bootstraps
systemctl restart datserver
}
function logging_on_datserver {
echo -n ''
}
function logging_off_datserver {
echo -n ''
}
function remove_user_datserver {
echo -n ''
}
function add_user_datserver {
echo -n ''
echo '0'
}
function change_password_datserver {
echo -n ''
}
function install_interactive_datserver {
echo -n ''
APP_INSTALLED=1
}
function reconfigure_datserver {
# This is used if you need to switch identity. Dump old keys and generate new ones
echo -n ''
}
function datserver_add_dat {
data=$(mktemp 2>/dev/null)
dialog --title $"Add a dat" \
--backtitle $"Freedombone Control Panel" \
--inputbox $"dat link:" 8 70 2>"$data"
sel=$?
case $sel in
0)
dat_link=$(<"$data")
if [ "$dat_link" ]; then
if [ ${#dat_link} -gt 5 ]; then
if ! grep -q "$dat_link" $DATSERVER_DIRECTORY/feeds; then
echo "$dat_link" >> $DATSERVER_DIRECTORY/feeds
chown -R datserver:datserver $DATSERVER_DIRECTORY/feeds
systemctl restart datserver
fi
fi
fi
;;
esac
rm -f "$data"
}
function configure_interactive_datserver {
W=(1 $"Add a dat"
2 $"Browse or edit feeds"
3 $"Bootstrap servers")
while true
do
# shellcheck disable=SC2068
selection=$(dialog --backtitle $"Freedombone Administrator Control Panel" --title $"dat server" --menu $"Choose an operation, or ESC for main menu:" 11 70 4 "${W[@]}" 3>&2 2>&1 1>&3)
if [ ! "$selection" ]; then
break
fi
case $selection in
1) datserver_add_dat
;;
2) cd $DATSERVER_DIRECTORY || break
editor feeds
chown -R datserver:datserver $DATSERVER_DIRECTORY/feeds
systemctl restart datserver
;;
3) datserver_configure_bootstraps
;;
esac
done
}
function upgrade_datserver {
CURR_DATSERVER_HYPERCORED_VERSION=$(get_completion_param "datserver hypercored version")
if [[ "$CURR_DATSERVER_HYPERCORED_VERSION" != "$DATSERVER_HYPERCORED_VERSION" ]]; then
cd $DATSERVER_DIRECTORY || exit 254274
systemctl stop datserver
if npm update hypercored@$DATSERVER_HYPERCORED_VERSION; then
set_completion_param "datserver hypercored version" "$DATSERVER_HYPERCORED_VERSION"
fi
datserver_generate_bootstraps
chown -R datserver:datserver "$DATSERVER_DIRECTORY"
systemctl restart datserver
fi
}
function backup_local_datserver {
source_directory=$DATSERVER_DIRECTORY
systemctl stop datserver
dest_directory=datserver
backup_directory_to_usb "$source_directory" $dest_directory
systemctl start datserver
}
function restore_local_datserver {
systemctl stop datserver
temp_restore_dir=/root/tempdatserver
datserver_dir=$DATSERVER_DIRECTORY
restore_directory_from_usb $temp_restore_dir datserver
if [ -d $temp_restore_dir ]; then
if [ -d "$temp_restore_dir$datserver_dir" ]; then
cp -rp "$temp_restore_dir$datserver_dir"/* "$datserver_dir"/
else
if [ ! -d "$datserver_dir" ]; then
mkdir "$datserver_dir"
fi
cp -rp "$temp_restore_dir"/* "$datserver_dir"/
fi
chown -R datserver:datserver "$datserver_dir"
rm -rf $temp_restore_dir
fi
systemctl start datserver
}
function backup_remote_datserver {
source_directory=$DATSERVER_DIRECTORY
systemctl stop datserver
dest_directory=datserver
backup_directory_to_friend "$source_directory" $dest_directory
systemctl start datserver
}
function restore_remote_datserver {
systemctl stop datserver
temp_restore_dir=/root/tempdatserver
datserver_dir=$DATSERVER_DIRECTORY
restore_directory_from_friend $temp_restore_dir datserver
if [ -d $temp_restore_dir ]; then
if [ -d "$temp_restore_dir$datserver_dir" ]; then
cp -rp "$temp_restore_dir$datserver_dir"/* "$datserver_dir"/
else
if [ ! -d "$datserver_dir" ]; then
mkdir "$datserver_dir"
fi
cp -rp $temp_restore_dir/* "$datserver_dir"/
fi
chown -R datserver:datserver "$datserver_dir"
rm -rf $temp_restore_dir
fi
systemctl start datserver
}
function remove_datserver {
if [ -f /etc/systemd/system/datserver.service ]; then
systemctl stop datserver
systemctl disable datserver
rm /etc/systemd/system/datserver.service
fi
userdel -r datserver
remove_nodejs datserver
if [ -d $DATSERVER_DIRECTORY ]; then
rm -rf $DATSERVER_DIRECTORY
fi
remove_app datserver
remove_completion_param install_datserver
sed -i '/datserver/d' "$COMPLETION_FILE"
firewall_remove $DATSERVER_PORT
}
function install_datserver {
apt-get -yq install wget
install_nodejs datserver
if [ -d $DATSERVER_DIRECTORY ]; then
rm -rf $DATSERVER_DIRECTORY
fi
mkdir $DATSERVER_DIRECTORY
cd $DATSERVER_DIRECTORY || exit 3658356
if ! npm install hypercored@$DATSERVER_HYPERCORED_VERSION; then
echo $'hypercored was not installed'
exit 4635439
fi
if ! npm install lil-pids@2.6.1; then
echo $'lil-pids was not installed'
exit 36483463
fi
echo "$DATSERVER_DIRECTORY/node_modules/.bin/hypercored --cwd $DATSERVER_DIRECTORY" > $DATSERVER_DIRECTORY/services
set_completion_param "datserver hypercored version" "$DATSERVER_HYPERCORED_VERSION"
adduser --system --home="$DATSERVER_DIRECTORY" --group datserver
if [ ! -d $DATSERVER_DIRECTORY ]; then
echo $'dat directory was not created'
exit 9568356
fi
datserver_generate_bootstraps
chown -R datserver:datserver "$DATSERVER_DIRECTORY"
firewall_add datserver $DATSERVER_PORT
{ echo '[Unit]';
echo 'After=syslog.target network.target remote-fs.target nss-lookup.target';
echo '';
echo '[Service]';
echo 'User=datserver';
echo 'Group=datserver';
echo "ExecStart=$DATSERVER_DIRECTORY/node_modules/.bin/lil-pids $DATSERVER_DIRECTORY/services $DATSERVER_DIRECTORY/pids";
echo 'Restart=always';
echo "WorkingDirectory=$DATSERVER_DIRECTORY";
echo 'StandardError=syslog';
echo '';
echo '[Install]';
echo 'WantedBy=multi-user.target'; } > /etc/systemd/system/datserver.service
systemctl enable datserver
systemctl start datserver
APP_INSTALLED=1
}
# NOTE: deliberately there is no "exit 0"