From c83e73fc058e085187ccb726caa90917314250c9 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Sat, 9 Apr 2016 20:26:46 +0100 Subject: [PATCH] Syncthing support --- src/freedombone | 98 +++++++++++-- src/freedombone-backup-local | 2 +- src/freedombone-syncthing | 273 +++++++++++++++++++++++++++++++++++ 3 files changed, 360 insertions(+), 13 deletions(-) create mode 100755 src/freedombone-syncthing diff --git a/src/freedombone b/src/freedombone index b632e94a..4a6534bb 100755 --- a/src/freedombone +++ b/src/freedombone @@ -239,6 +239,15 @@ OWNCLOUD_ADMIN_PASSWORD= OWNCLOUD_PATH=/var/www/owncloud OWNCLOUD_UPGRADE_PATH=/root/owncloud_upgrade +SYNCTHING_ID= +SYNCTHING_CONFIG_PATH=/root/.config/syncthing +SYNCTHING_CONFIG_FILE=$SYNCTHING_CONFIG_PATH/config.xml +SYNCTHING_RELAY_SERVER='https://relays.syncthing.net/endpoint' +SYNCTHING_RELEASES='https://api.github.com/repos/syncthing/syncthing/releases?per_page=30' +SYNCTHING_PORT=22000 +SYNCTHING_SHARED_DATA=/var/lib/syncthing/SyncShared +SYNCTHING_USER_IDS_FILE='.syncthingids' + # Domain name for your wiki WIKI_DOMAIN_NAME= WIKI_ADMIN_PASSWORD= @@ -1071,6 +1080,27 @@ function read_configuration { if [[ $CONFIGURATION_FILE != "/root/${PROJECT_NAME}.cfg" ]]; then cp $CONFIGURATION_FILE /root/${PROJECT_NAME}.cfg fi + if grep -q "SYNCTHING_ID" $CONFIGURATION_FILE; then + SYNCTHING_ID=$(grep "SYNCTHING_ID" $CONFIGURATION_FILE | awk -F '=' '{print $2}') + fi + if grep -q "SYNCTHING_CONFIG_PATH" $CONFIGURATION_FILE; then + SYNCTHING_CONFIG_PATH=$(grep "SYNCTHING_CONFIG_PATH" $CONFIGURATION_FILE | awk -F '=' '{print $2}') + fi + if grep -q "SYNCTHING_CONFIG_FILE" $CONFIGURATION_FILE; then + SYNCTHING_CONFIG_FILE=$(grep "SYNCTHING_CONFIG_FILE" $CONFIGURATION_FILE | awk -F '=' '{print $2}') + fi + if grep -q "SYNCTHING_RELAY_SERVER" $CONFIGURATION_FILE; then + SYNCTHING_RELAY_SERVER=$(grep "SYNCTHING_RELAY_SERVER" $CONFIGURATION_FILE | awk -F '=' '{print $2}') + fi + if grep -q "SYNCTHING_RELEASES" $CONFIGURATION_FILE; then + SYNCTHING_RELEASES=$(grep "SYNCTHING_RELEASES" $CONFIGURATION_FILE | awk -F '=' '{print $2}') + fi + if grep -q "SYNCTHING_PORT" $CONFIGURATION_FILE; then + SYNCTHING_PORT=$(grep "SYNCTHING_PORT" $CONFIGURATION_FILE | awk -F '=' '{print $2}') + fi + if grep -q "SYNCTHING_SHARED_DATA" $CONFIGURATION_FILE; then + SYNCTHING_SHARED_DATA=$(grep "SYNCTHING_SHARED_DATA" $CONFIGURATION_FILE | awk -F '=' '{print $2}') + fi if grep -q "VOIP_TURN_PORT" $CONFIGURATION_FILE; then VOIP_TURN_PORT=$(grep "VOIP_TURN_PORT" $CONFIGURATION_FILE | awk -F '=' '{print $2}') fi @@ -1557,7 +1587,7 @@ function rss_reader_modifications { if [ ! -d $RSS_READER_PATH ]; then return fi - + # ensure that socks5 proxy is used if ! grep -q "CURLOPT_PROXYTYPE" $RSS_READER_PATH/plugins/af_unburn/init.php; then sed -i '/curl_setopt($ch, CURLOPT_PROXY, _CURL_HTTP_PROXY);/a \\t\t\t\t\tcurl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);' $RSS_READER_PATH/plugins/af_unburn/init.php @@ -1614,13 +1644,13 @@ function set_repo_commit { systemctl restart gogs fi if [[ $repo_dir == *"toxcore" ]]; then - sed -i 's|ExecStart=.*|ExecStart=/usr/local/bin/tox-bootstrapd --config /etc/tox-bootstrapd.conf|g' $rootdir/etc/systemd/system/tox-bootstrapd.service - autoreconf -i - ./configure --enable-daemon - make - make install - systemctl daemon-reload - systemctl restart tox-bootstrapd.service + sed -i 's|ExecStart=.*|ExecStart=/usr/local/bin/tox-bootstrapd --config /etc/tox-bootstrapd.conf|g' $rootdir/etc/systemd/system/tox-bootstrapd.service + autoreconf -i + ./configure --enable-daemon + make + make install + systemctl daemon-reload + systemctl restart tox-bootstrapd.service fi if [[ $repo_dir == *"toxic" ]]; then make @@ -6213,6 +6243,49 @@ function upgrade_owncloud_start { fi } +function install_syncthing { + if [[ $SYSTEM_TYPE == "$VARIANT_WRITER" || $SYSTEM_TYPE == "$VARIANT_MAILBOX" || $SYSTEM_TYPE == "$VARIANT_CHAT" || $SYSTEM_TYPE == "$VARIANT_SOCIAL" || $SYSTEM_TYPE == "$VARIANT_MEDIA" || $SYSTEM_TYPE == "$VARIANT_DEVELOPER" || $SYSTEM_TYPE == "$VARIANT_MESH" ]]; then + return + fi + if grep -Fxq "install_syncthing" $COMPLETION_FILE; then + return + fi + + apt-get -y install curl + + curl -s https://syncthing.net/release-key.txt | apt-key add - + echo "deb http://apt.syncthing.net/ syncthing release" | tee /etc/apt/sources.list.d/syncthing.list + apt-get update + apt-get -y install syncthing + + # This probably does need to run as root so that it can access the Sync directories + # in each user's home directory + echo '[Unit]' > /etc/systemd/system/syncthing.service + echo 'Description=Syncthing - Open Source Continuous File Synchronization' >> /etc/systemd/system/syncthing.service + echo 'Documentation=man:syncthing(1)' >> /etc/systemd/system/syncthing.service + echo 'After=network.target' >> /etc/systemd/system/syncthing.service + echo 'Wants=syncthing-inotify@.service' >> /etc/systemd/system/syncthing.service + echo '' >> /etc/systemd/system/syncthing.service + echo '[Service]' >> /etc/systemd/system/syncthing.service + echo 'User=root' >> /etc/systemd/system/syncthing.service + echo 'ExecStart=/usr/bin/syncthing -no-browser -no-restart -logflags=0' >> /etc/systemd/system/syncthing.service + echo 'Restart=on-failure' >> /etc/systemd/system/syncthing.service + echo 'SuccessExitStatus=3 4' >> /etc/systemd/system/syncthing.service + echo 'RestartForceExitStatus=3 4' >> /etc/systemd/system/syncthing.service + echo '' >> /etc/systemd/system/syncthing.service + echo '[Install]' >> /etc/systemd/system/syncthing.service + echo 'WantedBy=multi-user.target' >> /etc/systemd/system/syncthing.service + systemctl enable syncthing + systemctl daemon-reload + systemctl start syncthing + + if ! grep -q "syncthing" /etc/crontab; then + echo "* * * * * root ${PROJECT_NAME}-syncthing > /dev/null" >> /etc/crontab + fi + + echo 'install_syncthing' >> $COMPLETION_FILE +} + function upgrade_owncloud_finish { # copies data from a temporary upgrade directory to the new # owncloud install @@ -7345,7 +7418,7 @@ function install_irc_server { if [ ! $IRC_PASSWORD ]; then IRC_PASSWORD="$(openssl rand -base64 32 | cut -c1-10)" fi - + echo '**************************************************' > /etc/ngircd/motd echo $'* F R E E D O M B O N E I R C *' >> /etc/ngircd/motd echo '* *' >> /etc/ngircd/motd @@ -8349,7 +8422,7 @@ function install_rss_reader { RSS_READER_ADMIN_PASSWORD_HASH=$(echo -n "${RSS_READER_ADMIN_PASSWORD}" | sha1sum | awk -F ' ' '{print $1}') mysql -u root --password="$MARIADB_PASSWORD" -e "update ttrss_users set pwd_hash = 'SHA1:${RSS_READER_ADMIN_PASSWORD_HASH}', salt= '' WHERE login = 'admin';" ttrss - rss_reader_modifications + rss_reader_modifications configure_php @@ -9668,7 +9741,7 @@ function install_mediagoblin { chown -hR mediagoblin:www-data /var/log/mediagoblin mkdir /var/run/mediagoblin chown -hR mediagoblin:www-data /var/run/mediagoblin - + if [ ! -f $MEDIAGOBLIN_WORKING_DIRECTORY/mediagoblin.ini ]; then echo $'mediagoblin.ini not found' exit 737529 @@ -10835,7 +10908,8 @@ script_for_attaching_usb_drive install_web_server install_web_server_access_control configure_firewall_for_web_server -install_owncloud_official_deb +install_syncthing +#install_owncloud_official_deb upgrade_golang install_gogs install_xmpp diff --git a/src/freedombone-backup-local b/src/freedombone-backup-local index 80b19e54..cd3b8e13 100755 --- a/src/freedombone-backup-local +++ b/src/freedombone-backup-local @@ -273,7 +273,7 @@ function backup_users { # Backup user files for d in /home/*/ ; do USERNAME=$(echo "$d" | awk -F '/' '{print $3}') - if [[ $USERNAME != "git" && $USERNAME != "mirrors" ]]; then + if [[ $USERNAME != "git" && $USERNAME != "mirrors" && $USERNAME != "sync" ]]; then # Backup any gpg keys if [ -d /home/$USERNAME/.gnupg ]; then diff --git a/src/freedombone-syncthing b/src/freedombone-syncthing new file mode 100755 index 00000000..5cb301ff --- /dev/null +++ b/src/freedombone-syncthing @@ -0,0 +1,273 @@ +#!/bin/bash +# +# .---. . . +# | | | +# |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-. +# | | (.-' (.-' ( | ( )| | | | )( )| | (.-' +# ' ' --' --' -' - -' ' ' -' -' -' ' - --' +# +# Freedom in the Cloud +# +# Checks for changed syncthing device IDs within user home directories +# and then recreates the syncthing configuration file accordingly +# +# License +# ======= +# +# Copyright (C) 2016 Bob Mottram +# +# 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 . + +NO_OF_ARGS=$# + +PROJECT_NAME='freedombone' + +export TEXTDOMAIN=$PROJECT_NAME-syncthing +export TEXTDOMAINDIR="/usr/share/locale" + +# File which keeps track of what has already been installed +COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt + +SYNCTHING_ID= +SYNCTHING_CONFIG_PATH=/root/.config/syncthing +SYNCTHING_CONFIG_FILE=$SYNCTHING_CONFIG_PATH/config.xml +SYNCTHING_RELAY_SERVER='https://relays.syncthing.net/endpoint' +SYNCTHING_RELEASES='https://api.github.com/repos/syncthing/syncthing/releases?per_page=30' +SYNCTHING_PORT=22000 +SYNCTHING_SHARED_DATA=/var/lib/syncthing/SyncShared +SYNCTHING_USER_IDS_FILE='.syncthingids' +CHANGED= +TEMP_IDS_FILE=/root/.synthingids + +function new_syncthing_id { + for i in {1..8} + do + v="" + for j in {1..2} + do + v2=`echo "obase=16;$RANDOM" | bc` + v=$v$v2 + done + v=$(echo "$v" | cut -c1-7) + if [[ ${i} < 8 ]]; then + v=$v"-" + fi + echo -n "$v" + done + echo "$v" +} + +function create_syncthing_config { + if grep -q "Syncthing ID" $COMPLETION_FILE; then + SYNCTHING_ID=$(cat $COMPLETION_FILE | grep "Syncthing ID" | awk -F ':' '{print $2}') + else + if [ -f $SYNCTHING_CONFIG_FILE ]; then + SYNCTHING_ID=$(cat $SYNCTHING_CONFIG_FILE | grep "device id=" | head -n 1 | awk -F '"' '{print $2}') + else + SYNCTHING_ID=$(new_syncthing_id) + fi + fi + + if ! grep -q "Syncthing ID" $COMPLETION_FILE; then + echo "Syncthing ID:$SYNCTHING_ID" >> $COMPLETION_FILE + else + sed -i "s|Syncthing ID.*|Syncthing ID:$SYNCTHING_ID|g" $COMPLETION_FILE + fi + + if [ ! -d $SYNCTHING_CONFIG_PATH ]; then + mkdir -p $SYNCTHING_CONFIG_PATH + fi + if [ ! -d $SYNCTHING_SHARED_DATA ]; then + mkdir -p $SYNCTHING_SHARED_DATA + fi + + echo '' > $SYNCTHING_CONFIG_FILE + + for d in /home/*/ ; do + USERNAME=$(echo "$d" | awk -F '/' '{print $3}') + if [[ $USERNAME != "git" && $USERNAME != "mirrors" && $USERNAME != "sync" ]]; then + echo " " >> $SYNCTHING_CONFIG_FILE + # include any specified device IDs for this user + if [ -f /home/$USERNAME/$SYNCTHING_USER_IDS_FILE ]; then + echo "" > $TEMP_IDS_FILE + while read -r line || [[ -n "$line" ]]; do + if [[ $line != *"#"* && $line != *"*"* && $line != *'/'* && $line == *"-"* ]]; then + if [ ${#line} -gt 10 ]; then + if ! grep -q "$line" $TEMP_IDS_FILE; then + echo " " >> $SYNCTHING_CONFIG_FILE + echo "$line" >> $TEMP_IDS_FILE + fi + fi + fi + done < /home/$USERNAME/$SYNCTHING_USER_IDS_FILE + rm $TEMP_IDS_FILE + fi + echo " " >> $SYNCTHING_CONFIG_FILE + echo ' 1' >> $SYNCTHING_CONFIG_FILE + echo ' ' >> $SYNCTHING_CONFIG_FILE + echo ' 0' >> $SYNCTHING_CONFIG_FILE + echo ' 0' >> $SYNCTHING_CONFIG_FILE + echo ' 0' >> $SYNCTHING_CONFIG_FILE + echo ' random' >> $SYNCTHING_CONFIG_FILE + echo ' false' >> $SYNCTHING_CONFIG_FILE + echo ' 0' >> $SYNCTHING_CONFIG_FILE + echo ' 0' >> $SYNCTHING_CONFIG_FILE + echo ' 0' >> $SYNCTHING_CONFIG_FILE + echo ' 10' >> $SYNCTHING_CONFIG_FILE + echo ' false' >> $SYNCTHING_CONFIG_FILE + echo ' ' >> $SYNCTHING_CONFIG_FILE + fi + done + + echo " " >> $SYNCTHING_CONFIG_FILE + # all user devices may access this shared directory + echo "" > $TEMP_IDS_FILE + for d in /home/*/ ; do + USERNAME=$(echo "$d" | awk -F '/' '{print $3}') + if [[ $USERNAME != "git" && $USERNAME != "mirrors" && $USERNAME != "sync" ]]; then + if [ -f /home/$USERNAME/$SYNCTHING_USER_IDS_FILE ]; then + while read -r line || [[ -n "$line" ]]; do + if [[ $line != *"#"* && $line != *"*"* && $line != *'/'* && $line == *"-"* ]]; then + if [ ${#line} -gt 10 ]; then + if ! grep -q "$line" $TEMP_IDS_FILE; then + echo " " >> $SYNCTHING_CONFIG_FILE + echo "$line" >> $TEMP_IDS_FILE + fi + fi + fi + done < /home/$USERNAME/$SYNCTHING_USER_IDS_FILE + fi + fi + done + rm $TEMP_IDS_FILE + echo " " >> $SYNCTHING_CONFIG_FILE + echo ' 1' >> $SYNCTHING_CONFIG_FILE + echo ' ' >> $SYNCTHING_CONFIG_FILE + echo ' 0' >> $SYNCTHING_CONFIG_FILE + echo ' 0' >> $SYNCTHING_CONFIG_FILE + echo ' 0' >> $SYNCTHING_CONFIG_FILE + echo ' random' >> $SYNCTHING_CONFIG_FILE + echo ' false' >> $SYNCTHING_CONFIG_FILE + echo ' 0' >> $SYNCTHING_CONFIG_FILE + echo ' 0' >> $SYNCTHING_CONFIG_FILE + echo ' 0' >> $SYNCTHING_CONFIG_FILE + echo ' 10' >> $SYNCTHING_CONFIG_FILE + echo ' false' >> $SYNCTHING_CONFIG_FILE + echo ' ' >> $SYNCTHING_CONFIG_FILE + + + echo " " >> $SYNCTHING_CONFIG_FILE + echo '
dynamic
' >> $SYNCTHING_CONFIG_FILE + echo '
' >> $SYNCTHING_CONFIG_FILE + + echo "" > $TEMP_IDS_FILE + for d in /home/*/ ; do + USERNAME=$(echo "$d" | awk -F '/' '{print $3}') + if [[ $USERNAME != "git" && $USERNAME != "mirrors" && $USERNAME != "sync" ]]; then + if [ -f /home/$USERNAME/$SYNCTHING_USER_IDS_FILE ]; then + while read -r line || [[ -n "$line" ]]; do + if [[ $line != *"#"* && $line != *"*"* && $line != *'/'* && $line == *"-"* ]]; then + if [ ${#line} -gt 10 ]; then + if ! grep -q "$line" $TEMP_IDS_FILE; then + echo " " >> $SYNCTHING_CONFIG_FILE + echo '
dynamic
' >> $SYNCTHING_CONFIG_FILE + echo '
' >> $SYNCTHING_CONFIG_FILE + echo "$line" >> $TEMP_IDS_FILE + fi + fi + fi + done < /home/$USERNAME/$SYNCTHING_USER_IDS_FILE + fi + fi + done + rm $TEMP_IDS_FILE + + echo ' ' >> $SYNCTHING_CONFIG_FILE + echo " tcp://0.0.0.0:$SYNCTHING_PORT" >> $SYNCTHING_CONFIG_FILE + echo ' default' >> $SYNCTHING_CONFIG_FILE + echo ' true' >> $SYNCTHING_CONFIG_FILE + echo ' true' >> $SYNCTHING_CONFIG_FILE + echo ' 21027' >> $SYNCTHING_CONFIG_FILE + echo ' [ff12::8384]:21027' >> $SYNCTHING_CONFIG_FILE + echo " dynamic+$SYNCTHING_RELAY_SERVER" >> $SYNCTHING_CONFIG_FILE + echo ' 0' >> $SYNCTHING_CONFIG_FILE + echo ' 0' >> $SYNCTHING_CONFIG_FILE + echo ' 60' >> $SYNCTHING_CONFIG_FILE + echo ' true' >> $SYNCTHING_CONFIG_FILE + echo ' 10' >> $SYNCTHING_CONFIG_FILE + echo ' true' >> $SYNCTHING_CONFIG_FILE + echo ' true' >> $SYNCTHING_CONFIG_FILE + echo ' 60' >> $SYNCTHING_CONFIG_FILE + echo ' 30' >> $SYNCTHING_CONFIG_FILE + echo ' 10' >> $SYNCTHING_CONFIG_FILE + echo ' -1' >> $SYNCTHING_CONFIG_FILE + echo ' ' >> $SYNCTHING_CONFIG_FILE + echo ' https://data.syncthing.net/newdata' >> $SYNCTHING_CONFIG_FILE + echo ' false' >> $SYNCTHING_CONFIG_FILE + echo ' 1800' >> $SYNCTHING_CONFIG_FILE + echo ' true' >> $SYNCTHING_CONFIG_FILE + echo ' 12' >> $SYNCTHING_CONFIG_FILE + echo ' 24' >> $SYNCTHING_CONFIG_FILE + echo ' true' >> $SYNCTHING_CONFIG_FILE + echo ' 5' >> $SYNCTHING_CONFIG_FILE + echo ' true' >> $SYNCTHING_CONFIG_FILE + echo ' false' >> $SYNCTHING_CONFIG_FILE + echo ' 1' >> $SYNCTHING_CONFIG_FILE + echo " $SYNCTHING_RELEASES" >> $SYNCTHING_CONFIG_FILE + echo ' ' >> $SYNCTHING_CONFIG_FILE + echo '
' >> $SYNCTHING_CONFIG_FILE + + # give each user account a file containing the device id for this server + # This allows it to appear within the user control panel + for d in /home/*/ ; do + USERNAME=$(echo "$d" | awk -F '/' '{print $3}') + if [[ $USERNAME != "git" && $USERNAME != "mirrors" && $USERNAME != "sync" ]]; then + echo "$SYNCTHING_ID" > /home/$USERNAME/.syncthing-server-id + chown $USERNAME:$USERNAME /home/$USERNAME/.syncthing-server-id + fi + done +} + +function user_devices_changed { + CHANGED= + if [ ! -f $SYNCTHING_CONFIG_FILE ]; then + CHANGED=1 + return + fi + + for d in /home/*/ ; do + USERNAME=$(echo "$d" | awk -F '/' '{print $3}') + if [[ $USERNAME != "git" && $USERNAME != "mirrors" && $USERNAME != "sync" ]]; then + if [ -f /home/$USERNAME/$SYNCTHING_USER_IDS_FILE ]; then + while read -r line || [[ -n "$line" ]]; do + if [[ $line != *"#"* && $line != *"*"* && $line != *'/'* && $line == *"-"* ]]; then + if [ ${#line} -gt 10 ]; then + if ! grep -q "$line" $SYNCTHING_CONFIG_FILE; then + CHANGED=1 + fi + fi + fi + done < /home/$USERNAME/$SYNCTHING_USER_IDS_FILE + fi + fi + done +} + +user_devices_changed +if [ $CHANGED ]; then + create_syncthing_config + systemctl restart syncthing +fi + +exit 0