freedombone-app-syncthing 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Syncthing application
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2014-2016 Bob Mottram <bob@robotics.uk.to>
  17. #
  18. # This program is free software: you can redistribute it and/or modify
  19. # it under the terms of the GNU Affero General Public License as published by
  20. # the Free Software Foundation, either version 3 of the License, or
  21. # (at your option) any later version.
  22. #
  23. # This program is distributed in the hope that it will be useful,
  24. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. # GNU Affero General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU Affero General Public License
  29. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. VARIANTS='full cloud'
  31. SYNCTHING_ID=
  32. SYNCTHING_CONFIG_PATH=/root/.config/syncthing
  33. SYNCTHING_CONFIG_FILE=$SYNCTHING_CONFIG_PATH/config.xml
  34. SYNCTHING_RELAY_SERVER='https://relays.syncthing.net/endpoint'
  35. SYNCTHING_RELEASES='https://api.github.com/repos/syncthing/syncthing/releases?per_page=30'
  36. SYNCTHING_PORT=22000
  37. SYNCTHING_SHARED_DATA=/var/lib/syncthing/SyncShared
  38. SYNCTHING_USER_IDS_FILE='.syncthingids'
  39. function syncthing_create_ids_file {
  40. if [ ! -f ~/.syncthing-server-id ]; then
  41. return
  42. fi
  43. SYNCTHING_ID=$(cat ~/.syncthing-server-id)
  44. if [ ! -f $SYNCTHING_CONFIG_FILE ]; then
  45. echo $'# Your syncthing configuration file' > $SYNCTHING_CONFIG_FILE
  46. echo '#' >> $SYNCTHING_CONFIG_FILE
  47. echo $"# The ${PROJECT_NAME} syncthing ID is: $SYNCTHING_ID" >> $SYNCTHING_CONFIG_FILE
  48. echo '#' >> $SYNCTHING_CONFIG_FILE
  49. echo '# Paste the IDs of your devices below' >> $SYNCTHING_CONFIG_FILE
  50. echo '#' >> $SYNCTHING_CONFIG_FILE
  51. fi
  52. }
  53. function syncthing_manual_edit {
  54. if [ ! -f ~/.syncthing-server-id ]; then
  55. return
  56. fi
  57. syncthing_create_ids_file
  58. editor $SYNCTHING_CONFIG_FILE
  59. # force an update of the configuration
  60. touch ~/.syncthing-update
  61. }
  62. function syncthing_show_id {
  63. if [ ! -f ~/.syncthing-server-id ]; then
  64. return
  65. fi
  66. SYNCTHING_ID=$(cat ~/.syncthing-server-id)
  67. dialog --title $"Device ID for ${PROJECT_NAME}" \
  68. --backtitle $"Freedombone User Control Panel" \
  69. --msgbox $"In a desktop terminal press shift and select the ID below,\nthen right click and copy.\n\nWithin Connectbot select Menu/Copy and then highlight the ID below\n\n$SYNCTHING_ID" 12 78
  70. }
  71. function syncthing_add_id {
  72. if [ ! -f ~/.syncthing-server-id ]; then
  73. return
  74. fi
  75. syncthing_create_ids_file
  76. data=$(tempfile 2>/dev/null)
  77. trap "rm -f $data" 0 1 2 5 15
  78. dialog --backtitle $"Freedombone User Control Panel" \
  79. --title $"Add a Syncthing device ID" \
  80. --form $"Paste the device ID for your laptop/desktop/netbook/phone/tablet below" 9 80 2 \
  81. $"Device ID:" 1 1 "" 1 26 80 80 \
  82. $"Description (optional):" 2 1 "" 2 26 80 80 \
  83. 2> $data
  84. sel=$?
  85. case $sel in
  86. 1) return;;
  87. 255) return;;
  88. esac
  89. SYNCTHING_DEVICE_ID=$(cat $data | sed -n 1p)
  90. SYNCTHING_DESCRIPTION=$(cat $data | sed -n 2p)
  91. if [ ${#SYNCTHING_DEVICE_ID} -lt 10 ]; then
  92. return
  93. fi
  94. if [[ $SYNCTHING_DEVICE_ID == *"#"* || $SYNCTHING_DEVICE_ID == *"*"* || $SYNCTHING_DEVICE_ID == *'/'* || $SYNCTHING_DEVICE_ID != *"-"* ]]; then
  95. dialog --title $"Add a Syncthing device ID" \
  96. --backtitle $"Freedombone User Control Panel" \
  97. --msgbox $"That doesn't look like a device ID" 6 50
  98. return
  99. fi
  100. if grep -q "$SYNCTHING_DEVICE_ID" $SYNCTHING_CONFIG_FILE; then
  101. dialog --title $"Add a Syncthing device ID" \
  102. --backtitle $"Freedombone User Control Panel" \
  103. --msgbox $"That ID has already been added" 6 50
  104. return
  105. fi
  106. if [ ${#SYNCTHING_DESCRIPTION} -gt 0 ]; then
  107. echo "# $SYNCTHING_DESCRIPTION" >> $SYNCTHING_CONFIG_FILE
  108. fi
  109. echo "$SYNCTHING_DEVICE_ID" >> $SYNCTHING_CONFIG_FILE
  110. # force an update of the configuration
  111. touch ~/.syncthing-update
  112. dialog --title $"Add a Syncthing device ID" \
  113. --backtitle $"Freedombone User Control Panel" \
  114. --msgbox $"The ID was added" 6 50
  115. }
  116. function syncthing_remove_id {
  117. if [ ! -f ~/.syncthing-server-id ]; then
  118. return
  119. fi
  120. syncthing_create_ids_file
  121. data=$(tempfile 2>/dev/null)
  122. trap "rm -f $data" 0 1 2 5 15
  123. dialog --backtitle $"Freedombone User Control Panel" \
  124. --title $"Remove a Syncthing device ID" \
  125. --form $"Paste the device ID which is to be removed below" 8 80 1 \
  126. $"Device ID:" 1 1 "" 1 14 80 80 \
  127. 2> $data
  128. sel=$?
  129. case $sel in
  130. 1) return;;
  131. 255) return;;
  132. esac
  133. SYNCTHING_DEVICE_ID=$(cat $data | sed -n 1p)
  134. if [ ${#SYNCTHING_DEVICE_ID} -lt 10 ]; then
  135. return
  136. fi
  137. if [[ $SYNCTHING_DEVICE_ID == *"#"* || $SYNCTHING_DEVICE_ID == *"*"* || $SYNCTHING_DEVICE_ID == *'/'* || $SYNCTHING_DEVICE_ID != *"-"* ]]; then
  138. dialog --title $"Remove a Syncthing device ID" \
  139. --backtitle $"Freedombone User Control Panel" \
  140. --msgbox $"That doesn't look like a device ID" 6 50
  141. return
  142. fi
  143. if ! grep -q "$SYNCTHING_DEVICE_ID" $SYNCTHING_CONFIG_FILE; then
  144. dialog --title $"Remove a Syncthing device ID" \
  145. --backtitle $"Freedombone User Control Panel" \
  146. --msgbox $"That ID wasn't registered anyway" 6 50
  147. return
  148. fi
  149. sed -i "/$SYNCTHING_DEVICE_ID/d" $SYNCTHING_CONFIG_FILE
  150. # force an update of the configuration
  151. touch ~/.syncthing-update
  152. dialog --title $"Remove a Syncthing device ID" \
  153. --backtitle $"Freedombone User Control Panel" \
  154. --msgbox $"The ID was removed" 6 50
  155. }
  156. function run_client_syncthing {
  157. SYNCTHING_CONFIG_FILE=~/.syncthingids
  158. SYNCTHING_ID=$(cat ~/.syncthing-server-id)
  159. while true
  160. do
  161. data=$(tempfile 2>/dev/null)
  162. trap "rm -f $data" 0 1 2 5 15
  163. dialog --backtitle $"Freedombone User Control Panel" \
  164. --title $"File Synchronization" \
  165. --radiolist $"Choose an operation:" 12 70 6 \
  166. 1 $"Show device ID for ${PROJECT_NAME}" off \
  167. 2 $"Add an ID for another machine or device" off \
  168. 3 $"Remove an ID for another machine or device" off \
  169. 4 $"Manually edit device IDs" off \
  170. 5 $"Back to main menu" on 2> $data
  171. sel=$?
  172. case $sel in
  173. 1) break;;
  174. 255) break;;
  175. esac
  176. case $(cat $data) in
  177. 1) syncthing_show_id;;
  178. 2) syncthing_add_id;;
  179. 3) syncthing_remove_id;;
  180. 4) syncthing_manual_edit;;
  181. 5) break;;
  182. esac
  183. done
  184. }
  185. function install_interactive_syncthing {
  186. echo -n ''
  187. }
  188. function change_password_syncthing {
  189. echo -n ''
  190. }
  191. function reconfigure_syncthing {
  192. echo -n ''
  193. }
  194. function upgrade_syncthing {
  195. echo -n ''
  196. }
  197. function backup_local_syncthing {
  198. if [ -d /var/lib/syncthing/SyncShared ]; then
  199. echo $"Backing up syncthing"
  200. function_check backup_directory_to_usb
  201. backup_directory_to_usb /var/lib/syncthing/SyncShared syncthingshared
  202. backup_directory_to_usb /root/.config/syncthing syncthingconfig
  203. echo $"Backup to syncthing complete"
  204. fi
  205. }
  206. function restore_local_syncthing {
  207. if [ -f /etc/systemd/system/syncthing.service ]; then
  208. systemctl stop syncthing
  209. systemctl stop cron
  210. fi
  211. temp_restore_dir=/root/tempsyncthing
  212. if [ -d $USB_MOUNT/backup/syncthingconfig ]; then
  213. echo $"Restoring syncthing configuration"
  214. function_check restore_directory_from_usb
  215. restore_directory_from_usb ${temp_restore_dir}config syncthingconfig
  216. cp -r ${temp_restore_dir}config/* /
  217. if [ ! "$?" = "0" ]; then
  218. set_user_permissions
  219. backup_unmount_drive
  220. systemctl start syncthing
  221. systemctl start cron
  222. exit 6833
  223. fi
  224. rm -rf ${temp_restore_dir}config
  225. fi
  226. if [ -d $USB_MOUNT/backup/syncthingshared ]; then
  227. echo $"Restoring syncthing shared files"
  228. restore_directory_from_usb ${temp_restore_dir}shared syncthingshared
  229. cp -r ${temp_restore_dir}shared/* /
  230. if [ ! "$?" = "0" ]; then
  231. set_user_permissions
  232. backup_unmount_drive
  233. systemctl start syncthing
  234. systemctl start cron
  235. exit 37904
  236. fi
  237. rm -rf ${temp_restore_dir}shared
  238. fi
  239. if [ -d $USB_MOUNT/backup/syncthing ]; then
  240. for d in $USB_MOUNT/backup/syncthing/*/ ; do
  241. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  242. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  243. if [ ! -d /home/$USERNAME ]; then
  244. ${PROJECT_NAME}-adduser $USERNAME
  245. fi
  246. echo $"Restoring syncthing files for $USERNAME"
  247. restore_directory_from_usb ${temp_restore_dir} syncthing/$USERNAME
  248. cp -r ${temp_restore_dir}/home/$USERNAME/Sync /home/$USERNAME/
  249. if [ ! "$?" = "0" ]; then
  250. rm -rf ${temp_restore_dir}
  251. set_user_permissions
  252. backup_unmount_drive
  253. systemctl start syncthing
  254. systemctl start cron
  255. exit 68438
  256. fi
  257. rm -rf ${temp_restore_dir}
  258. # restore device IDs from config settings
  259. if [ -f /home/$USERNAME/.config/syncthing/.syncthing-server-id ]; then
  260. cp /home/$USERNAME/.config/syncthing/.syncthing-server-id /home/$USERNAME/.syncthing-server-id
  261. chown $USERNAME:$USERNAME /home/$USERNAME/.syncthing-server-id
  262. fi
  263. if [ -f /home/$USERNAME/.config/syncthing/.syncthingids ]; then
  264. cp /home/$USERNAME/.config/syncthing/.syncthingids /home/$USERNAME/.syncthingids
  265. chown $USERNAME:$USERNAME /home/$USERNAME/.syncthingids
  266. fi
  267. fi
  268. done
  269. fi
  270. if [ -f /etc/systemd/system/syncthing.service ]; then
  271. systemctl start syncthing
  272. systemctl start cron
  273. fi
  274. }
  275. function backup_remote_syncthing {
  276. if [ -d /root/.config/syncthing ]; then
  277. echo $"Backing up syncthing configuration"
  278. function_check backup_directory_to_friend
  279. backup_directory_to_friend /root/.config/syncthing syncthingconfig
  280. echo $"Backup of syncthing configuration complete"
  281. fi
  282. if [ -d /var/lib/syncthing/SyncShared ]; then
  283. echo $"Backing up syncthing shared files"
  284. function_check backup_directory_to_friend
  285. backup_directory_to_friend /var/lib/syncthing/SyncShared syncthingshared
  286. echo $"Backup of syncthing shared files complete"
  287. fi
  288. }
  289. function restore_remote_syncthing {
  290. if [ -f /etc/systemd/system/syncthing.service ]; then
  291. systemctl stop syncthing
  292. systemctl stop cron
  293. fi
  294. if [ -d $SERVER_DIRECTORY/backup/syncthingconfig ]; then
  295. echo $"Restoring syncthing configuration"
  296. temp_restore_dir=/root/tempsyncthingconfig
  297. function_check restore_directory_from_friend
  298. restore_directory_from_friend $temp_restore_dir syncthingconfig
  299. cp -r $temp_restore_dir/* /
  300. if [ ! "$?" = "0" ]; then
  301. systemctl start syncthing
  302. systemctl start cron
  303. exit 6833
  304. fi
  305. rm -rf $temp_restore_dir
  306. fi
  307. if [ -d $SERVER_DIRECTORY/backup/syncthingshared ]; then
  308. echo $"Restoring syncthing shared files"
  309. temp_restore_dir=/root/tempsyncthingshared
  310. function_check restore_directory_from_friend
  311. restore_directory_from_friend $temp_restore_dir syncthingshared
  312. cp -r $temp_restore_dir/* /
  313. if [ ! "$?" = "0" ]; then
  314. systemctl start syncthing
  315. systemctl start cron
  316. exit 37904
  317. fi
  318. rm -rf $temp_restore_dir
  319. fi
  320. if [ -d $SERVER_DIRECTORY/backup/syncthing ]; then
  321. for d in $SERVER_DIRECTORY/backup/syncthing/*/ ; do
  322. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  323. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  324. if [ ! -d /home/$USERNAME ]; then
  325. ${PROJECT_NAME}-adduser $USERNAME
  326. fi
  327. echo $"Restoring syncthing files for $USERNAME"
  328. temp_restore_dir=/root/tempsyncthing
  329. function_check restore_directory_from_friend
  330. restore_directory_from_friend $temp_restore_dir syncthing/$USERNAME
  331. cp -r $temp_restore_dir/home/$USERNAME/Sync /home/$USERNAME/
  332. if [ ! "$?" = "0" ]; then
  333. rm -rf $temp_restore_dir
  334. systemctl start syncthing
  335. systemctl start cron
  336. exit 68438
  337. fi
  338. rm -rf $temp_restore_dir
  339. # restore device IDs from config settings
  340. if [ -f /home/$USERNAME/.config/syncthing/.syncthing-server-id ]; then
  341. cp /home/$USERNAME/.config/syncthing/.syncthing-server-id /home/$USERNAME/.syncthing-server-id
  342. chown $USERNAME:$USERNAME /home/$USERNAME/.syncthing-server-id
  343. fi
  344. if [ -f /home/$USERNAME/.config/syncthing/.syncthingids ]; then
  345. cp /home/$USERNAME/.config/syncthing/.syncthingids /home/$USERNAME/.syncthingids
  346. chown $USERNAME:$USERNAME /home/$USERNAME/.syncthingids
  347. fi
  348. echo $"Restore of syncthing files for $USERNAME complete"
  349. fi
  350. done
  351. fi
  352. if [ -f /etc/systemd/system/syncthing.service ]; then
  353. systemctl start syncthing
  354. systemctl start cron
  355. fi
  356. }
  357. function remove_syncthing {
  358. if ! grep -Fxq "install_syncthing" $COMPLETION_FILE; then
  359. return
  360. fi
  361. iptables -D INPUT -p udp --dport $SYNCTHING_PORT -j ACCEPT
  362. iptables -D INPUT -p tcp --dport $SYNCTHING_PORT -j ACCEPT
  363. function_check save_firewall_settings
  364. save_firewall_settings
  365. systemctl stop syncthing
  366. systemctl disable syncthing
  367. apt-get -y remove --purge syncthing
  368. rm /etc/systemd/system/syncthing.service
  369. sed -i "/${PROJECT_NAME}-syncthing/d" /etc/crontab
  370. sed -i '/install_syncthing/d' $COMPLETION_FILE
  371. sed -i '/configure_firewall_for_syncthing/d' $COMPLETION_FILE
  372. systemctl restart cron
  373. }
  374. function configure_firewall_for_syncthing {
  375. if grep -Fxq "configure_firewall_for_syncthing" $COMPLETION_FILE; then
  376. return
  377. fi
  378. iptables -A INPUT -p udp --dport $SYNCTHING_PORT -j ACCEPT
  379. iptables -A INPUT -p tcp --dport $SYNCTHING_PORT -j ACCEPT
  380. function_check save_firewall_settings
  381. save_firewall_settings
  382. OPEN_PORTS+=("Syncthing $SYNCTHING_PORT")
  383. echo 'configure_firewall_for_syncthing' >> $COMPLETION_FILE
  384. }
  385. function mesh_install_syncthing {
  386. chroot "$rootdir" wget -q https://syncthing.net/release-key.txt -O- | apt-key add -
  387. echo "deb http://apt.syncthing.net/ syncthing release" | tee $rootdir/etc/apt/sources.list.d/syncthing.list
  388. chroot "$rootdir" apt-get update
  389. chroot "$rootdir" apt-get -y --force-yes install syncthing
  390. # This probably does need to run as root so that it can access the Sync directories
  391. # in each user's home directory
  392. echo '[Unit]' > $rootdir/etc/systemd/system/syncthing.service
  393. echo 'Description=Syncthing - Open Source Continuous File Synchronization' >> $rootdir/etc/systemd/system/syncthing.service
  394. echo 'Documentation=man:syncthing(1)' >> $rootdir/etc/systemd/system/syncthing.service
  395. echo 'After=network.target' >> $rootdir/etc/systemd/system/syncthing.service
  396. echo 'Wants=syncthing-inotify@.service' >> $rootdir/etc/systemd/system/syncthing.service
  397. echo '' >> $rootdir/etc/systemd/system/syncthing.service
  398. echo '[Service]' >> $rootdir/etc/systemd/system/syncthing.service
  399. echo 'User=root' >> $rootdir/etc/systemd/system/syncthing.service
  400. echo 'ExecStart=/usr/bin/syncthing -no-browser -no-restart -logflags=0' >> $rootdir/etc/systemd/system/syncthing.service
  401. echo 'Restart=on-failure' >> $rootdir/etc/systemd/system/syncthing.service
  402. echo 'SuccessExitStatus=3 4' >> $rootdir/etc/systemd/system/syncthing.service
  403. echo 'RestartForceExitStatus=3 4' >> $rootdir/etc/systemd/system/syncthing.service
  404. echo '' >> $rootdir/etc/systemd/system/syncthing.service
  405. echo '[Install]' >> $rootdir/etc/systemd/system/syncthing.service
  406. echo 'WantedBy=multi-user.target' >> $rootdir/etc/systemd/system/syncthing.service
  407. chroot "$rootdir" systemctl enable syncthing
  408. echo 'mesh_install_syncthing'
  409. }
  410. function install_syncthing {
  411. if [ $INSTALLING_MESH ]; then
  412. mesh_install_syncthing
  413. return
  414. fi
  415. apt-get -y install curl
  416. curl -s https://syncthing.net/release-key.txt | apt-key add -
  417. echo "deb http://apt.syncthing.net/ syncthing release" | tee /etc/apt/sources.list.d/syncthing.list
  418. apt-get update
  419. apt-get -y install syncthing
  420. # This probably does need to run as root so that it can access the Sync directories
  421. # in each user's home directory
  422. echo '[Unit]' > /etc/systemd/system/syncthing.service
  423. echo 'Description=Syncthing - Open Source Continuous File Synchronization' >> /etc/systemd/system/syncthing.service
  424. echo 'Documentation=man:syncthing(1)' >> /etc/systemd/system/syncthing.service
  425. echo 'After=network.target' >> /etc/systemd/system/syncthing.service
  426. echo 'Wants=syncthing-inotify@.service' >> /etc/systemd/system/syncthing.service
  427. echo '' >> /etc/systemd/system/syncthing.service
  428. echo '[Service]' >> /etc/systemd/system/syncthing.service
  429. echo 'User=root' >> /etc/systemd/system/syncthing.service
  430. echo "Environment='all_proxy=socks5://localhost:9050'" >> /etc/systemd/system/syncthing.service
  431. echo 'ExecStart=/usr/bin/syncthing -no-browser -no-restart -logflags=0' >> /etc/systemd/system/syncthing.service
  432. echo 'Restart=on-failure' >> /etc/systemd/system/syncthing.service
  433. echo 'SuccessExitStatus=3 4' >> /etc/systemd/system/syncthing.service
  434. echo 'RestartForceExitStatus=3 4' >> /etc/systemd/system/syncthing.service
  435. echo '' >> /etc/systemd/system/syncthing.service
  436. echo '[Install]' >> /etc/systemd/system/syncthing.service
  437. echo 'WantedBy=multi-user.target' >> /etc/systemd/system/syncthing.service
  438. systemctl enable syncthing
  439. systemctl daemon-reload
  440. systemctl start syncthing
  441. function_check cron_add_mins
  442. cron_add_mins 1 "/usr/local/bin/${PROJECT_NAME}-syncthing > /dev/null"
  443. function_check configure_firewall_for_syncthing
  444. configure_firewall_for_syncthing
  445. }
  446. # NOTE: deliberately no exit 0