dat server app

This commit is contained in:
Bob Mottram 2018-04-09 13:32:43 +01:00
parent ab63f891ba
commit b200d55769
2 changed files with 324 additions and 4 deletions

323
src/freedombone-app-datserver Executable file
View File

@ -0,0 +1,323 @@
#!/bin/bash
#
# _____ _ _
# | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# | __| _| -_| -_| . | . | | . | . | | -_|
# |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
#
# Freedom in the Cloud
#
# 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_DAT_VERSION='13.10.0'
DATSERVER_DIRECTORY=/etc/datserver
datserver_variables=(MY_USERNAME)
function logging_on_datserver {
echo -n ''
}
function logging_off_datserver {
echo -n ''
}
function remove_user_datserver {
remove_username="$1"
"${PROJECT_NAME}-pass" -u "$remove_username" --rmapp datserver
}
function add_user_datserver {
new_username="$1"
new_user_password="$2"
"${PROJECT_NAME}-pass" -u "$new_username" -a datserver -p "$new_user_password"
echo '0'
}
function install_interactive_datserver {
echo -n ''
APP_INSTALLED=1
}
function change_password_datserver {
curr_username="$1"
new_user_password="$2"
"${PROJECT_NAME}-pass" -u "$curr_username" -a datserver -p "$new_user_password"
}
function reconfigure_datserver {
# This is used if you need to switch identity. Dump old keys and generate new ones
echo -n ''
}
function datserver_add_url {
data=$(mktemp 2>/dev/null)
dialog --title $"Add a file to dat using its URL" \
--backtitle $"Freedombone Control Panel" \
--inputbox $"File URL" 8 70 2>"$data"
sel=$?
case $sel in
0)
file_url=$(<"$data")
if [ "$file_url" ]; then
if [ ${#file_url} -gt 5 ]; then
clear
cd $DATSERVER_DIRECTORY || return
wget "$file_url"
chown -R datserver:datserver $DATSERVER_DIRECTORY/*
fi
fi
;;
esac
rm -f "$data"
}
function datserver_add_file {
read_config_param MY_USERNAME
selected_file=$(dialog --title "Choose a file to store within dat" --fselect "/home/$MY_USERNAME/" 30 60)
if [ ! "$selected_file" ]; then
return
fi
if [ ! -f "$selected_file" ]; then
return
fi
if [[ "$selected_file" == "$DATSERVER_DIRECTORY"* ]]; then
return
fi
clear
echo ''
echo $"Copying $selected_file"
cp "$selected_file" $DATSERVER_DIRECTORY
chown -R datserver:datserver $DATSERVER_DIRECTORY/*
}
function datserver_show_address {
cd $DATSERVER_DIRECTORY || return
dat_address=$(dat status | grep "dat:")
clear
echo ''
echo "$dat_address"
echo ''
echo -n "$dat_address" | qrencode -t UTF8
echo ''
echo ' '
# shellcheck disable=SC2034
read -n1 -r -p $"Press any key to continue..." key
}
function configure_interactive_datserver {
W=(1 $"Add a local file"
2 $"Add a file from its URL")
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:" 14 70 3 "${W[@]}" 3>&2 2>&1 1>&3)
if [ ! "$selection" ]; then
break
fi
case $selection in
1) datserver_show_address
;;
2) datserver_add_file
;;
3) datserver_add_url
;;
esac
done
}
function upgrade_datserver {
CURR_DATSERVER_DAT_VERSION=$(get_completion_param "datserver version")
if [[ "$CURR_DATSERVER_DAT_VERSION" == "$DATSERVER_DAT_VERSION" ]]; then
return
fi
if npm update -g dat@$DATSERVER_DAT_VERSION; then
set_completion_param "datserver version" "$DATSERVER_DAT_VERSION"
fi
chown -R datserver:datserver "$DATSERVER_DIRECTORY"
}
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
npm uninstall -g dat
remove_nodejs datserver
remove_onion_service datserver "${DATSERVER_ONION_PORT}"
if grep -q "datserver" /etc/crontab; then
sed -i "/datserver/d" /etc/crontab
fi
if [ -d $DATSERVER_DIRECTORY ]; then
rm -rf $DATSERVER_DIRECTORY
fi
remove_app datserver
remove_completion_param install_datserver
sed -i '/datserver/d' "$COMPLETION_FILE"
}
function install_datserver {
apt-get -yq install wget
install_nodejs datserver
if ! npm install -g dat@$DATSERVER_DAT_VERSION; then
exit 2468736
fi
if [ ! -f /root/.npm-global/bin/dat ]; then
echo $'dat was not installed'
exit 156835
fi
set_completion_param "datserver version" "$DATSERVER_DAT_VERSION"
useradd -d "$DATSERVER_DIRECTORY" -s /bin/false datserver
if [ ! -d $DATSERVER_DIRECTORY ]; then
echo $'dat directory was not created'
exit 9568356
fi
cd $DATSERVER_DIRECTORY || exit 3578635
echo -e "\\n\\n" | dat create
if [ ! -d $DATSERVER_DIRECTORY/.dat ]; then
echo $'dat was not created'
exit 753563
fi
dat_address=$(dat status | grep "dat:")
if [ ! "$dat_address" ]; then
echo $'dat address could not be found'
exit 235479763
fi
if [ ${#dat_address} -lt 10 ]; then
echo $'dat address is too short'
exit 4789264359
fi
echo "$dat_address created"
{ echo '[Unit]';
echo 'Description=datserver';
echo 'After=syslog.target';
echo 'After=network.target';
echo '';
echo '[Service]';
echo 'Type=simple';
echo 'User=datserver';
echo 'Group=datserver';
echo "WorkingDirectory=$DATSERVER_DIRECTORY";
echo "ExecStart=/root/.npm-global/bin/dat share $DATSERVER_DIRECTORY";
echo 'Environment=NODE_ENV=production';
echo 'Environment=USER=datserver';
echo 'Restart=always';
echo 'StandardError=syslog';
echo '';
echo '[Install]';
echo 'WantedBy=multi-user.target'; } >> "/etc/systemd/system/datserver.service"
systemctl enable datserver
chown -R datserver:datserver "$DATSERVER_DIRECTORY"
systemctl start datserver
APP_INSTALLED=1
}
# NOTE: deliberately there is no "exit 0"

View File

@ -269,10 +269,7 @@ function peertube_import_from_file {
nodecmd='torsocks node' nodecmd='torsocks node'
fi fi
data2=$(mktemp 2>/dev/null) selected_file=$(dialog --title "Choose the video file (select with spacebar)" --fselect "/home/$MY_USERNAME/" 30 60)
dialog --title "Choose the video file (select with spacebar)" --fselect "/home/$MY_USERNAME/" 30 60 2> "$data2"
selected_file=$(cat "$data2")
rm -f "$data2"
if [ ! "$selected_file" ]; then if [ ! "$selected_file" ]; then
return return
fi fi